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

题目链接:https://leetcode-cn.com/problems/length-of-last-word/

题目描述:给定一个仅包含大小写字母和空格 ‘ ‘ 的字符串 s,返回其最后一个单词的长度。

如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。

如果不存在最后一个单词,请返回 0 。

说明:一个单词是指仅由字母组成、不包含任何空格的 最大子字符串。

示例:

输入: "Hello World"
输出: 5

解法一:

思路:自行解题思路,从前往后遍历(相比从后开始遍历,这个比较麻烦,多遍历很多无用的内容)。

 1 class Solution {
 2     //从前往后
 3     public int lengthOfLastWord1(String s) {
 4         int cnt = 0;
 5         int wordLen = 0;
 6         for (int i = 0; i < s.length(); i++) {
 7             String tmp = s.substring(i, i+1);
 8             if (" ".equals(tmp)) {
 9                 if (cnt > 0) {
10                     wordLen = cnt;
11                 }
12                 cnt = 0;
13             } else {
14                 cnt++;
15             }
16         }
17         if (cnt > 0) {
18             wordLen = cnt;
19         }
20         return wordLen;
21     }
22 }

解法二:

思路:内容来源于精选题解,仅把格式调整了下,便于阅读。

 1 class Solution {
 2     public int lengthOfLastWord(String s) {
 3         int end = s.length() - 1;
 4         while (end >= 0 && s.charAt(end) == ‘ ‘) {
 5             end--;
 6         }
 7         if (end < 0) {
 8             return 0;
 9         }
10         int start = end;
11         while (start >= 0 && s.charAt(start) != ‘ ‘) {
12             start--;
13         }
14         return end - start;
15     }
16 }

解法三:

思路:使用String的库函数trim()和lastIndexOf()。

 1 class Solution {
 2     public int lengthOfLastWord1(String s) {
 3         s = s.trim();
 4         if (s.length() ==0) {
 5             return 0;
 6         }
 7         int a = s.lastIndexOf(" ");
 8         return s.length() - 1 - a;
 9     }
10 }

也可以采用简写的方式,简写为如下一行。其中lastIndexOf()函数如果查询不到对应的字符会返回-1,所以当字符串s为全部空格字符的时候,结果返回为

0 - (-1) -1 = 0,符合题目要求,故可以简写为如下一行代码。

1 class Solution {
2     public int lengthOfLastWord(String s) {
3         return s.trim().length()-s.trim().lastIndexOf(" ")-1;
4     }
5 }

原文地址:https://www.cnblogs.com/achilleskwok/p/12227964.html

时间: 2024-07-29 21:42:36

【LeetCode】58. 最后一个单词的长度的相关文章

leetcode 58. 最后一个单词的长度(Length of Last Word)

目录 题目描述: 示例: 解法: 题目描述: 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: "Hello World" 输出: 5 解法: class Solution { public: int lengthOfLastWord(string s) { int sz = s.size(); int i = 0, j = 0; string

leetcode——58. 最后一个单词的长度

class Solution: def lengthOfLastWord(self, s: str) -> int: if s==' '*len(s): return 0 if ' ' not in s: return len(s) a=s.split(' ') i=-1 while a[i]=='': i-=1 return len(a[i]) 执行用时 :44 ms, 在所有 python3 提交中击败了78.73%的用户 内存消耗 :13.6 MB, 在所有 python3 提交中击败了5

58. 最后一个单词的长度

给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: "Hello World"输出: 5 1/** 2 * @param  {string} s 3 * @return {number} 4 */ 5var lengthOfLastWord = function(s) { 6    if(!s.includes(' ')) return s.len

leetcode—58 Length of Last Word Total(字符串中最后一个单词的长度)

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 cha

【LeetCode】- 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. 给你一个字符串,设法获取它最后一个单词的长度.如果这个单词不存在,则返回0. [ 分析 : ] A word is defined

[Leetcode] 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 char

Leetcode(无重复字符的最长子串;删除排序链表中的重复元素II;加一;最后一个单词的长度;相同的树)

1.无重复字符的最长子串 这题需要用到滑动窗口法,有许多问题都可以考虑使用滑动窗口法:https://www.geeksforgeeks.org/tag/sliding-window/ 因为用c++,所以用到set容器:std::count 2.删除排序链表中的重复元素II 3.加一 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int>& digits) { 4 int n=digits.size()-

【华为练习题】 最后一个单词的长度(初级)

[华为练习题] 最后一个单词的长度(初级) 题目 描述: 计算字符串最后一个单词的长度,单词以空格隔开. 题目类别: 字符串 输入: 一行字符串,长度小于128. 输出: 整数N,最后一个单词的长度. 样例输入: hello world 样例输出: 5 解答 #include <iostream> #include <string> using namespace std; int last_word_length(const string &s){ auto begin

华为OJ:2290 字符串最后一个单词的长度

用JAVA就很简单,只要用spilt函数,再输出最后一个字符串. 题意是要求你先自己写分隔好字符串这样子.有个比较坑的地方就是测试用例应该有个全为空的,要注意. import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner input=new Scanner(System.in); String s=input.nextLine(); String ss[]=s.spli