Skip to content

Commit

Permalink
add solutions for Remove-Duplicates-from-Sorted-Array
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 committed Feb 20, 2015
1 parent bed6a64 commit dbc61a6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Remove-Duplicates-from-Sorted-Array/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int removeDuplicates(int A[], int n) {
int anchor = 0;

for (int i = 0; i < n; i++, anchor++) {
for (; i + 1 < n && A[i] == A[i+1]; i++);
if (i + 1 < n) {
A[anchor + 1] = A[i + 1];
}
}
return anchor;
}
};

int main() {
return 0;
}
13 changes: 13 additions & 0 deletions Remove-Duplicates-from-Sorted-Array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Solution {
public int removeDuplicates(int[] A) {
int anchor = 0;

for (int i = 0; i < A.length; i++, anchor++) {
for (; i + 1 < A.length && A[i] == A[i+1]; i++);
if (i + 1 < A.length) {
A[anchor + 1] = A[i + 1];
}
}
return anchor;
}
}
14 changes: 14 additions & 0 deletions Remove-Duplicates-from-Sorted-Array/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
anchor = 0
i = 0
while i < len(A):
while i + 1 < len(A) and A[i] == A[i + 1]:
i += 1
if i + 1 < len(A):
A[anchor + 1] = A[i + 1]
anchor += 1
i += 1
return anchor

0 comments on commit dbc61a6

Please sign in to comment.