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 ...
随机推荐
- 数据结构之c++感悟
#include<iostream.h> 头文件: #include<iostream.h> #define MAX 1024 typedef int Elemtype; ty ...
- 【转】Map/Reduce简介
转自:http://blog.csdn.net/opennaive/article/details/7514146 1. MapReduce是干啥的 因为没找到谷歌的示意图,所以我想借用一张Hadoo ...
- 【转】Unity5.x发布IOS项目Xcode8免签证调试发布教程
http://www.jianshu.com/p/b0fb49fbcc14 最近尝试发布一下IOS项目,发现现在发布已经简单很多了,不需要开发者账户也能简单快捷进行真机调试. 调试: 1.准备工作①硬 ...
- [SDOI2010] 所驼门王的宝藏 [建图+tarjan缩点+DAG dp]
题面传送门: 传送门 思路: 看完题建模,容易得出是求单向图最长路径的问题 那么把这张图缩强联通分量,再在DAG上面DP即可 然而 这道题的建图实际上才是真正的考点 如果对于每一个点都直接连边到它所有 ...
- RTSP会话基本流程
RTSP会话基本流程 RTSP交互流程: C表示RTSP客户端,S表示RTSP服务端 ① C->S: OPTION request //询问S有哪些方法可用 S->C: OPTION re ...
- nodeJS学习(7)--- WS开发 NodeJS 项目-节2 <安装&设置&启动 mongodb 数据库++遇到的问题>
本文系统 win7 参考:http://lib.csdn.net/article/mongodb/58097 http://www.cnblogs.com/lzrabbit/p/3682510.ht ...
- Python(2)-- 运算符
1. 算术运算符 常规: 加(+).减(-).乘(*).除(/).取模(%) 此外: 幂(**):返回x的y次幂, eg: 2**3---返回 2 的 5 次幂,输出结果32 取整除(//):返回商的 ...
- vue-router 页面切换后保持在页面顶部而不是保持原先的滚动位置的办法
vue-router有提供一个方法scrollBehavior,它可以使切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. 这个功能只在 HTML5 history 模 ...
- 简析JVM GC的根搜索算法
根搜索算法的基本思路是通过一系列的“GC Roots”的对象作为起始点,从这些节点开始往下搜索,搜索的走过的路径称为引用链,当一个对象到“GC Roots”没有引用链可达时(也就是用图论的话说就是从G ...
- mysql启动错误1067的解决
安装后MYSQL5后,发现启动出错,有时启动正常,但加接时马上出错. 出错代码:1067 解决办法如下: 删除%windows%/my.ini 删除其它地方的my.ini 在mysql安装 ...