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 ...
随机推荐
- VS2013使用单元测试
一.开发环境 开发工具:VS2013 二.开发流程 1.添加一个控制台项目UnitDemo namespace UnitDemo { public class Program { static voi ...
- Django--知识补充
自定义标签或过滤器 渲染变量的方法(过滤器:修改数据或格式转换) {{ var | add }} {{ var | date:"Y-m" }} {{ var | safe }} 渲 ...
- Python学习日记之中文支持
解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可
- 5步上手体验kettle快捷调度方式
https://my.oschina.net/u/944575/blog/1557410 kettle调度监控最佳实践 https://my.oschina.net/u/1026947/blog/15 ...
- JavaScript判断
if...else: if...else语句是在指定的条件成立时执行的代码,在条件不成立时执行else后的代码. 语法: if(条件) {条件成立时执行的代码 }else{ 条件不成立的时执行的代码} ...
- POJ_2387_最短路
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46859 Accepted ...
- day15-模块的基础及导入
目录 模块 什么是模块 使用模块 import 循环导入问题 解决方案一 解决方案二 模块的搜索路径 Python文件的两种用途 包 导入包内包 导入包内包的模块 绝对导入与相对导入 绝对导入 相对导 ...
- BZOJ5314: [Jsoi2018]潜入行动 (树形DP)
题意:一棵树选择恰好k个结点放置监听器 每个监听器只能监听相邻的节点 问能使得所有节点被监听的种类数 题解:反正就是很well-known的树形DP了 至于时间复杂度为什么是nk 不会不学 很好想到四 ...
- Appium 的xpath定位
Appium 的xpath定位 1.如果元素text是唯一的,可以通过text文本定位 //*[@text=’text文本属性’] # 定位text driver.find_element_by_xp ...
- ajax aspx调用webservice,返回json
1,创建一个asp.net网站 2.创建一个student类 using System; using System.Collections.Generic; using System.Linq; us ...