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
- string
- Binary Search
- 자바
- sorting
- database
- 구현
- Number Theory
- Math
- geometry
- Matrix
- array
- dynamic programming
- simulation
- Data Structure
- 코딩테스트
- hash table
- Tree
- Method
- Binary Tree
- 파이썬
- 코테
- greedy
- implement
- SQL
- Class
- bit manipulation
- java
- Stack
- two pointers
- Counting
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2164. Sort Even and Odd Indices Independently 본문
1. Input
1) int[] nums (0-indexed)
2. Output
1) nums에서 인덱스가 홀수인 것과 짝수인 것을 나눠서 정렬한 결과를 반환
- 인덱스가 홀수인 것 끼리는 내림차순으로 정렬한다.
- 인덱스가 짝수인 것 끼리는 오름차순으로 정렬한다.
3. Constraint
1) 1 <= nums.length <= 100
2) 1 <= nums[i] <= 100
4. Example
Input: nums = [4,1,2,3] -> Output: [2,3,4,1]
설명:
- 인덱스가 홀수인 것: 1, 3 -> 내림차순으로 정렬하면 3, 1
- 인덱스가 짝수인 것: 4, 2 -> 오름차순으로 정렬하면 2, 4
- 따라서 [2, 3, 4, 1]을 반환한다.
5. Code
1) 첫 코드(2023/06/21)
class Solution {
public int[] sortEvenOdd(int[] nums) {
List<Integer> odd = new ArrayList<>();
List<Integer> even = new ArrayList<>();
for(int i=0 ; i<nums.length ; i++){
if(i%2==0) even.add(nums[i]);
else odd.add(nums[i]);
}
odd.sort(Comparator.reverseOrder());
even.sort(Comparator.naturalOrder());
int a = 0, b = 0;
for(int i=0 ; i<nums.length ; i++){
if(i%2==0) nums[i] = even.get(a++);
else nums[i] = odd.get(b++);
}
return nums;
}
}
- 39%, 26%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 1018. 체스판 다시 칠하기 (0) | 2023.06.22 |
---|---|
[LeetCode/Easy] 2231. Largest Number After Digit Swaps by Parity (0) | 2023.06.21 |
[LeetCode/Easy] 2144. Minimum Cost of Buying Candies With Discount (0) | 2023.06.21 |
[LeetCode/Easy] 2085. Count Common Words With One Occurrence (0) | 2023.06.21 |
[LeetCode/Easy] 1971. Find if Path Exists in Graph (0) | 2023.06.21 |