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.

解法一:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         if (nums.empty() || nums.size() == 1)
 5         {
 6             return nums.size();
 7         }
 8
 9         int i = 0;
10         int j = 0;
11         nums[j++] = nums[i++];
12
13         while (i < nums.size() && j < nums.size())
14         {
15             if (nums[i] != nums[i - 1])
16             {
17                 nums[j++] = nums[i++];
18             }
19             else
20             {
21                 ++i;
22             }
23         }
24
25         return j;
26     }
27 };

思路:双指针,注意边界条件的判断

解法二:

 1 class Solution {
 2     public:
 3     int removeDuplicates(int A[], int n) {
 4         if(n < 2) return n;
 5         int id = 1;
 6         for(int i = 1; i < n; ++i)
 7             if(A[i] != A[i-1]) A[id++] = A[i];
 8         return id;
 9     }
10 };

可读性一般

解法三:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         int count = 0;
 5         for(int i = 1; i < nums.size(); i++){
 6             if(nums[i] == nums[i-1]) count++;
 7             else nums[i-count] = nums[i];
 8         }
 9         return nums.size()-count;
10     }
11 };

转载于:https://www.cnblogs.com/abc-begin/p/7538288.html

26. Remove Duplicates from Sorted Array【easy】相关推荐

  1. 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 ...

  2. 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++> 给出排序好的 ...

  3. LeetCode 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  4. leetcode python3 简单题26. Remove Duplicates from Sorted Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十六题 (1)题目 英文: Given a sorted array nums, ...

  5. leetCode #26 Remove Duplicates from Sorted Array

    删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...

  6. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  7. 2016.6.17——Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array 本题收获: 1."删除"数组中元素 2.数组输出 题目: Given a sorted array, rem ...

  8. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  9. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

最新文章

  1. 如何写出符合Python审美的代码风格?
  2. bzoj1072: [SCOI2007]排列perm
  3. 转:vc中如何通过http的post方式上传文件
  4. linux 正则查找email_Linux 正则表达式详解
  5. CentOS上安装Python3.7.4
  6. web编程 端口分配_以编程方式衡量分配
  7. 小程序onload_微信小程序开发入门之共享账本(十四)
  8. Spring Boot框架中使用Jackson的处理总结
  9. python安装opencv2.4.9_Python学习之一:Python2.7与opencv2.4安装配置
  10. java判断文件是否存在_Java判断文件、文件夹是否存在
  11. MLAPP————第十四章 核方法
  12. iframe 的基本用法
  13. espeak 开源离线语音合成工具
  14. 用计算机弹起风了乐谱,光遇起风了乐谱简谱弹奏攻略 光遇怎么弹奏起风了
  15. 64位oracle 和64位 PL/SQL 的配置
  16. 宠物王国四java正版_宠物王国单机版正版
  17. 计算机网络 P43 网络应用模型 P44 DNS系统 P45 FTP文件传输协议
  18. python解决哲学家就餐问题(and型信号量)
  19. linux系统可以装access吗,安装华为FusionAccess Linux版本的方法
  20. Win7下安装Ubuntu11.10(解决linux try(hd0,0):NTFS5:no ang0引导问题)

热门文章

  1. JAVA 集合Null 初始化_springboot使用mybatis-plus表单更新null值问题通用解决方案
  2. shell 字符串变量比对_Linux Shell 字符串操作(长度/查找/替换)详解
  3. ajax-loader.gif不存在怎么解决_战神引擎不开门怎么解决?
  4. QT/QML Text 部分功能(自动省略 自动换行 自动调节字体大小 调节行间距
  5. python socket 接口
  6. 大数据小视角1:从行存储到RCFile
  7. 私有属性的另类访问方式
  8. TQ2440实现触摸屏和qt图形 解决segmentation fault
  9. ext3,ext4,xfs和btrfs文件系统性能对比
  10. iPhone 3G 的摄像头怎么用啊~