LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10——434. Number of Segments in a String(easy)
目录
LeetCode刷题记录9——434. Number of Segments in a String(easy)
题目
语言
思路
源码
后记
题目
题目的输入是一个字符串s,输出是一个int型的整数。计算过程为:输出由空白字符切割出的片段数,如图所示,这个片段数为:Hello, my name is John这五个片段。特别要注意的是第一个不是Hello而是Hello,
语言
Java
思路
分析:这题不能只是找单词这么简单,因为它也可以有逗号,他是要找到由空格分开的,多少个空格都行,只要是空格把你们分开的,那不管是逗号还是字母,都算一个。
所以我先把字符串s转换为字符数组str,接着遍历字符数组,当碰到空格就返回循环继续;如果不是空格了,那就count计数器加加,显然可以看出,这里碰到一个字母就会加加,所以还得用个循环,把其余的字母“跳过”,找到下一个空格,最后返回count的值。
源码
class Solution {public int countSegments(String s) {int count = 0;char str[]=s.toCharArray();for (int i=0;i<str.length;i++) {if (str[i]==' ') continue;count++;while (i<str.length&& str[i]!=' ') i++;}return count;}
}
后记
因为笔者最近在上java的课程,所以大多用的是java语言,后期准备去学习C++,用C++来写。
LeetCode刷题记录10——434. Number of Segments in a String(easy)相关推荐
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录4——67. Add Binary(easy)
LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...
- LeetCode刷题记录2——217. Contains Duplicate(easy)
LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...
- LeetCode刷题记录1——717. 1-bit and 2-bit Characters(easy)
LeetCode刷题记录1--717. 1-bit and 2-bit Characters(easy) LeetCode刷题记录1--717. 1-bit and 2-bit Characters( ...
- LeetCode刷题:871. Minimum Number of Refueling Stops
LeetCode刷题:871. Minimum Number of Refueling Stops 原题链接:https://leetcode.com/problems/minimum-number- ...
- wy的leetcode刷题记录_Day15
wy的leetcode刷题记录_Day15 目录 wy的leetcode刷题记录_Day15 2441. 与对应负数同时存在的最大正整数 题目介绍 思路 代码 收获 2442. 反转之后不同整数的数目 ...
- 算法记录 牛客网 leetcode刷题记录
算法记录 & 牛客网 & leetcode刷题记录 解题思路 STL容器 常用算法模板 堆排序 插入排序 快速排序 BFS层序遍历 二叉树 JZ55 二叉树的深度 BST(binary ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
最新文章
- Android UI开发详解之ActionBar .
- 关于DataAdapter的问题-----Fill: SelectCommand.Connection 属性尚未初始化。
- 【原拼】从/etc/inetd.conf学习服务(1)
- file_get_contents设置响应时间timeout的方法
- POJ2228-Naptime【环形结构上的dp】
- git版本控制系统常用指令,Xmind笔记整理
- stm32 Flash读写[库函数]
- python合并 excel 表
- Java线程的一些api
- 让IE支持Css3属性(圆角、阴影、渐变)
- 剑指offer -- 反转链表
- 【原创】Mapped Statements collection does not contain value for DaoImpl.method
- 全面接触PDF:最好用的PDF软件汇总(2010-12-07更新)
- JLU数据结构第二次上机实验解题报告
- 微型计算机原理与接口周荷琴,微型计算机原理与接口技术周荷琴
- python 网格搜索_Python机器学习笔记:Grid SearchCV(网格搜索)
- 图片怎么做雕刻路径_浮雕做图的简要步骤
- Java和Android笔试题
- 分享 - 怎样实习才能成长最快
- 性能分析26-tomcat优化P61
热门文章
- LeetCode-198. 打家劫舍
- 2022-2028年中国医学模拟教育产品市场供需现状及投资战略研究报告
- AVR开发 Arduino方法(附四) 参考文献与网址
- ubuntu下安装 python 常用软件
- 不一样的命令行 – Windows PowerShell简介
- UVA 10714 - Ants
- IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
- android的布局流程,Android View 布局流程(Layout)全面解析
- linux救援模式无法识别分区,Linux操作系统出现严重故障后的救援模式
- 李白打酒c语言编程,搞定了“李白打酒”,还原问题都迎刃而解