This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<List<Integer>> generate(int numRows) { | |
List<List<Integer>> result = new ArrayList<List<Integer>>(); | |
for(int i=0;i<numRows;i++){ | |
List<Integer> row = new ArrayList<Integer>(); | |
for(int j=0;j<=i;j++){ | |
if(j==0 || j==i){ | |
row.add(1); | |
} else row.add(result.get(i-1).get(j-1)+result.get(i-1).get(j)); | |
} | |
result.add(row); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void merge(int A[], int m, int B[], int n) { | |
for(int i=m+n-1;i>=0;i--){ | |
if(m>0 && n>0) { | |
if(A[m-1]>B[n-1]){ | |
A[i] = A[m-1]; | |
m--; | |
} else { | |
A[i] = B[n-1]; | |
n--; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] plusOne(int[] digits) { | |
int i=digits.length-1; | |
while(i>=0){ | |
if(digits[i]<9){ | |
digits[i]++; | |
break; | |
} else { | |
digits[i] = 0; | |
i--; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int removeDuplicates(int[] A) { | |
int c=0, d=0; | |
while(d<A.length){ | |
if(d+1<A.length && A[d] == A[d+1]){ | |
d++; | |
} else { | |
A[c] = A[d]; | |
c++; | |
d++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int removeElement(int[] A, int elem) { | |
int c=0; | |
int d=A.length; | |
while(c<d){ | |
if(A[c] == elem){ | |
A[c] = A[d-1]; | |
d--; | |
} else c++; | |
} | |
return d; |
NewerOlder