LeetCode--Array--Remove Duplicates from Sorted Array (Easy)
26. Remove Duplicates from Sorted Array (Easy)
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 of nums being 1 and 2 respectively.
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 of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
solution##
解法一
class Solution {
public int removeDuplicates(int[] nums) {
int newlength = 0, k = 0;
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] < nums[i+1]) //如果两数不同
{
k = i + 1; //记录不同两数中下一个的下标
newlength++; //新数组长度++
if (k > newlength) //如果k与newlength不同步,说明有重复的数
nums[newlength] = nums[k]; 将nums[k]移动到newlength处
}
}
return newlength + 1; //此处不能用newlength++
}
}
解法二
class Solution {
public int removeDuplicates(int[] nums) {
int newlength = 0;
for (int i = 0; i < nums.length; i++)
{
if (nums[newlength] != nums[i])
nums[++newlength] = nums[i];
}
return newlength+1; //此处不能用newlength++
}
}
总结##
题意是给定一个排好序的数组,需要将里面不重复的数字按顺序放在数组左端,构成一个新的数组,且这些不重复数字的长度为length,超过length长度的数字不用理会。
第一种解法没第二种解法简单,故只描述第二种解法。第二种解法的思路是设置一个计数器newlength,初始值为0,然后遍历数组,如果nums[newlength] != nums[i],则将nums[++newlength]置为nums[i],否则什么都不做;遍历完成后返回newlength+1;
Notes
1.注意++i与i++的区别;
LeetCode--Array--Remove Duplicates from Sorted Array (Easy)的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 06-移动web之flex布局
一.基本概念 flex布局又叫伸缩布局 .弹性布局 .伸缩盒布局 .弹性盒布局 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. ...
- Docker-Bridge Network 01 容器间通信
本小节介绍bridge network模式下,单机上的容器网络拓扑及通信. 1.前言 对于单机上的容器,Docker提供了bridge.host.none三种网络.我们首先介绍经典的bridge模式. ...
- 运输层--------运输层与网络层的关系、UDP、TCP
一.运输层与网络的区别: 网络层提供了主机之间的逻辑通信,而运输层为运行在不同主机上的进程之间提供了逻辑通信 二.实例证明: 考虑有两个家庭,一家位于美国东岸,一家位于美国西海岸,每家有12孩子.东海 ...
- 第一节:python基础
2020-03-29 python基础: 多种python版本,直接编码让c解释的是cpython,pypy是最快的python 编码:ascll码只能表示256种无法表示中文,utf8个根据字符长短 ...
- 核心task
由于Ant具有跨平台的特性,因此编写Ant生成文件时可能会失去一些灵活性.为了弥补这个不足,Ant提供了一个“exec”核心task,允许执行特定操作系统上的命令.
- python基础入门:matplotlib绘制多Y轴画图(附源码)
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:屁屁酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...
- tensorflow-参数、超参数、卷积核权值共享
根据网上查询到的说法,参数就是在卷积神经网络中可以被训练的参数,比如卷积核的权值和偏移等等,而超参数是一些预先设定好并且无法改变的,比如说是卷积核的个数等. 另外还有一个最最基础的概念,就是卷积核的权 ...
- Springboot:logback日志管理(九)
Springboot默认使用的日志框架就是logback 创建自定义的logback-spring.xml放在resources类目录下即可 logback-spring.xml: <?xml ...
- 用多线程,实现并发,TCP
首先,开启新的线程,是不会新开辟内存空间的,即,子线程和主线程 都在同一个进程里,也就是主进程里,用os.pid(),os.ppid() 服务器: 方式一:Thread实例化 def task(con ...
- shift count is too large
STM8S是8 bit单片机在STM8S中 unsigned long是32位, unsigned short和unsigned int都是16位,unsigned char是8位. 以以下代码编译时 ...