Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Number Theory
- Data Structure
- geometry
- Math
- database
- simulation
- sorting
- greedy
- Binary Search
- 파이썬
- 자바
- Binary Tree
- dynamic programming
- Class
- Matrix
- implement
- 코테
- SQL
- string
- 코딩테스트
- Stack
- bit manipulation
- array
- Tree
- Method
- java
- 구현
- two pointers
- hash table
- Counting
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 520. Detect Capital 본문
1. Input
1) 문자열 word
2. Output
1) 대문자 사용이 올바를 경우 true, 아니면 false 반환
2) 대문자는 다음과 같이 사용했을 때 바르게 사용했다고 할 수 있다.
- “TUE”처럼 단어의 모든 글자가 대문자인 경우
- “gift”처럼 단어의 모든 글자가 소문자인 경우
- “Korea”처럼 단어의 앞 글자만 대문자인 경우
3. Constraint
1) 1 <= word.length <= 100
2) word는 영어 대소문자로만 이루어져 있다.
4. Example
Input: word=“phoNe” -> Output: false
5. Code
1) 첫 코드(2022/07/13)
if(word.length() == 1)
return true;
boolean isRight = true;
if(word.charAt(0)<=90){ // 대문자
if(word.charAt(1)<=90){ // 모두 대문자
for(int i=2 ; i<word.length() ; i++)
if(word.charAt(i)>=97){
isRight = false; break;
}
}
else{ // 나머지 모두 소문자
for(int i=2 ; i<word.length() ; i++)
if(word.charAt(i)<=90){
isRight = false; break;
}
}
} else{ // 소문자 시작
for(int i=1 ; i<word.length() ; i++)
if(word.charAt(i)<=90){
isRight = false; break;
}
}
return isRight;
2) 코드 줄 수를 줄여봄(2022/12/02)
boolean answer = false;
if(word.matches("[a-z]+")) answer = true;
else if(word.matches("[A-Z]+")) answer = true;
else if(word.charAt(0)<=90 && word.substring(1).matches("[a-z]+")) answer = true;
return answer;
- 1번이 더 빠르고 공간 절약도 많이 된다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 561. Array Partition (0) | 2022.12.02 |
---|---|
[LeetCode/Easy] 557. Reverse Words in a String III (0) | 2022.12.02 |
[LeetCode/Easy] 509. Fibonacci Number (0) | 2022.12.01 |
[LeetCode/Easy] 507. Perfect Number (0) | 2022.12.01 |
[LeetCode/Easy] 14. Longest Common Prefix (0) | 2022.11.30 |