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 P1107 最大整数
题目描述 设有n个正整数 (n<=20), 将它们连接成一排, 组成一个最大的多位整数. 例如: n=3时, 3个整数13, 312, 343连接成的最大整数为: 34331213 又如: n= ...
- 【二维莫队】【二维分块】bzoj2639 矩形计算
<法一>二维莫队,对n和m分别分块后,对块从上到下从左到右依次编号,询问以左上角所在块编号为第一关键字,以右下角标号为第二关键字排序,转移时非常厉害. O(q*n*sqrt(n)). #i ...
- 【小笔记】斜率优化的结论(WC)
- Exercise01_03
public class TuAn{ public static void main(String[] args){ System.out.println(" J A V V A" ...
- ListView(下)自定义适配器
(一) 1.效果图 2.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- iOS开发——MJExtension复杂数组用法
最近在看MJExtension的Demo,发现了一个plist文件直接转数组模型的方法.以前研究过但是浅尝辄止没有解决,这几天有时间,好好看了看,找到了解决办法,与大家分享. 如果大家的项目中有这种嵌 ...
- ClipboardJS复制粘贴插件的使用
1.简单的纯JS复制粘贴(兼容性差,只能用textarea标签) var btn=document.getElementsByClassName("btn")[0]; //复制按钮 ...
- final修饰符的三种使用场景
final有三种使用场景,各自是修饰变量.方法和类.不管哪种修饰.一旦声明为final类型.你将不能改变这个引用了,编译器会检查代码,假设你试图再次初始化,编译器会报错.以下我来详细说说每一种修饰场景 ...
- 更改Windows用户文件夹(Users)默认位置到其它盘
一.把 C盘Users文件夹里的用户数据,迁移到D盘Users文件夹中 系统环境:windows7 1.mklink命令详解 C:>mklink 创建符号链接. MKLINK [[/D] | [ ...
- [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息
一.介绍 本例子用Selenium +phantomjs爬取智能电视网站(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取图片信息. 给定关键字:数字:融合:电视 ...