LeetCode_26. Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2], Your function should return length =2, with the first two elements ofnumsbeing1and2respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length =5, with the first five elements ofnumsbeing modified to0,1,2,3, and4respectively. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
package leetcode.easy;
public class RemoveDuplicatesFromSortedArray {
@org.junit.Test
public void test() {
int[] nums1 = { 1, 1, 2 };
int[] nums2 = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
System.out.println(removeDuplicates(nums1));
System.out.println(removeDuplicates(nums2));
}
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
LeetCode_26. Remove Duplicates from Sorted Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- P2458 [SDOI2006]保安站岗[树形dp]
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
- MySQL 空间数据 简单操作
在做的项目中需要,自己绘制区域图形,并存储起来,后面还有更新的需要,存文件不方面,想到现在数据库都支持空间数据库. 现在用的就是 MySQL ,就继续用 MySQL 来存储.管理空间数据.下面就做一些 ...
- MySQL之left jion 、 right jion 和inner jion 的区别和使用方法
left jion 左联结 right jion 右联结 inner jion 等值联结 create table teacher( tid ) primary key auto_incremen ...
- redis中对list类型某个元素的查找和删除
我们的信息都是放到redis的缓存中,结构为list,如果知道特定的值的话,通过LREM key count value这样就可以.对于redis的list结构,获取某个位置的值通过 LINDE ...
- 获取历史K线数据的几个方法
1.通过已有的股票交易软件下载数据,如果他们是开源结构的,就可以解析他们的K线数据. 2.在互联网上抓取数据 int iStockCode;CString strUrl; 通过OpenUrl.Read ...
- glog的安装使用
参考 :https://blog.csdn.net/Pig_Pig_Bang/article/details/81632962 https://blog.csdn.net/cywtd/article/ ...
- 【中国剩余定理-入门】-C++
中国剩余定理也称孙子定理,是中国古代求解一次同余式组(见同余)的方法.是数论中一个重要定理. 这玩意在luogu居然有模板题: [TJOI2009]猜数字 先来看一个问题: 在<孙子算经> ...
- 009_Source Insight 3.5安装及使用
链接:https://pan.baidu.com/s/1CBEhIhYtZZ6rUjq7yu4J2w提取码:qkv0 复制这段内容后打开百度网盘手机App,操作更方便哦 SourceInsight3. ...
- 学到了武沛齐讲的Day14完
& 交 | 并 ^ 并-交 --------------------- 格式化 %s 字符串,数字,一切 %.4s 留前面4位 %d 数字 %f 小数保留6位 四舍五入 %0. ...
- zabbix的日志监控
前提条件是该日志文件对于启动zabbix agent进程的用户开启了可读权限,而且该日志的路径对该用户开方x权限,让能取到这个日志文件 第一个参数可以不用引号,前提是zabbix用户可以进入文件路径, ...