-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile
More file actions
85 lines (80 loc) · 2.34 KB
/
File
File metadata and controls
85 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# LeetCodePractice
import java.util.*;
public class Day01 {
public static void main(String[] args) {
int[] nums = {3,3};
int target = 6;
Day01 tool = new Day01();
ListNode l1 = new ListNode(2);
ListNode l2 = new ListNode(4);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(5);
ListNode l5 = new ListNode(6);
ListNode l6 = new ListNode(4);
l1.next = l2;
l2.next = l3;
l4.next = l5;
l5.next = l6;
ListNode.printList(tool.addTwoNumbers(l1,l4));
}
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
if (s.length() == 0){
return 0;
}
int i = 0;
int j = 0;
int maxLen = 0;
for(;i<s.length(); i++){
if (!set.contains(s.charAt(i))){
set.add(s.charAt(i));
maxLen = Math.max(maxLen, set.size());
}else {
while (set.contains(s.charAt(i))){
set.remove(s.charAt(j));
j++;
}
set.add(s.charAt(i));
}
}
return maxLen;
}
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
if (map.containsKey(target-nums[i])){
return new int[]{map.get(target-nums[i]),i};
}else {
map.put(nums[i],i);
}
}
return null;
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode tail = null;
int carry = 0;
while (l1!=null || l2!=null){
int val1 = l1==null? 0: l1.val;
int val2 = l2==null? 0: l2.val;
int sum = val1 + val2 + carry;
if (head==null){
head = tail = new ListNode(sum % 10);
}else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if (l1!=null){
l1 = l1.next;
}
if (l2!=null){
l2 = l2.next;
}
}
if (carry > 0){
tail.next = new ListNode(carry);
}
return head;
}
}