Skip to content

Commit 12a275a

Browse files
committed
1165-1176-1180-1228-1243-1271-1426-1564-1708 (9)
1 parent e5685f7 commit 12a275a

File tree

18 files changed

+570
-0
lines changed

18 files changed

+570
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Solution1165 {
5+
public int calculateTime(String keyboard, String word) {
6+
Map<Character, Integer> idxMap = new HashMap<>();
7+
for (int i = 0; i < 26; i++) {
8+
idxMap.put(keyboard.charAt(i), i);
9+
}
10+
11+
int res = 0;
12+
char pre = keyboard.charAt(0);
13+
for (char ch : word.toCharArray()) {
14+
res += Math.abs(idxMap.get(ch) - idxMap.get(pre));
15+
pre = ch;
16+
}
17+
return res;
18+
}
19+
}
20+
/*
21+
$1165. 单行键盘
22+
https://leetcode.cn/problems/single-row-keyboard/
23+
24+
我们定制了一款特殊的键盘,所有的键都 排列在一行上 。
25+
给定一个长度为 26 的字符串 keyboard ,来表示键盘的布局(索引从 0 到 25 )。一开始,你的手指在索引 0 处。要输入一个字符,你必须把你的手指移动到所需字符的索引处。手指从索引 i 移动到索引 j 所需要的时间是 |i - j|。
26+
您需要输入一个字符串 word 。写一个函数来计算用一个手指输入需要多少时间。
27+
提示:
28+
keyboard.length == 26
29+
keyboard 按某种特定顺序排列,并包含每个小写英文字母一次。
30+
1 <= word.length <= 104
31+
word[i] 为小写英文字母
32+
33+
HashMap模拟
34+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution1176 {
2+
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
3+
int len = calories.length;
4+
int[] preSum = new int[len + 1];
5+
for (int i = 0; i < len; i++) {
6+
preSum[i + 1] = preSum[i] + calories[i];
7+
}
8+
9+
int res = 0;
10+
for (int i = 0; i + k <= len; i++) {
11+
int T = preSum[i + k] - preSum[i];
12+
if (T < lower) {
13+
res--;
14+
} else if (T > upper) {
15+
res++;
16+
}
17+
}
18+
return res;
19+
}
20+
}
21+
/*
22+
$1176. 健身计划评估
23+
https://leetcode.cn/problems/diet-plan-performance/
24+
25+
你的好友是一位健身爱好者。前段日子,他给自己制定了一份健身计划。现在想请你帮他评估一下这份计划是否合理。
26+
他会有一份计划消耗的卡路里表,其中 calories[i] 给出了你的这位好友在第 i 天需要消耗的卡路里总量。
27+
为了更好地评估这份计划,对于卡路里表中的每一天,你都需要计算他 「这一天以及之后的连续几天」 (共 k 天)内消耗的总卡路里 T:
28+
- 如果 T < lower,那么这份计划相对糟糕,并失去 1 分;
29+
- 如果 T > upper,那么这份计划相对优秀,并获得 1 分;
30+
- 否则,这份计划普普通通,分值不做变动。
31+
请返回统计完所有 calories.length 天后得到的总分作为评估结果。
32+
注意:总分可能是负数。
33+
提示:
34+
1 <= k <= calories.length <= 10^5
35+
0 <= calories[i] <= 20000
36+
0 <= lower <= upper
37+
38+
前缀和 + 模拟
39+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution1180 {
2+
public int countLetters(String s) {
3+
int res = 0;
4+
int cnt = 0;
5+
char pre = ' ';
6+
for (char ch : s.toCharArray()) {
7+
if (ch != pre) {
8+
cnt = 1;
9+
} else {
10+
cnt++;
11+
}
12+
res += cnt;
13+
pre = ch;
14+
}
15+
return res;
16+
}
17+
}
18+
/*
19+
$1180. 统计只含单一字母的子串
20+
https://leetcode.cn/problems/count-substrings-with-only-one-distinct-letter/
21+
22+
给你一个字符串 s,返回 只含 单一字母 的子串个数 。
23+
示例 1:
24+
输入: s = "aaaba"
25+
输出: 8
26+
解释: 只含单一字母的子串分别是 "aaa", "aa", "a", "b"。
27+
"aaa" 出现 1 次。
28+
"aa" 出现 2 次。
29+
"a" 出现 4 次。
30+
"b" 出现 1 次。
31+
所以答案是 1 + 2 + 4 + 1 = 8。
32+
示例 2:
33+
输入: s = "aaaaaaaaaa"
34+
输出: 55
35+
提示:
36+
1 <= s.length <= 1000
37+
s[i] 仅由小写英文字母组成
38+
39+
计数
40+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1165Tests {
5+
private final Solution1165 solution1165 = new Solution1165();
6+
7+
@Test
8+
public void example1() {
9+
String keyboard = "abcdefghijklmnopqrstuvwxyz";
10+
String word = "cba";
11+
int expected = 4;
12+
Assertions.assertEquals(expected, solution1165.calculateTime(keyboard, word));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
String keyboard = "pqrstuvwxyzabcdefghijklmno";
18+
String word = "leetcode";
19+
int expected = 73;
20+
Assertions.assertEquals(expected, solution1165.calculateTime(keyboard, word));
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1176Tests {
5+
private final Solution1176 solution1176 = new Solution1176();
6+
7+
@Test
8+
public void example1() {
9+
int[] calories = {1, 2, 3, 4, 5};
10+
int k = 1;
11+
int lower = 3;
12+
int upper = 3;
13+
int expected = 0;
14+
Assertions.assertEquals(expected, solution1176.dietPlanPerformance(calories, k, lower, upper));
15+
}
16+
17+
@Test
18+
public void example2() {
19+
int[] calories = {3, 2};
20+
int k = 2;
21+
int lower = 0;
22+
int upper = 1;
23+
int expected = 1;
24+
Assertions.assertEquals(expected, solution1176.dietPlanPerformance(calories, k, lower, upper));
25+
}
26+
27+
@Test
28+
public void example3() {
29+
int[] calories = {6, 5, 0, 0};
30+
int k = 2;
31+
int lower = 1;
32+
int upper = 5;
33+
int expected = 0;
34+
Assertions.assertEquals(expected, solution1176.dietPlanPerformance(calories, k, lower, upper));
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1180Tests {
5+
private final Solution1180 solution1180 = new Solution1180();
6+
7+
@Test
8+
public void example1() {
9+
String s = "aaaba";
10+
int expected = 8;
11+
Assertions.assertEquals(expected, solution1180.countLetters(s));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
String s = "aaaaaaaaaa";
17+
int expected = 55;
18+
Assertions.assertEquals(expected, solution1180.countLetters(s));
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution1228 {
2+
public int missingNumber(int[] arr) {
3+
int n = arr.length;
4+
int d1 = arr[1] - arr[0];
5+
int d2 = arr[n - 1] - arr[n - 2];
6+
if (d1 == 0 || d2 == 0) {
7+
return arr[0];
8+
}
9+
// 公差
10+
int d = (d1 / d2 == 2) ? d2 : d1;
11+
for (int i = 1; i < n; i++) {
12+
if (arr[i] - arr[i - 1] != d) {
13+
return arr[i] - d;
14+
}
15+
}
16+
return -1;
17+
}
18+
}
19+
/*
20+
$1228. 等差数列中缺失的数字
21+
https://leetcode.cn/problems/missing-number-in-arithmetic-progression/
22+
23+
在某个数组 arr 中,值符合等差数列的数值规律:在 0 <= i < arr.length - 1 的前提下,arr[i+1] - arr[i] 的值都相等。
24+
我们会从该数组中删除一个 既不是第一个 也 不是最后一个的值,得到一个新的数组 arr。
25+
给你这个缺值的数组 arr,返回 被删除的那个数 。
26+
提示:
27+
3 <= arr.length <= 1000
28+
0 <= arr[i] <= 10^5
29+
给定的数组 保证 是一个有效的数组。
30+
31+
求出公差后枚举
32+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
5+
public class Solution1243 {
6+
public List<Integer> transformArray(int[] arr) {
7+
int n = arr.length;
8+
int[] arrCopy = arr.clone();
9+
for (int i = 1; i <= n - 2; i++) {
10+
if (arr[i] > arrCopy[i - 1] && arr[i] > arrCopy[i + 1]) {
11+
arr[i]--;
12+
} else if (arr[i] < arrCopy[i - 1] && arr[i] < arrCopy[i + 1]) {
13+
arr[i]++;
14+
}
15+
}
16+
if (Arrays.equals(arr, arrCopy)) {
17+
return Arrays.stream(arr).boxed().collect(Collectors.toList());
18+
}
19+
return transformArray(arr);
20+
}
21+
}
22+
/*
23+
$1243. 数组变换
24+
https://leetcode.cn/problems/array-transformation/
25+
26+
首先,给你一个初始数组 arr。然后,每天你都要根据前一天的数组生成一个新的数组。
27+
第 i 天所生成的数组,是由你对第 i-1 天的数组进行如下操作所得的:
28+
1. 假如一个元素小于它的左右邻居,那么该元素自增 1。
29+
2. 假如一个元素大于它的左右邻居,那么该元素自减 1。
30+
3. 首、尾元素 永不 改变。
31+
过些时日,你会发现数组将会不再发生变化,请返回最终所得到的数组。
32+
提示:
33+
1 <= arr.length <= 100
34+
1 <= arr[i] <= 100
35+
36+
暴力模拟
37+
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Solution1271 {
2+
public String toHexspeak(String num) {
3+
num = Long.toString(Long.parseLong(num), 16).toUpperCase()
4+
.replace("0", "O")
5+
.replace("1", "I");
6+
for (char ch : num.toCharArray()) {
7+
if (!"ABCDEFIO".contains(String.valueOf(ch))) {
8+
return "ERROR";
9+
}
10+
}
11+
return num;
12+
}
13+
}
14+
/*
15+
$1271. 十六进制魔术数字
16+
https://leetcode.cn/problems/hexspeak/
17+
18+
你有一个十进制数字,请按照此规则将它变成「十六进制魔术数字」:首先将它变成字母大写的十六进制字符串,然后将所有的数字 0 变成字母 O ,将数字 1 变成字母 I 。
19+
如果一个数字在转换后只包含 {"A", "B", "C", "D", "E", "F", "I", "O"} ,那么我们就认为这个转换是有效的。
20+
给你一个字符串 num ,它表示一个十进制数 N,如果它的十六进制魔术数字转换是有效的,请返回转换后的结果,否则返回 "ERROR" 。
21+
示例 1:
22+
输入:num = "257"
23+
输出:"IOI"
24+
解释:257 的十六进制表示是 101 。
25+
示例 2:
26+
输入:num = "3"
27+
输出:"ERROR"
28+
提示:
29+
1 <= N <= 10^12
30+
给定字符串不会有前导 0 。
31+
结果中的所有字母都应该是大写字母。
32+
33+
模拟
34+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1228Tests {
5+
private final Solution1228 solution1228 = new Solution1228();
6+
7+
@Test
8+
public void example1() {
9+
int[] arr = {5, 7, 11, 13};
10+
int expected = 9;
11+
Assertions.assertEquals(expected, solution1228.missingNumber(arr));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] arr = {15, 13, 12};
17+
int expected = 14;
18+
Assertions.assertEquals(expected, solution1228.missingNumber(arr));
19+
}
20+
21+
// 补充用例
22+
@Test
23+
public void example3() {
24+
// https://leetcode.cn/submissions/detail/396157152/
25+
// 公差为 0
26+
int[] arr = {0, 0, 0, 0, 0};
27+
int expected = 0;
28+
Assertions.assertEquals(expected, solution1228.missingNumber(arr));
29+
}
30+
31+
@Test
32+
public void example4() {
33+
// https://leetcode.cn/submissions/detail/396157210/
34+
// 公差为负数,不能取最小值
35+
int[] arr = {80387, 68178, 55969, 31551};
36+
int expected = 43760;
37+
Assertions.assertEquals(expected, solution1228.missingNumber(arr));
38+
}
39+
40+
@Test
41+
public void example5() {
42+
// https://leetcode.cn/submissions/detail/396157210/
43+
// 公差为 0
44+
int[] arr = {1, 1, 1};
45+
int expected = 1;
46+
Assertions.assertEquals(expected, solution1228.missingNumber(arr));
47+
}
48+
}

0 commit comments

Comments
 (0)