Skip to content

Commit ffc2bfa

Browse files
committed
feat: 066
1 parent 04a7bd8 commit ffc2bfa

File tree

6 files changed

+88
-33
lines changed

6 files changed

+88
-33
lines changed

note/058/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ return `5`.
1919

2020
## 思路
2121

22-
题意是让你从一个只包含大小字母和空格字符的字符串中得到最后一个单词的长度,很简单,我们倒序遍历,先得到最后一个非空格字符的索引,然后再得到它前面的空格字符索引,两者相减即可。当然,我们使用API来完成这件事更加方便,只需一行代码`return s.trim().length()-s.trim().lastIndexOf(" ")-1;`,但我相信作者出这道题的目的肯定不是考你API的使用,所以我们还是用自己的思路来实现。
22+
题意是让你从一个只包含大小字母和空格字符的字符串中得到最后一个单词的长度,很简单,我们倒序遍历,先得到最后一个非空格字符的索引,然后再得到它前面的空格字符索引,两者相减即可。当然,我们使用API来完成这件事更加方便,只需一行代码`return s.trim().length() - s.trim().lastIndexOf(" ") - 1;`,但我相信作者出这道题的目的肯定不是考你API的使用,所以我们还是用自己的思路来实现。
2323

2424
``` java
2525
public class Solution {

note/066/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Plus One][title]
2+
3+
## Description
4+
5+
Given a non-negative integer represented as a **non-empty** array of digits, plus one to the integer.
6+
7+
You may assume the integer do not contain any leading zero, except the number 0 itself.
8+
9+
The digits are stored such that the most significant digit is at the head of the list.
10+
11+
**Tags:** Array, Math
12+
13+
14+
## 思路
15+
16+
题意是
17+
18+
``` java
19+
20+
```
21+
22+
23+
## 结语
24+
25+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
26+
27+
28+
29+
[title]: https://leetcode.com/problems/plus-one
30+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_053/Solution.java

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,36 @@
1010
*/
1111

1212
public class Solution {
13+
// public int maxSubArray(int[] nums) {
14+
// int len = nums.length, dp = nums[0], max = dp;
15+
// for (int i = 1; i < len; ++i) {
16+
// dp = nums[i] + (dp > 0 ? dp : 0);
17+
// if (dp > max) max = dp;
18+
// }
19+
// return max;
20+
// }
1321
public int maxSubArray(int[] nums) {
14-
int len = nums.length, dp = nums[0], max = dp;
15-
for (int i = 1; i < len; ++i) {
16-
dp = nums[i] + (dp > 0 ? dp : 0);
17-
if (dp > max) max = dp;
22+
return helper(nums, 0, nums.length - 1);
23+
}
24+
25+
private int helper(int[] nums, int left, int right) {
26+
if (left >= right) return nums[left];
27+
int mid = (left + right) >> 1;
28+
int leftAns = helper(nums, left, mid);
29+
int rightAns = helper(nums, mid + 1, right);
30+
int leftMax = nums[mid], rightMax = nums[mid + 1];
31+
int temp = 0;
32+
for (int i = mid; i >= left; --i) {
33+
temp += nums[i];
34+
if (temp > leftMax) leftMax = temp;
35+
}
36+
temp = 0;
37+
for (int i = mid + 1; i <= right; ++i) {
38+
temp += nums[i];
39+
if (temp > rightMax) rightMax = temp;
1840
}
19-
return max;
41+
return Math.max(Math.max(leftAns, rightAns), leftMax + rightMax);
2042
}
21-
// public int maxSubArray(int[] nums) {
22-
// return helper(nums, 0, nums.length - 1);
23-
// }
24-
//
25-
// private int helper(int[] nums, int left, int right) {
26-
// if (left >= right) return nums[left];
27-
// int mid = (left + right) >> 1;
28-
// int leftAns = helper(nums, left, mid);
29-
// int rightAns = helper(nums, mid + 1, right);
30-
// int leftMax = nums[mid], rightMax = nums[mid + 1];
31-
// int temp = 0;
32-
// for (int i = mid; i >= left; --i) {
33-
// temp += nums[i];
34-
// if (temp > leftMax) leftMax = temp;
35-
// }
36-
// temp = 0;
37-
// for (int i = mid + 1; i <= right; ++i) {
38-
// temp += nums[i];
39-
// if (temp > rightMax) rightMax = temp;
40-
// }
41-
// return Math.max(Math.max(leftAns, rightAns), leftMax + rightMax);
42-
// }
4343

4444
public static void main(String[] args) {
4545
Solution solution = new Solution();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.blankj.easy._066;
2+
3+
import java.io.File;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2017/04/21
10+
* desc :
11+
* </pre>
12+
*/
13+
14+
public class Solution {
15+
public int[] plusOne(int[] digits) {
16+
17+
return digits;
18+
}
19+
20+
public static void main(String[] args) {
21+
Solution solution = new Solution();
22+
File file = new File("/dev/d/");
23+
System.out.println(file.getAbsolutePath());
24+
System.out.println("hello".substring(0, 2));
25+
int[] digits = new int[]{9, 9, 9};
26+
System.out.println(solution.plusOne(digits));
27+
}
28+
}

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_008/Solution.java renamed to project/LeetCode/leetcode/src/main/java/com/blankj/medium/_008/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.easy._008;
1+
package com.blankj.medium._008;
22

33
/**
44
* <pre>

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_019/Solution.java renamed to project/LeetCode/leetcode/src/main/java/com/blankj/medium/_019/Solution.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
package com.blankj.easy._019;
2-
3-
import java.util.Arrays;
4-
import java.util.HashMap;
1+
package com.blankj.medium._019;
52

63
/**
74
* <pre>

0 commit comments

Comments
 (0)