반응형
문제
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.
예제 1
Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5.
예제 2
Input: s = " fly me to the moon " Output: 4 Explanation: The last word is "moon" with length 4.
예제 3
Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6.
제약조건
✅ 1 <= s.length <= 104
✅ s consists of only English letters and spaces ' '.
✅ There will be at least one word in s.
문제풀이
class Solution {
public int lengthOfLastWord(String s) {
// 1. 변수 선언 및 초기화
String[] wordArr = s.split(" ");
String lastWord = wordArr[wordArr.length - 1];
// 2. 반환
return lastWord.length();
}
}
- 그저 String.split() 함수를 알고 있는지 물어보는 문제 같다. 다시 말해 이번 문제도 생각할 요소는 크게 없었다.
마치며
요즘 상황도 머리도 복잡해서 간단한 문제만 풀고 있는데, 상황이 마무리되면 백준도 병행할 예정이다.
앞으로 개인 프로젝트, 포트폴리오, 공부는 어떻게 할 지 계획이 다 되어 있는데 팀 프로젝트는 어떻게 구해야 할지 고민이다. 🙄
이번에도 그냥 독고다이로 가야 하나 ㅠㅠ
요즘 취업시장 많이 어려운데 취업, 이직 준비하시는 분들 모두 파이팅 하셨으면 좋겠다. 끝까지 살아남는 놈이 이기는 거래요.
이미지 출처
[김은우의 에듀테크 트렌드 따라잡기] 코딩 교육 사이트 LeetCode가 보여주는 코딩교육의 핵심
[김은우의 에듀테크 트렌드 따라잡기] 코딩 교육 사이트 LeetCode가 보여주는 코딩교육의 핵심
edu.chosun.com
'개발 > 코딩테스트' 카테고리의 다른 글
[LeetCode] 67. Add Binary #Easy #Java (3) | 2025.03.01 |
---|---|
[LeetCode] 66. Plus One #Easy #Java (1) | 2025.02.26 |
[LeetCode] 35. Search Insert Position #Easy #Java (2) | 2025.02.24 |
[LeetCode] 28. Find the Index of the First Occurrence in a String #Easy #Java (2) | 2025.02.23 |
[LeetCode] 27. Remove Element #Easy #Java (1) | 2025.02.23 |