조컴퓨터

58. Length of Last Word 본문

LeetCode/Algorithms

58. Length of Last Word

챠오위 2021. 10. 28. 23:58

 

Input 된 글자의 끝으로 간다.

for문을 돌려서 띄어쓰기가 없는 곳으로 간다.

띄어쓰기가 있을 때까지 count++ 

 

class Solution {
    public int lengthOfLastWord(String s) {
        int count = 0;
        
        for( int i=s.length()-1; i>=0; i-- ) {
            
            if( s.charAt(i)!=' ' ) {
                count++;
            } else if( count!=0 ) {
                break;
            }
            
        }
        return count;
    }
}

 

 

 

'LeetCode > Algorithms' 카테고리의 다른 글

*67. Add Binary  (0) 2021.11.02
66. Plus One  (0) 2021.11.02
53. Maximum Subarray  (0) 2021.10.27
35. Search Insert Position  (0) 2021.10.20
28. Implement strStr()  (0) 2021.10.20