반응형
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
- log error
- querydsl no sources given
- springbatch error
- no sources given
- java version
- Medium
- 스프링부트테스트
- easy
- parse
- springboottest
- property or field 'jobparameters' cannot be found on object of type
- java 버전 변경
- java 11
- error
- aws
- mac os git error
- java 여러개 버전
- springboot
- No tests found for given includes
- maybe not public or not valid?
- OpenFeign
- JUnit
- java
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)
- el1008e
- yum install java
- java 1.8 11
- AWS CLI
- LeetCode
- Java 1.8
Archives
- Today
- Total
쩨이엠 개발 블로그
[ Leetcode ] 324. Wiggle Sort II - Java 본문
728x90
반응형
Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
You may assume the input array always has a valid answer.
Example 1:
Input: nums = [1,5,1,1,6,4]
Output: [1,6,1,5,1,4]
Explanation: [1,4,1,5,1,6] is also accepted.
Example 2:
Input: nums = [1,3,2,2,3,1]
Output: [2,3,1,3,1,2]
Constraints:
- 1 <= nums.length <= 5 * 104
- 0 <= nums[i] <= 5000
- It is guaranteed that there will be an answer for the given input nums.
Solution
class Solution {
public void wiggleSort(int[] nums) {
Arrays.sort(nums);
int[] answer = new int[nums.length];
int length = nums.length / 2;
if(nums.length % 2 == 1){
length = length+1;
}
for(int i=0; i< length; i++){
answer[2*i] = nums[length-i-1];
}
for(int i=0; i< nums.length/2; i++){
answer[2*i+1] = nums[nums.length-i-1];
}
for(int i=0; i< nums.length; i++){
nums[i] = answer[i];
}
}
}
1. sort
2. 반으로 나눠서 짝수는 제일 큰수부터 / 홀수는 배열의 반부터
3. 배열의 갯수가 홀수인 경우는 홀수 길이를 1 더해준다
728x90
반응형
'개발 > Programming' 카테고리의 다른 글
[ Leetcode ] 1669. Merge In Between Linked Lists - Java (0) | 2021.02.13 |
---|---|
[ Leetcode ] 148. Sort List - Java (0) | 2021.02.10 |
[ Leetcode ] 1561. Maximum Number of Coins You Can Get - Java (0) | 2021.02.07 |
[ Leetcode ] 763. Partition Labels - Java (0) | 2021.02.03 |
[ Leetcode ] 1630. Arithmetic Subarrays - Java (0) | 2021.02.02 |
Comments