- 浏览: 18995 次
- 性别:
- 来自: 北京
文章分类
最新评论
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.
解题思路
本题是一个字符串相关的题。首先想到的是利用String自带的split方法,然后输出最后一个String的长度。还是需要注意的是一些特殊情况,比如有多个" "组成的字符串。
相关知识点
(1)String中查找一个字符
自己的代码
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)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 397题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 282题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 418题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 300题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 345题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 321题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 343题目描述 Determine whether an integ ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 306题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 255题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 292题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 230题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 254题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 316题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 334题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
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 ...
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-...
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刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome ...Length of Last Word,字符串处理.细节题 Rever
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 ...
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 听说 LeetCode 添加了 Rust 支持, 这岂不是双倍的快(fu)乐(za)? 于是来体验一下 用 Rust 写题真是酸爽无比啊! (各种意义上的) 刷了几天的感受 ...length_of_last_word -- --nocapture
last 返回sequence最后一个值 sequence?reverse 反转sequence的值 sequence?size 返回sequence的大小 sequence?sort 对sequence按里面的对象toString()的结果进行排序 sequence?sort_by(value) 对sequence 按...
outSPSS.addDateVar("update", DataConstants.DATE_TYPE_17, "Date of last update", null); // Create value labels ValueLabels valueLabels = new ValueLabels(); valueLabels.putLabel("Asia", "Asia ...
最后一个单词的长度](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...
1. 题目 给定一个仅包含大小写字母和空格...链接:https://leetcode-cn.com/problems/length-of-last-word 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 2. 解题 坑点:"a ", 1 class Soluti
- 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, ...
(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入门 一、 数据库概述 数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,它产生于距今五十年前。简单来说是本身可视...
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...
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 ...
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 ...
(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<=t.length&&73;===t[e]&&68;===t[e+1]&&51;==...
Tricks of the Windows video Game Programming <br>PART I Windows Programming Foundations 7 1 Journey into the Abyss 9 A Little History.............................................................