LeetCode Day46 length-of-last-word
去掉末尾的空格,計算最後一個單詞的長度
class Solution {
public:
int lengthOfLastWord(string s) {
if(s.empty()) return {};
int n=s.size()-1;
for(;s[n]==' ';n--);
cout<<n;
for(int i=n;i>=0;i--){
if(s[i]==' ') return (n-i);
}
return n+ 1;
}
};
相關推薦
LeetCode Day46 length-of-last-word
去掉末尾的空格,計算最後一個單詞的長度 class Solution { public: int lengthOfLastWord(string s) { if(s.empty()) return {}; int n=s.size()-1;
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
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可能以空格結尾,所
#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】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
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: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(最後單詞的長度)
翻譯 給定一個包含大小寫字母和空格字元的字串, 返回該字串中最後一個單詞的長度。 如果最後一個單詞不存在,返回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.
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] 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
[leetcode] length-of-last-word
example tco length lower == desc scribe urn [] 題目描述 Given a string s consists of upper/lower-case alphabets and empty space chara
【演算法】LeetCode演算法題-Length Of Last Word
這是悅樂書的第155次更新,第157篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第14題(順位題號是58)。給定一個字串,包含戴爾字母、小寫字母和空格,返回最後一個單詞的長度,如果最後一個單詞不存在則返回0。另外,單詞不包含空格。例如: