提交 dad7d566 编写于 作者: L liu13

20181224-5problem

上级 48bcd752
package code;
import java.util.List;
import java.util.Vector;
/*
* 17. Letter Combinations of a Phone Number
* 题意:手机键盘字母输入
* 难度:Medium
* 分类:String, Backtracking
*/
public class lc17 {
public static void main(String[] args) {
System.out.println(letterCombinations("23").toString());
}
public static List<String> letterCombinations(String digits) {
Vector<String> res = new Vector<>();
if(digits.length()==0)
return res;
String[] charmap = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
res.add("");
for (int i = 0; i < digits.length(); i++) {
int n = digits.charAt(i)-'0';
Vector<String> temp_res = new Vector<>();
for (int j = 0; j < charmap[n].length(); j++) {
for (int k = 0; k < res.size(); k++) {
temp_res.add(res.get(k)+charmap[n].charAt(j));
}
}
res = temp_res;
}
return res;
}
}
package code;
/*
* 19. Remove Nth Node From End of List
* 题意:删除链表中倒数第n个节点
* 难度:Medium
* 分类:Linked List, Two Pointers
* 思路:快慢指针,快指针达到链表尾部时,满指针所在位置即为操作的节点
* 注意:看清题意,是倒数第n个,且复杂度为n
*/
public class lc19 {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode low = new ListNode(0);
ListNode fast = new ListNode(0);
ListNode res = low;
low.next = head;
fast.next = head;
while(n>0){
fast = fast.next;
n--;
}
while(fast.next!=null){
low = low.next;
fast = fast.next;
}
ListNode temp = low.next.next;
low.next = temp;
return res.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
}
package code;
import java.util.HashMap;
import java.util.Stack;
/*
* 20. Valid Parentheses
* 题意:括号匹配
* 难度:Easy
* 分类:String, Stack
*/
public class lc20 {
public static void main(String[] args) {
System.out.println(isValid("]"));
}
public static boolean isValid(String s) {
Stack<String> st = new Stack();
HashMap<String,String> hm = new HashMap();
hm.put("(",")");
hm.put("[","]");
hm.put("{","}");
for (int i = 0; i < s.length() ; i++) {
char ch = s.charAt(i);
if(ch=='(' || ch=='[' || ch=='{'){
st.push(String.valueOf(ch));
}else{
if(st.size()==0)
return false;
String temp1 = hm.get(st.pop());
String temp2 = String.valueOf(ch);
if(!temp1.equals(temp2))
return false;
}
}
if(st.size()==0)
return true;
return false;
}
}
package code;
/*
* 22. Generate Parentheses
* 题意:正确括号组合的
* 难度:Medium
* 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class lc22 {
public static void main(String[] args) {
System.out.println(generateParenthesis2(4));
}
public static List<String> generateParenthesis(int n) {
//递归法
Set<String> res = new HashSet<String>();
if(n==1) {
res.add("()");
return new ArrayList(res);
}
List<String> temp = generateParenthesis(n-1);
// 一个括号,和n-1个括号的组合
for (int i = 0; i < temp.size(); i++) {
res.add("("+temp.get(i)+")");
res.add("()"+temp.get(i));
res.add(temp.get(i)+"()");
}
//2块拼一起
for (int j = 2; j <=n/2 ; j++) {
List<String> temp1 = generateParenthesis(j);
List<String> temp2 = generateParenthesis(n-j);
for (int i = 0; i <temp1.size() ; i++) {
for (int k = 0; k < temp2.size(); k++) {
res.add(temp1.get(i)+temp2.get(k));
res.add(temp2.get(k)+temp1.get(i));
}
}
}
return new ArrayList(res);
}
public static List<String> generateParenthesis2(int n) {
//回溯法
ArrayList<String> res = new ArrayList<>();
backtracking(res,"", 0, 0, n);
return res;
}
public static void backtracking(List<String> list,String str,int left, int right, int max){
if(right==max){
list.add(str);
}
if(left<max)
backtracking(list,str+"(",left+1,right,max);
if(right<left)
backtracking(list,str+")",left,right+1,max);
}
}
\ No newline at end of file
package code;
import java.util.Comparator;
import java.util.PriorityQueue;
/*
* 22. Generate Parentheses
* 题意:正确括号组合的
* 难度:Medium
* 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
*/
public class lc23 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
PriorityQueue<ListNode> pr = new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
@Override
public int compare(ListNode o1,ListNode o2){
if (o1.val<o2.val)
return -1;
else if (o1.val==o2.val)
return 0;
else
return 1;
}
});
for (ListNode l:lists) {
if (l!=null)
pr.add(l);
}
ListNode head = new ListNode(0);
ListNode ln = head;
while(pr.size()!=0){
ln.next = pr.remove();
ln = ln.next;
if(ln.next!=null)
pr.add(ln.next);
}
return head.next;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册