434:字符串中的单词数

This commit is contained in:
huangge1199@hotmail.com 2021-10-07 21:36:27 +08:00
parent 9ad861012b
commit 7b33638f23
4 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,38 @@
//统计字符串中的单词个数这里的单词指的是连续的不是空格的字符
//
// 请注意你可以假定字符串里不包括任何不可打印的字符
//
// 示例:
//
// 输入: "Hello, my name is John"
//输出: 5
//解释: 这里的单词是指连续的不是空格的字符所以 "Hello," 算作 1 个单词
//
// Related Topics 字符串 👍 144 👎 0
package leetcode.editor.cn;
//434:字符串中的单词数
class NumberOfSegmentsInAString {
public static void main(String[] args) {
//测试代码
Solution solution = new NumberOfSegmentsInAString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countSegments(String s) {
String[] strs = s.split(" ");
int count = 0;
for (String str : strs) {
if (!"".equals(str)) {
count++;
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
<p>统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。</p>
<p>请注意,你可以假定字符串里不包括任何不可打印的字符。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> &quot;Hello, my name is John&quot;
<strong>输出:</strong> 5
<strong>解释: </strong>这里的单词是指连续的不是空格的字符,所以 &quot;Hello,&quot; 算作 1 个单词。
</pre>
<div><div>Related Topics</div><div><li>字符串</li></div></div><br><div><li>👍 144</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long