leetcode_26_Remove Duplicates from Sorted Array
来源:互联网 发布:sql编写存储过程 编辑:程序博客网 时间:2024/06/06 01:08
欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
//vs2012测试代码#include<iostream>using namespace std;#define n 5int main(){int A[n];for(int i=0; i<n; i++)cin>>A[i];//要考虑空数组的情况if(n==0)return 0;int start=0;for(int i=0; i<n-1; i++){if( A[i]!=A[i+1]){A[start] = A[i];start++;}}A[start] = A[n-1];start++;//for(int j=start; j<n; j++)//A[j]=0;cout<<start<<endl;for(int j=0; j<start; j++)cout<<A[j];return start;}
//方法一:自测acceptedclass Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0) return 0; int length=0; for(int i=1; i<nums.size(); i++) { if(nums[length] != nums[i]) nums[++length] = nums[i]; } return length+1; }};
//方法二:参考其他class Solution {public:int removeDuplicates(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(0 == n) return 0;int len = 1;for (int i = 1; i < n; ++i){if(A[i] != A[len-1])A[len++] = A[i];}return len;}};
1 0
- Leetcode_26_Remove Duplicates from Sorted Array
- leetcode_26_Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 029 弦截法求根
- Java基础——自定义Annotation
- ASP.NET Repeater绑定数据并实现分页
- android实现对图片的放大缩小旋转等操作
- 开源图表工具
- leetcode_26_Remove Duplicates from Sorted Array
- 数组名 和 指针
- 测试
- Add Binary - LeetCode
- 有关twister的7788
- ZOJ 3201 Tree of Tree 树形DP
- python Django 1.7 中文入门 (官网) 01 开始
- Android获取assets文件夹的路径
- leetcode_27_Remove Element