1816:截断句子
This commit is contained in:
parent
b26dc421bb
commit
6c177df753
76
src/main/java/leetcode/editor/cn/TruncateSentence.java
Normal file
76
src/main/java/leetcode/editor/cn/TruncateSentence.java
Normal file
@ -0,0 +1,76 @@
|
||||
//句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前导或尾随空格。每个单词仅由大小写英文字母组成(不含标点符号)。
|
||||
//
|
||||
//
|
||||
// 例如,"Hello World"、"HELLO" 和 "hello world hello world" 都是句子。
|
||||
//
|
||||
//
|
||||
// 给你一个句子 s 和一个整数 k ,请你将 s 截断 ,使截断后的句子仅含 前 k 个单词。返回 截断 s 后得到的句子。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:s = "Hello how are you Contestant", k = 4
|
||||
//输出:"Hello how are you"
|
||||
//解释:
|
||||
//s 中的单词为 ["Hello", "how" "are", "you", "Contestant"]
|
||||
//前 4 个单词为 ["Hello", "how", "are", "you"]
|
||||
//因此,应当返回 "Hello how are you"
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:s = "What is the solution to this problem", k = 4
|
||||
//输出:"What is the solution"
|
||||
//解释:
|
||||
//s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]
|
||||
//前 4 个单词为 ["What", "is", "the", "solution"]
|
||||
//因此,应当返回 "What is the solution"
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:s = "chopper is not a tanuki", k = 5
|
||||
//输出:"chopper is not a tanuki"
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 500
|
||||
// k 的取值范围是 [1, s 中单词的数目]
|
||||
// s 仅由大小写英文字母和空格组成
|
||||
// s 中的单词之间由单个空格隔开
|
||||
// 不存在前导或尾随空格
|
||||
//
|
||||
// Related Topics 字符串
|
||||
// 👍 6 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//1816:截断句子
|
||||
public class TruncateSentence{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new TruncateSentence().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String truncateSentence(String s, int k) {
|
||||
String[] strings = s.split(" ");
|
||||
if (strings.length == 0) {
|
||||
return s;
|
||||
}
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
for (int i = 0; i < k; i++) {
|
||||
sBuilder.append(strings[i]).append(" ");
|
||||
}
|
||||
s = sBuilder.toString();
|
||||
s = s.substring(0, s.length() - 1);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
47
src/main/java/leetcode/editor/cn/TruncateSentence.md
Normal file
47
src/main/java/leetcode/editor/cn/TruncateSentence.md
Normal file
@ -0,0 +1,47 @@
|
||||
<p><strong>句子</strong> 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前导或尾随空格。每个单词仅由大小写英文字母组成(不含标点符号)。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"Hello World"</code>、<code>"HELLO"</code> 和 <code>"hello world hello world"</code> 都是句子。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个句子 <code>s</code> 和一个整数 <code>k</code> ,请你将 <code>s</code> <strong>截断</strong> ,使截断后的句子仅含 <strong>前</strong> <code>k</code> 个单词。返回 <strong>截断</strong> <code>s</code><em> </em>后得到的句子<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "Hello how are you Contestant", k = 4
|
||||
<strong>输出:</strong>"Hello how are you"
|
||||
<strong>解释:</strong>
|
||||
s 中的单词为 ["Hello", "how" "are", "you", "Contestant"]
|
||||
前 4 个单词为 ["Hello", "how", "are", "you"]
|
||||
因此,应当返回 "Hello how are you"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "What is the solution to this problem", k = 4
|
||||
<strong>输出:</strong>"What is the solution"
|
||||
<strong>解释:</strong>
|
||||
s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]
|
||||
前 4 个单词为 ["What", "is", "the", "solution"]
|
||||
因此,应当返回 "What is the solution"</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "chopper is not a tanuki", k = 5
|
||||
<strong>输出:</strong>"chopper is not a tanuki"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 500</code></li>
|
||||
<li><code>k</code> 的取值范围是 <code>[1, s 中单词的数目]</code></li>
|
||||
<li><code>s</code> 仅由大小写英文字母和空格组成</li>
|
||||
<li><code>s</code> 中的单词之间由单个空格隔开</li>
|
||||
<li>不存在前导或尾随空格</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 6</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user