Skip to content

Commit aa78e1b

Browse files
committed
Update
1 parent f6482ae commit aa78e1b

File tree

14 files changed

+287
-81
lines changed

14 files changed

+287
-81
lines changed

pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,15 @@
6161
</plugin>
6262
</plugins>
6363
</pluginManagement>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<configuration>
69+
<source>8</source>
70+
<target>8</target>
71+
</configuration>
72+
</plugin>
73+
</plugins>
6474
</build>
6575
</project>

src/main/java/com/fibers/algorithm/datastructure/Heap.java

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fibers.algorithm.datastructure;
2+
3+
/** Min Heap **/
4+
public class MinHeap {
5+
private int[] h;
6+
private int size;
7+
8+
9+
public MinHeap(int length) {
10+
h = new int[length];
11+
size = 0;
12+
}
13+
14+
public void shift_up(int i) {
15+
if( i >= 0 && i < h.length ){
16+
int parentIndex = (i - 1) / 2;
17+
if(parentIndex > 0 && this.h[parentIndex] > this.h[i]){
18+
int temp = this.h[parentIndex];
19+
this.h[parentIndex] = this.h[i];
20+
this.h[i] = temp;
21+
}
22+
}
23+
return;
24+
}
25+
26+
public void shift_down(int i) {
27+
if( i >= 0 && i < h.length ){
28+
int parentIndex = (i - 1) / 2;
29+
if(parentIndex > 0 && this.h[parentIndex] > this.h[i]){
30+
int temp = this.h[parentIndex];
31+
this.h[parentIndex] = this.h[i];
32+
this.h[i] = temp;
33+
}
34+
}
35+
return;
36+
}
37+
38+
39+
public void push(int v) {
40+
41+
}
42+
43+
public void top() {
44+
45+
}
46+
47+
public void pop() {
48+
49+
}
50+
51+
public void heap_sort() {
52+
53+
}
54+
55+
56+
}

src/main/java/com/fibers/algorithm/datastructure/TreeNode.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,12 @@ public void inorderTraversal() {
106106
t = t.left;
107107
}
108108

109-
if (!s.isEmpty()) {
110-
TreeNode temp = s.pop();
111-
System.out.print(temp.val);
112-
System.out.print(",");
109+
TreeNode temp = s.pop();
110+
System.out.print(temp.val);
111+
System.out.print(",");
113112

114-
if (temp.right != null) {
115-
t = temp.right;
116-
}
113+
if (temp.right != null) {
114+
t = temp.right;
117115
}
118116
}
119117

src/main/java/com/fibers/algorithm/leetcode/_002/Solution.java

+9-16
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,20 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
88
ListNode result = new ListNode(0);
99
ListNode temp = result;
1010
int carry = 0;
11-
while (l1 != null && l2 != null) {
12-
int reminder = (l1.val + l2.val + carry) % 10;
11+
while (l1 != null || l2 != null) {
12+
13+
int val1 = l1 == null? 0: l1.val;
14+
int val2 = l2 == null? 0: l2.val;
15+
16+
int reminder = (val1 + val2 + carry) % 10;
1317
temp.next = new ListNode(reminder);
14-
carry = (l1.val + l2.val + carry) / 10;
18+
carry = (val1 + val2 + carry) / 10;
1519

16-
l1 = l1.next;
17-
l2 = l2.next;
20+
l1 = l1 != null ? l1.next : null;
21+
l2 = l2 != null ? l2.next : null;
1822
temp = temp.next;
1923
}
2024

21-
if (l1 != null || l2 != null) {
22-
ListNode l = l1 == null ? l2 : l1;
23-
while (l != null) {
24-
int reminder = (l.val + carry) % 10;
25-
temp.next = new ListNode(reminder);
26-
carry = (l.val + carry) / 10;
27-
28-
l = l.next;
29-
temp = temp.next;
30-
}
31-
}
3225

3326
if (carry != 0) {
3427
temp.next = new ListNode(carry);

src/main/java/com/fibers/algorithm/leetcode/_046/Solution.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ public static void main(String[] args) {
2222
}
2323

2424
public List<List<Integer>> permute(int[] nums) {
25-
return permuteRecursion(new ArrayList<Integer>(), nums, 0);
25+
List<List<Integer>> list = new ArrayList<>();
26+
if (nums == null || nums.length == 0) {
27+
return list;
28+
}
29+
permuteRecursion(nums, 0, nums.length - 1, list);
30+
return list;
2631
}
2732

28-
private List<List<Integer>> permuteRecursion(List<Integer> tempList, int[] nums, int start) {
29-
List<List<Integer>> resultList = new ArrayList<>();
33+
private void permuteRecursion(int[] nums, int start, int end, List<List<Integer>> result) {
3034

31-
if (tempList.size() == nums.length) {
32-
resultList.add(tempList);
33-
return resultList;
35+
if (start == end) {
36+
List<Integer> temp = new ArrayList();
37+
for (int i : nums) {
38+
temp.add(i);
39+
}
40+
result.add(temp);
41+
return;
3442
}
3543

36-
for (int i = start; i <= nums.length - 1; i++) {
44+
for (int i = start; i <= end; i++) {
3745
Utils.swap(nums, start, i);
38-
tempList.add(nums[i]);
39-
List<List<Integer>> recursionList = permuteRecursion(tempList, nums, start + 1);
40-
resultList.addAll(recursionList);
41-
46+
permuteRecursion(nums, start + 1, end, result);
4247
Utils.swap(nums, start, i);
4348
}
44-
45-
return resultList;
49+
return;
4650
}
4751
}

src/main/java/com/fibers/algorithm/leetcode/_098/Solution.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ private boolean isValidBST(TreeNode node, int minValue, int maxValue) {
1212
if (node == null) {
1313
return true;
1414
}
15+
1516
if (node.val < minValue || node.val > maxValue) {
1617
return false;
1718
}
18-
if (node.val == minValue && node.left != null) {
19-
return false;
20-
}
21-
if (node.val == maxValue && node.right != null) {
19+
20+
if ((node.val == minValue && node.left != null)
21+
|| (node.val == maxValue && node.right != null)) {
2222
return false;
2323
}
24+
2425
return isValidBST(node.left, minValue, node.val - 1)
2526
&& isValidBST(node.right, node.val + 1, maxValue);
2627
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fibers.algorithm.leetcode._110;
2+
3+
import com.fibers.algorithm.datastructure.TreeNode;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) {
8+
Integer[] array = {1,2,2,3,null,null,3,4,null,null,4};
9+
TreeNode t = new TreeNode(array);
10+
11+
Solution s = new Solution();
12+
System.out.println(s.isBalanced(t));
13+
}
14+
15+
public boolean isBalanced(TreeNode root) {
16+
return height(root) != -1;
17+
}
18+
19+
private int height(TreeNode node){
20+
if(node == null){
21+
return 0;
22+
}
23+
24+
int lHeight = height(node.left);
25+
if(lHeight == -1){
26+
return -1;
27+
}
28+
29+
int rHeight = height(node.right);
30+
if(rHeight == -1){
31+
return -1;
32+
}
33+
34+
if(Math.abs(lHeight - rHeight) > 1){
35+
return -1;
36+
}
37+
38+
return Math.max(lHeight, rHeight) + 1;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fibers.algorithm.leetcode._112;
2+
3+
import com.fibers.algorithm.datastructure.TreeNode;
4+
5+
public class Solution {
6+
public boolean hasPathSum(TreeNode root, int sum) {
7+
if (root == null) {
8+
return false;
9+
}
10+
11+
if (root.left == null && root.right == null) {
12+
return root.val == sum;
13+
}
14+
15+
return hasPathSum(root.left, sum - root.val)
16+
|| hasPathSum(root.right, sum - root.val);
17+
}
18+
}

src/main/java/com/fibers/algorithm/leetcode/_240/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public boolean searchMatrix(int[][] matrix, int target) {
1212
int i = 0;
1313
int j = colLen - 1;
1414

15-
while (i >= 0 && i < rowLen && j >= 0 && j < colLen) {
15+
while (i < rowLen && j < colLen) {
1616
int val = matrix[i][j];
1717
if (val == target) {
1818
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fibers.algorithm.leetcode._704;
2+
3+
public class Solution {
4+
public int search(int[] nums, int target) {
5+
if (nums == null || nums.length == 0) {
6+
return -1;
7+
}
8+
9+
if (target > nums[nums.length - 1]) {
10+
return -1;
11+
}
12+
13+
int start = 0;
14+
int end = nums.length - 1;
15+
16+
while(start <= end){
17+
int mid = (start + end)/2;
18+
if(target == nums[mid]){
19+
return mid;
20+
}else if(target > nums[mid]){
21+
start = mid+1;
22+
}else{
23+
end = mid-1;
24+
}
25+
}
26+
return -1;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fibers.algorithm.leetcode._738;
2+
3+
public class Solution {
4+
5+
public int monotoneIncreasingDigits(int N) {
6+
String s = String.valueOf(N);
7+
char[] c = s.toCharArray();
8+
9+
int len = c.length;
10+
int lastIncrease = len;
11+
for (int i = len - 2; i >= 0; i--) {
12+
if (c[i] > c[i+1]) {
13+
lastIncrease = i;
14+
c[lastIncrease]--;
15+
}
16+
}
17+
18+
for(int i=lastIncrease + 1; i<len; i++ ){
19+
c[i] = '9';
20+
}
21+
22+
return Integer.valueOf(String.valueOf(c));
23+
}
24+
}

0 commit comments

Comments
 (0)