26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array【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.
解法一:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty() || nums.size() == )
{
return nums.size();
}
int i = ;
int j = ;
nums[j++] = nums[i++];
while (i < nums.size() && j < nums.size())
{
if (nums[i] != nums[i - ])
{
nums[j++] = nums[i++];
}
else
{
++i;
}
}
return j;
}
};
思路:双指针,注意边界条件的判断
解法二:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n < ) return n;
int id = ;
for(int i = ; i < n; ++i)
if(A[i] != A[i-]) A[id++] = A[i];
return id;
}
};
可读性一般
解法三:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int count = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == nums[i-]) count++;
else nums[i-count] = nums[i];
}
return nums.size()-count;
}
};
26. Remove Duplicates from Sorted Array【easy】的更多相关文章
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 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 ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- luogu P2134 百日旅行
题目链接 luogu P2134 百日旅行 题解 dp方程好想吧 优化有些玄学惹 不会证.... 不过我会三分和贪心 \滑稽 但还是写dp吧 代码 #include<cstdio> #in ...
- Spark小问题合集
1)在win7下使用spark shell运行spark程序,通过以下形式读取文件时 sc.sequenceFile[Int,String]("./sparkF") 偶尔会出现“I ...
- Java概述--Java开发实战经典
1)Java有三个发展方向,分别是Java SE,Java EE,Java ME.以下简要介绍. a.Java SE,Java Standard Edition(java标准版),包含了构成java语 ...
- java-List集合遍历,删除或增加特定的元素
1.for(int i=0; i<list.size(); i++) 2.for(Object object : list) 实质调用的 list.iterator() 3.list.iter ...
- centos 7.3systemctl工具
http://www.cnblogs.com/tswcypy/p/4479153.html
- Ubuntu -- 安装和部署php5.6 nginx php5.6-fpm
1.首先输入用户名和密码进行登录 2.升级更新软件包 sudo apt-get update sudo apt-get upgrade 判断都填y 3.安装nginx sudo apt-get i ...
- ThreadPoolExecutor 的三种提交任务方式
学习内容: ExecutorService线程池的应用... 1.如何创建线程池... 2.调用线程池的方法,获取线程执行完毕后的结果... 3.关闭线程... 首先我们先了解一下到底什么是线程池 ...
- Mac的文件比对工具:Meld、Beyond Compare
Meld开源免费 Beyond Compare可以在Windows.Mac下使用,但是收费,需要自己破解
- 1、Android项目框架搭建 (分析需求、整理资料)
闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 须要下面资料 1.android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.Vi ...
- uber shader
shader 合在一起 用一些宏来控制 选哪部分编成一个想要的shader https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.htm ...