1. 程式人生 > >58. Length of Last Word [easy] (Python)

58. Length of Last Word [easy] (Python)

題目連結

題目原文

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= “Hello World”,返回 5

思路方法

思路一

利用Python的內建函式string.rstrip()和string.split()。先將字串後面的空格部分刪除,再按照空格字元將剩餘部分分成若干部分,此時最後一部分即為最後一個單詞(也可能是”),直接返回其長度即可。

程式碼

class Solution(object):
    def lengthOfLastWord
(self, s):
""" :type s: str :rtype: int """ return len(s.rstrip().split(' ')[-1])

思路二

與上面類似,不過不再用內建string處理函數了,在刪除後面的空格後,從後面開始數非空格字元的個數,即為所求。

程式碼

class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
length, j = 0, len(s)-1 while j>=0: if s[j] != ' ': break j = j - 1 for i in xrange(j, -1, -1): if s[i] == ' ': return length length = length + 1 return length

相關推薦

58. Length of Last Word [easy] (Python)

題目連結 題目原文 Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of las

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可能以空格結尾,所

LeeCode from 0 ——58. Length of Last Word

n-1 循環 from num defined pty cte def str 58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space c

#Leetcode# 58. Length of Last Word

https://leetcode.com/problems/length-of-last-word/description/   Given a string s consists of upper/lower-case alphabets and empty space c

LeetCode(58)-Length of Last Word

58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last

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

思路:可以倒著找非空格字元,並用變數count統計連續非空字元數量。 注意考慮到字串全為空格的情況。 class Solution { public: int lengthOfLastWord(string s) { if(s == ""){

LeetCode算法系列:58. Length of Last Word

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

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

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

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。 批註: 一個單詞被定義為包含非空字元的字元序列。 例如, 給定 S = "H

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 t

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.

(C++練習) 58. Length of Last Word

NPU upper pty style The world per 字串 lower 題目 : Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, r

【leetcode】58. (EasyLength of Last Word

提交程式碼: class Solution { public int lengthOfLastWord(String s) { int len=0,p=s.length()-1; while(p>=0&&s.charAt(p)

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

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