技术自留地


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

101. Symmetric Tree

发表于 2017-12-01 | 分类于 LeetCode

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

1
2
3
4
5
6
7
>     1
> / \
> 2 2
> / \ / \
> 3 4 4 3
>
>

>

But the following [1,2,2,null,3,null,3] is not:

1
2
3
4
5
6
7
>     1
> / \
> 2 2
> \ \
> 3 3
>
>

>

Note:
Bonus points if you could solve it both recursively and iteratively.

阅读全文 »

53. Maximum Subarray

发表于 2017-12-01 | 分类于 LeetCode

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

题意:找出和最大的连续子序列。

阅读全文 »

39. Combination Sum & 40. Combination Sum II

发表于 2017-11-30 | 分类于 LeetCode

39:

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:

1
2
3
4
5
> [
> [7],
> [2, 2, 3]
> ]
>
阅读全文 »

560. Subarray Sum Equals K

发表于 2017-11-30 | 分类于 LeetCode

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

1
2
3
4
> Input:nums = [1,1,1], k = 2
> Output: 2
>
>

>

Note:

  1. The length of the array is in range [1, 20,000].

  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

    阅读全文 »

309. Best Time to Buy and Sell Stock with Cooldown

发表于 2017-11-28 | 分类于 LeetCode

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

阅读全文 »

96. Unique Binary Search Trees

发表于 2017-11-23 | 分类于 LeetCode

Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?

For example,
Given n = 3, there are a total of 5 unique BST’s.

1
2
3
4
5
6
>    1         3     3      2      1
> \ / / / \ \
> 3 2 1 1 3 2
> / / \ \
> 2 1 2 3
>

>

阅读全文 »

394. Decode String

发表于 2017-11-23 | 分类于 LeetCode

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

阅读全文 »

BUG合集

发表于 2017-11-22 | 分类于 Code
  1. 不能既使用迭代又同时更新。
    阅读全文 »

312. Burst Balloons

发表于 2017-11-21 | 分类于 LeetCode

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Given [3, 1, 5, 8]

Return 167

1
2
3
>     nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
> coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
>

​ 题意:戳破一个气球(序号为i)就可以得到nums[i-1]nums[i]\nums[i+1]的得分,求问怎么得戳破所有气球然后得分最高。

​ 一开始看这个题真的是一点思路都没有,别说敲代码实现了,就是自己手动算的都不知道从何而算起。

​ 没有思路的主要问题是没办法把这个题目划分为子问题,好像觉得在每一步要戳破哪个很难决定。最后看了discuss后恍然大悟,要这么划分自问题的:

​ 要戳破[i,j]区间的气球,那么如果k为最后一个戳破的,那么以d[i][j]表示得分,则

1
2
3
dp[i][j] = {
dp[i][k-1]/*戳破k左边的得分*/+dp[k+1][j]/*戳破k右边的得分*/+nums[i-1]*nums[k]*nums[j+1]/*戳破k的的得分*/
}

​ 这样子问题不就划分好了吗~?

​ 需要计算最大值,那么只要按个遍历[i,j]中的每一个可能值k,然后维护最大值即可。表示如下:

1
2
3
for k in [i,j]:
dp[i][j] = max(dp[i][j],
dp[i][k - 1] + dp[k + 1][j] + balloons[i - 1] * balloons[k] * balloons[j + 1]);

​ 虽然到此思路有了,但是一开始提交还是错掉了。主要是对dp二维表的计算顺序搞错了!

错误的计算顺序:

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
//
// Created by chaopengz on 2017/11/21.
//
#include "head.h"

class Solution {
public:
int maxCoins(vector<int> &nums)
{
int len = nums.size();
vector<int> balloons;
balloons.push_back(1);
for (auto x:nums)
balloons.push_back(x);
balloons.push_back(1);

vector<vector<int>> dp(balloons.size(), vector<int>(balloons.size(), 0));

for (int i = 1; i <= len; ++i)
{
for (int j = i; j <= len; ++j)
{//计算dp[i][j]
for (int k = i; k <= j; ++k)//k为[i,j]最后一个戳破的气球
{
dp[i][j] = max(
dp[i][j],
dp[i][k - 1] + dp[k + 1][j] + balloons[i - 1] * balloons[k] * balloons[j + 1]
);
}
}
}

return dp[1][len];
}
};

​ 错误主要是因为:跟之前的第i行的结果依赖第i-1行的思想不一样,这次的是i+1行对i行也有影响,所以对角线的要先算,比如在计算dp[1][2]时,当k=1时

1
dp[1][2] = max(dp[1][2],dp[1][0]+dp[2][2]+balloons[0]*ballons[1]*ballons[3])//这时要dp[1][2]就得先有dp[2][2]

​

正确的AC代码:

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
//
// Created by chaopengz on 2017/11/21.
//
#include "head.h"

class Solution {
public:
int maxCoins(vector<int> &nums) {
int len = nums.size();
vector<int> balloons;
balloons.push_back(1);
for (auto x:nums)
balloons.push_back(x);
balloons.push_back(1);

vector<vector<int>> dp(balloons.size(), vector<int>(balloons.size(), 0));

for (int step = 0; step < len; ++step) {
for (int i = 0; i <= len - step; ++i) {
int j = i + step;
for (int k = i; k <= j; ++k)//k为[i,j]最后一个戳破的气球
{
dp[i][j] = max(
dp[i][j],
dp[i][k - 1] + dp[k + 1][j] + balloons[i - 1] * balloons[k] * balloons[j + 1]
);
}
}
}
return dp[1][len];
}
};

494. Target Sum

发表于 2017-11-17 | 分类于 LeetCode

You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

阅读全文 »
1234…6
chaopengz

chaopengz

北航|北大|码农|图形学

56 日志
12 分类
15 标签
© 2021 chaopengz
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.3