5.7LeetCode

5.7LeetCode

昨天太累了,就没更新。。今天一起更下。
昨天主要做的基本都是比较简单的dp题目。

55. Jump Game

55.跳跃游戏

因为是dp专题,所以肯定有dp解法:其实感觉也不算严谨的dp吧。。canGo[n]表示n可否到达

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public boolean canJump(int[] nums) {
boolean[] canGo=new boolean[nums.length];
canGo[0]=true;
for(int i=0;i<nums.length;i++){
if(canGo[i]){
int j=i+1;
while(j<nums.length&&j<=i+nums[i]){
canGo[j]=true;
j++;
}
}
if(canGo[nums.length-1]) return true;
}
return canGo[nums.length-1];
}
}

还有种复杂度是O(n)的解法,求出从0出发可以到达的最远点。

1
2
3
4
5
6
7
8
9
10
class Solution {
public boolean canJump(int[] nums) {
int maxlength=nums[0];
for(int i=0;i<=maxlength;i++){
if(maxlength>=nums.length-1) return true;
if(nums[i]+i>maxlength) maxlength=nums[i]+i;
}
return false;
}
}

62. Unique Paths

62.不同路径

dp解法非常简单。。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp=new int[m][n];
for(int i=0;i<n;i++)
dp[0][i]=1;
for(int i=0;i<m;i++)
dp[i][0]=1;
for(int i=1;i<m;i++)
for(int j=1;j<n;j++)
dp[i][j]=dp[i-1][j]+dp[i][j-1];
return dp[m-1][n-1];
}
}

另外一种解法就是求C(m-1,m+n-2)。

322. Coin Change

  1. 零钱兑换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int coinChange(int[] coins, int amount) {
if(amount==0) return 0;
int[] dp=new int[amount+1];
for(int i=0;i<amount+1;i++)
dp[i]=Integer.MAX_VALUE;
for(int i:coins)
if(i<=amount)
dp[i]=1;
for(int i=1;i<amount+1;i++)
for(int j:coins){
if(i-j>0&&dp[i-j]!=Integer.MAX_VALUE&&dp[i-j]+1<dp[i])
dp[i]=dp[i-j]+1;
}
//return dp[amount];
return dp[amount]==Integer.MAX_VALUE? -1:dp[amount];
}
}

300. Longest Increasing Subsequence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp=new int[nums.length];
for(int i=0;i<nums.length;i++)
dp[i]=1;
for(int i=1;i<nums.length;i++)
for(int j=i-1;j>=0;j--)
if(nums[i]>nums[j]&&dp[j]+1>dp[i])
dp[i]=dp[j]+1;
int max=0;
for(int i:dp){
//System.out.println(i);
if(i>max)
max=i;

}

return max;
}
}

297. Serialize and Deserialize Binary Tree

  1. 二叉树的序列化与反序列化
    一道不是非常hard的hard题,深切感受到了StringBuffer的快速。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>();
StringBuffer res=new StringBuffer();
if(root==null) return res.toString();
res.append(String.valueOf(root.val));
queue.offer(root.left);
queue.offer(root.right);
while(!queue.isEmpty())
{
int size=queue.size();
for(int i=0;i<size;i++){
TreeNode t=queue.poll();
if(t==null){
res.append(",null");
}
else{
res.append(",");
res.append(String.valueOf(t.val));
queue.offer(t.left);
queue.offer(t.right);
}
}
}
//System.out.println(res);
return res.toString();

}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.length()==0) return null;
String[] s=data.split(",");
int n=s.length;
for(int i=s.length-1;i>=0;i--)
if(s[i].equals("null"))
n--;
else break;
// for(int i=0;i<n;i++)
// System.out.println(s[i]);

TreeNode[] node=new TreeNode[n];
int j=1;
for(int i=0;i<n;i++)
if(!s[i].equals("null"))
node[i]=new TreeNode(0);
else node[i]=null;
for(int i=0;i<n;i++)
if(!s[i].equals("null")){
node[i].val=Integer.parseInt(s[i]);
if(j<n&&!s[j].equals("null")) node[i].left=node[j];
j++;
if(j<n&&!s[j].equals("null")) node[i].right=node[j];
j++;

}
else node[i]=null;

return node[0];

}
}

172. Factorial Trailing Zeroes

  1. 阶乘后的零
    数学题。找规律。还以优化下,从两次循环减少到1次循环。
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int trailingZeroes(int n) {
int max=0; int t=n;
while(t!=0){
t=t/5;
max++;
}
int res=0;
for(int i=1;i<max;i++)
res+=n/Math.pow(5,i);
return res;
}
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×