- 浏览: 183562 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
首先检查s是否为空,如果为空直接返回0。如果不为空, 将s装换为一个字符数组,然后从后往前遍历字符数组,如果是空字符就继续执行,直到遇到第一个不为空的字符,用count记录,如果再遇到空的字符,这时就终止程序,返回count的值。其中要注意数组的边界,在记录count值得时候不要越界。代码如下:
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.
首先检查s是否为空,如果为空直接返回0。如果不为空, 将s装换为一个字符数组,然后从后往前遍历字符数组,如果是空字符就继续执行,直到遇到第一个不为空的字符,用count记录,如果再遇到空的字符,这时就终止程序,返回count的值。其中要注意数组的边界,在记录count值得时候不要越界。代码如下:
public class Solution { public int lengthOfLastWord(String s) { if(s == null || s.length() == 0) return 0; char[] c = s.toCharArray(); int count = 0; int flag = 1; for(int i = c.length - 1; i >= 0; i--) { if(flag == 0) break; if(c[i] == ' ') continue; while(i >= 0 && c[i] != ' ') { count ++; i --; } flag = 0; } return count; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
Console.WriteLine($"The length of the last word is: {lastWordLength}"); } } ``` 这段代码首先移除了输入字符串两端的空格,然后使用`Split(' ')`方法将字符串拆分为单词数组。如果数组为空(即输入字符串只...
js js_leetcode题解之58-length-of-last-word.js
在本压缩包中,我们关注的是C++编程基础与LeetCode题目的结合,特别是针对第58题“最后一个单词的长度”(Length of Last Word)的解决方案。LeetCode是一个在线平台,提供各种算法题目,旨在提升程序员的编程技能和...
c语言入门 C语言_leetcode题解之58-length-of-last-word.c
- Length of Last Word(最后一个单词的长度) - Count and Say(猜数字序列) - **Integer Array**(整型数组操作) - Remove Element(移除元素) - Zero Sum Subarray(连续子数组的最大和) - Subarray Sum...
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 -...
Length of Last Word **知识点:** - **问题描述:** - 给定一个字符串,返回最后一个单词的长度。 - **解决方案分析:** - **双指针:** - 从字符串末尾开始遍历,找到第一个非空格字符 `end`。 - 从 `end`...
leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome ...Length of Last Word,字符串处理.细节题 Rever
var last_three_letters = the_word.substring(the_word.length - 3, the_word.length); // "key" ``` `substring`与`charAt`不同,它能获取多个连续的字符。 除了这些基础方法,JavaScript还提供了其他字符串操作...
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 ...
var the_last_letter = the_word.charAt(the_word.length - 1); // "y" ``` 注意,字符串的索引是从0开始的,因此最后一个字符的索引是`length - 1`。 `substring()`方法用于提取字符串的一部分,它接受两个参数,...
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. 最后一个单词的长度 题目描述 给定一个仅包含大小写字母和空格 ’ ’ 的字符...
时间相关的过渡词如 "after a while", "afterward", "again", "also", "at last", "at length", "at that time", "before", "besides", "earlier", "eventually", "finally", "formerly", "further", "furthermore",...