280:摆动排序
This commit is contained in:
parent
a887caa8e0
commit
07a32ff2ac
39
src/main/java/leetcode/editor/cn/WiggleSort.java
Normal file
39
src/main/java/leetcode/editor/cn/WiggleSort.java
Normal file
@ -0,0 +1,39 @@
|
||||
//给你一个无序的数组 nums, 将该数字 原地 重排后使得 nums[0] <= nums[1] >= nums[2] <= nums[3]...。
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// 输入: nums = [3,5,2,1,6,4]
|
||||
//输出: 一个可能的解答是 [3,5,1,6,2,4]
|
||||
// Related Topics 贪心 数组 排序 👍 71 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
//280:摆动排序
|
||||
class WiggleSort {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new WiggleSort().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public void wiggleSort(int[] nums) {
|
||||
int[] temp = nums.clone();
|
||||
Arrays.sort(temp);
|
||||
int index = nums.length;
|
||||
for (int i = 1; i < nums.length; i += 2) {
|
||||
index--;
|
||||
nums[i] = temp[index];
|
||||
}
|
||||
for (int i = 0; i < nums.length; i += 2) {
|
||||
index--;
|
||||
nums[i] = temp[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<p>给你一个无序的数组 <code>nums</code>, 将该数字 <strong>原地 </strong>重排后使得 <code>nums[0] <= nums[1] >= nums[2] <= nums[3]...</code>。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>nums = [3,5,2,1,6,4]</code>
|
||||
<strong>输出:</strong> 一个可能的解答是 [3,5,1,6,2,4]</pre>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>排序</li></div></div><br><div><li>👍 71</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user