Skip to content

Commit ae711a4

Browse files
committed
leetcode: 2022-04-22 LeetCode396_MaxRotateFunction.java
1 parent 2aeb98a commit ae711a4

4 files changed

Lines changed: 89 additions & 12 deletions

File tree

leetcode/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## LeetCode
22

3-
| 日期 | 题目 | 解题 |
4-
|------------|-----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
5-
| 2022-04-24 | | |
6-
| 2022-04-23 | | |
7-
| 2022-04-22 | | |
8-
| 2022-04-21 | [LeetCode 824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/) | [LeetCode824.java](https://github.com/niumoo/JavaNotes/blob/master/leetcode/src/main/java/com/wdbyte/leetcode/LeetCode824.java) |
9-
| 2022-04-20 | [LeetCode 388. 文件的最长绝对路径](https://leetcode-cn.com/problems/longest-absolute-file-path/) | [LeetCode388.java](https://github.com/niumoo/JavaNotes/blob/master/leetcode/src/main/java/com/wdbyte/leetcode/LeetCode388.java) |
3+
| 日期 | 题目 | 解题 |
4+
|------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| 2022-04-24 | | |
6+
| 2022-04-23 | | |
7+
| 2022-04-22 | [LeetCode 396. 旋转函数](https://leetcode-cn.com/problems/rotate-function/) | [LeetCode396_MaxRotateFunction.java](https://github.com/niumoo/JavaNotes/blob/master/leetcode/src/main/java/com/wdbyte/leetcode/LeetCode396_MaxRotateFunction.java) |
8+
| 2022-04-21 | [LeetCode 824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/) | [LeetCode824_ToGoatLatin.java](https://github.com/niumoo/JavaNotes/blob/master/leetcode/src/main/java/com/wdbyte/leetcode/LeetCode824_ToGoatLatin.java) |
9+
| 2022-04-20 | [LeetCode 388. 文件的最长绝对路径](https://leetcode-cn.com/problems/longest-absolute-file-path/) | [LeetCode388_LengthLongestPath.java](https://github.com/niumoo/JavaNotes/blob/master/leetcode/src/main/java/com/wdbyte/leetcode/LeetCode388_LengthLongestPath.java) |
1010

1111

leetcode/src/main/java/com/wdbyte/leetcode/LeetCode388.java renamed to leetcode/src/main/java/com/wdbyte/leetcode/LeetCode388_LengthLongestPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* @author niulang
1010
* @date 2022/04/20
1111
*/
12-
public class LeetCode388 {
12+
public class LeetCode388_LengthLongestPath {
1313
public static void main(String[] args) {
14-
LeetCode388 leetCode388 = new LeetCode388();
14+
LeetCode388_LengthLongestPath leetCode388 = new LeetCode388_LengthLongestPath();
1515
//int path = leetCode388.lengthLongestPath2("dir\n file.txt");
1616
int path = leetCode388.lengthLongestPath2("dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext");
1717
System.out.println(path);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.wdbyte.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/rotate-function/
7+
* 给定一个长度为 n 的整数数组nums。
8+
*
9+
* 假设arrk是数组nums顺时针旋转 k 个位置后的数组,我们定义nums的 旋转函数F为:
10+
*
11+
* F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1]
12+
* 返回F(0), F(1), ..., F(n-1)中的最大值。
13+
*
14+
* 生成的测试用例让答案符合32 位 整数。
15+
* 例子:
16+
* 输入: nums = [4,3,2,6]
17+
* 输出: 26
18+
* 解释:
19+
* F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
20+
* F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
21+
* F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
22+
* F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
23+
* 所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
24+
*
25+
* @author niulang
26+
* @date 2022/04/22
27+
*/
28+
public class LeetCode396_MaxRotateFunction {
29+
30+
public static void main(String[] args) {
31+
int[] nums = new int[] {4, 3, 2, 6};
32+
//int[] nums = new int[] {100};
33+
System.out.println(new LeetCode396_MaxRotateFunction().maxRotateFunction(nums));
34+
}
35+
36+
/**
37+
* 寻找规律
38+
*
39+
* @param nums
40+
* @return
41+
*/
42+
public int maxRotateFunction(int[] nums) {
43+
int sum = 0, numSum = Arrays.stream(nums).sum();
44+
for (int i = 0; i < nums.length; i++) {
45+
sum += i * nums[i];
46+
}
47+
int max = sum;
48+
for (int i = 1; i < nums.length; i++) {
49+
// 规律
50+
sum = sum + numSum - nums.length * nums[nums.length - i];
51+
max = Math.max(max, sum);
52+
}
53+
return max;
54+
}
55+
}

leetcode/src/main/java/com/wdbyte/leetcode/LeetCode824.java renamed to leetcode/src/main/java/com/wdbyte/leetcode/LeetCode824_ToGoatLatin.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* @author niulang
88
* @date 2022/04/21
99
*/
10-
public class LeetCode824 {
10+
public class LeetCode824_ToGoatLatin {
1111

1212
public static void main(String[] args) {
1313
//String goatLatin = new LeetCode824().toGoatLatin("I speak Goat Latin");
14-
String goatLatin = new LeetCode824().toGoatLatin("The quick brown fox jumped over the lazy dog");
14+
String goatLatin = new LeetCode824_ToGoatLatin().toGoatLatin("The quick brown fox jumped over the lazy dog");
1515
//String goatLatin = new LeetCode824().toGoatLatin("goat");
1616
System.out.println(goatLatin);
1717
}
@@ -38,7 +38,7 @@ public String toGoatLatin(String sentence) {
3838
// 元音开头
3939
char[] chars = word.toCharArray();
4040
if (chars[0] == 'A' || chars[0] == 'E' || chars[0] == 'I' || chars[0] == 'O' || chars[0] == 'U' ||
41-
chars[0] == 'a' || chars[0] == 'e' || chars[0] == 'i' || chars[0] == 'o' || chars[0] == 'u') {
41+
chars[0] == 'a' || chars[0] == 'e' || chars[0] == 'i' || chars[0] == 'o' || chars[0] == 'u' ) {
4242
builder.append(word).append("ma");
4343
} else {
4444
// 辅音开头
@@ -54,4 +54,26 @@ public String toGoatLatin(String sentence) {
5454
}
5555
return builder.toString();
5656
}
57+
58+
public String toGoatLatin2(String sentence) {
59+
String[] array = sentence.split(" ");
60+
StringBuilder builder = new StringBuilder();
61+
for (int i = 0; i < array.length; i++) {
62+
String word = array[i];
63+
// 元音开头
64+
char charAt0 = word.charAt(0);
65+
if (charAt0 == 'A' || charAt0 == 'E' || charAt0 == 'I' || charAt0 == 'O' || charAt0 == 'U' ||
66+
charAt0 == 'a' || charAt0 == 'e' || charAt0 == 'i' || charAt0 == 'o' || charAt0 == 'u') {
67+
builder.append(word).append("ma");
68+
} else {
69+
// 辅音开头
70+
builder.append(word.substring(1)).append(charAt0).append("ma");
71+
}
72+
for (int j = 0; j <= i; j++) {
73+
builder.append("a");
74+
}
75+
builder.append(" ");
76+
}
77+
return builder.toString().trim();
78+
}
5779
}

0 commit comments

Comments
 (0)