`
淡淡的一抹
  • 浏览: 18995 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.

解题思路
本题是一个字符串相关的题。首先想到的是利用String自带的split方法,然后输出最后一个String的长度。还是需要注意的是一些特殊情况,比如有多个" "组成的字符串。

相关知识点
(1)String中查找一个字符
char c = s.charAt(i);//查找s中的第i个字符


自己的代码
package leetcode;

public class LengthOfLastWord {
	public int lengthOfLastWord(String s) {
		//处理null
		if(s == null) return 0;
		//处理空串
		if(s.length() == 0) return 0;
		//处理带空格的字符串
		boolean hasWorld = false;
		for(int i = 0; i < s.length(); i++){
			if(s.charAt(i) != ' ') {
				hasWorld = true;
				break;
			}
		}
		if(!hasWorld) return 0;
			
		String str[] = s.split(" ");
		String tempStr = str[str.length-1];
		return tempStr.length();
    }
	
	public static void main(String[] args) {
		//String s = "hello world ";
		//String s = "";
		String s = "    ";
		
		LengthOfLastWord lolw = new LengthOfLastWord();
		System.out.println(lolw.lengthOfLastWord(s));
	}
}

分享到:
评论

相关推荐

    cpp-算法精粹

    Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish Notation Implement Stack ...

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    leetcode浇花-LCSolutions:我的力扣解决方案

    Length of Last Word #0066 - Plus One #0083 - Remove Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 -...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome ...Length of Last Word,字符串处理.细节题 Rever

    kgb档案压缩console版+源码

    leading characters of y are known, the range of possible x narrows, so the leading digits can be output as they become known. For decompression, as the digits of x are read, the set of possible y ...

    TCL TK 语言8.5编程指导

    Determining the length of a string 49 Comparing strings 50 Comparing a string of characters 51 Locating the first instance of a character 52 Locating the index of a character 53 Determining the class ...

    leetcode题库-LeetCode-Rust:听说刷题建repo是立flag一般的行为?

    leetcode题库 LeetCode-Rust 听说 LeetCode 添加了 Rust 支持, 这岂不是双倍的快(fu)乐(za)? 于是来体验一下 用 Rust 写题真是酸爽无比啊! (各种意义上的) 刷了几天的感受 ...length_of_last_word -- --nocapture

    freemarker总结

    last 返回sequence最后一个值 sequence?reverse 反转sequence的值 sequence?size 返回sequence的大小 sequence?sort 对sequence按里面的对象toString()的结果进行排序 sequence?sort_by(value) 对sequence 按...

    spssw-184.zip

    outSPSS.addDateVar("update", DataConstants.DATE_TYPE_17, "Date of last update", null); // Create value labels ValueLabels valueLabels = new ValueLabels(); valueLabels.putLabel("Asia", "Asia ...

    58. 最后一个单词的长度

    最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word/)题目描述solution idea一次遍历参考文献 leetcode58:58. 最后一个单词的长度 题目描述 给定一个仅包含大小写字母和空格 ’ ’ 的字符...

    计算机网络第六版答案

    Part of protocol has always been the acknowledgment of the hierarchical standing of all present. Protocol rules are based on the principles of civility. Standards are important for protocols so that...

    LeetCode 58. 最后一个单词的长度

    1. 题目 给定一个仅包含大小写字母和空格...链接:https://leetcode-cn.com/problems/length-of-last-word 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 2. 解题 坑点:"a ", 1 class Soluti

    S7A驱动720版本

    - Problems with datablocks which have a length between 180 and 200 bytes. - Unsufficient exception handling in S7AMemory.Dll caused blocking of driver and all other clients (WSACTASK, Workspace, ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    (the last one can be overridden for the implementation of new alignment rules). - ADD: The property TFlexPanel.GridControl moved to the public section and is now available for writing (during the ...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 第一章 Oracle入门 一、 数据库概述 数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,它产生于距今五十年前。简单来说是本身可视...

    一个win32下的ARM开源编译器

    The last part of the principles, the smaller the better, may impact THUMB code where a particular alignment is required. If you need to ensure that a particular instruction is always 32-bit, or always...

    Sakemail

    that.1.8.9- Some ‘moderns‘ pop3 servers doesn‘t support the LAST command, so I‘ve added a little code to cope with this and added a boolean property ServerSupportLastCmd. See TSakPOP.Init for ...

    au3反编译源码

    Hint for best matching of includes look at the version properties of the au3.exe download/install(unpack) that version from http://www.autoitscript.com/autoit3/files/beta/autoit/ and ...

    hls.min.js

    (t instanceof e))throw new TypeError("Cannot call a class as a function")}var a=function(){function t(){i(this,t)}return t.isHeader=function(t,e){return e+10&lt;=t.length&&73;===t[e]&&68;===t[e+1]&&51;==...

    Tricks of the Windows video Game Programming---part1

    Tricks of the Windows video Game Programming &lt;br&gt;PART I Windows Programming Foundations 7 1 Journey into the Abyss 9 A Little History.............................................................

Global site tag (gtag.js) - Google Analytics