leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World",
return 5.
思路:题目非常easy,没什么好说的,唯一要注意的就是后面的结尾可能非常多空格。
详细代码例如以下:
public class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
boolean isEmpty = false;//从后往前数,直到不是空格
for(int i = s.length()-1; i > -1; i--){
if(s.charAt(i) != ' '){
len++;
isEmpty = true;
}else if(isEmpty){//倒数第二个空格出现
return len;
}
}
return len;
}
}
leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法的更多相关文章
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode: 58. Length of Last Word(Easy)
1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Leetcode 58 Length of Last Word 难度:0
https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...
- Leetcode 58 Length of Last Word 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- Java学习之字符串类
String在Java中是一个类类型(非主类型),是一个不可被继承的final类,而且字符串对象是一个不可变对象.声明的String对象应该被分配到堆中,声明的变量名应该持有的是String对象的引用 ...
- oracle function用法
函数调用限制1.SQL语句中只能调用存储函数(服务器端),而不能调用客户端的函数2.SQL只能调用带有输入参数,不能带有输出,输入输出函数3.SQL不能使用PL/SQL的特有数据类型(boolean, ...
- git 远程仓库回滚
git branch backup #创建备份分支 git push origin backup:backup #push到远程 git reset --hard commit_id #本地分支回滚 ...
- BZOJ4002 [JLOI2015]有意义的字符串 【数学 + 矩乘】
题目链接 BZOJ4002 题解 容易想到\(\frac{b + \sqrt{d}}{2}\)是二次函数\(x^2 - bx + \frac{b^2 - d}{4} = 0\)的其中一根 那么就有 \ ...
- BZOJ1491 [NOI2007]社交网络 【floyd】
题目 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这个关系网络对应到一 ...
- 洛谷P1908 逆序对
P1908 逆序对 2.2K通过 4.4K提交 题目提供者该用户不存在 标签云端 难度普及/提高- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 归并排序党注意了!数组要开… ...
- #ifndef 的用法介绍
ifndef是 if not define 的缩写,一种宏定义.它是预处理功能中三种(宏定义,文件包含和条件编译)中的第三种--条件编译. 其使用方式是: #define X ... #endif / ...
- 平滑升级nginx
平滑升级nginx版本技术文档 作者 联系方式 日期 版本号 马坤 852115346@qq.com 2017-12-31 V1.0.0 备注:作者水平有限,难免出现错误.如若发现错误,请您及时与作者 ...
- node.js express配置允许跨域
app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*& ...
- cf 512D - Fox And Travelling
题目大意 给定一颗\(n\le 100\)个点的图,可以进行随机游走,求游走\(k=0...n\)个点的方案数 游走的规则是:每次只能访问一个度数\(\le 1\)的点,并将其删除 分析 看完傻眼 问 ...