leetcode 434. 字符串中的单词数(Number of Segments in a String)

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。

请注意,你可以假定字符串里不包括任何不可打印的字符。

示例:

    输入: "Hello, my name is John"
    输出: 5

解法:

class Solution {
public:
    int countSegments(string s) {
        int res = 0;
        int sz = s.size();
        int i = 0, j = 0;
        while(i < sz){
            while(j < sz && s[j] != ' '){
                j++;
            }
            if(i != j){
                res++;
            }
            j++;
            i = j;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10579878.html

时间: 2024-07-31 05:37:56

leetcode 434. 字符串中的单词数(Number of Segments in a String)的相关文章

力扣(LeetCode)字符串中的单词数 个人题解

统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 题目描述比较不清楚,这里只要是用空格隔开的一律当作字符,包括非字母.使用JAVA自带库函数解决问题.记得忽略空格情况 当然这里使用了较大的内存保存分割后的ss字符串数组,如果对内存比较敏感的可以对字符串手动以空格划分.(这里空格可能多个,所以可以使用正则表达式较为方便去匹配) 代码如下: cl

[Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an arra

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

Leetcode 557.反转字符串中的单词III

反转字符串中的单词III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 实现思路: 刚看题目心想直接用String.split会不会更容易些,不过这样就失去了这个算法的意义了.还是采用最原始的方法,先

[LeetCode 557] 反转字符串中的单词 III

题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/reverse-words-in-a-stri

#Leetcode# 434. Number of Segments in a String

https://leetcode.com/problems/number-of-segments-in-a-string/ Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. Please note that the string does not contain any non-printable cha

557. 反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路:先将给定的字符串(s)中的单词拆分出来(str),然后单个处理每个单词,拼接成一个新的字符串(ans),返回ans. 总结: 1 static co

前端与算法 leetcode 387. 字符串中的第一个唯一字符

目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode",