1. 程式人生 > >Length of Last Word(返回最後一個字母的長度)

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.

思路:

返回最後一個單詞的長度,從後遍歷整個字串,遇到空格跳出,不遇到空格,變數加1.

程式碼實現:

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len=s.length();
        int i=len-1;
        int j=0;
        while(i>=0 && s[i]==' ')
        {
            i--;
        }
        while(i>=0 && s[i]!=' ')
        {
            i--;
            j++;
        }
        return j;
    }
};

相關推薦

Length of Last Word(返回最後一個字母長度)

題目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret

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 s

LeetCode 58. Length of Last Word最後一個單詞的長度

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t

LeetCode 58 Length of Last Word最後單詞的長度

翻譯 給定一個包含大小寫字母和空格字元的字串, 返回該字串中最後一個單詞的長度。 如果最後一個單詞不存在,返回0。 批註: 一個單詞被定義為包含非空字元的字元序列。 例如, 給定 S = "H

[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 d

[Leetcode] Length of last word 最後一個單詞的長度

== length spa ast class define 跳過 要求 怎麽辦 Given a string s consists of upper/lower-case alphabets and empty space characters‘ ‘, return th

length-of-last-word 最後一個單詞的長度

pac char space rac class strong 順序 character string Given a string s consists of upper/lower-case alphabets and empty space characters‘ ‘

leetcode-58 length-of-last-word(最後一個單詞的長度

這道題讓我和難受,我的程式碼醜陋不說,還沒有通過。先看一下題目描述: 從題目描述看確實很簡單,先看一下我的程式碼: 1 public static int lengthOfLastWord(String s) { 2 if (s.length() == 0) { 3

LeetCode 58. 最後一個單詞的長度 Length of Last Word(C語言)

題目描述: 給定一個僅包含大小寫字母和空格 ’ ’ 的字串,返回其最後一個單詞的長度。 如果不存在最後一個單詞,請返回 0 。 說明:一個單詞是指由字母組成,但不包含任何空格的字串。 示例: 輸入: “Hello World” 輸出: 5 題目解答: 方

leetcode 58 Length of Last Word(最後一個單詞長度)

題目要求: 給一個包含大寫或者小寫以及空格的字串,返回這個串最後一個單詞的長度。如果最後一個的單詞不存在,那麼返回0。 注意:一個單詞意味著,連續字母之間沒有空格。 示例: Example 1 Input : "Hello World" Output : 5

LeetCode 最後一個單詞的長度Length of Last Word

題目 給定一個僅包含大小寫字母和空格 ’ ’ 的字串,返回其最後一個單詞的長度。 如果不存在最後一個單詞,請返回 0 。 說明:一個單詞是指由字母組成,但不包含任何空格的字串。 描述 示例:

leetcode 58. Length of Last Word(C語言,計算最後一個單詞的長度)19

貼原題: Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, r

Leetcode 58 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.

Leetcode演算法——58、最後單詞的長度length of last word

給定一個字串,包含大小寫字母和空格。返回字串中最後一個單詞的長度。 如果最後一個單詞不存在,返回0。 備註: 一個單詞定義為不包含空格的字元序列。 示例: Input: "Hello World" Output: 5 思路 從後向前,尋找到第一個出現的非空格。

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 d

LeetCode Length of Last Word

archive n-1 art fine ive lan ets hello style 1. 題目Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘

LeetCode --- 58. Length of Last Word

res ase span mon n) 時間復雜度 ace dsm courier Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, r

58. Length of Last Word

|| int char last span ret pre rim length 求最後一個單詞的長度 Java 1: 1 class Solution { 2 public int lengthOfLastWord(String s) { 3

LeetCode 58. Length of Last Word

per 這樣的 int last -c efi empty ets != Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the l

LeetCode-58. Length of Last Word

hello pos 組成 amp 第一個 == span spa code 一、問題描述   給定一個字符串s,s是由多個單詞組成,單詞之間以‘ ’分開,找出最後一個單詞的長度。如果沒有最後單詞則返回0。 二、問題描述   先找到最後一個單詞的末尾,因為s可能以空格結尾,所