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 ...
随机推荐
- w3wp CPU 100%问题解决
问题: web服务器w3wp CPU占用率非常高,导致整个服务器CPU 100%占用,问题无法正常重现 解决方法: --问题尚未解决,此处记录目前的解决状态 1)下载windbg 参考https:// ...
- opendatasource问题
EXEC sp_configure 'show advanced options', 1 GO RECONFIGURE GO EXEC sp_configure 'Ad Hoc Distributed ...
- RESTful-rest_framework版本控制、分页器-第六篇
版本控制: 源码位置分析第一步: 源码位置分析第二步:在APIView-despath方法-initial-determine_version #执行determine_version,返回两个值, ...
- CSS简单的四种引入方式
CSS一共有四种引入方式 (1)最简单的两种方式是直接在html标签里面引入,或者在html文件前面声明,以下是简单的代码示例 <!DOCTYPE html> <html lang= ...
- firewalld的防火墙
firewalld的介绍与简单应用 CentOS7的默认防火墙是firewalld,在之前使用iptables时,关闭了firewalld服务,现在反过来关闭iptables服务,打开firewall ...
- 【转】Twitter-Snowflake,64位自增ID算法详解
Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统 ...
- home.php
home.php <?php error_reporting(0); //抑制所有错误信息 @header("content-Type: text/html; charset=utf- ...
- 百度地图API 根据地址查询经纬度
html页面.引用上API: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> ...
- Bajtman i Okrągły Robin
Bajtman i Okrągły Robin 题目描述 你是一个保安,你发现有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i] ...
- windows下Call to undefined function curl_init() error问题
本地项目中使用到curl_init()时出现Call to undefined function curl_init()的错误,去掉php.ini中的extension=php_curl.dll前的分 ...