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
- Matrix
- geometry
- dynamic programming
- Class
- Stack
- Number Theory
- bit manipulation
- SQL
- java
- hash table
- Tree
- two pointers
- array
- 파이썬
- Counting
- Binary Tree
- implement
- 구현
- sorting
- 자바
- string
- Math
- greedy
- 코테
- Data Structure
- 코딩테스트
- Method
- database
- simulation
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 14. Longest Common Prefix 본문
1. Input
1) 단어가 담긴 문자열 배열 strs
2. Output
1) strs의 단어들의 공통 접두사 중 가장 긴 것을 담은 문자열
3. Constraint
1) 1 <= strs.length <= 200
2) 0 <= strs[i].length <= 200
3) strs[i]는 영어 소문자로만 이루어져있다.
4. Example
Input: strs={"flower","flow","flight"} -> Output: “fl”
5. Code
1) 첫 코드(2022/11/30)
import java.util.*;
Arrays.sort(strs);
String prefix = strs[0];
for(int i=1 ; i<strs.length ; i++){
for(int j=0 ; j<prefix.length() ; j++){
if(prefix.charAt(j)!=strs[i].charAt(j)){
prefix = prefix.substring(0,j);
break;
}
} // for j
if(prefix.equals(""))
break;
} // for i
return prefix;
- sort()없이 푸는 방법도 있을 것 같은데, 당장은 생각이 나지 않는다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 509. Fibonacci Number (0) | 2022.12.01 |
---|---|
[LeetCode/Easy] 507. Perfect Number (0) | 2022.12.01 |
[LeetCode/Easy] 504. Base 7 (0) | 2022.11.30 |
[LeetCode/Easy] 500. Keyboard Row (0) | 2022.11.30 |
[LeetCode/Easy] 476. Number Complement (0) | 2022.11.30 |