Subject to introduce
434. Number of words in a string
Count the number of words in the string, where words are consecutive characters rather than Spaces.
Note that you can assume that the string does not contain any non-printable characters.
Example:
Input: "Hello, my name is John" Output: 5 Explanation: Words here refer to consecutive characters that are not Spaces, so "Hello," counts as one word.Copy the code
Their thinking
Given the string s, we are asked to count the number of words in the string, which must be consecutive and not blank.
So we can use split to split strings into arrays with Spaces.
Filter is then used to filter the empty elements in the array.
And then return the rest of the array length that we’re looking for in this problem.
The problem solving code
var countSegments = function (s) { return s.split(" ").filter((item) => item ! == "").length; }Copy the code
Swipe questions and punch out records
Here is the previous swipe card records, you can have a look, if you have any different views and views or feel what is wrong, welcome to leave a message in the comment area! 🙏 🙏 🙏
[LeetCode0303 topic area and retrieval – array immutable] | punch brush
[LeetCode1200. Minimum absolute difference] | punch brush
[LeetCode0304 topic 2 d area and retrieval – matrix immutable] | punch brush
[LeetCode11 topic containers of most water] | punch brush
[LeetCode0338 topic bit count] | punch brush
[LeetCode209 topic length minimum subarray] | punch brush
[LeetCode236 topic in recent common ancestor of binary tree] | punch brush
[LeetCode141 topic circular linked list] | punch brush
[LeetCode53 topic most architectural sequence and] | punch brush
[LeetCode48 topic rotating images] | punch brush
[LeetCode155 topic minimum stack] | punch brush
[LeetCode1124 problem well, the longest period of a] | punch brush
[LeetCode274 problem H index] | punch brush
[LeetCode367 problem effective completely square number] | punch brush
[LeetCode1047 problem delete all adjacent duplicates of the string] | punch brush
[LeetCode160 topic cross linked list] | punch brush
[LeetCode1438 topic absolute difference does not exceed the limit of the longest continuous subarray] | punch brush
conclusion
Come on! HXDM!!!!!! 💪 💪 💪
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign