Skip to content

Commit dad7d56

Browse files
committed
20181224-5problem
1 parent 48bcd75 commit dad7d56

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed

code/lc17.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package code;
2+
import java.util.List;
3+
import java.util.Vector;
4+
/*
5+
* 17. Letter Combinations of a Phone Number
6+
* 题意:手机键盘字母输入
7+
* 难度:Medium
8+
* 分类:String, Backtracking
9+
*/
10+
public class lc17 {
11+
public static void main(String[] args) {
12+
System.out.println(letterCombinations("23").toString());
13+
}
14+
15+
public static List<String> letterCombinations(String digits) {
16+
Vector<String> res = new Vector<>();
17+
if(digits.length()==0)
18+
return res;
19+
String[] charmap = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
20+
res.add("");
21+
for (int i = 0; i < digits.length(); i++) {
22+
int n = digits.charAt(i)-'0';
23+
Vector<String> temp_res = new Vector<>();
24+
for (int j = 0; j < charmap[n].length(); j++) {
25+
for (int k = 0; k < res.size(); k++) {
26+
temp_res.add(res.get(k)+charmap[n].charAt(j));
27+
}
28+
}
29+
30+
res = temp_res;
31+
}
32+
return res;
33+
}
34+
}

code/lc19.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package code;
2+
/*
3+
* 19. Remove Nth Node From End of List
4+
* 题意:删除链表中倒数第n个节点
5+
* 难度:Medium
6+
* 分类:Linked List, Two Pointers
7+
* 思路:快慢指针,快指针达到链表尾部时,满指针所在位置即为操作的节点
8+
* 注意:看清题意,是倒数第n个,且复杂度为n
9+
*/
10+
public class lc19 {
11+
public ListNode removeNthFromEnd(ListNode head, int n) {
12+
ListNode low = new ListNode(0);
13+
ListNode fast = new ListNode(0);
14+
ListNode res = low;
15+
low.next = head;
16+
fast.next = head;
17+
while(n>0){
18+
fast = fast.next;
19+
n--;
20+
}
21+
while(fast.next!=null){
22+
low = low.next;
23+
fast = fast.next;
24+
}
25+
ListNode temp = low.next.next;
26+
low.next = temp;
27+
return res.next;
28+
}
29+
30+
public class ListNode {
31+
int val;
32+
ListNode next;
33+
ListNode(int x) { val = x; }
34+
}
35+
}

code/lc20.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package code;
2+
3+
import java.util.HashMap;
4+
import java.util.Stack;
5+
6+
/*
7+
* 20. Valid Parentheses
8+
* 题意:括号匹配
9+
* 难度:Easy
10+
* 分类:String, Stack
11+
*/
12+
public class lc20 {
13+
public static void main(String[] args) {
14+
System.out.println(isValid("]"));
15+
}
16+
public static boolean isValid(String s) {
17+
Stack<String> st = new Stack();
18+
HashMap<String,String> hm = new HashMap();
19+
hm.put("(",")");
20+
hm.put("[","]");
21+
hm.put("{","}");
22+
for (int i = 0; i < s.length() ; i++) {
23+
char ch = s.charAt(i);
24+
if(ch=='(' || ch=='[' || ch=='{'){
25+
st.push(String.valueOf(ch));
26+
}else{
27+
if(st.size()==0)
28+
return false;
29+
String temp1 = hm.get(st.pop());
30+
String temp2 = String.valueOf(ch);
31+
if(!temp1.equals(temp2))
32+
return false;
33+
}
34+
}
35+
if(st.size()==0)
36+
return true;
37+
return false;
38+
}
39+
}

code/lc22.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package code;
2+
/*
3+
* 22. Generate Parentheses
4+
* 题意:正确括号组合的
5+
* 难度:Medium
6+
* 分类:String, Backtracking
7+
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
8+
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
9+
*/
10+
import java.util.ArrayList;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Set;
14+
15+
public class lc22 {
16+
public static void main(String[] args) {
17+
System.out.println(generateParenthesis2(4));
18+
}
19+
20+
public static List<String> generateParenthesis(int n) {
21+
//递归法
22+
Set<String> res = new HashSet<String>();
23+
if(n==1) {
24+
res.add("()");
25+
return new ArrayList(res);
26+
}
27+
List<String> temp = generateParenthesis(n-1);
28+
// 一个括号,和n-1个括号的组合
29+
for (int i = 0; i < temp.size(); i++) {
30+
res.add("("+temp.get(i)+")");
31+
res.add("()"+temp.get(i));
32+
res.add(temp.get(i)+"()");
33+
}
34+
//2块拼一起
35+
for (int j = 2; j <=n/2 ; j++) {
36+
List<String> temp1 = generateParenthesis(j);
37+
List<String> temp2 = generateParenthesis(n-j);
38+
for (int i = 0; i <temp1.size() ; i++) {
39+
for (int k = 0; k < temp2.size(); k++) {
40+
res.add(temp1.get(i)+temp2.get(k));
41+
res.add(temp2.get(k)+temp1.get(i));
42+
}
43+
}
44+
}
45+
return new ArrayList(res);
46+
}
47+
48+
public static List<String> generateParenthesis2(int n) {
49+
//回溯法
50+
ArrayList<String> res = new ArrayList<>();
51+
backtracking(res,"", 0, 0, n);
52+
return res;
53+
}
54+
55+
public static void backtracking(List<String> list,String str,int left, int right, int max){
56+
if(right==max){
57+
list.add(str);
58+
}
59+
if(left<max)
60+
backtracking(list,str+"(",left+1,right,max);
61+
if(right<left)
62+
backtracking(list,str+")",left,right+1,max);
63+
}
64+
65+
66+
}

code/lc23.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package code;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
/*
7+
* 22. Generate Parentheses
8+
* 题意:正确括号组合的
9+
* 难度:Medium
10+
* 分类:String, Backtracking
11+
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
12+
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
13+
*/
14+
public class lc23 {
15+
public class ListNode {
16+
int val;
17+
ListNode next;
18+
ListNode(int x) { val = x; }
19+
}
20+
21+
public ListNode mergeKLists(ListNode[] lists) {
22+
if (lists==null||lists.length==0) return null;
23+
PriorityQueue<ListNode> pr = new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
24+
@Override
25+
public int compare(ListNode o1,ListNode o2){
26+
if (o1.val<o2.val)
27+
return -1;
28+
else if (o1.val==o2.val)
29+
return 0;
30+
else
31+
return 1;
32+
}
33+
});
34+
for (ListNode l:lists) {
35+
if (l!=null)
36+
pr.add(l);
37+
}
38+
ListNode head = new ListNode(0);
39+
ListNode ln = head;
40+
while(pr.size()!=0){
41+
ln.next = pr.remove();
42+
ln = ln.next;
43+
if(ln.next!=null)
44+
pr.add(ln.next);
45+
}
46+
return head.next;
47+
}
48+
}

0 commit comments

Comments
 (0)