首页 > 编程语言 > 详细

(C++練習) 26. Remove Duplicates from Sorted Array

时间:2019-02-26 00:43:30      阅读:207      评论:0      收藏:0      [点我收藏+]

題目 :

Given a sorted array nums, 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.

大意 :

給一個排列好的 array, 移除重複的元素, 而且不許另外分配額外的 array.

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

 

(C++練習) 26. Remove Duplicates from Sorted Array

原文:https://www.cnblogs.com/ollie-lin/p/10434688.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!