코딩테스트 풀이/JAVA
[LeetCode/Easy] 2042. Check if Numbers Are Ascending in a Sentence
무지맘
2023. 1. 12. 08:54
1. Input
1) 문자열 s
- s의 토큰들은 공백 문자 1개로 나누어져 있다.
2. Output
1) s에 있는 수들이 오름차순으로 들어있다면 true, 아니면 false를 반환
3. Constraint
1) 3 <= s.length <= 200
2) s는 영어 소문자, 공백 문자, 숫자로 이루어져 있다.
3) s의 토큰의 수는 [2, 100]개이다.
4) s에 있는 수들은 [1, 99]의 자연수이다.
4. Example
Input: s = "hello world 5 x 5" -> Output: false
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles" -> Output: true
설명:
- 5, 5는 오름차순이 아니다.
- 1, 3, 4, 6, 12는 오름차순이다.
5. Code
1) 첫 코드(2022/06/23)
String[] narr = s.split("[a-z\\s]+");
for(int i=0 ; i<narr.length-1 ; i++){
if(narr[i] == "") continue;
if(Integer.parseInt(narr[i]) >= Integer.parseInt(narr[i+1]))
return false;
}
return true;