Skip to content

Commit abc12e7

Browse files
authored
feat: add new lc problems (doocs#2582)
1 parent 2659300 commit abc12e7

File tree

50 files changed

+1785
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1785
-58
lines changed

solution/0100-0199/0139.Word Break/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<pre>
2828
<strong>输入:</strong> s = "applepenapple", wordDict = ["apple", "pen"]
2929
<strong>输出:</strong> true
30-
<strong>解释:</strong> 返回 true 因为 <code>"</code>applepenapple<code>"</code> 可以由 <code>"</code>apple" "pen" "apple<code>" 拼接成</code>
30+
<strong>解释:</strong> 返回 true 因为 "applepenapple" 可以由 "apple" "pen" "apple" 拼接成。
3131
&nbsp; 注意,你可以重复使用字典中的单词。
3232
</pre>
3333

solution/0100-0199/0140.Word Break II/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<p><strong class="example">示例 1:</strong></p>
1818

1919
<pre>
20-
<strong>输入:</strong>s = "<code>catsanddog</code>", wordDict = <code>["cat","cats","and","sand","dog"]</code>
21-
<strong>输出:</strong><code>["cats and dog","cat sand dog"]</code>
20+
<strong>输入:</strong>s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
21+
<strong>输出:</strong>["cats and dog","cat sand dog"]
2222
</pre>
2323

2424
<p><strong class="example">示例 2:</strong></p>

solution/0300-0399/0351.Android Unlock Patterns/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0300-0399/0351.Android%20Unlock%20Patterns/README_EN.md)
44

5-
<!-- tags:动态规划,回溯 -->
5+
<!-- tags:位运算,动态规划,回溯,状态压缩 -->
66

77
## 题目描述
88

solution/0300-0399/0351.Android Unlock Patterns/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0300-0399/0351.Android%20Unlock%20Patterns/README.md)
44

5-
<!-- tags:Dynamic Programming,Backtracking -->
5+
<!-- tags:Bit Manipulation,Dynamic Programming,Backtracking,Bitmask -->
66

77
## Description
88

solution/0400-0499/0418.Sentence Screen Fitting/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0400-0499/0418.Sentence%20Screen%20Fitting/README_EN.md)
44

5-
<!-- tags:字符串,动态规划,模拟 -->
5+
<!-- tags:数组,字符串,动态规划 -->
66

77
## 题目描述
88

solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0400-0499/0418.Sentence%20Screen%20Fitting/README.md)
44

5-
<!-- tags:String,Dynamic Programming,Simulation -->
5+
<!-- tags:Array,String,Dynamic Programming -->
66

77
## Description
88

solution/0500-0599/0548.Split Array with Equal Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0500-0599/0548.Split%20Array%20with%20Equal%20Sum/README_EN.md)
44

5-
<!-- tags:数组,前缀和 -->
5+
<!-- tags:数组,哈希表,前缀和 -->
66

77
## 题目描述
88

solution/0500-0599/0548.Split Array with Equal Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0500-0599/0548.Split%20Array%20with%20Equal%20Sum/README.md)
44

5-
<!-- tags:Array,Prefix Sum -->
5+
<!-- tags:Array,Hash Table,Prefix Sum -->
66

77
## Description
88

solution/0600-0699/0636.Exclusive Time of Functions/README.md

+8-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>有一个 <strong>单线程</strong> CPU 正在运行一个含有 <code>n</code> 道函数的程序。每道函数都有一个位于  <code>0</code> 和 <code>n-1</code> 之间的唯一标识符。</p>
11+
<p>有一个 <strong>单线程</strong> CPU 正在运行一个含有 <code>n</code> 道函数的程序。每道函数都有一个位于&nbsp; <code>0</code> 和 <code>n-1</code> 之间的唯一标识符。</p>
1212

1313
<p>函数调用 <strong>存储在一个 <a href="https://baike.baidu.com/item/%E8%B0%83%E7%94%A8%E6%A0%88/22718047?fr=aladdin" target="_blank">调用栈</a> 上</strong> :当一个函数调用开始时,它的标识符将会推入栈中。而当一个函数调用结束时,它的标识符将会从栈中弹出。标识符位于栈顶的函数是 <strong>当前正在执行的函数</strong> 。每当一个函数开始或者结束时,将会记录一条日志,包括函数标识符、是开始还是结束、以及相应的时间戳。</p>
1414

@@ -17,7 +17,7 @@
1717
<p>函数的 <strong>独占时间</strong> 定义是在这个函数在程序所有函数调用中执行时间的总和,调用其他函数花费的时间不算该函数的独占时间。例如,如果一个函数被调用两次,一次调用执行 <code>2</code> 单位时间,另一次调用执行 <code>1</code> 单位时间,那么该函数的 <strong>独占时间</strong> 为 <code>2 + 1 = 3</code> 。</p>
1818

1919
<p>以数组形式返回每个函数的 <strong>独占时间</strong> ,其中第 <code>i</code> 个下标对应的值表示标识符 <code>i</code> 的函数的独占时间。</p>
20-
 
20+
&nbsp;
2121

2222
<p><strong>示例 1:</strong></p>
2323
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0636.Exclusive%20Time%20of%20Functions/images/diag1b.png" style="width: 550px; height: 239px;" />
@@ -58,32 +58,18 @@
5858
函数 0(初始调用)在时间戳 7 的起始恢复执行,执行 1 个单位时间,于时间戳 7 的末尾结束执行。
5959
所以函数 0 总共执行 2 + 4 + 1 = 7 个单位时间,函数 1 总共执行 1 个单位时间。 </pre>
6060

61-
<p><strong>示例 4:</strong></p>
62-
63-
<pre>
64-
<strong>输入:</strong>n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"]
65-
<strong>输出:</strong>[8,1]
66-
</pre>
67-
68-
<p><strong>示例 5:</strong></p>
69-
70-
<pre>
71-
<strong>输入:</strong>n = 1, logs = ["0:start:0","0:end:0"]
72-
<strong>输出:</strong>[1]
73-
</pre>
74-
75-
<p> </p>
61+
<p>&nbsp;</p>
7662

7763
<p><strong>提示:</strong></p>
7864

7965
<ul>
80-
<li><code>1 <= n <= 100</code></li>
81-
<li><code>1 <= logs.length <= 500</code></li>
82-
<li><code>0 <= function_id < n</code></li>
83-
<li><code>0 <= timestamp <= 10<sup>9</sup></code></li>
66+
<li><code>1 &lt;= n &lt;= 100</code></li>
67+
<li><code>1 &lt;= logs.length &lt;= 500</code></li>
68+
<li><code>0 &lt;= function_id &lt; n</code></li>
69+
<li><code>0 &lt;= timestamp &lt;= 10<sup>9</sup></code></li>
8470
<li>两个开始事件不会在同一时间戳发生</li>
8571
<li>两个结束事件不会在同一时间戳发生</li>
86-
<li>每道函数都有一个对应 <code>"start"</code> 日志的 <code>"end"</code> 日志</li>
72+
<li>每道函数都有一个对应&nbsp;<code>"start"</code> 日志的 <code>"end"</code> 日志</li>
8773
</ul>
8874

8975
## 解法

solution/0600-0699/0648.Replace Words/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>在英语中,我们有一个叫做&nbsp;<code>词根</code>(root) 的概念,可以词根<strong>后面</strong>添加其他一些词组成另一个较长的单词——我们称这个词为&nbsp;<code>继承词</code>(successor)。例如,词根<code>an</code>,跟随着单词&nbsp;<code>other</code>(其他),可以形成新的单词&nbsp;<code>another</code>(另一个)。</p>
11+
<p>在英语中,我们有一个叫做&nbsp;<strong>词根</strong>(root) 的概念,可以词根&nbsp;<strong>后面&nbsp;</strong>添加其他一些词组成另一个较长的单词——我们称这个词为 <strong>继承词</strong>&nbsp;(successor)。例如,词根&nbsp;<code>help</code>,跟随着 <strong>继承</strong>词&nbsp;<code>"ful"</code>,可以形成新的单词&nbsp;<code>"helpful"</code>。</p>
1212

13-
<p>现在,给定一个由许多<strong>词根</strong>组成的词典 <code>dictionary</code> 和一个用空格分隔单词形成的句子 <code>sentence</code>。你需要将句子中的所有<strong>继承词</strong>用<strong>词根</strong>替换掉。如果<strong>继承词</strong>有许多可以形成它的<strong>词根</strong>,则用<strong>最短</strong>的词根替换它。</p>
13+
<p>现在,给定一个由许多&nbsp;<strong>词根&nbsp;</strong>组成的词典 <code>dictionary</code> 和一个用空格分隔单词形成的句子 <code>sentence</code>。你需要将句子中的所有&nbsp;<strong>继承词&nbsp;</strong>用&nbsp;<strong>词根&nbsp;</strong>替换掉。如果&nbsp;<strong>继承词&nbsp;</strong>有许多可以形成它的&nbsp;<strong>词根</strong>,则用&nbsp;<strong>最短&nbsp;</strong>的 <strong>词根</strong> 替换它。</p>
1414

1515
<p>你需要输出替换之后的句子。</p>
1616

@@ -38,7 +38,7 @@
3838
<li><code>1 &lt;= dictionary.length&nbsp;&lt;= 1000</code></li>
3939
<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>
4040
<li><code>dictionary[i]</code>&nbsp;仅由小写字母组成。</li>
41-
<li><code>1 &lt;= sentence.length &lt;= 10^6</code></li>
41+
<li><code>1 &lt;= sentence.length &lt;= 10<sup>6</sup></code></li>
4242
<li><code>sentence</code>&nbsp;仅由小写字母和空格组成。</li>
4343
<li><code>sentence</code> 中单词的总量在范围 <code>[1, 1000]</code> 内。</li>
4444
<li><code>sentence</code> 中每个单词的长度在范围 <code>[1, 1000]</code> 内。</li>

solution/0600-0699/0648.Replace Words/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## Description
88

9-
<p>In English, we have a concept called <strong>root</strong>, which can be followed by some other word to form another longer word - let&#39;s call this word <strong>successor</strong>. For example, when the <strong>root</strong> <code>&quot;an&quot;</code> is followed by the <strong>successor</strong> word <code>&quot;other&quot;</code>, we can form a new word <code>&quot;another&quot;</code>.</p>
9+
<p>In English, we have a concept called <strong>root</strong>, which can be followed by some other word to form another longer word - let&#39;s call this word <strong>successor</strong>. For example, when the <strong>root</strong> <code>&quot;help&quot;</code> is followed by the <strong>successor</strong> word <code>&quot;ful&quot;</code>, we can form a new word <code>&quot;helpful&quot;</code>.</p>
1010

1111
<p>Given a <code>dictionary</code> consisting of many <strong>roots</strong> and a <code>sentence</code> consisting of words separated by spaces, replace all the <strong>successors</strong> in the sentence with the <strong>root</strong> forming it. If a <strong>successor</strong> can be replaced by more than one <strong>root</strong>, replace it with the <strong>root</strong> that has <strong>the shortest length</strong>.</p>
1212

solution/0600-0699/0681.Next Closest Time/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0600-0699/0681.Next%20Closest%20Time/README_EN.md)
44

5-
<!-- tags:字符串,枚举 -->
5+
<!-- tags:哈希表,字符串,回溯,枚举 -->
66

77
## 题目描述
88

solution/0600-0699/0681.Next Closest Time/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0600-0699/0681.Next%20Closest%20Time/README.md)
44

5-
<!-- tags:String,Enumeration -->
5+
<!-- tags:Hash Table,String,Backtracking,Enumeration -->
66

77
## Description
88

solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0700-0799/0786.K-th%20Smallest%20Prime%20Fraction/README_EN.md)
44

5-
<!-- tags:数组,二分查找,排序,堆(优先队列) -->
5+
<!-- tags:数组,双指针,二分查找,排序,堆(优先队列) -->
66

77
## 题目描述
88

solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0700-0799/0786.K-th%20Smallest%20Prime%20Fraction/README.md)
44

5-
<!-- tags:Array,Binary Search,Sorting,Heap (Priority Queue) -->
5+
<!-- tags:Array,Two Pointers,Binary Search,Sorting,Heap (Priority Queue) -->
66

77
## Description
88

solution/1700-1799/1766.Tree of Coprimes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1700-1799/1766.Tree%20of%20Coprimes/README_EN.md)
44

5-
<!-- tags:树,深度优先搜索,广度优先搜索,数学 -->
5+
<!-- tags:树,深度优先搜索,广度优先搜索,数组,数学,数论 -->
66

77
## 题目描述
88

solution/1700-1799/1766.Tree of Coprimes/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1700-1799/1766.Tree%20of%20Coprimes/README.md)
44

5-
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Math -->
5+
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Array,Math,Number Theory -->
66

77
## Description
88

solution/2400-2499/2446.Determine if Two Events Have Conflict/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<p><b>提示:</b></p>
5353

5454
<ul>
55-
<li><code>event1.length == event2.length == 2.</code></li>
55+
<li><code>event1.length == event2.length == 2</code></li>
5656
<li><code>event1[i].length == event2[i].length == 5</code></li>
5757
<li><code>startTime<sub>1</sub> &lt;= endTime<sub>1</sub></code></li>
5858
<li><code>startTime<sub>2</sub> &lt;= endTime<sub>2</sub></code></li>

solution/2400-2499/2446.Determine if Two Events Have Conflict/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<p><strong>Constraints:</strong></p>
4949

5050
<ul>
51-
<li>event1<code>.length == </code>event2<code>.length == 2.</code></li>
51+
<li><code>event1.length == event2.length == 2</code></li>
5252
<li><code>event1[i].length == event2[i].length == 5</code></li>
5353
<li><code>startTime<sub>1</sub> &lt;= endTime<sub>1</sub></code></li>
5454
<li><code>startTime<sub>2</sub> &lt;= endTime<sub>2</sub></code></li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# [3110. 字符串的分数](https://leetcode.cn/problems/score-of-a-string)
2+
3+
[English Version](/solution/3100-3199/3110.Score%20of%20a%20String/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;。一个字符串的&nbsp;<strong>分数</strong>&nbsp;定义为相邻字符 <strong>ASCII</strong>&nbsp;码差值绝对值的和。</p>
12+
13+
<p>请你返回 <code>s</code>&nbsp;的 <strong>分数</strong>&nbsp;。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<div class="example-block">
20+
<p><span class="example-io"><b>输入:</b>s = "hello"</span></p>
21+
22+
<p><span class="example-io"><b>输出:</b>13</span></p>
23+
24+
<p><strong>解释:</strong></p>
25+
26+
<p><code>s</code>&nbsp;中字符的 <strong>ASCII </strong>码分别为:<code>'h' = 104</code>&nbsp;,<code>'e' = 101</code>&nbsp;,<code>'l' = 108</code>&nbsp;,<code>'o' = 111</code>&nbsp;。所以&nbsp;<code>s</code>&nbsp;的分数为&nbsp;<code>|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13</code>&nbsp;。</p>
27+
</div>
28+
29+
<p><strong class="example">示例 2:</strong></p>
30+
31+
<div class="example-block">
32+
<p><span class="example-io"><b>输入:</b>s = "zaz"</span></p>
33+
34+
<p><span class="example-io"><b>输出:</b>50</span></p>
35+
36+
<p><strong>解释:</strong></p>
37+
38+
<p><code>s</code>&nbsp;中字符的 <strong>ASCII&nbsp;</strong>码分别为:<code>'z' = 122</code>&nbsp;,<code>'a' = 97</code>&nbsp;。所以&nbsp;<code>s</code>&nbsp;的分数为&nbsp;<code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code>&nbsp;。</p>
39+
</div>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
47+
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
48+
</ul>
49+
50+
## 解法
51+
52+
### 方法一:模拟
53+
54+
我们直接遍历字符串 $s$,计算相邻字符的 ASCII 码差值的绝对值之和即可。
55+
56+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
57+
58+
<!-- tabs:start -->
59+
60+
```python
61+
class Solution:
62+
def scoreOfString(self, s: str) -> int:
63+
return sum(abs(a - b) for a, b in pairwise(map(ord, s)))
64+
```
65+
66+
```java
67+
class Solution {
68+
public int scoreOfString(String s) {
69+
int ans = 0;
70+
for (int i = 1; i < s.length(); ++i) {
71+
ans += Math.abs(s.charAt(i - 1) - s.charAt(i));
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
int scoreOfString(string s) {
82+
int ans = 0;
83+
for (int i = 1; i < s.size(); ++i) {
84+
ans += abs(s[i] - s[i - 1]);
85+
}
86+
return ans;
87+
}
88+
};
89+
```
90+
91+
```go
92+
func scoreOfString(s string) (ans int) {
93+
for i := 1; i < len(s); i++ {
94+
ans += abs(int(s[i-1]) - int(s[i]))
95+
}
96+
return
97+
}
98+
99+
func abs(x int) int {
100+
if x < 0 {
101+
return -x
102+
}
103+
return x
104+
}
105+
```
106+
107+
```ts
108+
function scoreOfString(s: string): number {
109+
let ans = 0;
110+
for (let i = 1; i < s.length; ++i) {
111+
ans += Math.abs(s.charCodeAt(i) - s.charCodeAt(i - 1));
112+
}
113+
return ans;
114+
}
115+
```
116+
117+
<!-- tabs:end -->
118+
119+
<!-- end -->

0 commit comments

Comments
 (0)