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;... |
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 =... |
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... |
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");
Arr... |
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
{
... |
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
... |
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... |
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-... |
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] !... |
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]... |
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... |
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.... |
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++)
... |
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... |
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 = n... |
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[startp... |
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-... |
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]);
... |
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[star... |
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 !='.')
{
... |
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... |
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
{
... |
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... |
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];
... |
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)
{
ro... |
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] &... |
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<matr... |
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] - ... |
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)
... |
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... |
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)... |
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
{
... |
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 Arra... |
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=='}' &&... |
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;
... |
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) ... |
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(n... |
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);
re... |
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+... |
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-... |
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])
... |
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);
distan... |
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 ... |
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>(... |
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){
re... |
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... |
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; inde... |
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;
}
... |
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];
... |
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... |
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;... |
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)
{
... |
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.ad... |
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++, start... |
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.sta... |
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));
}
retur... |
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==... |
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.cha... |
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;
... |
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.s... |
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(mi... |
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!=... |
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)
{... |
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.... |
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;
... |
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);
carr... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.