【Leetcode长征系列】Remove Duplicates from Sorted Array
来源:互联网 发布:免费百度推广软件 编辑:程序博客网 时间:2024/05/21 11:49
原题:
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
和处理指针时的想法类似,不过我多开了一个数组空间,这样A数组就可以用来记录没有新的数组返回值了。
代码如下:
class Solution {public: int removeDuplicates(int A[], int n) { int tmp = A[0]; int *a = new int[n]; int j, i; for(i = 0; i<n; i++) a[i] = A[i]; for(i = 1, j = 1; i<n && j<n; i++) { if( a[i]!=tmp){ A[j] = a[i]; tmp = a[i]; j++; } } return j; }};
卡在了最后一个情况上,但是,数组这种东西怎么会出现空阿?完全无法理解这个呀,求指点debug!
=============================================================================================================================
男喷油指点后加了一句“if(n==0) return 0;”判断数组长度,然后AC了=。=
0 0
- 【Leetcode长征系列】Remove Duplicates from Sorted Array
- 【Leetcode长征系列】Remove Duplicates from Sorted Array II
- 【Leetcode长征系列】Remove Duplicates from Sorted List
- 【Leetcode长征系列】Remove Duplicates from Sorted List II
- [leetcode刷题系列]Remove Duplicates from Sorted Array
- [leetcode刷题系列]Remove Duplicates from Sorted Array II
- leetcode刷题系列C++-Remove Duplicates From Sorted Array
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- leetcode 35: Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array II
- Spring RESTful服务接收和返回JSON最佳实践
- struts-2.3.16.3+hibernate-4.2.14+spring-framework-4.0.6整合
- 管技理术
- 判断一个字符串是否为回文,以及求一个字符串中最长回文串
- 安卓颜色秀----颜色查询应用
- 【Leetcode长征系列】Remove Duplicates from Sorted Array
- 如何提高多线程程序的cpu利用率
- Oracle SQL基础
- 12864液晶并行方式显示
- USACO 2.2.1 Preface Numbering
- Android——内存调试
- objc学习笔记-基础
- shell sort命令
- shell脚本之如何一行行读文件