leetcode_linearList
2015-07-03 09:46
1266 查看
linearList02:Remove Duplicates from Sorted Array II
描述:
Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3] ,
Your function should return length = 5, and A is now [1,1,2,2,3]
解决方法:
描述:
Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3] ,
Your function should return length = 5, and A is now [1,1,2,2,3]
解决方法:
// 2015.7.3 #include <iostream> using namespace std; int removeDuplicates(int A[],int n); void main() { int A[6]={1,1,1,2,2,3}; cout<<"长度是"<<removeDuplicates(A,6)<<endl; } int removeDuplicates(int A[],int n) { if(n<=2) return 0; int index=2; for(int i=2;i<n;i++) { if(A[index-2]!=A[i]) { A[index++]=A[i]; } } for(int j=0;j<index;j++) { cout<<A[j]<<endl; } return index; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)