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; |
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[] 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 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 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 List<Integer> getRow(int rowIndex) { | |
List<Integer> result = new ArrayList<Integer>(); | |
for(int i=0; i<=rowIndex; i++){ | |
if(i == 0 ) | |
result.add(1); | |
else{ | |
List<Integer> temp = new ArrayList<Integer>(result); | |
for(int j=1;j<i;j++){ | |
result.set(j, temp.get(j-1) + temp.get(j)); | |
} |
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
import java.util.Hashtable; | |
public class Solution { | |
public int majorityElement(int[] num) { | |
Hashtable<Integer,Integer> count = new Hashtable<Integer, Integer>(); | |
int len = num.length; | |
int curr; | |
for(int i=0; i<len; i++){ | |
curr = num[i]; | |
if(count.containsKey(curr)){ | |
int temp = count.get(curr); |
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
import java.util.Hashtable; | |
public class Solution { | |
public int[] twoSum(int[] numbers, int target) { | |
Hashtable<Integer, Integer> check = new Hashtable<Integer, Integer>(); | |
for(int i = 0; i<numbers.length;i++){ | |
if(check.containsKey(target-numbers[i])) | |
return new int[]{check.get(target-numbers[i]),i+1}; | |
else check.put(numbers[i],i+1); | |
} | |
return null; |
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 threeSumClosest(int[] num, int target) { | |
HashSet<Integer> partSum = new HashSet<Integer>(); | |
partSum.add(num[0]+num[1]); | |
int result=num[0]+num[1]+num[2]; | |
for(int i=2; i<num.length; i++){ | |
for(int n: partSum){ | |
if(Math.abs(n+num[i]-target)<Math.abs(result-target)){ | |
if(n+num[i] == target) | |
return target; | |
else result = n+num[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 List<Integer> spiralOrder(int[][] matrix) { | |
List<Integer> result = new ArrayList<Integer>(); | |
if(matrix == null || matrix.length == 0) | |
return result; | |
int endy=matrix.length; | |
int endx=matrix[0].length; | |
int starty = 0; | |
int startx = 0; | |
while(startx!=endx && starty!=endy){ | |
if(starty < endy){ |
OlderNewer