Skip to content

Commit f6482ae

Browse files
committed
Update
1 parent 13d20bf commit f6482ae

File tree

15 files changed

+380
-18
lines changed

15 files changed

+380
-18
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fibers.algorithm.datastructure;
2+
3+
public class Heap {
4+
private int[] h;
5+
6+
7+
public Heap(int[] a){
8+
9+
}
10+
11+
public void shift_up(int i){
12+
13+
}
14+
15+
public void shift_down(int i){
16+
17+
}
18+
19+
20+
public void push(int v){
21+
22+
}
23+
24+
public void top(){
25+
26+
}
27+
28+
public void pop(){
29+
30+
}
31+
32+
public void heap_sort(){
33+
34+
}
35+
36+
37+
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.util.Queue;
66
import java.util.Stack;
77

8+
/**
9+
* Binary Tree Operations
10+
*/
811
public class TreeNode {
912
public int val;
1013
public TreeNode left;
@@ -35,18 +38,6 @@ public static void main(String[] args) {
3538
t.rightView();
3639
}
3740

38-
private TreeNode createBinaryTreeByArray(Integer[] t, int index) {
39-
TreeNode tn = null;
40-
int len = t.length;
41-
if (len > 0 && index < len && t[index] != null) {
42-
tn = new TreeNode(t[index]);
43-
tn.left = createBinaryTreeByArray(t, 2 * index + 1);
44-
tn.right = createBinaryTreeByArray(t, 2 * index + 2);
45-
}
46-
47-
return tn;
48-
}
49-
5041
public void breadthFirstTravel() {
5142
Queue<TreeNode> q = new LinkedList<>();
5243
q.add(this);
@@ -178,6 +169,10 @@ public void rightView() {
178169
System.out.println();
179170
}
180171

172+
public void makeMaxHeap(){
173+
174+
}
175+
181176
public TreeNode nearestAncestor(TreeNode root, TreeNode t1, TreeNode t2) {
182177
if (root == null || t1 == null || t2 == null) {
183178
return null;
@@ -201,4 +196,16 @@ public TreeNode nearestAncestor(TreeNode root, TreeNode t1, TreeNode t2) {
201196
}
202197

203198
}
199+
200+
private TreeNode createBinaryTreeByArray(Integer[] t, int index) {
201+
TreeNode tn = null;
202+
int len = t.length;
203+
if (len > 0 && index < len && t[index] != null) {
204+
tn = new TreeNode(t[index]);
205+
tn.left = createBinaryTreeByArray(t, 2 * index + 1);
206+
tn.right = createBinaryTreeByArray(t, 2 * index + 2);
207+
}
208+
209+
return tn;
210+
}
204211
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fibers.algorithm.leetcode._004;
2+
3+
public class Solution {
4+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
5+
int sizeNums1 = nums1.length;
6+
int sizeNums2 = nums2.length;
7+
8+
int totalSize = sizeNums1 + sizeNums2;
9+
10+
int[] sortedArray = new int[totalSize];
11+
12+
int i = 0;
13+
int j = 0;
14+
int k = 0;
15+
16+
while (i < sizeNums1 && j < sizeNums2) {
17+
if (nums1[i] <= nums2[j]) {
18+
sortedArray[k++] = nums1[i++];
19+
} else {
20+
sortedArray[k++] = nums2[j++];
21+
}
22+
}
23+
24+
while (i < sizeNums1) {
25+
sortedArray[k++] = nums1[i++];
26+
}
27+
28+
while (j < sizeNums2) {
29+
sortedArray[k++] = nums2[j++];
30+
}
31+
32+
int indexMedian1 = (sizeNums1 + sizeNums2 - 1) / 2;
33+
int indexMedian2 = (sizeNums1 + sizeNums2) / 2;
34+
35+
return (sortedArray[indexMedian1] + sortedArray[indexMedian2]) / 2.0;
36+
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fibers.algorithm.leetcode._009;
2+
3+
public class Solution {
4+
public boolean isPalindrome(int x) {
5+
if (x < 0 || (x != 0 && x % 10 == 0)) {
6+
return false;
7+
}
8+
9+
int palindrome = 0;
10+
while (x > palindrome) {
11+
palindrome = palindrome * 10 + x % 10;
12+
x /= 10;
13+
}
14+
15+
return palindrome == x || palindrome == x / 10;
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fibers.algorithm.leetcode._011;
2+
3+
public class Solution {
4+
public int maxArea(int[] height) {
5+
int l = 0;
6+
int r = height.length - 1;
7+
int max = 0;
8+
9+
while (l < r) {
10+
int minHeight = height[l] > height[r] ? height[r] : height[l];
11+
max = Math.max(max, minHeight * (r - l));
12+
if (height[l] < height[r]) {
13+
l++;
14+
} else {
15+
r--;
16+
}
17+
}
18+
return max;
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.fibers.algorithm.leetcode._012;
2+
3+
public class Solution {
4+
public String intToRoman(int num) {
5+
String M[] = {"", "M", "MM", "MMM"};
6+
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
7+
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
8+
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
9+
10+
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
11+
}
12+
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.fibers.algorithm.leetcode._022;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.*;
54

65
public class Solution {
76
public List<String> generateParenthesis(int n) {
8-
List<String> list = new ArrayList<>();
9-
if (n == 0) {
10-
return list;
11-
}
7+
Set<String> set = new HashSet<>();
8+
9+
if (n == 1) {
10+
set.add("()");
11+
} else if (n > 1) {
12+
List<String> tempList = generateParenthesis(n - 1);
13+
for (String s : tempList) {
14+
set.add(s + "()");
15+
set.add("()" + s);
16+
set.add("(" + s + ")");
17+
}
1218

19+
}
1320

21+
return new ArrayList<>(set);
1422
}
1523
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fibers.algorithm.leetcode._026;
2+
3+
public class Solution {
4+
public static void main(String[] args) {
5+
int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
6+
Solution s = new Solution();
7+
System.out.println(s.removeDuplicates(nums));
8+
}
9+
10+
public int removeDuplicates(int[] nums) {
11+
if (nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
if (nums != null && nums.length == 1) {
16+
return 1;
17+
}
18+
19+
int index = 0;
20+
for (int i = 1; i < nums.length; i++) {
21+
if (nums[i] != nums[index]) {
22+
index++;
23+
nums[index] = nums[i];
24+
}
25+
}
26+
return index + 1;
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.fibers.algorithm.leetcode._041;
2+
3+
public class Solution {
4+
public int firstMissingPositive(int[] nums) {
5+
if (nums == null || nums.length == 0) {
6+
return 1;
7+
}
8+
9+
int minPositive = Integer.MAX_VALUE;
10+
int maxContinuousPositive = Integer.MAX_VALUE;
11+
for (int i : nums) {
12+
if (i < minPositive && i > 0) {
13+
minPositive = i;
14+
}
15+
}
16+
17+
return minPositive;
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fibers.algorithm.leetcode._046;
2+
3+
import com.fibers.utils.Utils;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
10+
public static void main(String[] args) {
11+
int[] nums = new int[]{1, 2, 3};
12+
13+
Solution s = new Solution();
14+
15+
List<List<Integer>> list = s.permute(nums);
16+
for (List<Integer> tempList : list) {
17+
for (Integer i : tempList) {
18+
System.out.print(i);
19+
}
20+
System.out.println();
21+
}
22+
}
23+
24+
public List<List<Integer>> permute(int[] nums) {
25+
return permuteRecursion(new ArrayList<Integer>(), nums, 0);
26+
}
27+
28+
private List<List<Integer>> permuteRecursion(List<Integer> tempList, int[] nums, int start) {
29+
List<List<Integer>> resultList = new ArrayList<>();
30+
31+
if (tempList.size() == nums.length) {
32+
resultList.add(tempList);
33+
return resultList;
34+
}
35+
36+
for (int i = start; i <= nums.length - 1; i++) {
37+
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+
42+
Utils.swap(nums, start, i);
43+
}
44+
45+
return resultList;
46+
}
47+
}

0 commit comments

Comments
 (0)