auto commit
This commit is contained in:
parent
2dee14d978
commit
62b7690c3f
|
@ -1,21 +1,21 @@
|
|||
<!-- GFM-TOC -->
|
||||
* [分配饼干](#分配饼干)
|
||||
* [不重叠的区间个数](#不重叠的区间个数)
|
||||
* [投飞镖刺破气球](#投飞镖刺破气球)
|
||||
* [根据身高和序号重组队列](#根据身高和序号重组队列)
|
||||
* [分隔字符串使同种字符出现在一起](#分隔字符串使同种字符出现在一起)
|
||||
* [种植花朵](#种植花朵)
|
||||
* [判断是否为子序列](#判断是否为子序列)
|
||||
* [修改一个数成为非递减数组](#修改一个数成为非递减数组)
|
||||
* [股票的最大收益](#股票的最大收益)
|
||||
* [子数组最大的和](#子数组最大的和)
|
||||
* [买入和售出股票最大的收益](#买入和售出股票最大的收益)
|
||||
* [1. 分配饼干](#1-分配饼干)
|
||||
* [2. 不重叠的区间个数](#2-不重叠的区间个数)
|
||||
* [3. 投飞镖刺破气球](#3-投飞镖刺破气球)
|
||||
* [3. 根据身高和序号重组队列](#3-根据身高和序号重组队列)
|
||||
* [4. 买卖股票最大的收益](#4-买卖股票最大的收益)
|
||||
* [5. 买卖股票的最大收益 II](#5-买卖股票的最大收益-ii)
|
||||
* [6. 种植花朵](#6-种植花朵)
|
||||
* [7. 判断是否为子序列](#7-判断是否为子序列)
|
||||
* [8. 修改一个数成为非递减数组](#8-修改一个数成为非递减数组)
|
||||
* [9. 子数组最大的和](#9-子数组最大的和)
|
||||
* [10. 分隔字符串使同种字符出现在一起](#10-分隔字符串使同种字符出现在一起)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。
|
||||
|
||||
# 分配饼干
|
||||
# 1. 分配饼干
|
||||
|
||||
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
|
||||
|
||||
|
@ -49,7 +49,7 @@ public int findContentChildren(int[] g, int[] s) {
|
|||
}
|
||||
```
|
||||
|
||||
# 不重叠的区间个数
|
||||
# 2. 不重叠的区间个数
|
||||
|
||||
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
|
||||
|
||||
|
@ -107,7 +107,7 @@ Arrays.sort(intervals, new Comparator<Interval>() {
|
|||
});
|
||||
```
|
||||
|
||||
# 投飞镖刺破气球
|
||||
# 3. 投飞镖刺破气球
|
||||
|
||||
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
|
||||
|
||||
|
@ -119,7 +119,7 @@ Output:
|
|||
2
|
||||
```
|
||||
|
||||
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
|
||||
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都被刺破。求解最小的投飞镖次数使所有气球都被刺破。
|
||||
|
||||
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2] 和 [2, 3] 在本题中算是重叠区间。
|
||||
|
||||
|
@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) {
|
|||
}
|
||||
```
|
||||
|
||||
# 根据身高和序号重组队列
|
||||
# 3. 根据身高和序号重组队列
|
||||
|
||||
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
|
||||
|
||||
|
@ -157,7 +157,7 @@ Output:
|
|||
|
||||
为了使插入操作不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入的第 k 个位置可能会变成第 k+1 个位置。
|
||||
|
||||
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
|
||||
身高 h 降序、个数 k 值升序,然后将某个学生插入队列的第 k 个位置中。
|
||||
|
||||
```java
|
||||
public int[][] reconstructQueue(int[][] people) {
|
||||
|
@ -173,7 +173,161 @@ public int[][] reconstructQueue(int[][] people) {
|
|||
}
|
||||
```
|
||||
|
||||
# 分隔字符串使同种字符出现在一起
|
||||
# 4. 买卖股票最大的收益
|
||||
|
||||
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
|
||||
|
||||
题目描述:一次股票交易包含买入和卖出,只进行一次交易,求最大收益。
|
||||
|
||||
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int n = prices.length;
|
||||
if (n == 0) return 0;
|
||||
int soFarMin = prices[0];
|
||||
int max = 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (soFarMin > prices[i]) soFarMin = prices[i];
|
||||
else max = Math.max(max, prices[i] - soFarMin);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# 5. 买卖股票的最大收益 II
|
||||
|
||||
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
|
||||
|
||||
题目描述:可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。
|
||||
|
||||
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += (prices[i] - prices[i - 1]);
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# 6. 种植花朵
|
||||
|
||||
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
|
||||
|
||||
```html
|
||||
Input: flowerbed = [1,0,0,0,1], n = 1
|
||||
Output: True
|
||||
```
|
||||
|
||||
题目描述:flowerbed 数组中 1 表示已经种下了花朵。花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
|
||||
|
||||
```java
|
||||
public boolean canPlaceFlowers(int[] flowerbed, int n) {
|
||||
int len = flowerbed.length;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < len && cnt < n; i++) {
|
||||
if (flowerbed[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
int pre = i == 0 ? 0 : flowerbed[i - 1];
|
||||
int next = i == len - 1 ? 0 : flowerbed[i + 1];
|
||||
if (pre == 0 && next == 0) {
|
||||
cnt++;
|
||||
flowerbed[i] = 1;
|
||||
}
|
||||
}
|
||||
return cnt >= n;
|
||||
}
|
||||
```
|
||||
|
||||
# 7. 判断是否为子序列
|
||||
|
||||
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
|
||||
|
||||
```html
|
||||
s = "abc", t = "ahbgdc"
|
||||
Return true.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int index = -1;
|
||||
for (char c : s.toCharArray()) {
|
||||
index = t.indexOf(c, index + 1);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 8. 修改一个数成为非递减数组
|
||||
|
||||
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
|
||||
|
||||
```html
|
||||
Input: [4,2,3]
|
||||
Output: True
|
||||
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
|
||||
```
|
||||
|
||||
题目描述:判断一个数组是否能只修改一个数就成为非递减数组。
|
||||
|
||||
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]。
|
||||
|
||||
```java
|
||||
public boolean checkPossibility(int[] nums) {
|
||||
int cnt = 0;
|
||||
for (int i = 1; i < nums.length && cnt < 2; i++) {
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
|
||||
nums[i] = nums[i - 1];
|
||||
} else {
|
||||
nums[i - 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return cnt <= 1;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# 9. 子数组最大的和
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
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.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
# 10. 分隔字符串使同种字符出现在一起
|
||||
|
||||
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
|
||||
|
||||
|
@ -214,158 +368,6 @@ private int char2Index(char c) {
|
|||
```
|
||||
|
||||
|
||||
# 种植花朵
|
||||
|
||||
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
|
||||
|
||||
```html
|
||||
Input: flowerbed = [1,0,0,0,1], n = 1
|
||||
Output: True
|
||||
```
|
||||
|
||||
题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
|
||||
|
||||
```java
|
||||
public boolean canPlaceFlowers(int[] flowerbed, int n) {
|
||||
int len = flowerbed.length;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < len && cnt < n; i++) {
|
||||
if (flowerbed[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
int pre = i == 0 ? 0 : flowerbed[i - 1];
|
||||
int next = i == len - 1 ? 0 : flowerbed[i + 1];
|
||||
if (pre == 0 && next == 0) {
|
||||
cnt++;
|
||||
flowerbed[i] = 1;
|
||||
}
|
||||
}
|
||||
return cnt >= n;
|
||||
}
|
||||
```
|
||||
|
||||
# 判断是否为子序列
|
||||
|
||||
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
|
||||
|
||||
```html
|
||||
s = "abc", t = "ahbgdc"
|
||||
Return true.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int index = -1;
|
||||
for (char c : s.toCharArray()) {
|
||||
index = t.indexOf(c, index + 1);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 修改一个数成为非递减数组
|
||||
|
||||
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
|
||||
|
||||
```html
|
||||
Input: [4,2,3]
|
||||
Output: True
|
||||
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
|
||||
```
|
||||
|
||||
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
|
||||
|
||||
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]。
|
||||
|
||||
```java
|
||||
public boolean checkPossibility(int[] nums) {
|
||||
int cnt = 0;
|
||||
for (int i = 1; i < nums.length && cnt < 2; i++) {
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
|
||||
nums[i] = nums[i - 1];
|
||||
} else {
|
||||
nums[i - 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return cnt <= 1;
|
||||
}
|
||||
```
|
||||
|
||||
# 股票的最大收益
|
||||
|
||||
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
|
||||
|
||||
题目描述:一次股票交易包含买入和卖出,多个交易之间不能交叉进行。
|
||||
|
||||
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,从而在局部最优的情况下也保证全局最优。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += (prices[i] - prices[i - 1]);
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
```
|
||||
|
||||
# 子数组最大的和
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
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.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
# 买入和售出股票最大的收益
|
||||
|
||||
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
|
||||
|
||||
题目描述:只进行一次交易。
|
||||
|
||||
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int n = prices.length;
|
||||
if (n == 0) return 0;
|
||||
int soFarMin = prices[0];
|
||||
int max = 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (soFarMin > prices[i]) soFarMin = prices[i];
|
||||
else max = Math.max(max, prices[i] - soFarMin);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</br><div align="center">🎨 </br></br> 更多精彩内容将发布在公众号 **CyC2018**,公众号提供了该项目的离线阅读版本,后台回复"下载" 即可领取。也提供了一份技术面试复习思维导图,不仅系统整理了面试知识点,而且标注了各个知识点的重要程度,从而帮你理清多而杂的面试知识点,后台回复"资料" 即可领取。我基本是按照这个思维导图来进行复习的,对我拿到了 BAT 头条等 Offer 起到很大的帮助。你们完全可以和我一样根据思维导图上列的知识点来进行复习,就不用看很多不重要的内容,也可以知道哪些内容很重要从而多安排一些复习时间。</div></br>
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
<!-- GFM-TOC -->
|
||||
* [分配饼干](#分配饼干)
|
||||
* [不重叠的区间个数](#不重叠的区间个数)
|
||||
* [投飞镖刺破气球](#投飞镖刺破气球)
|
||||
* [根据身高和序号重组队列](#根据身高和序号重组队列)
|
||||
* [分隔字符串使同种字符出现在一起](#分隔字符串使同种字符出现在一起)
|
||||
* [种植花朵](#种植花朵)
|
||||
* [判断是否为子序列](#判断是否为子序列)
|
||||
* [修改一个数成为非递减数组](#修改一个数成为非递减数组)
|
||||
* [股票的最大收益](#股票的最大收益)
|
||||
* [子数组最大的和](#子数组最大的和)
|
||||
* [买入和售出股票最大的收益](#买入和售出股票最大的收益)
|
||||
* [1. 分配饼干](#1-分配饼干)
|
||||
* [2. 不重叠的区间个数](#2-不重叠的区间个数)
|
||||
* [3. 投飞镖刺破气球](#3-投飞镖刺破气球)
|
||||
* [3. 根据身高和序号重组队列](#3-根据身高和序号重组队列)
|
||||
* [4. 买卖股票最大的收益](#4-买卖股票最大的收益)
|
||||
* [5. 买卖股票的最大收益 II](#5-买卖股票的最大收益-ii)
|
||||
* [6. 种植花朵](#6-种植花朵)
|
||||
* [7. 判断是否为子序列](#7-判断是否为子序列)
|
||||
* [8. 修改一个数成为非递减数组](#8-修改一个数成为非递减数组)
|
||||
* [9. 子数组最大的和](#9-子数组最大的和)
|
||||
* [10. 分隔字符串使同种字符出现在一起](#10-分隔字符串使同种字符出现在一起)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。
|
||||
|
||||
# 分配饼干
|
||||
# 1. 分配饼干
|
||||
|
||||
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
|
||||
|
||||
|
@ -49,7 +49,7 @@ public int findContentChildren(int[] g, int[] s) {
|
|||
}
|
||||
```
|
||||
|
||||
# 不重叠的区间个数
|
||||
# 2. 不重叠的区间个数
|
||||
|
||||
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
|
||||
|
||||
|
@ -107,7 +107,7 @@ Arrays.sort(intervals, new Comparator<Interval>() {
|
|||
});
|
||||
```
|
||||
|
||||
# 投飞镖刺破气球
|
||||
# 3. 投飞镖刺破气球
|
||||
|
||||
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
|
||||
|
||||
|
@ -119,7 +119,7 @@ Output:
|
|||
2
|
||||
```
|
||||
|
||||
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
|
||||
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都被刺破。求解最小的投飞镖次数使所有气球都被刺破。
|
||||
|
||||
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2] 和 [2, 3] 在本题中算是重叠区间。
|
||||
|
||||
|
@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) {
|
|||
}
|
||||
```
|
||||
|
||||
# 根据身高和序号重组队列
|
||||
# 3. 根据身高和序号重组队列
|
||||
|
||||
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
|
||||
|
||||
|
@ -157,7 +157,7 @@ Output:
|
|||
|
||||
为了使插入操作不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入的第 k 个位置可能会变成第 k+1 个位置。
|
||||
|
||||
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
|
||||
身高 h 降序、个数 k 值升序,然后将某个学生插入队列的第 k 个位置中。
|
||||
|
||||
```java
|
||||
public int[][] reconstructQueue(int[][] people) {
|
||||
|
@ -173,7 +173,161 @@ public int[][] reconstructQueue(int[][] people) {
|
|||
}
|
||||
```
|
||||
|
||||
# 分隔字符串使同种字符出现在一起
|
||||
# 4. 买卖股票最大的收益
|
||||
|
||||
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
|
||||
|
||||
题目描述:一次股票交易包含买入和卖出,只进行一次交易,求最大收益。
|
||||
|
||||
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int n = prices.length;
|
||||
if (n == 0) return 0;
|
||||
int soFarMin = prices[0];
|
||||
int max = 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (soFarMin > prices[i]) soFarMin = prices[i];
|
||||
else max = Math.max(max, prices[i] - soFarMin);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# 5. 买卖股票的最大收益 II
|
||||
|
||||
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
|
||||
|
||||
题目描述:可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。
|
||||
|
||||
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += (prices[i] - prices[i - 1]);
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# 6. 种植花朵
|
||||
|
||||
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
|
||||
|
||||
```html
|
||||
Input: flowerbed = [1,0,0,0,1], n = 1
|
||||
Output: True
|
||||
```
|
||||
|
||||
题目描述:flowerbed 数组中 1 表示已经种下了花朵。花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
|
||||
|
||||
```java
|
||||
public boolean canPlaceFlowers(int[] flowerbed, int n) {
|
||||
int len = flowerbed.length;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < len && cnt < n; i++) {
|
||||
if (flowerbed[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
int pre = i == 0 ? 0 : flowerbed[i - 1];
|
||||
int next = i == len - 1 ? 0 : flowerbed[i + 1];
|
||||
if (pre == 0 && next == 0) {
|
||||
cnt++;
|
||||
flowerbed[i] = 1;
|
||||
}
|
||||
}
|
||||
return cnt >= n;
|
||||
}
|
||||
```
|
||||
|
||||
# 7. 判断是否为子序列
|
||||
|
||||
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
|
||||
|
||||
```html
|
||||
s = "abc", t = "ahbgdc"
|
||||
Return true.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int index = -1;
|
||||
for (char c : s.toCharArray()) {
|
||||
index = t.indexOf(c, index + 1);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 8. 修改一个数成为非递减数组
|
||||
|
||||
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
|
||||
|
||||
```html
|
||||
Input: [4,2,3]
|
||||
Output: True
|
||||
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
|
||||
```
|
||||
|
||||
题目描述:判断一个数组是否能只修改一个数就成为非递减数组。
|
||||
|
||||
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]。
|
||||
|
||||
```java
|
||||
public boolean checkPossibility(int[] nums) {
|
||||
int cnt = 0;
|
||||
for (int i = 1; i < nums.length && cnt < 2; i++) {
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
|
||||
nums[i] = nums[i - 1];
|
||||
} else {
|
||||
nums[i - 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return cnt <= 1;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# 9. 子数组最大的和
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
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.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
# 10. 分隔字符串使同种字符出现在一起
|
||||
|
||||
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
|
||||
|
||||
|
@ -214,158 +368,6 @@ private int char2Index(char c) {
|
|||
```
|
||||
|
||||
|
||||
# 种植花朵
|
||||
|
||||
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
|
||||
|
||||
```html
|
||||
Input: flowerbed = [1,0,0,0,1], n = 1
|
||||
Output: True
|
||||
```
|
||||
|
||||
题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
|
||||
|
||||
```java
|
||||
public boolean canPlaceFlowers(int[] flowerbed, int n) {
|
||||
int len = flowerbed.length;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < len && cnt < n; i++) {
|
||||
if (flowerbed[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
int pre = i == 0 ? 0 : flowerbed[i - 1];
|
||||
int next = i == len - 1 ? 0 : flowerbed[i + 1];
|
||||
if (pre == 0 && next == 0) {
|
||||
cnt++;
|
||||
flowerbed[i] = 1;
|
||||
}
|
||||
}
|
||||
return cnt >= n;
|
||||
}
|
||||
```
|
||||
|
||||
# 判断是否为子序列
|
||||
|
||||
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
|
||||
|
||||
```html
|
||||
s = "abc", t = "ahbgdc"
|
||||
Return true.
|
||||
```
|
||||
|
||||
```java
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int index = -1;
|
||||
for (char c : s.toCharArray()) {
|
||||
index = t.indexOf(c, index + 1);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# 修改一个数成为非递减数组
|
||||
|
||||
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
|
||||
|
||||
```html
|
||||
Input: [4,2,3]
|
||||
Output: True
|
||||
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
|
||||
```
|
||||
|
||||
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
|
||||
|
||||
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]。
|
||||
|
||||
```java
|
||||
public boolean checkPossibility(int[] nums) {
|
||||
int cnt = 0;
|
||||
for (int i = 1; i < nums.length && cnt < 2; i++) {
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
cnt++;
|
||||
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
|
||||
nums[i] = nums[i - 1];
|
||||
} else {
|
||||
nums[i - 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return cnt <= 1;
|
||||
}
|
||||
```
|
||||
|
||||
# 股票的最大收益
|
||||
|
||||
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
|
||||
|
||||
题目描述:一次股票交易包含买入和卖出,多个交易之间不能交叉进行。
|
||||
|
||||
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,从而在局部最优的情况下也保证全局最优。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int profit = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
if (prices[i] > prices[i - 1]) {
|
||||
profit += (prices[i] - prices[i - 1]);
|
||||
}
|
||||
}
|
||||
return profit;
|
||||
}
|
||||
```
|
||||
|
||||
# 子数组最大的和
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
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.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
# 买入和售出股票最大的收益
|
||||
|
||||
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
|
||||
|
||||
题目描述:只进行一次交易。
|
||||
|
||||
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
|
||||
|
||||
```java
|
||||
public int maxProfit(int[] prices) {
|
||||
int n = prices.length;
|
||||
if (n == 0) return 0;
|
||||
int soFarMin = prices[0];
|
||||
int max = 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (soFarMin > prices[i]) soFarMin = prices[i];
|
||||
else max = Math.max(max, prices[i] - soFarMin);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</br><div align="center">🎨 </br></br> 更多精彩内容将发布在公众号 **CyC2018**,公众号提供了该项目的离线阅读版本,后台回复"下载" 即可领取。也提供了一份技术面试复习思维导图,不仅系统整理了面试知识点,而且标注了各个知识点的重要程度,从而帮你理清多而杂的面试知识点,后台回复"资料" 即可领取。我基本是按照这个思维导图来进行复习的,对我拿到了 BAT 头条等 Offer 起到很大的帮助。你们完全可以和我一样根据思维导图上列的知识点来进行复习,就不用看很多不重要的内容,也可以知道哪些内容很重要从而多安排一些复习时间。</div></br>
|
||||
|
|
Loading…
Reference in New Issue
Block a user