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
- geometry
- Binary Tree
- simulation
- implement
- array
- dynamic programming
- 코테
- Tree
- database
- Data Structure
- two pointers
- Matrix
- Stack
- greedy
- Number Theory
- Counting
- hash table
- Math
- sorting
- 구현
- string
- SQL
- 자바
- 파이썬
- java
- bit manipulation
- Method
- Class
- 코딩테스트
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 657. Robot Return to Origin 본문
1. Input
1) 문자열 moves
2) moves에는 상, 하, 좌, 우 4방향으로 각 1칸씩 움직이라는 명령어인 'U', 'D', 'L', 'R'이 담겨져있다.
2. Output
1) (0, 0)에 있는 로봇이 moves를 따라 움직였을 때 다시 (0, 0)으로 돌아왔다면 true, 아니라면 false를 반환
3. Constraint
1) 1 <= moves.length <= 2 * 104
2) moves에는 'U', 'D', 'L', 'R' 외의 다른 문자는 없다.
4. Example
Input: moves=“LL” -> Output: false
설명: 왼쪽으로 2번 움직이면 (-2, 0)이 되므로 원점으로 돌아오지 않아 false를 반환한다.
5. Code
1) 첫 코드(2022/06/16)
int[] counts = new int[2];
for(int i=0 ; i<moves.length() ; i++){
if(moves.charAt(i) == 'U') counts[0]++;
else if(moves.charAt(i) == 'D') counts[0]--;
else if(moves.charAt(i) == 'L') counts[1]++;
else counts[1]--;
}
return counts[0]==0 && counts[1]==0;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 709. To Lower Case (0) | 2022.12.05 |
---|---|
[LeetCode/Easy] 693. Binary Number with Alternating Bits (0) | 2022.12.05 |
[LeetCode/Easy] 598. Range Addition II (0) | 2022.12.04 |
[LeetCode/Easy] 566. Reshape the Matrix (0) | 2022.12.04 |
[프로그래머스/Lv.2] 점프와 순간 이동 (0) | 2022.12.02 |