Leetcode动态规划题答案第二期

leetcode的第97题,两个字符串交错形成新的字符串,当时不理解,自己实现了一个时间复杂度太高的方法,当然超时了,笑笑就好

import java.util.Stack;


public class 动态规划之两个字符串交错形成字符串_97 {
	public static void main(String[] args) {
		动态规划之两个字符串交错形成字符串_97 test=new 动态规划之两个字符串交错形成字符串_97();
		String s1="a";
		String s2="d";
		String s3="a";
		boolean interleave = test.isInterleave2(s1, s2, s3);
		System.out.println(interleave);
	}
	//dp动态规划实现
	 private boolean isInterleave2(String s1, String s2, String s3) {
		 int len1=s1.length();
		 int len2=s2.length();
		 int len3=s3.length();
		 if (len1+len2!=len3) {
			return false;
		}
		 char[] charArray1 = s1.toCharArray();
		 char[] charArray2 = s2.toCharArray();
		 char[] charArray3 = s3.toCharArray();
		//dp[i][j]代表 chs1[0...i]  chs2[0...j]能否顺序匹配chs3[i+j]
		 boolean[][] dp=new boolean [len1+1][len2+1];
		 //表示s1和s2都为空串形成s3空串情况时肯定能形成的
		 dp[0][0]=true;
		    //s1中取0个s2中取i个 去和s3中0+i个匹配,如果之前出现过不匹配情况,那么只会的会直接为false
	        for(int i = 1 ; i < len2 + 1; i ++ ){
	          dp[0][i] = dp[0][i-1] && charArray2[i-1] == charArray3[i-1];
	        }
	        //s2中取0个s1中取i个 去和s3中0+i 个匹配,如果之前出现过不匹配情况,那么只会的会直接为false
	        for(int i = 1 ; i < len1 + 1; i ++ ){
	          dp[i][0] = dp[i-1][0] && charArray1[i-1] == charArray3[i-1];
	        }
	        //dp[i][j]位置来源于dp[i-1][j]和dp[i][j-1]两个位置
	        for(int i = 1 ; i < len1+1 ; i ++ ){
	          for(int j = 1 ; j < len2+1 ; j ++ ){
	            dp[i][j] = dp[i-1][j] && (charArray3[i+j-1] == charArray1[i-1])
	                || dp[i][j-1] && (charArray3[i+j-1] == charArray2[j-1]);
	          }
	        }
	        return dp[len1][len2];
	}
	 
	 
    //使用三个栈实现回溯法,时间复杂度很高
	public boolean isInterleave(String s1, String s2, String s3) {
		char[] charS1 = s1.toCharArray(); 
		char[] charS2 = s2.toCharArray(); 
		char[] charS3 = s3.toCharArray(); 
		Stack<Character> stack1=new Stack<>();
		for (int i = 0; i < charS1.length; i++) {
			stack1.add(charS1[i]);
		}
		Stack<Character> stack2=new Stack<>();
		for (int i = 0; i < charS2.length; i++) {
			stack2.add(charS2[i]);
		}
		Stack<Character> stack3=new Stack<>();
		for (int i = 0; i < charS3.length; i++) {
			stack3.add(charS3[i]);
		}
		if (s3.length()!=s1.length()+s2.length()) {
			return false;
		}
		return isInterleave(stack1, stack2, stack3);
	 }

	private boolean isInterleave(Stack<Character> stack1,Stack<Character> stack2,
			Stack<Character> stack3) {
		if (stack1.isEmpty()&&stack2.isEmpty()&&stack3.isEmpty()) {
			return true;
		}
		if (stack3.peek()!=(stack1.isEmpty()?null:stack1.peek())&&stack3.peek()!=(stack2.isEmpty()?null:stack2.peek())) {
			return false;
		}
		if (stack3.peek()==(stack1.isEmpty()?null:stack1.peek())&&stack3.peek()!=(stack2.isEmpty()?null:stack2.peek())) {
			Character char3=stack3.pop();
			Character char1=stack1.pop();
			boolean interleave = isInterleave(stack1, stack2, stack3);
			if (interleave) {
				return true;
			}else {
				//恢复现场
				stack1.add(char1);
				stack3.add(char3);
			}
		}
		if (stack3.peek()!=(stack1.isEmpty()?null:stack1.peek())&&stack3.peek()==(stack2.isEmpty()?null:stack2.peek())) {
			Character char3=stack3.pop();
			Character char2=stack2.pop();
			boolean interleave = isInterleave(stack1, stack2, stack3);
			if (interleave) {
				return true;
			}else {
				//恢复现场
				stack2.add(char2);
				stack3.add(char3);
			}
		}
		if (stack3.peek()==(stack1.isEmpty()?null:stack1.peek())&&stack3.peek()==(stack2.isEmpty()?null:stack2.peek())) {
			boolean interleave1=false;
			boolean interleave2=false;
			for (int i = 0; i <=1; i++) {
				if (i==0) {
					Character char3=stack3.pop();
					Character char1=stack1.pop();
					interleave1 = isInterleave(stack1, stack2, stack3);
					if (interleave1) {
						return true;
					}else {
						//恢复现场
				    	stack1.add(char1);
						stack3.add(char3);
					}
				}
				if (i==1) {
					Character char3=stack3.pop();
					Character char2=stack2.pop();
					interleave2 = isInterleave(stack1, stack2, stack3);
					if (interleave2) {
						return true;
					}else {
						//恢复现场
						stack2.add(char2);
						stack3.add(char3);
					}
				}
			}
		}
		return false;
	}
}

leetcode的120题:帕斯卡矩阵变形改编题,从上到下最小步长,将真,这个思想我打死没想到啊。。

import java.util.List;

public class 动态规划之帕斯卡尔矩形改编从上到下最小步长_120 {
	public int minimumTotal(List<List<Integer>> triangle) {
	   int [] total=new int[triangle.size()];
	   int l=triangle.size()-1;
	   for (int i = 0; i < triangle.get(l).size(); i++) {
		total[i]=triangle.get(l).get(i);
	   }
	  for (int i = triangle.size()-2; i >=0; i--) {
		for (int j = 0; j < triangle.get(i+1).size()-1; j++) {
			total[j]=triangle.get(i).get(j)+Math.min(total[j], total[j+1]);
		  }
	  }
		return total[0];
	}
}

leetcode的121-123题股票问题就是最大连续和子序列变形题:

public class 动态规划之最好时机买股票和出售股票_124 {
	public static void main(String[] args) {
		动态规划之最好时机买股票和出售股票_124 test=new 动态规划之最好时机买股票和出售股票_124();
		int[] prices=new int[]{
				1
		};
		test.maxProfit(prices);
	}
	
	 public int maxProfit(int[] prices) {
	        if(prices.length<=1) return 0;
	        int diff[]=new int[prices.length-1];
	        for(int i=0;i<diff.length;i++){
	            diff[i]=prices[i+1]-prices[i];
	        }
	        int leftEnd=0;
	        int leftAll=0;
	        int rightMax[]=new int[diff.length];
	        int rightEnd=0;
	        int rightAll=0;
	        for(int i=diff.length-1;i>=0;i--){
	            rightEnd=Math.max(rightEnd+diff[i],diff[i]);
	            rightAll=Math.max(rightAll,rightEnd);
	            rightMax[i]=rightAll;
	        }
	        if(rightMax.length<=1){
	            return rightMax[0];
	        }
	        
	        int max=0;
	        for(int i=0;i<diff.length-1;i++){
	            leftEnd=Math.max(leftEnd+diff[i],diff[i]);
	            leftAll=Math.max(leftAll,leftEnd);
	            max=Math.max(max,(leftAll+rightMax[i+1]));
	        }
	        return max;
	    }
}

leetcode第354题最长递增子序列变形题:这道题很吊,给出的都是n*log(n)方法,二分缩短时间查找

import java.util.Arrays;
import java.util.Comparator;
public class 动态规划之最长递增子序列_俄国沙皇问题354 {
	public static void main(String[] args) {
		动态规划之最长递增子序列_俄国沙皇问题354 test=new 动态规划之最长递增子序列_俄国沙皇问题354();
		int[][] envelopes=new int[][]{
				{1,2},
				{2,3},
				{3,5},
				{3,4},
				{4,5},
				{5,6},
				{5,5},
				{6,7},
				{7,8}
		};
		int maxEnvelopes = test.maxEnvelopes(envelopes);
		System.out.println(maxEnvelopes);
	}
	
	
	   public int maxEnvelopes(int[][] envelopes) {
			if (envelopes==null||envelopes.length==0) {
				return 0;
			}
			Dos [] dos=new Dos[envelopes.length];
			for (int i = 0; i < dos.length; i++) {
				dos[i]=new Dos(envelopes[i][0],envelopes[i][1]);
			}
			Arrays.sort(dos,new Comparator<Dos>() {

				public int compare(Dos arg0,Dos arg1) {
					if (arg0.w==arg1.w) {
						if (arg0.h<arg1.h) {
							return 1;
						}else if (arg0.h>arg1.h) {
							return -1;
						}else {
							return 0;
						}
					}else if (arg0.w<arg1.w) {
						return -1;
					}else {
						return 1;
					}
				}
			});
			//存放的是长度为i+1的最长递增子序列的结尾处值为h的最小值
			int dp[]=new int[dos.length];
			dp[0]=dos[0].h;
			int maxIndex=0;
			for (int i = 1; i < dos.length; i++) {
				if (dos[i].h>dp[maxIndex]) {
					dp[++maxIndex]=dos[i].h;
				}else {
					//二分
					int left=0;
					int right=maxIndex;
					int mid=left;
					while (left<=right) {
						if (right-left<=1) {
							if (dos[i].h<=dp[left]) {
								dp[left]=dos[i].h;
							}else {
								dp[right]=dos[i].h;
							}
							break;
						}
						mid=(left+right)/2;
						if (dp[mid]<dos[i].h) {
							left=mid;
						}else {
							right=mid;
						}
					}
				}
			}
			return maxIndex+1;
		 }
		 static class Dos{
			 int w;
			 int h;
			public Dos(int w, int h) {
				this.w = w;
				this.h = h;
			}
		 } 
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值