提交 1bb9af43 编写于 作者: L liu13

20181225

上级 dad7d566
package code;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
/*
* 22. Generate Parentheses
* 题意:正确括号组合的
* 难度:Medium
* 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
* 23. Merge k Sorted Lists
* 题意:K个有序链表合并
* 难度:Hard
* 分类:Linked List, Divide and Conquer, Heap
* 思路:优先队列或分治方法
* 注意:优先队列如何定义比较方法
*/
public class lc23 {
public class ListNode {
......@@ -45,4 +46,44 @@ public class lc23 {
}
return head.next;
}
public ListNode mergeKLists2(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
return Partition(lists,0,lists.length-1);
}
public ListNode Partition(ListNode[] lists, int start, int end){
if(end-start==0)
return lists[end];
else{
int mid = (start+end)/2;
ListNode l1 = Partition(lists,start,mid);
ListNode l2 = Partition(lists,mid+1,end);
return Merge2List(l1,l2);
}
}
public ListNode Merge2List(ListNode ln1, ListNode ln2){
ListNode res = new ListNode(0);
ListNode head = res;
while(ln1!=null && ln2!=null){
if(ln1.val<ln2.val){
head.next = ln1;
head = head.next;
ln1 = ln1.next;
}else {
head.next = ln2;
head = head.next;
ln2 = ln2.next;
}
}
if(ln1!=null){
head.next = ln1;
}
if(ln2!=null){
head.next = ln2;
}
return res.next;
}
}
package code;
/*
* 31. Next Permutation
* 题意:找出排列组合的下一个排列
* 难度:Medium
* 分类:Array
* 思路:从后往前找第一个变小的数x,从这个数之后的数中找出第一个比x大的数,交换,再把之后的数逆序即可
* Tips:很典型的排列组合题,思路方法记忆一下。注意比较时是否有=。
*/
public class lc31 {
public static void main(String[] args) {
int[] nums = {2,3,1,3,3};
nextPermutation(nums);
for (int i:nums){
System.out.println(i);
}
}
public static void nextPermutation(int[] nums) {
int ptr = nums.length-1;
//从后往前找第一个变小的数x
while(ptr>0&&nums[ptr-1]>=nums[ptr]){// 注意是 >= {5,1,1} , 等于--
ptr--;
}
if(ptr!=0){
//从这个数之后的数中找出第一个比x大的数
int n = nums[ptr];
int ptr2 = ptr;
for(int i=ptr+1; i<nums.length; i++){
if( nums[i]>nums[ptr-1] && nums[i]<=n ) {//注意 <= {2,3,1,3,3}
n = nums[i];
ptr2 = i;
}
}
nums[ptr2] = nums[ptr-1];
nums[ptr-1] = n;
}
//把之后的数逆序
ReverseNums(nums,ptr,nums.length-1);
}
public static void ReverseNums(int[] nums, int start, int end){
int l = end+start;
for (int i = start; i < (start+end+1)/2 ; i++) {
int temp = nums[i];
nums[i] = nums[l-i];
nums[l-i] = temp;
}
}
}
package code;
import java.util.Stack;
/*
* 31. Longest Valid Parentheses
* 题意:最长有效子串
* 难度:Hard
* 分类:Dynamic Programming, String
* 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。
* Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。
*/
public class lc32 {
public static void main(String[] args) {
System.out.println(longestValidParentheses2("()(())"));
}
public static int longestValidParentheses(String s) {
if(s.length()==0)
return 0;
// dp 方法
int[] dp = new int[s.length()+1];
int res=0;
for (int i = 2; i <dp.length; i++) {
if(s.charAt(i-1)=='(')
dp[i] = 0;
else{
if(s.charAt(i-2)=='('){ // 这种情况:(())()
dp[i] = dp[i-2]+2;
}else if(i-2-dp[i-1]>=0 && s.charAt(i-2-dp[i-1])=='('){ // 这种情况:()(())
dp[i] = dp[i-1] + dp[i-2-dp[i-1]]+2;
}
}
if(dp[i]>res)
res = dp[i];
}
return res;
}
public static int longestValidParentheses2(String s) {
//栈方法 ()(())
Stack<Integer> st = new Stack();
st.add(-1);
int res = 0;
for (int i = 0; i < s.length() ; i++) {
char ch = s.charAt(i);
if(ch=='(')
st.add(i);
else if(ch==')'){
st.pop();
if(st.isEmpty())
st.push(i);
res = Math.max(res,i-st.peek());
}
}
return res;
}
}
package code;
/*
* 31. Search in Rotated Sorted Array
* 题意:Longest Valid Parentheses
* 难度:Hard
* 分类:Array, Binary Search
* 思路:二分查找的思路,多了一步判断,判断哪部分有序,是否在这部分中
* Tips:注意边界判断,是否有等号
*/
public class lc33 {
public static void main(String[] args) {
int[] nums = {1,3};
System.out.println(search(nums, 3));
}
public static int search(int[] nums, int target) {
int start = 0;
int end = nums.length-1;
while(start<=end){
if(start==end){
if(nums[start]==target)
return start;
return -1;
}
int mid = (start+end)/2;
if(target == nums[mid])
return mid;
if(nums[mid]>nums[end]){//左边是有序的
if(nums[start]<=target && target<=nums[mid])
end = mid-1;
else
start = mid+1;
}else{//右边是有序的
if(nums[mid]<=target && target<=nums[end])
start = mid+1;
else
end =mid-1;
}
}
return -1;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册