422. Length of Last Word【LintCode java】
Description
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
.
A word is defined as a character sequence consists of non-space characters only.
Example
Given s = "Hello World"
, return 5
.
解题:返回最后一个单词,注意是要用一个变量缓冲一下,否则一个字符串的最后几个字符为空时,可能会丢失数据。代码如下:
public class Solution {
/**
* @param s: A string
* @return: the length of last word
*/
public int lengthOfLastWord(String s) {
// write your code here
int count = 0;
int res = 0;
for(int i = 0; i < s.length(); i++){
if( Character.isLetter(s.charAt(i)) ){
count++;
}else {
res = count;
count = 0;
}
}
if(count != 0)
return count;
else
return res;
}
}
422. Length of Last Word【LintCode java】的更多相关文章
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 407. Plus One【LintCode java】
Description Given a non-negative number represented as an array of digits, plus one to the number. T ...
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
随机推荐
- 理解numpy exp函数
exp,高等数学里以自然常数e为底的指数函数 Exp:返回e的n次方,e是一个常数为2.71828 Exp 函数 返回 e(自然对数的底)的幂次方. a = 1 print np.exp(a) a ...
- zookeeper启动时报错:Error contacting service. It is probably not running问题
查看zookeeper.out发现启动日志报错未找到java路径. 启动日志位于zookeeper-4.0.10/bin目录下 修改/etc/profile中环境变量得以解决.
- 微服务前端开发框架React-Admin
前言 React-Admin是基于React16.x.Ant Design3.x的管理系统架构. 采用前后端分离,内置了许多管理系统常用功能,通过一些脚本.封装帮助开发人员快速开发管理系统,集中精力处 ...
- DBA手记-BBED 的说明
在10g中连接生成bbed:cd $ORACLE_HOME/rdbms/libmake -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed 11g中缺省未提供BBE ...
- c# Reverse()的两点用法
Rervese的基本用途是:反转数组中元素的顺序,常见的两种用法如下: 1.void Array.Reverse(Array array) static void Main(string[] args ...
- linux 学习第十二天(网络会话connection、bond、ssh配置)
一.网络会话 使用 con-name 参数指定公司所使用的网络会话名称company,然后依次用ifname 参 数指定本机的网卡名称,用autoconnect no 参数设置该网络会话默认不被自动激 ...
- [已解决]Vistual Stdio 2015 installer Bootstrapper Packages 路径
VS2015 installer 的预装包的地址变更成 C:\Program Files (x86)\Microsoft Visual Studio 14.0\SDK\Bootstrapper\Pac ...
- TP5.0中多图上传文件名重复问题
最近在做项目的时候出现了一个问题,这里记录一下: 问题: 使用TP5.0框架自带的文件上传方法后,发现多图上传可能会出现文件名重复的问题. 问题代码: 找到TP5框架上传文件命名方法,/thinkph ...
- 数据结构09—— 并查集(Union-Find)
一.关于并查集 并查集(Union-Find)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.并查集(Union-Find)从名字可以看出,主要它涉及两种 ...
- usb驱动之打印usb设备信息(一)
1. 定义usb支持的设备类型: static const struct usb_device_id mouse_table[] = { { USB_INTERFACE_INFO(USB_INTERF ...