58. Length of Last Word【leetcode】
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.
public class Solution {
public int lengthOfLastWord(String s) {
int count=0;
int len=s.length();
char [] sc=s.toCharArray();
for(int i=len-1;i>=0;i--){
if(len==0){
return 0;
}
else{
if(sc[i]==' '){
if(count>0){
return count;
}
else{
continue;
}
}
else{
count++;
}
}
}
return count;
}
}
解题思路:
首先这个题的意思是寻找一个字符串中最后一个词,就是说如果最后一个词的前后的空格都去除,取这个词的长度
特殊情况:空字符串,全是空字符,末尾有空字符+普通字符串四中情况
58. Length of Last Word【leetcode】的更多相关文章
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
随机推荐
- php变量双击选择无法选择$符号
创建/Data/Packages/User/PHP.sublime-settings文件,内容为 { "word_separators": "./\\()\&qu ...
- [leetcode-567-Permutation in String]
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 【Android Developers Training】 91. 解决云储存冲突
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 同网段电脑互ping
两台同网段的主机(host)之间的网络通信是不经过网关的. 今天试了一下,用一根网线连接两台电脑,然后 在一台电脑上设置: ip地址:192.168.0.1 子网掩码:255.255.255.0 在另 ...
- Hadoop启动方式
启动方式 1.逐一启动 hdfs hadoop-daemon.sh start|stop namenode|datanode|secondrynamenode yarn yarn-daemon.sh ...
- opencv如何实现【不用全局变量进行滚动条控制】
opencv中自带滚动条,其中一个问题是该回调函数(on_trackbar)大多使用的是全局变量,大型项目调试时弊端众多,比如下图: 为此,留意了void on_Trackbar(int par1, ...
- Linux学习笔记(二)——文件/目录/VIM
文件和目录管理 及 VI编辑器的使用 文件和目录管理,刚开始学这块的时候感觉内容很多很杂,但是学完进行总结后,发现其实很有条理的而且没什么难度,只是熟练掌握这些常用的命令就行了.至于Vim编辑器,不得 ...
- 打造属于自己的支持版本迭代的Asp.Net Web Api Route
在目前的主流架构中,我们越来越多的看到web Api的存在,小巧,灵活,基于Http协议,使它在越来越多的微服务项目或者移动项目充当很好的service endpoint. 问题 以Asp.Net W ...
- Centos7架设NMP服务器笔记
安装centos7.3 1.从mirrors.163.com下载7.3 2.准备虚拟机vitualbox,网络我使用的桥接到无线网卡,直接连我到路由器,IP自动分配(本来想搞静态IP的,搞了好久没成功 ...
- Chromium模块和进程模型
i. Chromium基本模块 Chromium各模块层级图a) Chromium主要包括如下模块: WebKit: Safari和Chromium,以及任何其他基于WebKit内核的浏览器所共享的 ...