๋ฌธ์
์ฝ๋
class Solution {
public:
int lengthOfLastWord(string s) {
vector<string>parsing;
string temp;
for(int i=0; i<s.length()+1; i++){
if(s[i]!=' '&&s[i]!='\0') temp+=s[i];
else{
parsing.push_back(temp);
temp.clear();
}
}
reverse(parsing.begin(),parsing.end());
for(string s:parsing){
if(s.length()!=0) return s.length();
}
return 0;
}
};
ํ์ด
๋์ด์ฐ๊ธฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ฅธ ๋จ์ด๋ค์ parsing ๋ฒกํฐ์ ๋ฃ์ด์ฃผ๊ณ ๋ฒกํฐ์ ๋ง์ง๋ง ์ธ๋ฑ์ค์ ๋ฌธ์์ด์ ์ถ๋ ฅํ๋๋ฐ " fly me to the moon " ์ ๊ฐ์ด ๋งจ ๋ค๊ฐ ๋์ด์ง ์ฑ๋ก ์์ํ๋ input๊ฐ์ด ๋ค์ด์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 0 ์ด์์ธ ๋งจ ๋ง์ง๋ง ์ธ๋ฑ์ค์ ๋ฌธ์์ด์ ์ถ๋ ฅํ์ฌ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Find First and Last Position of Element in Sorted Array (0) | 2021.11.14 |
---|---|
[Leetcode,c++] Group Anagrams (0) | 2021.11.14 |
[Leetcode,c++] Next Permutation (0) | 2021.11.14 |
[Leetcode,c++] Container With Most Water (0) | 2021.11.14 |
[Leetcode,c++] Remove Duplicates from Sorted Array (0) | 2021.11.14 |
[Leetcode,c++] Longest Common Prefix (0) | 2021.11.11 |
๋๊ธ