LeetCode – Remove Duplicates from Sorted Array (Java)
来源:互联网 发布:mysql数据库的优点 编辑:程序博客网 时间:2024/06/11 04:43
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
Thoughts
The problem is pretty straightforward. It returns the length of array with unique elements, but the original array need to be changed also. This problem should be reviewed with Remove Duplicates from Sorted Array II.
Java Solution
public class Solution { public int removeDuplicates(int[] A) { if(A.length < 2) return A.length; int j = 0; int i = 1; while(i < A.length){ if(A[i] == A[j]){ i++; }else{ A[++j] = A[i++]; } } return j+1; }}
0 0
- [Leetcode] Remove Duplicates from Sorted Array (Java)
- [LeetCode][Java] Remove Duplicates from Sorted Array
- 【leetcode】Remove Duplicates from Sorted Array【java】
- leetcode:Remove Duplicates from Sorted Array 【Java】
- LeetCode : Remove Duplicates from Sorted Array [java]
- LeetCode – Remove Duplicates from Sorted Array (Java)
- LeetCode – Remove Duplicates from Sorted Array II (Java)
- LeetCode – Remove Duplicates from Sorted Array (Java)
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II (Java)
- 【Leetcode】Remove Duplicates from Sorted Array in JAVA
- [LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)
- Remove Duplicates from Sorted Array---leetcode 我的java题解
- LeetCode(026) Remove Duplicates from Sorted Array (Java)
- LeetCode(080) Remove Duplicates from Sorted Array II (Java)
- [LeetCode][Java] Remove Duplicates from Sorted Array II
- LeetCode 26 : Remove Duplicates from Sorted Array (Java)
- Cocos2dx游戏源码合集
- PAT-B 1012. 数字分类(python)
- 不错的建议
- LeetCode – Maximum Subarray (Java)
- eas bos 查看工作流sql 当前任务名称 任务创建时间 审批人
- LeetCode – Remove Duplicates from Sorted Array (Java)
- 我的第二篇博客
- 【足迹C++ primer】10.函数基础
- 动态规划
- 《计算机程序的构造与解释》(四)
- linphoned的拨号盘的dialer
- XAML属性声明简介
- 修改Tomcat默认编码
- SQL Server 2008将数据导出到SQL脚本文件