Skip to content

Commit 9110462

Browse files
author
chenweijie
committed
添加algorithm 1 2 3
1 parent 5d6d903 commit 9110462

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.chen.algorithm.study.test1;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-02 23:52
8+
*/
9+
public class Solution {
10+
11+
public int[] twoSum(int[] nums, int target) {
12+
for (int i = 0; i < nums.length; i++) {
13+
for (int j = i + 1; j < nums.length; j++) {
14+
if (nums[i] + nums[j] == target) {
15+
return new int[]{i, j};
16+
}
17+
}
18+
}
19+
throw new RuntimeException("没有适合的结果");
20+
}
21+
22+
23+
@Test
24+
public void testCase() {
25+
26+
int[] array = {4, 5, 6, 7, 2};
27+
28+
int[] result = twoSum(array, 9);
29+
30+
for (int value : result) {
31+
System.out.println(value);
32+
33+
}
34+
35+
}
36+
37+
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chen.algorithm.study.test2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-09-02 23:06
6+
*/
7+
public class ListNode {
8+
9+
int val;
10+
11+
ListNode next;
12+
13+
ListNode(int val) {
14+
this.val = val;
15+
}
16+
17+
18+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.chen.algorithm.study.test2;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-02 23:05
8+
*/
9+
public class Solution {
10+
11+
12+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13+
ListNode result = new ListNode(0);
14+
ListNode l3 = result;
15+
int nextNum = 0;
16+
while (l1 != null || l2 != null) {
17+
int x = l1 != null ? l1.val : 0;
18+
int y = l2 != null ? l2.val : 0;
19+
20+
int num = x + y + nextNum;
21+
nextNum = 0;
22+
if (num > 9) {
23+
num = num % 10;
24+
nextNum = 1;
25+
}
26+
if (l1 != null) {
27+
l1 = l1.next;
28+
}
29+
30+
if (l2 != null) {
31+
l2 = l2.next;
32+
}
33+
34+
l3.next = new ListNode(num);
35+
l3 = l3.next;
36+
}
37+
38+
if (nextNum > 0) {
39+
l3.next = new ListNode(nextNum);
40+
}
41+
return result.next;
42+
}
43+
44+
45+
@Test
46+
public void testCase() {
47+
48+
ListNode l1_1 = new ListNode(2);
49+
ListNode l1_2 = new ListNode(4);
50+
ListNode l1_3 = new ListNode(3);
51+
52+
l1_1.next = l1_2;
53+
l1_2.next = l1_3;
54+
55+
56+
ListNode l2_1 = new ListNode(5);
57+
ListNode l2_2 = new ListNode(6);
58+
ListNode l2_3 = new ListNode(4);
59+
60+
l2_1.next = l2_2;
61+
l2_2.next = l2_3;
62+
63+
64+
ListNode result = addTwoNumbers(l1_1, l2_1);
65+
66+
System.out.println(result.val);
67+
System.out.println(result.next.val);
68+
System.out.println(result.next.next.val);
69+
70+
71+
}
72+
73+
74+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.chen.algorithm.study.test3;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/**
9+
* @author : chen weijie
10+
* @Date: 2019-09-03 00:00
11+
*/
12+
public class Solution {
13+
14+
public int lengthOfLongestSubstring(String s) {
15+
16+
if (s == null || "".equals(s)) {
17+
return 0;
18+
}
19+
20+
if (" ".equals(s)) {
21+
return 1;
22+
}
23+
24+
int max = 0;
25+
char[] chars = s.toCharArray();
26+
Set<Character> set = new HashSet<>();
27+
int start = 0;
28+
for (int i = 0; i < chars.length; i++) {
29+
int count = 0;
30+
for (int j = start; j < chars.length; j++) {
31+
if (!set.add(chars[j])) {
32+
max = Math.max(max, set.size());
33+
set.clear();
34+
start++;
35+
break;
36+
} else {
37+
count++;
38+
}
39+
}
40+
max = Math.max(max, count);
41+
}
42+
return max;
43+
}
44+
45+
46+
@Test
47+
public void testCase() {
48+
System.out.println(lengthOfLongestSubstring("cdfg"));
49+
}
50+
51+
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.chen.algorithm.study.test3;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* @author : chen weijie
10+
* @Date: 2019-09-03 00:33
11+
*/
12+
public class Solution2 {
13+
14+
public int lengthOfLongestSubstring(String s) {
15+
int n = s.length();
16+
int max = 0;
17+
Map<Character, Integer> characterIntegerMap = new HashMap<>();
18+
for (int i = 0, j = 0; j < n; j++) {
19+
if (characterIntegerMap.containsKey(s.charAt(j))) {
20+
i = Math.max(i, characterIntegerMap.get(s.charAt(j)));
21+
}
22+
max = Math.max(max, j - i + 1);
23+
characterIntegerMap.put(s.charAt(j), j + 1);
24+
}
25+
return max;
26+
}
27+
28+
@Test
29+
public void testCase() {
30+
System.out.println(lengthOfLongestSubstring("cdfkfg"));
31+
}
32+
}

0 commit comments

Comments
 (0)