码迷,mamicode.com
首页 > 编程语言 > 详细

【Leetcode解题报告】数组元素删除

时间:2016-08-01 09:09:45      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

Leetcode 27 Remove Element

问题描述

  Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

  For example, given input array nums = [3,2,2,3]val = 3. Your function should return length = 2, with the first two elements of nums being 2.

分析与解法

  注意:the order of elements can be changed,则可以用双指针,一个从头开始找到等于val的位置,一个从尾开始找到不等于val的位置,交换;直到头指针和尾指针交叉即可。参考代码如下所示:

技术分享
 1 class Solution 
 2 {
 3 public:
 4     int removeElement(vector<int>& nums, int val) 
 5     {
 6         int len = nums.size(), i = 0, j = len - 1; 
 7         if (j < 0) return 0; 
 8         while (i < j)
 9         {
10             while (i < len && nums[i] != val) i++; 
11             while (j >= 0 && nums[j] == val) j--; 
12             if (i < j) swap(nums[i++], nums[j--]); 
13         }
14         return (nums[j] == val) ? j : j+1; 
15     }
16 };
View Code

Leetcode 26 Remove Duplicates from Sorted Array

问题描述

  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 {
 3 public:
 4     int removeDuplicates(vector<int>& nums) 
 5     {
 6         int ret = 0, length = nums.size();
 7         if (length < 2) return length; 
 8         for (int i = 1; i < length; i++)
 9         {
10             if (nums[i] != nums[ret]) nums[++ret] = nums[i]; 
11         }
12         return ret+1; 
13     }
14 };
View Code

Leetcode 80 Remove Duplicates from Sorted Array II

问题描述

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

  For example, given sorted array nums = [1,1,1,2,2,3]. Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn‘t matter what you leave beyond the new length.

分析与解法

  去重,但要注意的是,结果中同一个值可以出现两次,上一个问题的扩展;其实可以扩展到同一个值出现k次。参考代码如下:

技术分享
 1 class Solution 
 2 {
 3 public:
 4     int removeDuplicates(vector<int>& nums) 
 5     {
 6         int len = nums.size(), i = 1, j = 2; 
 7         if (len < 3) return len; 
 8         while (j < len)
 9         {
10             if (nums[j] != nums[i-1]) nums[++i] = nums[j]; 
11             j++; 
12         }
13         return i + 1; 
14     }
15 };
View Code

Leetcode 283 Move Zeros

问题描述

  Given an array nums, write a function to move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.

  For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

  Note:

  1. You must do this in-place without making a copy of the array. 

  2. Minimize the total number of operations.

分析与解法

  注意,除0以外的元素,要保持原来的次序;依然是双指针,不过这回是都从头开始,i指向第一个0,j指向第一个非0,交换;直到j到数组结尾即可。参考代码如下所示:

技术分享
 1 class Solution 
 2 {
 3 public:
 4     void moveZeroes(vector<int>& nums) 
 5     {
 6         int len = nums.size(), i = 0, j = 0; 
 7         while (i < len)
 8         {
 9             if (nums[i] != 0) swap(nums[j++], nums[i]); 
10             i++; 
11         }
12     }
13 };
View Code

 

【Leetcode解题报告】数组元素删除

标签:

原文地址:http://www.cnblogs.com/xiaoxxmu/p/5724362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!