Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]
题目:
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 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 new length.
翻译:
给定一个排序好的数组,就地移除反复的元素。来使得每一个元素仅仅出现一次,而且返回新的长度。
不要分配额外的空间给还有一个数组。你必须完毕它在原数组上。而且仅仅使用常数级的内存。
分析:
因为须要移除反复的元素,所以必须有移动元素的操作。当遍历的i指针指向的元素与上一个元素反复时。须要採用一个指针nextEmpty来记录当前这个位置(须要被移除的位置,也就是要把后面的元素复制过来的位置),当遍历到下一个不反复的元素时。再拷贝到这个位置。
代码:
public class Solution {
public int removeDuplicates(int[] nums) {
int nextEmpty=1;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]){
nums[nextEmpty]=nums[i];
nextEmpty++;
}
}
return nextEmpty;
}
}
Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]的更多相关文章
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 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练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- [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 ...
随机推荐
- 往文件内写入内容(java)
新建个工具类,并标记成静态的,方便调用. package util; import java.io.File;import java.io.FileOutputStream; public class ...
- Python中深拷贝与浅拷贝的区别
转自:http://blog.csdn.net/u014745194/article/details/70271868 定义: 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给 ...
- text-shadow的用法详解
1.兼容性:text-shadow 和 box-shadow 这两个属性在主流现代浏览器上得到了很好的支持( > Chrome 4.0, > Firefox 3.5, > Safar ...
- 使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)
编译前准备 1. 安装mingw 安装mingw(不管是直接安装mingw还是其他如code::blocks附带安装的mingw),输入:gcc -v可显示如下图的版本信息,我的版本是mingw ...
- FusionCharts之我用
fusioncharts:(此控件需flash支持) 介绍:http://baike.baidu.com/link?url=SOheR7sQlb93S5TqYmeI7FhtJ0V9ABNwH6OsAa ...
- ThinkPHP---thinkphp模型(M)拓展
(1)创建数据对象 数据对象就是父类模型中的$this->data,AR模式的底层数据操作用到了数据对象.模型实例化之前数据对象只是空数组,后来使用了魔术方法__set设置了数据对象的值. 上述 ...
- C/C++格式化输入,输出
C/C++格式化输入,输出 1.C语言 1. 语言函数 scanf(); printf(); sscanf() --> 不安全 sscanf_s() ---> 安全 sprintf() - ...
- c++运行程序闪退
以最简单程序为例 法一:在主函数末尾下一行getchar();即可.需要注意的是这种方法并不适合所有程序, 法二:<1>先在程序开头加上头文件#includ ...
- Linux---shell基本指令
1. 显示当前目录 pwd wangzhengchao@ubuntu:~$ cd /home/wangzhengchao/Desktop/ wangzhengchao@ubuntu:~/Desktop ...
- Linux查看Port状态命令、密钥SSH、会话同步
查看Port状态 ss -ntl命令,参数: 参数 作用 -a 显示所有的套接字 -l 显示所有连接状态的套接字 -e 显示详细的套接字信息 -m 显示套接字的内存使用情况 -p 显示套接字的进程信息 ...