Skip to content

Commit ea56c23

Browse files
committed
1. 新增部分题目
1 parent 69cf9f3 commit ea56c23

4 files changed

Lines changed: 223 additions & 1 deletion

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.leosanqing.leetcode.easy.list;
2+
3+
/**
4+
* @Author: rtliu
5+
* @Date: 2020/7/7 下午4:46
6+
* @Package: com.leosanqing.leetcode.easy.list
7+
* @Description: Merge two sorted linked lists and return it as a new sorted list.
8+
* The new list should be made by splicing together the nodes of the first two lists.
9+
* Example:
10+
* Input: 1->2->4, 1->3->4
11+
* Output: 1->1->2->3->4->4
12+
* @Version: 1.0
13+
*/
14+
public class _21_merge_two_sorted_lists {
15+
16+
public static void main(String[] args) {
17+
ListNode node1 = new ListNode(4);
18+
ListNode node2 = new ListNode(2,node1);
19+
ListNode node3 = new ListNode(1,node2);
20+
ListNode node4 = new ListNode(4);
21+
ListNode node5 = new ListNode(3,node4);
22+
ListNode node6 = new ListNode(1,node5);
23+
24+
System.out.println(mergeTwoLists(node3,node6));
25+
}
26+
27+
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
28+
29+
if (l1 == null && l2 == null) {
30+
return new ListNode();
31+
} else if (l1 == null) {
32+
return l2;
33+
} else if (l2 == null) {
34+
return l1;
35+
}
36+
37+
ListNode newList = new ListNode();
38+
ListNode fakeHead = newList;
39+
ListNode curr1 = l1;
40+
ListNode curr2 = l2;
41+
42+
while (curr1 != null && curr2 != null) {
43+
if (curr1.val <= curr2.val) {
44+
newList.next = curr1;
45+
curr1 = curr1.next;
46+
}else {
47+
newList.next = curr2;
48+
curr2 = curr2.next;
49+
}
50+
newList = newList.next;
51+
}
52+
53+
if(curr1 != null){
54+
newList.next = curr1;
55+
}
56+
if(curr2 != null){
57+
newList.next = curr2;
58+
}
59+
60+
return fakeHead.next;
61+
}
62+
63+
64+
static class ListNode {
65+
int val;
66+
ListNode next;
67+
68+
ListNode() {
69+
}
70+
71+
ListNode(int val) {
72+
this.val = val;
73+
}
74+
75+
ListNode(int val, ListNode next) {
76+
this.val = val;
77+
this.next = next;
78+
}
79+
}
80+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.leosanqing.leetcode.easy.string;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @Author: rtliu
7+
* @Date: 2020/7/7 下午4:20
8+
* @Package: com.leosanqing.leetcode.easy.string
9+
* @Description: ` Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
10+
* ` determine if the input string is valid.
11+
* ` An input string is valid if:
12+
* ` Open brackets must be closed by the same type of brackets.
13+
* ` Open brackets must be closed in the correct order.
14+
* ` Note that an empty string is also considered valid.
15+
* ` Example 1:
16+
* ` Input: "()"
17+
* ` Output: true
18+
* ` Example 2:
19+
* ` Input: "()[]{}"
20+
* ` Output: true
21+
* ` Example 3:
22+
* ` Input: "(]"
23+
* ` Output: false
24+
* ` Example 4:
25+
* ` Input: "([)]"
26+
* ` Output: false
27+
* ` Example 5:
28+
* ` Input: "{[]}"
29+
* ` Output: true
30+
* @Version: 1.0
31+
*/
32+
public class _20_valid_parentheses {
33+
public static void main(String[] args) {
34+
35+
}
36+
37+
public boolean isValid(String s) {
38+
39+
if (s == null || "".equals(s)) {
40+
return true;
41+
}
42+
43+
char[] charArray = s.toCharArray();
44+
if (charArray.length % 2 != 0) {
45+
return false;
46+
}
47+
48+
Stack<Character> stack = new Stack<>();
49+
for (char c : charArray) {
50+
if (c == '[') {
51+
stack.push(']');
52+
} else if (c == '(') {
53+
stack.push(')');
54+
} else if (c == '{') {
55+
stack.push('}');
56+
} else if (stack.isEmpty() || stack.pop() != c) {
57+
return false;
58+
}
59+
60+
}
61+
62+
return stack.isEmpty();
63+
}
64+
}

java-note-algorithm/src/main/java/com/leosanqing/leetcode/medium/array/_17_letter_combinations_of_a_phone_number.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ public static void main(String[] args) {
2424
}
2525

2626
public static List<String> letterCombinations(String digits) {
27+
List<String> list = new LinkedList<>();
28+
if(digits == null || "".equals(digits)){
29+
return list;
30+
}
2731
char[] charArray = digits.toCharArray();
2832

29-
List<String> list = new LinkedList<>();
33+
3034
char[][] map = {
3135
{},
3236
{},
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.leosanqing.leetcode.medium.list;
2+
3+
/**
4+
* @Author: rtliu
5+
* @Date: 2020/7/7 下午3:47
6+
* @Package: com.leosanqing.leetcode.medium.list
7+
* @Description: Given a linked list, remove the n-th node from the end of list and return its head.
8+
*
9+
*
10+
*` `Example:
11+
*` ` Give linked list: 1->2->3->4->5, and n = 2.
12+
*` ` After removing the second node from the end, the linked list becomes
13+
*` ` 1->2->3->5.
14+
*` ` Note: Given n will always be valid.
15+
* @Version: 1.0
16+
*/
17+
public class _19_remove_Nth_node_from_end_of_list {
18+
19+
public static void main(String[] args) {
20+
21+
}
22+
23+
/**
24+
* 思路就是使用两个节点,一个快节点先走 n步,然后快节点走到最后,后面的那个节点就是我们要去掉的节点
25+
* 设置一个虚拟头结点,可以帮助我们解决边界值问题
26+
* @param head
27+
* @param n
28+
* @return
29+
*/
30+
public ListNode removeNthFromEnd(ListNode head, int n) {
31+
32+
// 设置一个虚拟的头结点,便于后面做边界值判断
33+
ListNode fakeHead = new ListNode();
34+
fakeHead.next = head;
35+
36+
// 快节点,用于记录n
37+
ListNode fastNode = head;
38+
for (int i = 0; i < n; i++) {
39+
fastNode = fastNode.next;
40+
}
41+
ListNode pre = fakeHead;
42+
ListNode curr = head;
43+
while (fastNode != null) {
44+
fastNode = fastNode.next;
45+
pre = curr;
46+
curr = curr.next;
47+
}
48+
49+
50+
pre.next = curr.next;
51+
52+
53+
return fakeHead.next;
54+
}
55+
56+
57+
static class ListNode {
58+
int val;
59+
ListNode next;
60+
61+
ListNode() {
62+
}
63+
64+
ListNode(int val) {
65+
this.val = val;
66+
}
67+
68+
ListNode(int val, ListNode next) {
69+
this.val = val;
70+
this.next = next;
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)