Skip to content

Commit

Permalink
Update Common Numbers Of Two Sorted Arrays.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ly16 authored Dec 31, 2017
1 parent 8875bf4 commit 8912249
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Common Numbers Of Two Sorted Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ Find all numbers that appear in both of two sorted arrays (the two arrays are al
*/

public class Solution {
public List<Integer> common(List<Integer> A, List<Integer> B) {
// Write your solution here.List<Integer>
List<Integer> common = new ArrayList<Integer>();
int i = 0, j = 0;
while (i < A.size() && j < B.size()) { //size(), length for int[]
if (A.get(i) == B.get(j)) { //.get() for List, A[i] for int[]
common.add(A.get(i));
i++;
j++;
} else if (A.get(i) < B.get(j)) {
i++;
} else {
j++;
}
}
public List<Integer> common(List<Integer> A, List<Integer> B) {
// Write your solution here.
List<Integer> common = new ArrayList<Integer>();
int i = 0;
int j = 0;
while (i < A.size() && j < B.size()) {
if (A.get(i) == B.get(j)) {
common.add(A.get(i));
i++;
j++;
} else if (A.get(i) < B.get(j)) {
i++;
} else {
j++;
}
}
return common;
}
}

0 comments on commit 8912249

Please sign in to comment.