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 |
Tags
- Tree
- array
- geometry
- sorting
- bit manipulation
- hash table
- greedy
- Counting
- simulation
- Number Theory
- Binary Search
- SQL
- 파이썬
- 구현
- 자바
- Data Structure
- 코딩테스트
- Class
- implement
- Method
- Binary Tree
- two pointers
- string
- 코테
- Matrix
- java
- Math
- dynamic programming
- database
- Stack
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2490. Circular Sentence 본문
1. Input
1) String sentence
2. Output
1) sentence의 단어들이 순서대로 끝말잇기로 연결된다면 true, 아니면 false
- 단어는 공백문자 1개로 구분되어 있다.
- 가장 마지막 단어와 첫 단어도 연결되어야 한다.
3. Constraint
1) 1 <= sentence.length <= 500
2) sentence는 영어 대소문자와 공백문자로 이루어져 있다.
3) 불필요한 공백은 없다.
4. Example
Input: sentence = "leetcode exercises sound delightful" -> Output: true
Input: sentence = "Leetcode is cool" -> Output: false
5. Code
1) 첫 코드(2023/05/05)
class Solution {
public boolean isCircularSentence(String sentence) {
String[] word = sentence.split(" ");
boolean answer = true;
int n = word.length;
for(int i=0 ; i<n-1 && answer; i++)
if(word[i].charAt(word[i].length()-1)!=word[i+1].charAt(0))
answer = false;
return answer && word[n-1].charAt(word[n-1].length()-1)==word[0].charAt(0);
}
}
2) 힌트를 보고 split() 없이 해본 코드(2023/05/05)
class Solution {
public boolean isCircularSentence(String sentence) {
boolean answer = true;
for(int i=1 ; i<sentence.length()-1 && answer ; i++)
if(sentence.charAt(i)==' ')
if(sentence.charAt(i-1)!=sentence.charAt(i+1))
answer = false;
return answer? sentence.charAt(0)==sentence.charAt(sentence.length()-1) : false;
}
}
- 실행 시간은 같았고, 메모리 사용량이 확 줄었다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2500. Delete Greatest Value in Each Row (0) | 2023.05.05 |
---|---|
[LeetCode/Easy] 2496. Maximum Value of a String in an Array (0) | 2023.05.05 |
[프로그래머스/Lv.0] 옹알이 (1) (0) | 2023.05.04 |
[프로그래머스/Lv.0] 그림 확대 (0) | 2023.05.04 |
[LeetCode/Easy] 2485. Find the Pivot Integer (0) | 2023.05.04 |