[array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy
descrition
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 by modifying the input array in-place with O(1) extra memory.
Example
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 new length.
解析
注意题目的要求,in_place,即原址,不能借助额外的辅助空间。
再关注到另一个有利的前提条件,数组是有序的,因此重复的数字一定挨着,这是达到最优解的关键,时间复杂度 O(n)。
code
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
int removeDuplicates(vector<int>& nums){
if(nums.empty()) // don't forget the special case!!!!
return 0;
int cur = 0; // the end of new nums
for(int i=1; i<nums.size(); i++){
if(nums[i] != nums[cur]){
nums[++cur] = nums[i];
}
}
return cur+1; // return the new length
}
};
int main()
{
return 0;
}
[array] leetCode-26. Remove Duplicates from Sorted Array - Easy的更多相关文章
- 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 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- 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 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
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
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有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 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 ...
随机推荐
- centos配置tomcat编辑修改
https://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html
- matplotlib散点图笔记
定义: 由一组不连续的点完成的图形 散点图: 包含正相关性,负相关性和不相关性. 散点图生成函数: plt.scatter(x,y) 演示代码如下: import numpy as np import ...
- SpringMVC & Struts2
这两个框架可谓Java中的经典,Java开发必懂的框架,这两天在面试中又问道两者的异同.这里简单做了整理供大家參考交流. 概念:
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
- 分享一段php获取随意时间的前一天代码
<?php /** * 获取给定日期的前一天 * @param string $date * @return string $yesterday */ function getYesterday ...
- Vue Invalid handler for event "": got undefined
原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.
- ActiveMQ学习总结(6)——ActiveMQ集成Spring和Log4j实现异步日志
我的团队和我正在创建一个由一组RESTful JSON服务组成的服务平台,该平台中的每个服务在平台中的作用就是分别提供一些独特的功能和/或数据.由于平台中产生的日志四散各处,所以我们想,要是能将这些日 ...
- HDU 3584 Cube (三维树状数组)
Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the nu ...
- hbs模板(zmaze ui用的)
hbs模板(zmaze ui用的) 一.总结 1.模板引擎:就是来生成界面的啊,只不过实现了view和数据分离以及一些其它的功能(预加载等). 2.Handlebars :但他是一个单纯的模板引擎,在 ...
- 1.1 Introduction中 Kafka for Stream Processing官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka for Stream Processing kafka的流处理 It i ...