• 문제 링크
Length of Last Word - LeetCode
Can you solve this real interview question? Length of Last Word - Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Example 1: Input:
leetcode.com
• 풀이 코드
public class Solution {
public int lengthOfLastWord(String s) {
int i = s.length() - 1;
while (i >= 0 && s.charAt(i) == ' ') i--;
int j = i;
while (j >= 0 && s.charAt(j) != ' ') j--;
return i - j;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 67. Add Binary - Java (0) | 2026.05.21 |
|---|---|
| [LeetCode] 66. Plus One - Java (0) | 2026.05.20 |
| [LeetCode] 35. Search Insert Position - Java (0) | 2026.05.18 |
| [LeetCode] 24. Swap Nodes in Pairs - Java (0) | 2026.05.17 |
| [LeetCode] 28. Find the Index of the First Occurrence in a String - Java (0) | 2026.05.16 |
댓글