id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
b73cc7cf-3d52-48c7-9606-81cf98cb1cd8
|
public ListNode mergeKLists(ArrayList<ListNode> lists) {
ListNode res = null;
for(ListNode a: lists)
{
res = mergeLists(res, a);
}
return res;
}
|
cf876dfe-bebb-43fb-8ca1-dca235d0a8ba
|
public ListNode mergeLists(ListNode A, ListNode B)
{
if(A == null) return B;
if(B == null) return A;
ListNode dummy = new ListNode(0);
ListNode currA = A, currB = B, curr = dummy;
while(currA!=null || currB!=null)
{
if(currA == null)
{
curr.next = currB;
currB = currB.next;
}else if(currB == null)
{
curr.next = currA;
currA = currA.next;
}else if(currA.val < currB.val)
{
curr.next = currA;
currA = currA.next;
}else
{
curr.next = currB;
currB = currB.next;
}
curr = curr.next;
}
return dummy.next;
}
|
9b26210d-fdca-40cc-8adb-dc034fa81fb9
|
public ListNode insertionSortList(ListNode head) {
ListNode res = null, curr=head;
while(curr!=null)
{
ListNode next =curr.next;
curr.next = null;
res = InsertNode(curr, res);
curr= next;
}
return res;
}
|
e3b21cc1-3e10-40e2-a38c-99d5020e60dc
|
private ListNode InsertNode(ListNode node, ListNode list)
{
if(list==null) return node;
ListNode dummy =new ListNode(-1);
dummy.next = list;
ListNode curr=list, pre = dummy;
while(curr != null && curr.val<node.val)
{
pre = curr;
curr= curr.next;
}
pre.next = node;
node.next = curr;
return dummy.next;
}
|
24aca5cd-0eab-4ba8-b126-6394a3e0954d
|
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode slow=head, fast = head;
while(fast.next!=null && fast.next.next !=null)
{
fast=fast.next.next;
slow = slow.next;
}
ListNode right = slow.next;
slow.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(right);
return mergeLists(l1, l2);
}
|
578515e8-5652-488e-bc20-4a12a092ce95
|
public static void main (String args[]) {
int A[][] = {{1,2,3},{8,1,4}, {7,6,5}};
int B[] = {2,2,3,2};
HashSet<String> dict = new HashSet<String>();
dict.add("hot");
dict.add("cog");
dict.add("dog");
dict.add("tot");
dict.add("hog");
dict.add("hop");
dict.add("pot");
dict.add("dot");
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
triangle.add(a);
ArrayList<Integer> b = new ArrayList<Integer>();
b.add(2);
b.add(3);
triangle.add(b);
test testA =new test();
testA.singleNumber2(B);
System.out.println();
}
|
30a7d1b1-d19a-4fef-9ece-8b1e98e0843a
|
Interval() { start = 0; end = 0; }
|
f3563786-ac07-4438-abce-c4de6a0904b9
|
Interval(int s, int e) { start = s; end = e; }
|
3037c2b2-56be-4fe8-9da5-42e8b2983241
|
public double findMedianSortedArrays(int A[], int B[]) {
int a = A.length;
int b = B.length;
if((a+b)%2 == 0)
{
double first = find_K(A, 0, B, 0, (a+b)/2);
double second = find_K(A, 0, B, 0, (a+b)/2+1);
return (first+second)/2.0;
}else
{
return find_K(A, 0, B, 0, (a+b)/2+1);
}
}
|
534cb283-1b99-4208-aa27-e03fc8763f6e
|
private int find_K(int A[], int startA, int B[], int startB, int k)
{
if(A.length ==0 || A.length <= startA) return B[startB+k-1];
if(B.length ==0 || B.length <= startB) return A[startA+k-1];
if(k==1)
{
if(A[startA]<B[startB])
return A[startA];
else
return B[startB];
}
int checkAIndex = k/2;
if(startA+checkAIndex > A.length) checkAIndex = A.length -startA;
int checkBIndex = k-checkAIndex;
checkAIndex--;
checkBIndex--;
if(A[startA+checkAIndex] == B[startB+checkBIndex]) return A[startA+checkAIndex];
if(A[startA+checkAIndex] < B[startB+checkBIndex])
{
startA += checkAIndex+1;
k = k-checkAIndex-1;
return find_K(A, startA, B, startB, k);
}else
{
startB += checkBIndex+1;
k = k-checkBIndex-1;
return find_K(A, startA, B, startB, k);
}
}
|
2c6806f0-c4b4-4637-b143-632f5d0dec6e
|
public int singleNumberBF(int[] A) {
ArrayList<Integer> arrlist = new ArrayList<Integer>();
if(A.length == 1) return A[0];
for(int i=1; i<A.length; i++)
{
if(arrlist.contains(i))
arrlist.remove(i);
else
arrlist.add(i);
}
return arrlist.get(0);
}
|
2b4951b4-b704-4559-a06e-042f6bc41f4b
|
public int singleNumber(int[] A) {
int x=0;
if(A.length == 1) return A[0];
for(int i=0; i<A.length; i++)
{
x=x^A[i];
System.out.println(x);
}
return x;
}
|
81460a29-e64b-47f0-942f-07efd19f7011
|
public int longestConsecutive(int[] num) {
HashMap<Integer, Boolean> maps= new HashMap<Integer, Boolean>();
int maxLengh = 0;
for(int i=0; i<num.length; i++)
maps.put(num[i], false);
for(int i=0; i<num.length; i++)
{
if(maps.get(num[i]) ==true) continue;
int length=1;
int j = num[i]+1;
maps.put(num[i], true);
while(maps.containsKey(j))
{
maps.put(j, true);
j++;
length++;
}
j = num[i]-1;
while(maps.containsKey(j))
{
maps.put(j, true);
j--;
length++;
}
if (length > maxLengh) maxLengh = length;
}
return maxLengh;
}
|
8a48a781-999d-43b5-b127-71f3ff638955
|
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> maps = new HashMap<Integer, Integer>();
for(int i=0; i<numbers.length; i++)
{
maps.put(numbers[i], i);
}
for(int i=0; i<numbers.length; i++)
{
if(maps.containsKey(target-numbers[i]))
{
int index1 = i + 1;
int index2 = maps.get(target-numbers[i]) + 1;
if(index1 > index2)
{
return new int[]{index2, index1};
}
else
return new int[]{index1, index2};
}
}
return numbers;
}
|
06a043e7-3275-457e-8a82-f5b071067dae
|
public int[] twoSumBF(int[] numbers, int target) {
for(int i=0; i<numbers.length; i++)
{
for(int j=i+1; j<numbers.length; j++)
{
if (numbers[i] + numbers[j] == target)
return new int[]{i, j};
}
}
return numbers;
}
|
af38943c-7bb3-40a0-bef4-a84a9a27f653
|
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num.length<3) return result;
Arrays.sort(num);
int lastUniqe = num[0];
for(int i=0; i<num.length; i++)
{
if(i==0 || num[i] != lastUniqe)
{
lastUniqe = num[i];
ArrayList<ArrayList<Integer>> two = twoSumInternal(num, 0-num[i], i+1);
if( !two.isEmpty())
{
for(ArrayList<Integer> ele:two)
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(num[i]);
res.add(ele.get(0));
res.add(ele.get(1));
result.add(res);
}
}
}
}
return result;
}
|
754efba9-ca89-4381-9eea-cb415a38817e
|
private ArrayList<ArrayList<Integer>> twoSumInternal(int[] numbers, int target, int startpos) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numbers.length<2) return result;
HashMap<Integer, Integer> maps = new HashMap<Integer, Integer>();
int lastUniqe = numbers[0];
for(int i=startpos; i<numbers.length; i++)
{
maps.put(numbers[i], i);
}
for(int i=startpos; i<numbers.length; i++)
{
if(i==startpos || numbers[i] != lastUniqe)
{
lastUniqe = numbers[i];
if(maps.containsKey(target-numbers[i])&& i< maps.get(target-numbers[i]))
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(numbers[i]);
res.add(target-numbers[i]);
result.add(res);
}
}
}
return result;
}
|
0465773e-c39d-490c-a146-44f1af352f46
|
public int threeSumClosest(int[] num, int target) {
int result = 0;
int gap = Integer.MAX_VALUE;
if(num.length<3) return 0;
Arrays.sort(num);
for(int i=0; i<num.length-2; i++)
{
int two = twoSumClosest(num, target-num[i], i+1);
if(Math.abs(num[i]+two-target) < gap)
{
result = num[i]+two;
gap = Math.abs(num[i]+two-target);
if(result==target) return target;
}
}
return result;
}
|
7cf5f166-dae6-4a10-b753-b679cfdb8d29
|
private int twoSumClosest(int[] numbers, int target, int startpos) {
int result = 0;
int gap = Integer.MAX_VALUE;
int start = startpos;
int end = numbers.length -1;
while (start<end)
{
if(Math.abs(numbers[start] + numbers[end] - target) < gap)
{
gap = Math.abs(numbers[start] + numbers[end] - target);
result = numbers[start] + numbers[end];
}
int sum = numbers[start] + numbers[end];
if(sum>target) end--;
if(sum<target) start ++;
if(sum==target) return target;
}
return result;
}
|
33eb5f03-0fd4-455a-8b5f-ec311fc5c77d
|
public int removeElement(int[] A, int elem) {
int newindex = 0;
for(int i=0; i< A.length; i++)
{
if (A[i] != elem)
{
A[newindex] = A[i];
newindex++;
}
}
return newindex;
}
|
c2353ef0-10b8-4a4c-b17c-ad917f855ae8
|
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
for(int i=0; i<num.length-3; i++)
{
int a = num[i];
for (int j=i+1; j<num.length-2; j++)
{
int b=num[j];
int start = j+1;
int end = num.length-1;
while(start < end)
{
int c = num[start];
int d = num[end];
int sum = a + b + c + d;
if(sum == target)
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(a);
res.add(b);
res.add(c);
res.add(d);
results.add(res);
start++;
end--;
}
if(sum>target) end--;
if(sum<target) start++;
}
}
}
HashSet<String> unique = new HashSet<String>();
for(int i=results.size()-1; i>=0; i--)
{
String converted = results.get(i).toString();
if(unique.contains(converted))
{
results.remove(i);
}else
{
unique.add(converted);
}
}
return results;
}
|
d6dde750-dcde-439b-8f38-1c2c8860240e
|
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<candidates.length; i++)
{
}
HashSet<String> unique = new HashSet<String>();
for(int i=results.size()-1; i>=0; i--)
{
String converted = results.get(i).toString();
if(unique.contains(converted))
{
results.remove(i);
}else
{
unique.add(converted);
}
}
return results;
}
|
541d7801-3166-4173-8cf3-ee63106a9a0a
|
public void nextPermutation1(int[] num) {
if(num.length<=1) return;
if(IsLargest(num, 0))
{
Arrays.sort(num);
return;
}
int startpos=0;
while(startpos<num.length)
{
if(!IsLargest(num, startpos+1))
{
startpos++;
}else
{
int oldStart = num[startpos];
Arrays.sort(num, startpos, num.length);
for(int i=startpos; i<num.length; i++)
{
if(num[i]>oldStart)
{
int newstart = num[i];
for(int j=i; j>startpos; j--)
{
num[j] = num[j-1];
}
num[startpos] = newstart;
return;
}
}
}
}
}
|
e8c36fa4-2cda-462d-ad15-9a5f968305a8
|
private boolean IsLargest(int[] num, int startPos)
{
if (startPos>=num.length-1) return true;
for(int i=startPos; i< num.length-1; i++)
{
if(num[i] >= num[i+1])
continue;
else
return false;
}
return true;
}
|
4b6621d1-c941-4131-a140-56683a2f8386
|
public void nextPermutation(int[] num) {
if(num.length<=1) return;
int startpos=num.length-1;
while(startpos>0)
{
if(num[startpos] <= num[startpos-1])
{
startpos--;
}else
{
for(int i=num.length-1; i>startpos-1; i--)
{
if(num[i]>num[startpos-1])
{
int newstart = num[i];
num[i] = num[startpos-1];
num[startpos-1] =newstart;
Arrays.sort(num, startpos, num.length);
return;
}
}
}
}
Arrays.sort(num);
}
|
85e04a6d-ff0f-40e1-85de-235c7e440991
|
public String getPermutation(int n, int k) {
String result = "";
ArrayList<Integer> num = new ArrayList<Integer>();
//construct the first element
for(Integer i=0; i<n; i++)
{
num.add(i+1);
}
int skipnumber = factorial(n-1);
for(int i=n; i>1; i-- )
{
int a = k/skipnumber;
if(k % skipnumber ==0 && a>0)
{
a--;
}
Integer value = num.get(a);
result = result + value.toString();
num.remove(a);
k = k - a*skipnumber;
skipnumber = skipnumber/(i-1);
}
return result + num.get(0);
}
|
3fe83106-949e-4020-91dd-c111b76777c3
|
private int factorial(int n)
{
int result= 1;
for(int i=1; i<=n; i++)
{
result *=i;
}
return result;
}
|
8d1b80d1-9d65-48d4-9753-9a73bc1b4508
|
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
return permuteInternal(num, 0);
}
|
0effe987-c0ce-4f8d-8633-f0cea86481cb
|
private ArrayList<ArrayList<Integer>> permuteInternal(int[] num, int start)
{
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
if(start>=num.length-1)
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(num[start]);
results.add(res);
return results;
}
int i = num[start];
ArrayList<ArrayList<Integer>> subResults = permuteInternal(num, start+1);
for(ArrayList<Integer> sres:subResults)
{
for(int j=0; j<=sres.size(); j++)
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.addAll(sres);
res.add(j, i);
results.add(res);
}
}
return results;
}
|
6974d7da-622c-4e4a-9e96-efca21561676
|
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
results = permuteInternalUnique(num, 0);
return results;
}
|
d0785ff4-9f55-48b8-b408-535480321c87
|
private ArrayList<ArrayList<Integer>> permuteInternalUnique(int[] num, int start)
{
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
if(start>=num.length-1)
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(num[start]);
results.add(res);
return results;
}
int i = num[start];
ArrayList<ArrayList<Integer>> subResults = permuteInternalUnique(num, start+1);
for(ArrayList<Integer> sres:subResults)
{
for(int j=0; j<=sres.size(); j++)
{
if( j ==sres.size() || i != sres.get(j))
{
ArrayList<Integer> res = new ArrayList<Integer>();
res.addAll(sres);
res.add(j, i);
results.add(res);
}
}
}
return results;
}
|
7ecd967c-7094-40df-83b7-08bd9b22d1b1
|
public boolean isValidSudoku(char[][] board) {
Boolean[] used = new Boolean[9];
for(int i=0; i<9; i++)
{
for(int k=0; k<9; k++)
used[k]=false;
for(int j=0; j<9; j++)
{
char value = board[i][j];
if(value !='.')
{
int number = Character.getNumericValue(value);
if(used[number] ==false)
used[number] = true;
else
return false;
}
}
for(int k=0; k<9; k++)
used[k]=false;
for(int j=0; j<9; j++)
{
char value = board[j][i];
if(value !='.')
{
int number = Character.getNumericValue(value);
if(used[number] ==false)
used[number] = true;
else
return false;
}
}
}
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
int rstart=x*3;
int rend = rstart+ 3;
int cstart = y*3;
int cend = cstart + 3;
for(int k=0; k<9; k++)
used[k]=false;
for(int i=rstart; i<rend; i++)
{
for(int j=cstart; j<cend; j++)
{
char value = board[j][i];
if(value !='.')
{
int number = Character.getNumericValue(value);
if(used[number] ==false)
used[number] = true;
else
return false;
}
}
}
}
}
return true;
}
|
fac77a76-bffc-471a-8edc-8312e225ff04
|
public int trap(int[] A) {
if(A.length<3) return 0;
int water = 0;
int maxLeft[] = new int[A.length];
maxLeft[0] = 0;
int maxRight[] = new int[A.length];
maxRight[A.length-1] = 0;
for(int i=1; i<A.length; i++)
{
if(maxLeft[i-1] < A[i-1])
{
maxLeft[i] = A[i-1];
}else
{
maxLeft[i] = maxLeft[i-1];
}
}
for(int i=A.length-2; i>=0; i--)
{
if(maxRight[i+1] < A[i+1])
{
maxRight[i] = A[i+1];
}else
{
maxRight[i] = maxRight[i+1];
}
}
for(int i=1; i<A.length; i++)
{
if(Math.min(maxLeft[i], maxRight[i]) > A[i])
water += Math.min(maxLeft[i], maxRight[i]) - A[i];
}
return water;
}
|
8e42365c-4173-47b0-b9b4-c6a5c6a11fba
|
public int climbStairsRecursion(int n) {
if(n==1) return 1;
if (n==2) return 2;
return climbStairsRecursion(n-1) + climbStairsRecursion(n-2);
}
|
eecfe633-4e68-4cae-aca8-ac76a9a0282a
|
public int climbStairs(int n) {
if(n==1) return 1;
if (n==2) return 2;
int[] num= new int[n+1];
num[1] = 1;
num[2] = 2;
for(int i=3; i<=n; i++)
{
num[i] = num[i-1] + num[i-2];
}
return num[n];
}
|
7813d12b-c38d-44a6-a694-046f5c4065fc
|
public int candy(int[] ratings) {
int[] candy = new int[ratings.length];
int res = 0;
candy[0] = 1;
for(int i=1; i<ratings.length;i++)
{
if(ratings[i]>ratings[i-1])
{
candy[i] = candy[i-1] + 1;
}else
{
candy[i] = 1;
}
}
for(int i=ratings.length-2; i>=0;i--)
{
if(ratings[i]>ratings[i+1])
{
candy[i] = Math.max(candy[i], candy[i+1] + 1) ;
}
}
for(int i=1; i<ratings.length;i++)
{
res +=candy[i];
}
return res;
}
|
88eb1cab-8478-4616-a3f5-a02795dbc822
|
public void rotate(int[][] matrix) {
int temp = 0;
int midWidh = (matrix.length+1)/2;
int midHeigh = matrix.length/2;
int n = matrix.length;
if(n==1) return;
for(int i=0; i<midWidh; i++)
{
for(int j=0; j<midHeigh; j++)
{
temp = matrix[j][i];
matrix[j][i] = matrix[n-1-i][j];
matrix[n-1-i][j] = matrix[n-1-j][n-1-i];
matrix[n-1-j][n-1-i] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp;
}
}
}
|
4357559e-3810-44e8-aa5e-a2b5d2fdbf9c
|
public int[] plusOne(int[] digits) {
for(int i=digits.length-1; i>=0; i--)
{
if(digits[i] ==9)
{
digits[i] = 0;
}else
{
digits[i] ++;
return digits;
}
}
int[] result = new int[digits.length+1];
Arrays.fill(result, 0);
result[0] = 1;
return digits;
}
|
b4769592-9df0-4527-98d8-7fc104e707ce
|
public void setZeroes(int[][] matrix) {
boolean[] row = new boolean[matrix.length];
boolean[] col = new boolean[matrix[0].length];
for(int i=0; i<matrix.length; i++)
{
for(int j=0; j<matrix[i].length; j++)
{
if(matrix[i][j] ==0)
{
row[i] = true;
col[j] = true;
}
}
}
for(int i=0; i<matrix.length; i++)
{
for(int j=0; j<matrix[i].length; j++)
{
if(row[i] || col[j])
matrix[i][j]=0;
}
}
}
|
fb93dd8e-3a99-4df5-8a25-3be171272651
|
public boolean searchMatrix1(int[][] matrix, int target) {
int start = 0;
int end = matrix.length-1;
int row=0;
while(start<=end)
{
int midrow = (start + end)/2;
if(target<matrix[midrow][0])
{
end = midrow-1;
}else if(target>=matrix[midrow][0] && (midrow==matrix.length-1 || target<matrix[midrow+1][0]))
{
row = midrow;
break;
}else
{
start = midrow++;
}
}
start = 0;
end = matrix[0].length-1;
while(start<=end)
{
int mid = (start + end)/2;
if(target<matrix[row][mid])
{
end = mid-1;
}else if(target==matrix[row][mid])
{
return true;
}else
{
start = mid+1;
}
}
return false;
}
|
304aa374-2129-4774-bbe0-9f8bb6f472ac
|
public boolean searchMatrix(int[][] matrix, int target) {
int start = 0;
int end = matrix.length *matrix[0].length-1;
int row=0;
while(start<=end)
{
int mid = (start + end)/2;
int x = mid/matrix[0].length;
int y = mid % matrix[0].length;
if(target<matrix[x][y])
{
end = mid-1;
}else if(target==matrix[x][y])
{
return true;
}else
{
start = mid+1;
}
}
/*start = 0;
end = matrix[0].length-1;
while(start<=end)
{
int mid = (start + end)/2;
if(target<matrix[row][mid])
{
end = mid-1;
}else if(target==matrix[row][mid])
{
return true;
}else
{
start = mid++;
}
}*/
return false;
}
|
6bf634f4-c071-44f0-acd4-9e7fa29af378
|
public int canCompleteCircuit(int[] gas, int[] cost) {
int res = -1;
int n = gas.length;
int total = 0;
int[] gap = new int[n];
for(int i=0, j=0; i<n; i++, j++)
{
if(i==0)
gap[i] = gas[i] - cost[i];
else
gap[i] = gap[i-1] + gas[i] - cost[i];
total += gas[i] - cost[i];
}
if(total<0)
return res;
{
int least = gap[0];
res = 0;
for(int i=1; i<n; i++)
{
if(gap[i] <= least)
{
least = gap[i];
res = i;
}
}
res = res + 1;
if(res == n) res =0;
return res;
}
}
|
b87f5055-1611-46ab-8db4-072ea04808a7
|
public int largestRectangleAreaTLE(int[] height) {
int [] area = new int[height.length];
int [] area2 = new int[height.length];
for(int i=0; i<height.length; i++)
{
int left = height[i];
int unit = 1;
for(int j=i+1; j<height.length; j++)
{
if(height[j]>=left)
{
unit++;
if(j==height.length-1)
{
area[i] = unit*left;
}
}
else
{
area[i] = unit*left;
break;
}
}
}
for(int i=height.length-1; i>=0; i--)
{
int right = height[i];
int unit = 1;
for(int j=i-1; j>=0; j--)
{
if(height[j]>=right)
{
unit++;
if(j==0)
{
area2[i] = unit*right;
}
}
else
{
area2[i] = unit*right;
break;
}
}
}
int max = 0;
for(int i=0; i<height.length; i++)
{
int larger = Math.max(area[i], area2[i]);
if(larger > max)
max = larger;
}
return max;
}
|
bebce364-8702-4a62-bfa5-e84faf29cf71
|
public void merge(int A[], int m, int B[], int n) {
int i=m-1,j=n-1, k=m+n-1;
while(i>=0 && j>=0)
{
if(A[i] > B[j])
{
A[k] = A[i];
i--;
}else
{
A[k] = B[j];
j--;
}
k--;
}
while(j>=0)
{
A[k] = B[j];
j--;
k--;
}
}
|
f20360af-955a-418d-90be-3c785aa76756
|
public int firstMissingPositive(int[] A) {
for(int i =1; i<=A.length; i++)
{
int j;
for(j=0; j<A.length; j++)
{
if(A[j] == i) break;
}
if(j==A.length) return i;
}
return A.length+1;
}
|
71ea72f5-f5c5-4f53-84d4-63debb776f37
|
public void sortColors(int[] A) {
quicksort(A, 0);
quicksort(A, 1);
}
|
8ab3e1ec-30e5-469c-844e-e9ba6d25fa17
|
private void quicksort(int[] A, int mid)
{
int i=0, j=A.length-1;
while(i<j)
{
if(A[i]<=mid)
i++;
else if(A[j]>mid)
j--;
else{
int t = A[i];
A[i] = A[j];
A[j] = t;
i++;
j--;
}
}
}
|
76456de7-1b3c-4f15-be52-c8aae77b0130
|
public int[] searchRange(int[] A, int target) {
int l=0, r=A.length-1, mid=A.length/2;
boolean bfound = false;
while(l<=r)
{
if(A[mid]<target)
{
l=mid+1;
mid=(l+r)/2;
}else if(A[mid]>target)
{
r=mid-1;
mid=(l+r)/2;
}else
{
bfound = true;
break;
}
}
if(!bfound)
{
int[] res = {-1, -1};
return res;
}
int min = mid, max = min;
while(min>=0 && A[min] == target)
{
min--;
}
while(max<A.length && A[max] == target)
{
max++;
}
int[] res = {min+1, max-1};
return res;
}
|
b5dfe431-e9e4-4ff5-9b1d-e2e0a8c9eda0
|
public int searchInsert(int[] A, int target) {
int l=0, r=A.length-1, mid=A.length/2;
while(l<=r)
{
if(A[mid]<target)
{
l=mid+1;
mid=(l+r)/2;
}else if(A[mid]>target)
{
r=mid-1;
mid=(l+r)/2;
}else
{
return mid;
}
}
if(A[mid]>target)
return mid;
else
return mid+1;
}
|
bd29edd5-acd2-4e2e-a2fd-16d4ee3c63c9
|
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
Arrays.sort(S);
return subsets(S, 0);
}
|
261ab4e8-0837-4948-89df-96f49c9e748e
|
private ArrayList<ArrayList<Integer>> subsets(int[] S, int startpos)
{
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(startpos == S.length-1)
{
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(S[startpos]);
res.add(t);
res.add(new ArrayList<Integer>());
}else
{
ArrayList<ArrayList<Integer>> r = subsets(S, startpos+1);
res.addAll(r);
for(ArrayList<Integer> rt:r)
{
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(S[startpos]);
t.addAll(rt);
if(!res.contains(t))res.add(t);
}
}
return res;
}
|
246f5a32-4030-4dec-8c6f-1d75bccbf942
|
public static void main (String args[]) {
int A[] = {2,1};
int B[] = {1,2};
test testA =new test();
testA.canCompleteCircuit(B, A);
System.out.println();
}
|
dffb59d9-6c9b-434d-93e8-c8dbbd3d0029
|
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
Arrays.sort(num);
return subsetsWithDup(num, 0);
}
|
a1d58eff-39d5-4c7a-904a-2ba4d40b4376
|
public boolean isValid(String s) {
Stack<Character> a =new Stack<Character>();
for(char x:s.toCharArray())
{
if(x=='[' || x=='(' || x=='{' )
{
a.push(x);
}else
{
if(a.empty()) return false;
char y=(Character)a.pop();
if((x==']' && y=='[') || (x=='}' && y=='{') || (x==')' && y=='(') )
{
}else
{
return false;
}
}
}
if(!a.empty()) return false;
return true;
}
|
3718d186-529e-4b4a-ad59-bb0a53ef5ae7
|
public int longestValidParentheses(String s) {
Stack<Character> a =new Stack<Character>();
int size = 0;
int longest = 0;
char[] arrays = s.toCharArray();
for(char x:arrays)
{
if(x=='(')
{
a.push(x);
}else
{
if(a.empty())
{
size = 0;
}else
{
size++;
if(a.empty() && size>longest) longest =size;
}
}
}
size = 0;
a.clear();
for(int i = arrays.length-1; i>=0; i--)
{
char x = arrays[i];
if(x==')')
{
a.push(x);
}else
{
if(a.empty())
{
size = 0;
}else
{
size++;
if(a.empty() && size>longest) longest =size;
}
}
}
return longest*2;
}
|
0f878fc1-15b3-490d-b4d3-f6d3f2f54b43
|
public int evalRPN(String[] tokens) {
Stack<Integer> cal = new Stack<Integer>();
for(int i=0; i<tokens.length; i++)
{
try{
int num = Integer.parseInt(tokens[i]);
cal.push(num);
} catch (NumberFormatException e) {
int op2 = (int) cal.pop();
int op1 = (int) cal.pop();
switch(tokens[i])
{
case "+":
cal.push(op1 + op2);
break;
case "-":
cal.push(op1 - op2);
break;
case "*":
cal.push(op1 * op2);
break;
case "/":
cal.push(op1 / op2);
break;
default:
return 0;
}
}
}
return (int) cal.pop();
}
|
913f1291-9bba-4b65-99c0-2ed3fef5e568
|
private ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S, int startpos)
{
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(startpos == S.length-1)
{
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(S[startpos]);
res.add(t);
res.add(new ArrayList<Integer>());
}else
{
ArrayList<ArrayList<Integer>> r = subsetsWithDup(S, startpos+1);
res.addAll(r);
int prenumber = 0;
int i = startpos+1;
while(i<S.length && S[i]==S[startpos])
{
i++;
prenumber++;
}
for(ArrayList<Integer> rt:r)
{
if(prenumber==0 || (rt.size() >= prenumber && rt.get(prenumber-1)==S[startpos])){
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(S[startpos]);
t.addAll(rt);
res.add(t);
}
}
}
return res;
}
|
4062d381-55e5-4f72-b2aa-9de3543a12a4
|
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(k==0 || k>n) return res;
if(k==1)
{
for(int i=1; i<=n; i++)
{
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(i);
res.add(t);
}
return res;
}
ArrayList<ArrayList<Integer>> res1 = combine(n-1, k);
ArrayList<ArrayList<Integer>> res2 = combine(n-1, k-1);
res.addAll(res1);
for(ArrayList<Integer> r:res2)
{
r.add(n);
res.add(r);
}
return res;
}
|
9821f991-3b8f-41cb-8023-b87cdbe935e5
|
public double pow(double x, int n) {
if(n==0) return 1;
if(n==1) return x;
if(n==-1) return 1/x;
double res = pow(x, n/2);
return res * res * pow(x, n-(n/2)*2);
}
|
159e0368-9191-40d8-a712-50547dce3884
|
public int sqrt(int x) {
if(x<=1) return x;
int l=1, r=x/2, mid, res=0;
while(l<=r)
{
mid = (l+r)/2;
if(x/mid == mid)
return mid;
else if(x/mid < mid)
r=mid-1;
else{
l=mid+1;
res = mid;
}
}
return res;
}
|
67ba18bd-54ee-444a-8689-a77ca2fa7af2
|
public boolean canJump(int[] A) {
if(A.length<=1) return true;
int reach = 1;
for(int i=0; i<A.length-1; i++)
{
reach = Math.max(reach-1, A[i]);
if(reach<=0) return false;
}
return reach >=0;
}
|
3d06189e-bd6c-4146-a162-6c969c664898
|
public int jump(int[] A) {
if(A.length<=1) return 0;
int left = 1;
int end = A[0];
int step = 1;
while(end<A.length)
{
int max = 0;
int i = left;
for(i=left; i<=end; i++)
{
if(i>=A.length-1) return step;
max = Math.max(max, i+A[i]);
}
step++;
left = i;
end = max;
}
return step;
}
|
826dcf77-afd2-4313-9248-a31005ce0c4b
|
public int maxProfit1(int[] prices) {
if(prices.length<=1) return 0;
int min=prices[0];
int gap = 0;
for(int i=1; i<prices.length; i++)
{
min = Math.min(min, prices[i]);
gap = Math.max(gap, prices[i]-min);
}
return gap;
}
|
365ddfbd-35c1-4a26-9e0e-ca440beefa06
|
public int maxProfit2(int[] prices) {
if(prices.length<=1) return 0;
int min=prices[0];
int gap = 0;
for(int i=1; i<prices.length; i++)
{
if(prices[i]<prices[i-1])
min = Math.min(min, prices[i]);
else
gap += prices[i]-prices[i-1];
}
return gap;
}
|
2f46d754-8f22-49ea-a168-23d553fecfec
|
public int maxProfit(int[] prices) {
int sum = 0;
for(int i=1; i<prices.length-1; i++)
{
int[] left = Arrays.copyOfRange(prices, 0, i);
int[] right = Arrays.copyOfRange(prices, i, prices.length);
sum = Math.max(sum, maxProfit1(left)+ maxProfit1(right));
}
return sum;
}
|
59803aa7-72f1-44f4-8f5f-05b03ad3170b
|
public int maxArea(int[] height) {
if(height.length<=1) return 0;
int left = 0;
int right = height.length-1;
int max = 0;
while(left<right)
{
int t = Math.min(height[left], height[right]);
max = Math.max(max, t*(right-left));
if(height[left] <= height[right])
left++;
else
right--;
}
return max;
}
|
489ce446-6965-4f44-998c-956cc0d84185
|
public int ladderLength(String start, String end, HashSet<String> dict) {
if (dict.size() == 0)
return 0;
LinkedList<String> wordQueue = new LinkedList<String>();
LinkedList<Integer> distanceQueue = new LinkedList<Integer>();
wordQueue.add(start);
distanceQueue.add(1);
while(!wordQueue.isEmpty()){
String currWord = wordQueue.pop();
Integer currDistance = distanceQueue.pop();
if(currWord.equals(end)){
return currDistance;
}
for(int i=0; i<currWord.length(); i++){
char[] currCharArr = currWord.toCharArray();
for(char c='a'; c<='z'; c++){
currCharArr[i] = c;
String newWord = new String(currCharArr);
if(newWord == end) return currDistance+1;
if(dict.contains(newWord)){
wordQueue.add(newWord);
distanceQueue.add(currDistance+1);
dict.remove(newWord);
}
}
}
}
return 0;
}
|
3351be6c-1789-43c2-9489-5400ce12c16b
|
public ArrayList<ArrayList<String>> findLaddersETL(String start, String end, HashSet<String> dict) {
ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
HashMap<String, ArrayList<ArrayList<String>>> path = new HashMap<String, ArrayList<ArrayList<String>>>();
HashSet<String> used = new HashSet<String>();
if(dict.size() ==0 || start==end) return res;
ArrayList<String> word = new ArrayList<String>();
word.add(start);
ArrayList<Integer> step = new ArrayList<Integer>();
step.add(0);
ArrayList<String> pathSteps = new ArrayList<String>();
pathSteps.add(start);
ArrayList<ArrayList<String>> pathList = new ArrayList<ArrayList<String>>();
pathList.add(pathSteps);
path.put(start, pathList);
int minStep = -1;
int level = 0;
while(word.size()>0)
{
String currentWord = word.get(0);
word.remove(0);
Integer currentStep = step.get(0);
step.remove(0);
if(currentStep>level)
{
level = currentStep;
dict.removeAll(used);
}
if(currentStep+1>minStep && minStep >0) break;
for(int i=0; i<currentWord.length(); i++)
{
for(char c='a'; c<='z'; c++)
{
char[] wordArray = currentWord.toCharArray();
if(c==wordArray[i]) continue;
wordArray[i] = c;
String newword =new String(wordArray);
if(newword.equals(end))
{
minStep = currentStep+1;
}
if(dict.contains(newword) || newword.equals(end))
{
word.add(newword);
step.add(currentStep+1);
used.add(newword);
ArrayList<ArrayList<String>> oldList = path.get(currentWord);
ArrayList<ArrayList<String>> newlist = new ArrayList<ArrayList<String>>();
if(path.containsKey(newword))
{
newlist = path.get(newword);
}
for(ArrayList<String> steps:oldList)
{
ArrayList<String> newSteps = new ArrayList<String>();
newSteps.addAll(steps);
newSteps.add(newword);
newlist.add(newSteps);
}
if(!path.containsKey(newword))
path.put(newword, newlist);
}
}
}
}
if(path.containsKey(end)) res = path.get(end);
return res;
}
|
021555d2-465d-41a6-bbff-bd5c8a0f0c4b
|
public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
HashMap<String, HashSet<String>> visited = new HashMap<String, HashSet<String>>();
HashMap<String, Integer> level = new HashMap<String, Integer>();
LinkedList<String> queue = new LinkedList<String>();
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
if(start == null || end == null || start.length()!=end.length()) return result;
HashSet<String> path = new HashSet<String>();
int minLen = Integer.MAX_VALUE;
visited.put(start, path);//only difference from word ladder I
level.put(start, 1);
queue.add(start);
while(!queue.isEmpty()){
String head = queue.remove();
char[] chars = head.toCharArray();
for(int i = 0; i < head.length(); i++){
char old = chars[i];
for(char letter = 'a'; letter <= 'z'; letter++){
chars[i] = letter;
String nextWord = new String(chars);
//two possible situations
//level does not contain nextWord
//level contains nextWord and near the start
if(dict.contains(nextWord) && (!level.containsKey(nextWord)
|| (level.containsKey(nextWord) && level.get(nextWord) > level.get(head)))){
//we update the path, visited, level
if(visited.containsKey(nextWord)){
visited.get(nextWord).add(head);
}else{
path = new HashSet<String>();
path.add(head);
visited.put(nextWord, path);
level.put(nextWord, level.get(head) + 1);
queue.add(nextWord);
}
}
if(nextWord.equals(end)){
if(level.get(head) < minLen){
ArrayList<String> entry = new ArrayList<String>();
entry.add(end);
result.addAll(backtrace(head,visited,entry));
minLen = level.get(head)+1;
}else{
break;
}
}
chars[i] = old;
}
}
}
return result;
}
|
8e1ce29c-3eee-495e-aedb-d0f2bebaa5f4
|
private ArrayList<ArrayList<String>> backtrace(String end,
HashMap<String, HashSet<String>> visited, ArrayList<String> path) {
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
ArrayList<String> entry = new ArrayList<String>(path);
entry.add(0,end);
if(visited.get(end).size()<1){
result.add(entry);
return result;
}
for(String str : visited.get(end)){
result.addAll(backtrace(str,visited,entry));
}
return result;
}
|
0d4ec012-b840-4052-bd05-2ea7190f30d3
|
public void solve(char[][] board) {
if(board.length==0 || board[0].length==0) return;
int length = board.length;
int width = board[0].length;
ArrayList<Integer> Iindexes =new ArrayList<Integer>();
ArrayList<Integer> Jindexes =new ArrayList<Integer>();
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
{
if(board[i][j] == 'O' && (i==0||i==length-1||j==0||j==width-1))
{
Iindexes.add(i);
Jindexes.add(j);
}else
continue;
}
}
while(!Iindexes.isEmpty())
{
int x = Iindexes.get(0);
int y = Jindexes.get(0);
Iindexes.remove(0);
Jindexes.remove(0);
board[x][y] = 'p';
if(x>0 && board[x-1][y] == 'O')
{
Iindexes.add(x-1);
Jindexes.add(y);
}
if(x<length-1 && board[x+1][y] == 'O')
{
Iindexes.add(x+1);
Jindexes.add(y);
}
if(y>0 && board[x][y-1] == 'O')
{
Iindexes.add(x);
Jindexes.add(y-1);
}
if(y<width-1 && board[x][y+1] == 'O')
{
Iindexes.add(x);
Jindexes.add(y+1);
}
}
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
{
if(board[i][j] =='o'|| board[i][j] == 'O')board[i][j] = 'X';
if(board[i][j] =='p')board[i][j] = 'O';
}
}
}
|
bb75099b-d5ef-411c-b3f3-fd41e965cb80
|
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
if(triangle.size() ==0) return 0;
int[] minvalues = new int[triangle.size()];
int min = Integer.MAX_VALUE;
for(int row =0; row<triangle.size(); row ++)
{
for(int index = triangle.get(row).size()-1; index >=0; index--)
{
int res = getMin(row, index, triangle, minvalues);
if(row==triangle.size()-1)
{
min = Math.min(min, res);
}
}
}
return min;
}
|
976b361f-93da-45f0-a35f-5c22dccfadfd
|
private int getMin(int row, int index, ArrayList<ArrayList<Integer>> triangle, int[] minvalues)
{
int val = triangle.get(row).get(index);
int minvalue = 0;
if(row == 0)
{
minvalue = val;
}else
{
if(index==0)
{
minvalue = minvalues[index] + val;
}
else if(index==triangle.get(row).size()-1)
{
minvalue = minvalues[index-1] + val;
}
else
{
minvalue = Math.min(val+minvalues[index], val+minvalues[index-1]);
}
}
minvalues[index] = minvalue;
return minvalue;
}
|
fed20eaf-6ac5-4e31-a780-1f855ccb7672
|
public int maxSubArray(int[] A) {
int[] largest = new int[A.length];
largest[0] = A[0];
for(int i=1; i<A.length; i++)
{
if(largest[i-1]<0)
{
largest[i] = A[i];
}else
{
largest[i] = largest[i-1]+A[i];
}
}
int max = A[0];
for(int i=1; i<A.length; i++)
{
max = Math.max(max, largest[i]);
}
return max;
}
|
769463e9-5ea0-4170-89a5-978a7909a078
|
public ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<numRows; i++)
{
ArrayList<Integer> row = new ArrayList<Integer>();
row.add(1);
if(i>0)
{
ArrayList<Integer> prevrow = res.get(i-1);
for(int j=1; j<prevrow.size(); j++)
{
row.add(prevrow.get(j-1)+prevrow.get(j));
}
row.add(1);
}
res.add(row);
}
return res;
}
|
41522528-2377-480d-b5d1-20664940184b
|
public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<Integer>();
for(int i=0; i<rowIndex+1; i++)
{
res.add(0, 1);
for(int j=1; j<res.size()-1; j++)
{
res.set(j, res.get(j+1)+res.get(j));
}
}
return res;
}
|
3d83919b-9ece-4033-bf84-b94f63d56d1e
|
public int minPathSum(int[][] grid) {
int[][] sum = new int[grid.length][grid[0].length];
sum[0][0] = grid[0][0];
for(int i=1; i<grid.length; i++)
sum[i][0] = grid[i][0] + sum[i-1][0];
for(int j=1; j<grid[0].length; j++)
sum[0][j] = grid[0][j] + sum[0][j-1];
for(int i=1; i<grid.length; i++)
{
for(int j=1; j<grid[i].length; j++)
{
sum[i][j] = Math.min(sum[i-1][j], sum[i][j-1])+ grid[i][j];
}
}
return sum[grid.length-1][grid[0].length-1];
}
|
fc1d74a8-3daf-4bfe-bf8c-21e8444badaf
|
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(matrix.length==0 || matrix[0].length==0) return res;
int left = 0, top = 0;
int right = matrix[0].length, bottom = matrix.length;
while(top<bottom&&left<right)
{
loop(matrix, res, left, top, right, bottom);
left++;
right--;
top++;
bottom--;
}
return res;
}
|
973dae6e-3f00-41dd-b510-17be9e805714
|
private void loop(int[][] matrix, ArrayList<Integer> res, int left, int top, int right, int bottom)
{
for(int j=left; j<right; j++)
res.add(matrix[top][j]);
for(int i=top+1; i<bottom; i++)
res.add(matrix[i][right-1]);
for(int j=right-2; j>=left && top<bottom-1; j--)
res.add(matrix[bottom-1][j]);
for(int i=bottom-2; i>top && left<right-1; i--)
res.add(matrix[i][left]);
}
|
20726559-cd1f-4f22-b33c-b92102c5723f
|
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int size = n;
int startNumber = 1;
int startpos = 0;
while(size>0)
{
loop(res, startpos, size, startNumber);
startNumber= startNumber+(size-1)*4;
startpos++;
size=size-2;
}
return res;
}
|
7930b013-f458-4746-b544-ed392b2e0036
|
private void loop(int[][] res, int startpos, int size, int startNumber)
{
if(size==1)
{
res[startpos][startpos] = startNumber;
return;
}
for(int j=startpos; j<startpos+size-1; j++, startNumber++)
res[startpos][j]=startNumber;
for(int i=startpos; i<startpos+size-1; i++, startNumber++)
res[i][startpos+size-1]=startNumber;
for(int j=startpos+size-1; j>startpos; j--, startNumber++)
res[startpos+size-1][j]=startNumber;
for(int i=startpos+size-1; i>startpos; i--, startNumber++)
res[i][startpos]=startNumber;
}
|
c4950fb7-cfca-4336-a84f-eec553848736
|
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
if(intervals.size() == 0)
{
intervals.add(newInterval);
return intervals;
}
boolean bMerged = false;
for(int i=0; i<intervals.size(); i++){
if(!bMerged && intervals.get(i).end<newInterval.start)
{
if(i==intervals.size()-1) intervals.add(newInterval);
continue;
}
if(intervals.get(i).start>newInterval.end)
{
if(!bMerged) intervals.add(i, newInterval);
break;
}
if(bMerged)
{
intervals.get(i-1).end = Math.max(newInterval.end, intervals.get(i).end);
intervals.remove(i);
i--;
}
if(!bMerged && intervals.get(i).end>=newInterval.start)
{
intervals.get(i).end = Math.max(newInterval.end, intervals.get(i).end);
bMerged = true;
}
if(intervals.get(i).start<=newInterval.end)
{
intervals.get(i).start = Math.min(newInterval.start, intervals.get(i).start);
bMerged = true;
}
}
return intervals;
}
|
1cbddbf9-c2a3-4de3-a187-a9df1f6f42eb
|
public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
if(intervals.size() <=1) return intervals;
ArrayList<Interval> res =new ArrayList<Interval>();
res.add(intervals.get(0));
for(int i=0; i<intervals.size(); i++)
{
res = insert(res, intervals.get(i));
}
return res;
}
|
788db22f-6ea1-447a-a505-453fbe2f7836
|
public int minDistance(String word1, String word2) {
if(Math.min(word1.length(), word2.length()) ==0 ) return Math.max(word1.length(), word2.length());
int[][] min1 = new int[word1.length()][word2.length()];
return getMin(word1, word2, min1, 0, 0);
}
|
95ee8ddf-a3ab-4935-b852-1ade4a1b28e8
|
private int getMin(String word1, String word2, int[][] min1, int index1, int index2) {
if(index1==word1.length())
{
for(int j=index2; j<word2.length(); j++)
min1[index1][j] = word2.substring(index2+1).length();
return word2.substring(index2).length();
}else if(index2==word2.length())
{
for(int j=index1; j<word1.length(); j++)
min1[j][index2] = word1.substring(index1+1).length();
return word1.substring(index1).length();
}
int first = 0;
if(min1[index1+1][index2+1]>0)
first = min1[index1+1][index2+1];
else
first = getMin(word1, word2, min1, index1+1, index2+1);
int sec = 0;
if(min1[index1+1][index2]>0)
sec = min1[index1+1][index2];
else
sec = getMin(word1, word2, min1, index1+1, index2);
int third = 0;
if(min1[index1][index2+1]>0)
third = min1[index1][index2+1];
else
third = getMin(word1, word2, min1, index1, index2+1);
int sum = Math.min(third, Math.min(first, sec))+1;
min1[index1][index2] = sum;
return sum;
}
|
9442b38a-fdc9-4760-abe2-c1d548aaba76
|
public int numDecodings(String s) {
if(s.length() == 0) return 0;
int[] num = new int[s.length()];
if(s.charAt(0)>'0')
num[0]=1;
else
num[0]=0;
for(int i=1; i<s.length(); i++)
{
int path1 = 0, path2=0;
if(s.charAt(i)>'0') path1 = num[i-1];
if((s.charAt(i)<='6' && s.charAt(i-1)=='2' ) || s.charAt(i-1)=='1')
{
if(i==1)
path2 = 1;
else
path2 = num[i-2];
}
num[i] = path1+path2;
}
return num[s.length()-1];
}
|
1fe5602b-00a6-49e8-ad3c-99ca776b32d3
|
public int reverse(int x) {
int y=0;
while(x!=0 && y*10+x%10<Integer.MAX_VALUE && y*10+x%10>Integer.MIN_VALUE)
{
y = y*10+x%10;
x = x/10;
}
return y;
}
|
9e392b90-b001-44ff-80a6-a29af6788932
|
public boolean isPalindrome(int x) {
if(x == reverse(x) && x>=0) return true;
return false;
}
|
e2d74a65-e246-46c5-b351-22d318468b33
|
public int divide(int dividend, int divisor) {
if(divisor == 0) return 0;
int res = 0;
int loop = 0;
boolean sign = ((dividend>0&&divisor<0) || (dividend<0&&divisor>0)) ;
int newdividend = dividend>0?-1*dividend:dividend;
int newdivisor = divisor>0?-1*divisor:divisor;
divisor = newdivisor;
while(newdividend<=newdivisor)
{
loop = loop==0?-1:loop+loop;
res+=loop;
newdividend = newdividend-newdivisor;
if(Integer.MIN_VALUE - newdivisor > newdivisor || newdivisor+ newdivisor <newdividend)
{
newdivisor = divisor;
loop = 0;
}else
newdivisor = newdivisor+ newdivisor;
}
return sign?res:-res;
}
|
347c67a6-9d85-4124-b3a1-8f3a44d1c9a2
|
public int singleNumber2(int[] A) {
HashMap<Integer, Integer> rec = new HashMap<Integer, Integer>();
for(int a:A)
{
if(!rec.containsKey(a)) rec.put(a, 1);
else if(rec.get(a)<2) rec.put(a, rec.get(a)+1);
else rec.remove(a);
}
return (int) rec.keySet().toArray()[0];
}
|
b6472c2d-df00-432d-9d05-3e7f04bf4560
|
public ArrayList<Integer> grayCode(int n) {
ArrayList<Integer> res = new ArrayList<Integer>();
ArrayList<String> tempres = new ArrayList<String>();
if(n==0){
res.add(0);
return res;
}
for(int i=1; i<=n; i++)
{
int size = tempres.size();
if(size == 0)
{
tempres.add("0");
tempres.add("1");
}else
{
boolean bzero = true;
for(int j=0; j<size; j++)
{
String curr = tempres.get(0);
tempres.remove(0);
if(bzero)
{
tempres.add(curr + "0");
tempres.add(curr + "1");
bzero =false;
}else
{
tempres.add(curr + "1");
tempres.add(curr + "0");
bzero =true;
}
}
}
}
for(String s:tempres)
{
res.add(Integer.parseInt(s, 2));
}
return res;
}
|
8b28d68c-713f-4adf-8a71-92a22c444067
|
public int maxProfit3(int[] prices) {
if(prices.length<2) return 0;
int[] first = new int[prices.length];
int[] second = new int[prices.length];
int min=prices[0];
int max = prices[prices.length-1];
for(int i=1; i<prices.length; i++)
{
min = Math.min(min, prices[i]);
first[i] = Math.max(first[i-1], prices[i]-min);
}
for(int i=prices.length-2; i>=0; i--)
{
max = Math.max(max, prices[i]);
second[i] = Math.max(second[i+1], max - prices[i]);
}
max = 0;
for(int i=0; i<prices.length; i++)
{
max = Math.max(max, first[i]+second[i]);
}
return max;
}
|
47f7d8c2-524a-409c-9a47-8e3bf54007c9
|
ListNode(int x) {
val = x;
next = null;
}
|
6a4d1f17-3a04-4d6f-b4a9-d6ccd26156d9
|
public boolean isPalindrome(ListNode head)
{
if(head==null || head.next==null) return true;
ListNode cur = head;
ListNode tail=null;
while(cur!=null)
{
ListNode copy = new ListNode(cur.val);
copy.next = tail;
tail = copy;
cur = cur.next;
}
cur = head;
while(cur!=null)
{
if(cur.val!=tail.val)
return false;
tail = tail.next;
cur = cur.next;
}
return true;
}
|
eca0f2bc-030f-4321-8ea0-e430f731ae82
|
public ListNode partitionList(ListNode head, int val)
{
if(head==null || head.next==null) return head;
ListNode dummy=new ListNode(-1);
dummy.next = head;
ListNode cur=head, prev = dummy;
while(cur!=null)
{
if(cur.val>= val)
{
prev = cur;
cur=cur.next;
}else
{
if(cur==head)
{
head=head.next;
prev = cur;
cur=cur.next;
}else
{
prev.next = cur.next;
cur.next = dummy.next;
dummy.next = cur;
cur=prev.next;
}
}
}
return dummy.next;
}
|
3ea0a737-4166-4157-842d-8baf2204764a
|
public void deletenode (ListNode head)
{
if(head==null) return;
if(head.next == null) return;
head.val =head.next.val;
head.next = head.next.next;
return;
}
|
7a04d18a-719b-4a76-b907-d914a341db15
|
public void removedup1(ListNode head)
{
if(head==null || head.next==null) return;
HashSet<Integer> set = new HashSet<Integer>();
ListNode cur= head, pre=null;
while(cur!=null)
{
if(!set.contains(cur.val))
{
set.add(cur.val);
pre = cur;
}
else
{
pre.next = cur.next;
}
cur=cur.next;
}
return;
}
|
0c4753e3-6fe1-4283-bfe7-788002d48298
|
public void removedup(ListNode head)
{
if(head == null || head.next == null) return;
ListNode curr = head;
ListNode unique = head;
while(unique!=null)
{
curr = unique;
while(curr.next!=null)
{
if(curr.next.val==unique.val)
{
curr.next = curr.next.next;
}else
curr = curr.next;
}
unique = unique.next;
}
}
|
2348be1a-d123-484a-bcde-d6bd0419ae54
|
public ListNode kthtoLast(ListNode head, int k)
{
if(head==null) return null;
if(k==0) return head;
ListNode p1 = head;
ListNode p2 = head;
while(k>0 && p1!=null)
{
p1=p1.next;
k--;
}
if(k>0) return null;
while(p1!=null)
{
p2=p2.next;
p1=p1.next;
}
return p2;
}
|
da74ffa5-72ae-4d46-921e-b2de9cb90f2f
|
public ListNode add(ListNode l1, ListNode l2)
{
ListNode c1 = l1;
ListNode c2 = l2;
ListNode dummy = new ListNode(-1);
ListNode c3 = dummy;
int carry = 0;
while(c1!=null || c2!=null || carry>0)
{
int x = (c1==null?0:c1.val) + (c2==null?0:c2.val) + carry;
ListNode t = new ListNode(x%10);
carry = x/10;
c3.next = t;
c1=c1==null?null:c1.next;
c2=c2==null?null:c2.next;
c3 = t;
}
return dummy.next;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.