From 41e37fb6b015f634d5ba8358d31305ecacb422b4 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Thu, 7 Oct 2021 06:51:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20434?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 "LeetCode/431-440/434. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 1e0e8328..a6c9a40b 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -29,6 +29,7 @@ | [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 | | [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 | | [414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/) | 中等 | 🤩🤩🤩🤩 | +| [434. 字符串中的单词数](https://leetcode-cn.com/problems/number-of-segments-in-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-segments-in-a-string/solution/gong-shui-san-xie-jian-dan-zi-fu-mo-ni-t-0gx6/) | 简单 | 🤩🤩🤩🤩 | | [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/string-compression/solution/gong-shui-san-xie-shuang-zhi-zhen-yuan-d-bppu/) | 中等 | 🤩🤩🤩🤩 | | [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 | | [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/431-440/434. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/431-440/434. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..abad8ebf --- /dev/null +++ "b/LeetCode/431-440/434. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,57 @@ +### 题目描述 + +这是 LeetCode 上的 **[434. 字符串中的单词数](https://leetcode-cn.com/problems/number-of-segments-in-a-string/solution/gong-shui-san-xie-jian-dan-zi-fu-mo-ni-t-0gx6/)** ,难度为 **简单**。 + +Tag : 「模拟」 + +统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。 + +请注意,你可以假定字符串里不包括任何不可打印的字符。 + +示例: +``` +输入: "Hello, my name is John" + +输出: 5 + +解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。 +``` + +--- + +### 模拟 + +题目对于「单词」的定义为「连续的不是空格的字符」。 + +因此,我们可以从前往后处理字符串 `s` 并进行计数,对于是空格的字符进行跳过(不计数),而对于非空格字符,则在遍历完一个完整单词(连续一段)后进行一次计数。 + +代码: +```Java +class Solution { + public int countSegments(String s) { + int n = s.length(); + int ans = 0; + for (int i = 0; i < n; ) { + if (s.charAt(i) == ' ' && i++ >= 0) continue; + while (i < n && s.charAt(i) != ' ') i++; + ans++; + } + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.434` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +