Skip to content

Commit 63d1ff3

Browse files
committed
Update
1 parent 56f1608 commit 63d1ff3

File tree

12 files changed

+296
-13
lines changed

12 files changed

+296
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fibers.algorithm.datastructure;
2+
3+
import java.util.Stack;
4+
5+
public class MyQueue {
6+
7+
public static void main(String[] args) {
8+
MyQueue q = new MyQueue();
9+
10+
q.push(1);
11+
q.push(2);
12+
q.push(3);
13+
14+
System.out.println(q.pop());
15+
16+
q.push(4);
17+
q.push(5);
18+
19+
System.out.println(q.pop());
20+
}
21+
22+
private Stack<Integer> stack1;
23+
private Stack<Integer> stack2;
24+
25+
private Integer top;
26+
27+
/**
28+
* Initialize your data structure here.
29+
*/
30+
public MyQueue() {
31+
stack1 = new Stack<>();
32+
stack2 = new Stack<>();
33+
top = null;
34+
}
35+
36+
/**
37+
* Push element x to the back of queue.
38+
*/
39+
public void push(int x) {
40+
if (stack1.empty()) {
41+
top = x;
42+
}
43+
stack1.push(x);
44+
}
45+
46+
/**
47+
* Removes the element from in front of queue and returns that element.
48+
*/
49+
public int pop() {
50+
if (stack2.empty()) {
51+
while (!stack1.empty()) {
52+
stack2.push(stack1.pop());
53+
}
54+
}
55+
return stack2.pop();
56+
}
57+
58+
/**
59+
* Get the front element.
60+
*/
61+
public int peek() {
62+
if (!stack2.isEmpty()) {
63+
return stack2.peek();
64+
}
65+
return top;
66+
}
67+
68+
/**
69+
* Returns whether the queue is empty.
70+
*/
71+
public boolean empty() {
72+
return stack1.empty() && stack2.empty();
73+
}
74+
/**
75+
* Your MyQueue object will be instantiated and called as such:
76+
* MyQueue obj = new MyQueue();
77+
* obj.push(x);
78+
* int param_2 = obj.pop();
79+
* int param_3 = obj.peek();
80+
* boolean param_4 = obj.empty();
81+
*/
82+
83+
}
84+
85+

Diff for: src/main/java/com/fibers/algorithm/leetcode/_040/Solution.java

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
import java.util.List;
66

77
public class Solution {
8+
9+
public static void main(String[] args) {
10+
int[] candidates = new int[]{2, 5, 2, 1, 2};
11+
12+
Solution s = new Solution();
13+
System.out.println(s.combinationSum2(candidates, 5));
14+
}
15+
816
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
917
List<List<Integer>> result = new ArrayList<>();
1018
List<Integer> list = new ArrayList<>();
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
package com.fibers.algorithm.leetcode._088;
2+
23
public class Solution {
4+
public void merge(int[] nums1, int m, int[] nums2, int n) {
5+
if (nums1 == null || nums1.length == 0) {
6+
return;
7+
}
8+
9+
int i = m - 1;
10+
int j = n - 1;
11+
int k = m + n - 1;
12+
13+
while (i >= 0 || j >= 0) {
14+
if (i < 0) {
15+
nums1[k--] = nums2[j--];
16+
} else if (j < 0) {
17+
nums1[k--] = nums1[i--];
18+
} else if (nums1[i] > nums2[j]) {
19+
nums1[k--] = nums1[i--];
20+
} else {
21+
nums1[k--] = nums2[j--];
22+
}
23+
}
24+
return;
25+
}
326
}
4-
27+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
package com.fibers.algorithm.leetcode._125;
2+
23
public class Solution {
4+
public boolean isPalindrome(String s) {
5+
if (s == null) {
6+
return false;
7+
}
8+
9+
s = s.toLowerCase();
10+
11+
if (s.length() == 0) {
12+
return true;
13+
}
14+
15+
int i = 0;
16+
int j = s.length() - 1;
17+
18+
while (i < j) {
19+
20+
if (!Character.isLetterOrDigit(s.charAt(i))) {
21+
i++;
22+
} else if (!Character.isLetterOrDigit(s.charAt(j))) {
23+
j--;
24+
} else {
25+
if ((s.charAt(i++) != s.charAt(j--))) {
26+
return false;
27+
}
28+
}
29+
}
30+
31+
return true;
32+
}
333
}
4-
34+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
package com.fibers.algorithm.leetcode._136;
22
public class Solution {
3+
4+
public int singleNumber(int[] nums) {
5+
if(nums == null || nums.length == 0){
6+
return 0;
7+
}
8+
9+
int result = 0;
10+
for(int i : nums){
11+
result ^= i;
12+
}
13+
14+
return result;
15+
}
316
}
4-
17+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
package com.fibers.algorithm.leetcode._137;
2+
3+
import java.util.Arrays;
4+
25
public class Solution {
6+
public int singleNumber(int[] nums) {
7+
if (nums == null || nums.length == 0) {
8+
return 0;
9+
}
10+
11+
int[] sum = new int[32];
12+
Arrays.fill(sum, 0);
13+
14+
for (int i = 0; i < nums.length; i++) {
15+
int num = nums[i];
16+
for (int j = 0; j < 32; j++) {
17+
sum[j] += num >> j & 1;
18+
}
19+
}
20+
21+
int result = 0;
22+
for (int i = 0; i < sum.length; i++) {
23+
if (sum[i] % 3 != 0) {
24+
result += 1 << i;
25+
}
26+
}
27+
return result;
28+
}
329
}
4-
30+

Diff for: src/main/java/com/fibers/algorithm/leetcode/_141/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
public class Solution {
66
public boolean hasCycle(ListNode head) {
7-
if (head == null || head.next == null) {
7+
if (head == null) {
88
return false;
99
}
1010

1111
ListNode first = head;
1212
ListNode second = head.next;
1313

14-
while (first != second) {
15-
if (second == null || second.next == null) {
16-
return false;
14+
while (first != null && second != null && second.next != null) {
15+
if (first == second) {
16+
return true;
1717
}
1818

1919
first = first.next;
2020
second = second.next.next;
2121
}
22-
return true;
22+
return false;
2323
}
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.fibers.algorithm.leetcode._232;
22
public class Solution {
3+
34
}
4-
5+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
package com.fibers.algorithm.leetcode._260;
2+
23
public class Solution {
4+
public int[] singleNumber(int[] nums) {
5+
if (nums == null || nums.length == 0) {
6+
return new int[]{0, 0};
7+
}
8+
9+
int xor = 0;
10+
for (int i : nums) {
11+
xor ^= i;
12+
}
13+
14+
int lastSetBit = (xor & (xor - 1)) ^ xor;
15+
int[] result = new int[2];
16+
for (int i : nums) {
17+
if ((i & lastSetBit) == 0) {
18+
result[0] ^= i;
19+
} else {
20+
result[1] ^= i;
21+
}
22+
}
23+
24+
return result;
25+
}
326
}
4-
27+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
11
package com.fibers.algorithm.leetcode._524;
2+
3+
import java.util.List;
4+
25
public class Solution {
6+
public String findLongestWord(String s, List<String> d) {
7+
String longestWord = "";
8+
for (String target : d) {
9+
int l1 = longestWord.length();
10+
int l2 = target.length();
11+
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
12+
continue;
13+
}
14+
if(isValid(s, target)){
15+
longestWord = target;
16+
}
17+
}
18+
return longestWord;
19+
}
20+
21+
private boolean isValid(String s, String target) {
22+
int i = 0;
23+
int j = 0;
24+
25+
int iLen = s.length();
26+
int jLen = target.length();
27+
28+
while (i < iLen && j < jLen) {
29+
if (s.charAt(i) == target.charAt(j)) {
30+
j++;
31+
}
32+
i++;
33+
}
34+
return j == jLen;
35+
}
36+
37+
338
}
4-
39+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
package com.fibers.algorithm.leetcode._680;
22

33
public class Solution {
4-
4+
5+
public static void main(String[] args) {
6+
Solution s = new Solution();
7+
System.out.println(s.validPalindrome("eeccccbebaeeabebccceea"));
8+
}
9+
10+
public boolean validPalindrome(String s) {
11+
if (s == null || s.length() == 0) {
12+
return false;
13+
}
14+
15+
int i = -1;
16+
int j = s.length();
17+
18+
while (++i < --j) {
19+
if (s.charAt(i) != s.charAt(j)) {
20+
return isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1);
21+
}
22+
}
23+
24+
return true;
25+
}
26+
27+
private boolean isPalindrome(String s, int i, int j) {
28+
while (i < j) {
29+
if (s.charAt(i++) != s.charAt(j--)) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
535
}
636

Diff for: src/main/java/com/fibers/algorithm/others/Solution.java

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public static void main(String[] args) {
1414
s.quickSort(a, 0, a.length - 1);
1515
Utils.printArray(a);
1616

17+
18+
int b = 45;
19+
int c = b | (1 << (6 & 0x1F));
20+
int d = b | (1 << 6);
21+
22+
23+
System.out.println(c);
24+
System.out.println(d);
25+
1726
}
1827

1928
public int fibonacci(int n) {

0 commit comments

Comments
 (0)