GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/Remove Duplicates from Sorted Array.cpp at master · handongfeng/leetcode Expand file tree/ Copy pathRemove Duplicates from Sorted Array.cppMore file actions 35 lines (26 loc) · 742 Bytes / Copy pathRemove Duplicates from Sorted Array.cppFile metadata and controls35 lines (26 loc) · 742 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 /* 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]. */ class Solution { public: int removeDuplicates(int A[], int n) { if (n<=1) return n; int j = 0; for(int i=1;i<n;i++) { if(A[i] != A[j]) { j++; int tmp =A[j]; A[j]= A[i]; A[i]=tmp; } } return j+1; } }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |