Skip to content

Commit 44029aa

Browse files
committed
小傅哥 | update
1 parent dd71783 commit 44029aa

File tree

8 files changed

+415
-0
lines changed

8 files changed

+415
-0
lines changed

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>algorithm-leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.alibaba</groupId>
14+
<artifactId>fastjson</artifactId>
15+
<version>1.2.62</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.itstack.algorithm.leetcode.s0001;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Solution {
7+
8+
public static int[] twoSum_0(int[] nums, int target) {
9+
int[] idxs = new int[2];
10+
for (int i = 0; i < nums.length; i++) {
11+
for (int j = i + 1; j < nums.length; j++) {
12+
if (nums[i] + nums[j] == target) {
13+
idxs[0] = i;
14+
idxs[1] = j;
15+
}
16+
}
17+
}
18+
return idxs;
19+
}
20+
21+
public static int[] twoSum_1(int[] nums, int target) {
22+
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>(nums.length);
23+
for (int i = 0; i < nums.length; i++) {
24+
if (hashMap.containsKey(target - nums[i])) {
25+
return new int[]{hashMap.get(target - nums[i]), i};
26+
}
27+
hashMap.put(nums[i], i);
28+
}
29+
return new int[]{-1, -1};
30+
}
31+
32+
public static int[] towSum_2(int[] nums, int target) {
33+
int volume = 2048;
34+
int bitMode = volume - 1;
35+
int[] t = new int[volume];
36+
for (int i = 0; i < nums.length; i++) {
37+
int c = (target - nums[i]) & bitMode;
38+
if (t[c] != 0) return new int[]{t[c] - 1, i};
39+
t[nums[i] & bitMode] = i + 1;
40+
}
41+
return new int[]{-1, -1};
42+
}
43+
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.itstack.algorithm.leetcode.s0002;
2+
3+
/**
4+
* 公众号:bugstack虫洞栈 | 沉淀、分享、成长,让自己和他人都能有所收获!
5+
* 博 客:http://bugstack.cn
6+
* Create by 小傅哥 on @2020
7+
*/
8+
public class ListNode {
9+
10+
public int val;
11+
public ListNode next;
12+
13+
public ListNode(int x) {
14+
val = x;
15+
}
16+
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.itstack.algorithm.leetcode.s0002;
2+
3+
/**
4+
* 公众号:bugstack虫洞栈 | 沉淀、分享、成长,让自己和他人都能有所收获!
5+
* 博 客:http://bugstack.cn
6+
* Create by 小傅哥 on @2020
7+
*/
8+
public class Solution {
9+
10+
//(2 -> 4 -> 3) + (5 -> 6 -> 4)
11+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
12+
13+
ListNode root = new ListNode(0);
14+
ListNode current = root;
15+
int carry = 0; // 进位
16+
17+
while (l1 != null || l2 != null || carry != 0) {
18+
19+
int l1Val = null == l1 ? 0 : l1.val;
20+
int l2Val = null == l2 ? 0 : l2.val;
21+
22+
int sum = l1Val + l2Val + carry;
23+
24+
if (sum >= 10) {
25+
ListNode nextNode = new ListNode(sum - 10);
26+
current.next = nextNode;
27+
current = nextNode;
28+
carry = 1;
29+
} else {
30+
ListNode nextNode = new ListNode(sum);
31+
current.next = nextNode;
32+
current = nextNode;
33+
carry = 0;
34+
}
35+
36+
l1 = null == l1 ? null : l1.next;
37+
l2 = null == l2 ? null : l2.next;
38+
}
39+
40+
return root.next;
41+
}
42+
43+
}
44+
45+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.itstack.algorithm.leetcode.s0003;
2+
3+
public class Solution {
4+
5+
public int lengthOfLongestSubstring_1(String s) {
6+
if (null == s || "".equals(s)) return 0;
7+
if (" ".equals(s) || s.length() == 1) return 1;
8+
9+
int volume = 128;
10+
int bitMode = volume - 1;
11+
int[] t = new int[volume];
12+
13+
int beginIdx = s.charAt(0) & bitMode, endIdx = 0, maxSize = 0;
14+
t[beginIdx] = 1;
15+
for (int i = 1; i < s.length(); i++) {
16+
endIdx = s.charAt(i) & bitMode;
17+
int val = t[endIdx];
18+
19+
if (val != 0 && val >= t[beginIdx]) {
20+
21+
beginIdx = s.charAt(val) & bitMode;
22+
t[beginIdx] = val + 1;
23+
24+
}
25+
t[endIdx] = i + 1;
26+
27+
int v = t[endIdx] - t[beginIdx] + 1;
28+
if (v > maxSize) {
29+
maxSize = v;
30+
}
31+
}
32+
33+
return maxSize;
34+
}
35+
36+
public int lengthOfLongestSubstring_2(String s) {
37+
if (null == s || "".equals(s)) return 0;
38+
if (" ".equals(s) || s.length() == 1) return 1;
39+
40+
int beginIdx = 0, endIdx = 0, maxSize = 0;
41+
for (int i = 1; i < s.length(); i++) {
42+
endIdx = i;
43+
44+
int existIdx = s.indexOf(s.charAt(i), beginIdx);
45+
if (existIdx < endIdx) {
46+
beginIdx = existIdx + 1;
47+
}
48+
49+
int eval = endIdx - beginIdx + 1;
50+
if (maxSize < eval) {
51+
maxSize = eval;
52+
}
53+
}
54+
55+
return maxSize;
56+
}
57+
58+
public int lengthOfLongestSubstring_3(String s) {
59+
char[] array = s.toCharArray();
60+
int[] exist = new int[127];
61+
int result = 0;
62+
int i1 = 0;
63+
int i2 = 0;
64+
for (; i2 < array.length; i2++) {
65+
if (exist[array[i2]] >= i1 + 1) {
66+
result = Math.max(i2 - i1, result);
67+
i1 = exist[array[i2]];
68+
}
69+
exist[array[i2]] = i2 + 1;
70+
}
71+
return Math.max(i2 - i1, result);
72+
}
73+
74+
public int lengthOfLongestSubstring_4(String s) {
75+
if (null == s || "".equals(s)) return 0;
76+
if (" ".equals(s) || s.length() == 1) return 1;
77+
78+
char[] array = s.toCharArray();
79+
int[] exist = new int[127];
80+
exist[array[0]] = 1;
81+
int beginIdx = 1, maxSize = 0;
82+
for (int i = 1; i < array.length; i++) {
83+
if (exist[array[i]] >= beginIdx) {
84+
beginIdx = exist[array[i]] + 1;
85+
}
86+
exist[array[i]] = i + 1;
87+
88+
maxSize = Math.max(exist[array[i]] - beginIdx + 1, maxSize);
89+
}
90+
return maxSize;
91+
}
92+
93+
public int lengthOfLongestSubstring_5(String s) {
94+
if (null == s || "".equals(s)) return 0;
95+
if (" ".equals(s) || s.length() == 1) return 1;
96+
97+
char[] array = s.toCharArray();
98+
int[] exist = new int[127];
99+
exist[array[0]] = 1;
100+
int beginIdx = 1, endIdx = 1, maxSize = 0;
101+
for (int i = 1; i < array.length; i++) {
102+
endIdx = i;
103+
if (exist[array[i]] >= beginIdx) {
104+
maxSize = Math.max(i - beginIdx + 1, maxSize);
105+
beginIdx = exist[array[i]] + 1;
106+
}
107+
exist[array[i]] = i + 1;
108+
109+
}
110+
maxSize = Math.max(exist[array[endIdx]] - beginIdx + 1, maxSize);
111+
return maxSize;
112+
}
113+
114+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.itstack.test.s0001;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.itstack.algorithm.leetcode.s0001.Solution;
5+
import org.junit.Test;
6+
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
10+
public class ApiTest {
11+
12+
private int[] nums = {2, 7, 11, 15};
13+
14+
@Test
15+
public void test_twoSum_0() {
16+
int[] ints = Solution.twoSum_0(nums, 9);
17+
System.out.println(ints[0]);
18+
System.out.println(ints[1]);
19+
}
20+
21+
@Test
22+
public void test_twoSum_1() {
23+
int[] ints = Solution.twoSum_1(nums, 9);
24+
System.out.println(ints[0]);
25+
System.out.println(ints[1]);
26+
}
27+
28+
@Test
29+
public void test_towSum_2() {
30+
int[] ints = Solution.towSum_2(new int[]{3, 7, 1, 0, 4, 2}, 9);
31+
System.out.println(ints[0]);
32+
System.out.println(ints[1]);
33+
}
34+
35+
@Test
36+
public void test_idx() {
37+
38+
int volume = 32; // 100000000000
39+
int bitMode = volume - 1; // 011111111111
40+
int[] t = new int[volume];
41+
42+
int[] nums = {-3, 4, 3, 29, 0, -2};
43+
44+
for (int i = 0; i < nums.length; i++) {
45+
int idx = nums[i] & bitMode;
46+
int val = i + 1;
47+
t[idx] = i + 1;
48+
System.out.println("值:" + nums[i] + " 位置:" + idx + " 结果:" + val);
49+
}
50+
51+
System.out.println(JSON.toJSONString(t));
52+
53+
}
54+
55+
@Test
56+
public void test_for() {
57+
int sum = 1, n = 10;
58+
while (sum < n) {
59+
sum = sum * 2;
60+
}
61+
}
62+
63+
@Test
64+
public void test(){
65+
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
66+
System.out.println(f.format(new Date()));
67+
}
68+
69+
70+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.itstack.test.s0002;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.itstack.algorithm.leetcode.s0002.ListNode;
5+
import org.itstack.algorithm.leetcode.s0002.Solution;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
/**
10+
* 公众号:bugstack虫洞栈 | 沉淀、分享、成长,让自己和他人都能有所收获!
11+
* 博 客:http://bugstack.cn
12+
* Create by 小傅哥 on @2020
13+
*/
14+
public class ApiTest {
15+
16+
private ListNode l1;
17+
private ListNode l2;
18+
19+
@Before
20+
public void init() {
21+
l1 = new ListNode(2);
22+
l1.next = new ListNode(4);
23+
l1.next.next = new ListNode(3);
24+
25+
l2 = new ListNode(5);
26+
l2.next = new ListNode(6);
27+
l2.next.next = new ListNode(4);
28+
29+
}
30+
31+
32+
@Test
33+
public void test_addTwoNumbers() {
34+
35+
Solution solution = new Solution();
36+
ListNode listNode = solution.addTwoNumbers(l1, l2);
37+
38+
System.out.println(JSON.toJSONString(listNode));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)