Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 1st/homework_1/id_55/addTwoNumbers_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* https://leetcode.com/problems/add-two-numbers/description/
*
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode p = head;
int sum = 0;

while(l1 != null || l2 != null || sum > 0){
sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + sum;

ListNode cur = new ListNode(sum % 10);
sum = sum / 10;

p.next = cur;
p = cur;

if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}

return head.next;
}
}
16 changes: 16 additions & 0 deletions 1st/homework_10/id_55/triangle_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0) return 0;

int n = triangle.size();
int[] min = new int[triangle.get(n - 1).size() + 1]; //as add one layer to the bottom

for(int i = n - 1; i >= 0; i--){
for(int j = 0; j < triangle.get(i).size(); j++){
min[j] = triangle.get(i).get(j) + Math.min(min[j], min[j + 1]);
}
}

return min[0];
}
}
38 changes: 38 additions & 0 deletions 1st/homework_2/id_55/3sum_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* https://leetcode.com/problems/3sum/description/
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);

List<List<Integer>> ret = new ArrayList<>();
int n = nums.length;

for(int i = 0; i < n; i++){
if(i == 0 || (i>0 && nums[i-1] != nums[i])){
int left = i + 1;
int right = n - 1;
int target = -nums[i];

while(left < right){
if(nums[left] + nums[right] == target){
ret.add( Arrays.asList(nums[i], nums[left], nums[right]) );
left++;
right--;
while(left < right && nums[left] == nums[left-1]) left++;
while(left < right && nums[right] == nums[right+1]) right--;
}else if(nums[left] + nums[right] > target){
right--;
}else{
left++;
}


}

}
}

return ret;
}
}
73 changes: 73 additions & 0 deletions 1st/homework_3/id_55/4Sum_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ret = new ArrayList<>();

Arrays.sort(nums);

if(nums == null || nums.length < 4){
return ret;
}

NSum(4, nums, target, 0, ret, new ArrayList<Integer>(4));

return ret;
}

public static void NSum(int N,
int[] nums,
int target,
int left,
List<List<Integer>>ret,
List<Integer> preList){
int n = nums.length;

if(N*nums[left] > target || N*nums[n-1] < target){
return;
}

if(N == 2){
TwoSum(nums, target, left, ret, preList);
return;
}

for(int i = left; i < n - N + 1; i++){
if(i == left || (i > left && nums[i] != nums[i-1])){
List<Integer> list = new ArrayList<Integer>(preList);
list.add(nums[i]);
NSum(N-1, nums, target - nums[i], i+1, ret, list);
}
}

}

public static void TwoSum(int[] nums,
int target,
int left,
List<List<Integer>>ret,
List<Integer> preList){
int n = nums.length;
int l = left;
int r = n - 1;

while(l < r){
if(nums[l] + nums[r] == target){
List<Integer> list = new ArrayList<Integer>(preList);
list.add(nums[l]);
list.add(nums[r]);
ret.add(list);

l++;
r--;

while(l < r && nums[l-1] == nums[l]) l++;
while(l < r && nums[r+1] == nums[r]) r--;
}else if(nums[l] + nums[r] > target){
r--;
}else{
l++;
}
}

}

}
18 changes: 18 additions & 0 deletions 1st/homework_4/id_55/sortColors_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public void sortColors(int[] nums) {
int len = nums.length;
int left = 0;
int right = len - 1;

for(int i = left; i <= right; i++){
while(nums[i] == 2 && i < right) swap(nums, i, right--);
while(nums[i] == 0 && i > left) swap(nums, i, left++);
}
}

public void swap(int[] nums, int a, int b){
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
}
15 changes: 15 additions & 0 deletions 1st/homework_5/id_55/maximumSubarry_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int maxSubArray(int[] nums) {
int[] maxSubArr = new int[nums.length];
maxSubArr[0] = nums[0];
int max = nums[0];

for(int i = 1; i< nums.length; i++){
maxSubArr[i] = nums[i] + (maxSubArr[i-1] > 0 ? maxSubArr[i-1] : 0);
max = Math.max(max, maxSubArr[i]);
}

return max;

}
}
12 changes: 12 additions & 0 deletions 1st/homework_6/id_55/removeDuplicatesFromSortedArrayIi_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for(int j = 0; j < nums.length; j++){
if(j < 2 || nums[j] > nums[i-2]){
nums[i++] = nums[j];
}
}

return i;
}
}
33 changes: 33 additions & 0 deletions 1st/homework_7/id_55/longestConsecutiveSequence_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null || nums.length == 0) return 0;

PriorityQueue<Integer> pq = new PriorityQueue<Integer>();

for(int i = 0; i < nums.length; i++){
pq.add(nums[i]);
}

int longestLen = 1;
int lastTempVal = pq.poll();
int currentLen = 1;

while(pq.size() > 0){
int tempVal = pq.poll();

if(lastTempVal+1 == tempVal){
currentLen++;
longestLen = Math.max(longestLen, currentLen);
}else if(lastTempVal == tempVal){
continue;
}else{
currentLen = 1;
longestLen = Math.max(longestLen, currentLen);
}

lastTempVal = tempVal;
}

return longestLen;
}
}
26 changes: 26 additions & 0 deletions 1st/homework_8/id_55/rotateImage_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0) return;

int n = matrix.length;

for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
swap(matrix, i, j, j, i);

}
}

for(int i = 0; i < n; i++){
for(int j = 0; j < n/2; j++){
swap(matrix, i, j, i, n - j -1);
}
}
}

public void swap(int[][] matrix, int p1, int p2, int q1, int q2){
int t = matrix[p1][p2];
matrix[p1][p2] = matrix[q1][q2];
matrix[q1][q2] = t;
}
}
23 changes: 23 additions & 0 deletions 1st/homework_9/id_55/countNumbersWithUniqueDigits_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//n = 0 => 1
//n = 1 _ 1+9 => 10
//n = 2 __ 9*9 => 81+10 => 91
//n = 3 ___ 9*9*8
//..
//n = 10


class Solution {
public int countNumbersWithUniqueDigits(int n) {
if(n == 0) return 1;
if(n > 11) return 0;

int ret = 10;
int base = 9;
for(int i = 1; i < 11 && i < n; i++){
base = base*(9 - i + 1);
ret += base;
}

return ret;
}
}
24 changes: 24 additions & 0 deletions 2nd/homework_1/id_55/minimumDepthOfBinaryTree_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;

if(root.left != null && root.right != null){
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}else if(root.left == null && root.right != null){
return minDepth(root.right) + 1;
}else if(root.right == null && root.left != null){
return minDepth(root.left) + 1;
}else{
return 1;
}
}
}
27 changes: 27 additions & 0 deletions 2nd/homework_2/id_55/symmetricTree_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;

return isMirror(root.left, root.right);
}

public boolean isMirror(TreeNode l, TreeNode r){
if(l == null && r == null) return true;
if(l == null && r != null) return false;
if(l != null && r == null) return false;

return l.val == r.val && isMirror(l.left, r.right) && isMirror(l.right, r.left);
}


}
25 changes: 25 additions & 0 deletions 2nd/homework_3/id_55/flattenBinaryTreeToLinkedList_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode prev = null;

public void flatten(TreeNode root) {
if(root == null) return;

flatten(root.right);
flatten(root.left);

root.right = prev;
root.left = null;
prev = root;
}


}
Loading