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
- two pointers
- string
- Binary Search
- Matrix
- hash table
- Counting
- Stack
- array
- 자바
- 코딩테스트
- greedy
- Data Structure
- Tree
- Binary Tree
- bit manipulation
- database
- 코테
- Number Theory
- sorting
- implement
- dynamic programming
- Method
- 파이썬
- 구현
- SQL
- geometry
- Class
- simulation
- java
- Math
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1816. Truncate Sentence 본문
1. Input
1) 문장을 담은 문자열 s
- s는 단어들 사이에 공백 문자가 1개 있다.
2) 정수 k
2. Output
1) s에 있는 단어들 중 앞 k개만 담아서 문자열로 반환
3. Constraint
1) 1 <= s.length <= 500
2) k의 범위는 [1, s의 단어 수]이다.
3) s는 영어 대소문자와 공백 문자로 이루어져 있다.
4) 공백 문자는 단어 사이에 1개씩 있고, 불필요한 공백 문자는 존재하지 않는다.
4. Example
Input: s = "Hello how are you Contestant", k = 4 -> Output: "Hello how are you“
설명: s에는 ["Hello", "how" "are", "you", "Contestant"]의 단어 5개가 들어있고, k가 4이므로 앞에서부터 4개 단어만 담아서 "Hello how are you“를 반환한다.
5. Code
1) 첫 코드(2022/06/07)
String[] words = s.split(" ");
String result = "";
for(int i=0 ; i<k ; i++)
result += words[i] + " ";
return result.substring(0, result.length()-1);
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1822. Sign of the Product of an Array (0) | 2023.01.05 |
---|---|
[LeetCode/Easy] 20. Valid Parentheses (0) | 2023.01.05 |
[LeetCode/Easy] 1812. Determine Color of a Chessboard Square (0) | 2023.01.04 |
[LeetCode/Easy] 1791. Find Center of Star Graph (0) | 2023.01.04 |
[LeetCode/Easy] 1773. Count Items Matching a Rule (0) | 2023.01.04 |