Skip to content

Commit 23db332

Browse files
committed
feat: update leetcode solutions: No.0016. Truncate Sentence
1 parent 054965b commit 23db332

File tree

21 files changed

+832
-4
lines changed

21 files changed

+832
-4
lines changed

solution/1800-1899/1816.Truncate Sentence/README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,65 @@ s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```python
67-
67+
class Solution:
68+
def truncateSentence(self, s: str, k: int) -> str:
69+
for i, c in enumerate(s):
70+
if c == ' ':
71+
k -= 1
72+
if k == 0:
73+
return s[:i]
74+
return s
6875
```
6976

7077
### **Java**
7178

7279
<!-- 这里可写当前语言的特殊实现逻辑 -->
7380

7481
```java
82+
class Solution {
83+
public String truncateSentence(String s, int k) {
84+
for (int i = 0; i < s.length(); ++i) {
85+
if (s.charAt(i) == ' ' && (--k) == 0) {
86+
return s.substring(0, i);
87+
}
88+
}
89+
return s;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
string truncateSentence(string s, int k) {
100+
for (int i = 0; i < s.size(); ++i) {
101+
if (s[i] == ' ' && (--k) == 0) {
102+
return s.substr(0, i);
103+
}
104+
}
105+
return s;
106+
}
107+
};
108+
```
75109
110+
### **JavaScript**
111+
112+
```js
113+
/**
114+
* @param {string} s
115+
* @param {number} k
116+
* @return {string}
117+
*/
118+
var truncateSentence = function (s, k) {
119+
for (let i = 0; i < s.length; ++i) {
120+
if (s[i] == " " && --k == 0) {
121+
return s.substring(0, i);
122+
}
123+
}
124+
return s;
125+
};
76126
```
77127

78128
### **...**

solution/1800-1899/1816.Truncate Sentence/README_EN.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,63 @@ Hence, you should return &quot;What is the solution&quot;.</pre>
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def truncateSentence(self, s: str, k: int) -> str:
64+
for i, c in enumerate(s):
65+
if c == ' ':
66+
k -= 1
67+
if k == 0:
68+
return s[:i]
69+
return s
6370
```
6471

6572
### **Java**
6673

6774
```java
75+
class Solution {
76+
public String truncateSentence(String s, int k) {
77+
for (int i = 0; i < s.length(); ++i) {
78+
if (s.charAt(i) == ' ' && (--k) == 0) {
79+
return s.substring(0, i);
80+
}
81+
}
82+
return s;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
string truncateSentence(string s, int k) {
93+
for (int i = 0; i < s.size(); ++i) {
94+
if (s[i] == ' ' && (--k) == 0) {
95+
return s.substr(0, i);
96+
}
97+
}
98+
return s;
99+
}
100+
};
101+
```
68102
103+
### **JavaScript**
104+
105+
```js
106+
/**
107+
* @param {string} s
108+
* @param {number} k
109+
* @return {string}
110+
*/
111+
var truncateSentence = function (s, k) {
112+
for (let i = 0; i < s.length; ++i) {
113+
if (s[i] == " " && --k == 0) {
114+
return s.substring(0, i);
115+
}
116+
}
117+
return s;
118+
};
69119
```
70120

71121
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string truncateSentence(string s, int k) {
4+
for (int i = 0; i < s.size(); ++i) {
5+
if (s[i] == ' ' && (--k) == 0) {
6+
return s.substr(0, i);
7+
}
8+
}
9+
return s;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public String truncateSentence(String s, int k) {
3+
for (int i = 0; i < s.length(); ++i) {
4+
if (s.charAt(i) == ' ' && (--k) == 0) {
5+
return s.substring(0, i);
6+
}
7+
}
8+
return s;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
var truncateSentence = function (s, k) {
7+
for (let i = 0; i < s.length; ++i) {
8+
if (s[i] == " " && --k == 0) {
9+
return s.substring(0, i);
10+
}
11+
}
12+
return s;
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def truncateSentence(self, s: str, k: int) -> str:
3+
for i, c in enumerate(s):
4+
if c == ' ':
5+
k -= 1
6+
if k == 0:
7+
return s[:i]
8+
return s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [1827. 最少操作使数组递增](https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-increasing)
2+
3+
[English Version](/solution/1800-1899/1827.Minimum%20Operations%20to%20Make%20the%20Array%20Increasing/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组 <code>nums</code> (<strong>下标从 0 开始</strong>)。每一次操作中,你可以选择数组中一个元素,并将它增加 <code>1</code> 。</p>
10+
11+
<ul>
12+
<li>比方说,如果 <code>nums = [1,2,3]</code> ,你可以选择增加 <code>nums[1]</code> 得到 <code>nums = [1,<b>3</b>,3]</code> 。</li>
13+
</ul>
14+
15+
<p>请你返回使 <code>nums</code> <strong>严格递增</strong> 的 <strong>最少</strong> 操作次数。</p>
16+
17+
<p>我们称数组 <code>nums</code> 是 <strong>严格递增的</strong> ,当它满足对于所有的 <code>0 &lt;= i &lt; nums.length - 1</code> 都有 <code>nums[i] &lt; nums[i+1]</code> 。一个长度为 <code>1</code> 的数组是严格递增的一种特殊情况。</p>
18+
19+
<p> </p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<pre><b>输入:</b>nums = [1,1,1]
24+
<b>输出:</b>3
25+
<b>解释:</b>你可以进行如下操作:
26+
1) 增加 nums[2] ,数组变为 [1,1,<strong>2</strong>] 。
27+
2) 增加 nums[1] ,数组变为 [1,<strong>2</strong>,2] 。
28+
3) 增加 nums[2] ,数组变为 [1,2,<strong>3</strong>] 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre><b>输入:</b>nums = [1,5,2,4,1]
34+
<b>输出:</b>14
35+
</pre>
36+
37+
<p><strong>示例 3:</strong></p>
38+
39+
<pre><b>输入:</b>nums = [8]
40+
<b>输出:</b>0
41+
</pre>
42+
43+
<p> </p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
50+
</ul>
51+
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **...**
76+
77+
```
78+
79+
```
80+
81+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [1827. Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing)
2+
3+
[中文文档](/solution/1800-1899/1827.Minimum%20Operations%20to%20Make%20the%20Array%20Increasing/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer array <code>nums</code> (<strong>0-indexed</strong>). In one operation, you can choose an element of the array and increment it by <code>1</code>.</p>
8+
9+
<ul>
10+
<li>For example, if <code>nums = [1,2,3]</code>, you can choose to increment <code>nums[1]</code> to make <code>nums = [1,<u><b>3</b></u>,3]</code>.</li>
11+
</ul>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of operations needed to make</em> <code>nums</code> <em><strong>strictly</strong> <strong>increasing</strong>.</em></p>
14+
15+
<p>An array <code>nums</code> is <strong>strictly increasing</strong> if <code>nums[i] &lt; nums[i+1]</code> for all <code>0 &lt;= i &lt; nums.length - 1</code>. An array of length <code>1</code> is trivially strictly increasing.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong>Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [1,1,1]
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> You can do the following operations:
24+
1) Increment nums[2], so nums becomes [1,1,<u><strong>2</strong></u>].
25+
2) Increment nums[1], so nums becomes [1,<u><strong>2</strong></u>,2].
26+
3) Increment nums[2], so nums becomes [1,2,<u><strong>3</strong></u>].
27+
</pre>
28+
29+
<p><strong>Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,5,2,4,1]
33+
<strong>Output:</strong> 14
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [8]
40+
<strong>Output:</strong> 0
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
48+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
49+
</ul>
50+
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
```java
65+
66+
```
67+
68+
### **...**
69+
70+
```
71+
72+
```
73+
74+
<!-- tabs:end -->

0 commit comments

Comments
 (0)