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

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

示例:

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

  1. class Solution {
  2. public:
  3. int countSegments(string s) {
  4. int len = s.size();
  5. string temp = "";
  6. int res = 0;
  7. for(int i = 0; i < len; i++)
  8. {
  9. if(s[i] == ' ')
  10. {
  11. if(temp == "")
  12. continue;
  13. else
  14. {
  15. res++;
  16. temp = "";
  17. }
  18. }
  19. else
  20. {
  21. temp += s[i];
  22. }
  23. if(i == len - 1 && temp != "")
  24. res++;
  25. }
  26. return res;
  27. }
  28. };

Leetcode434.Number of Segments in a String字符串中的单词数的更多相关文章

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

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  2. [LeetCode] 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 ...

  3. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  4. 【easy】Number of Segments in a String 字符串中的分段数量

    以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...

  5. 每天一道LeetCode--434. 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 ...

  6. Java实现 LeetCode 434 字符串中的单词数

    434. 字符串中的单词数 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my nam ...

  7. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  8. [Swift]LeetCode434. 字符串中的单词数 | 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 ...

  9. 【LeetCode】434. 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 ...

随机推荐

  1. springboot+springsecurity+thymeleaf

    来源:听秦疆老师的课笔记 springsecurity是一个权限管理框架,用来授权,认证,加密等等......类似的工具还有shiro 1.整合 我用的是springboot2.2.0版本,导入以下依 ...

  2. USACO training course Mother's Milk /// DFS(有点意思) oj10120

    题目大意: 输入 A B C 为三个容器的容量 一开始A B是空的 C是满的 每一次倾倒只能在 盛的容器满 或 倒的容器空 时才停止 输出当A容器空时 C容器内剩余量的所有可能值 Sample Inp ...

  3. webservice、httpClient、dubbo的区别

    在开发中,对于同一个war包中的对象方法我们可以直接调用,但是很多情况下需要在不同项目或者不同服务器进行相互调用 webservice webservice技术可以实现不同服务器项目直接的调用和交换数 ...

  4. linux普通用户无法登录mysql

    一.前言 本帖方法只适用于普通用户无法登录,但root用户可以登录的情况. 今天将war包放入linux后,运行报错,经过检查发现是数据库连接不上.奇怪的是,用户名和密码都是正确的,所以有了以下发现. ...

  5. js正则笔记

    //内容 var innerhtml = $('.reading_box_m').html().replace(/ tag="?[一二三四五六七八九十]+"?/ig, " ...

  6. Python全栈开发:Mysql(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  7. idea-----idea的项目中output框出现乱码

    找到tomcat的安装目录中的logging.properties 我的在这里:E:\tools\tomcat\apache-tomcat-8.5.38\conf 改其中的参数 修改前  java.u ...

  8. bootStrap-treeview插件

    简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...

  9. System.Web.Mvc.ViewResult.cs

    ylbtech-System.Web.Mvc.ViewResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...

  10. iOS开发UIView.h简介

    1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...