博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#280] Wiggle Sort
阅读量:4339 次
发布时间:2019-06-07

本文共 2356 字,大约阅读时间需要 7 分钟。

Problem:

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....

For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

Analysis:

Once you encounter a new problem, you should not feel anxious with the new setting. Try to conclude a principle underlying it, you could find out it is extraordinarily easy.---------------------------------------------------------nums[0] <= nums[1] >= nums[2] <= nums[3]....---------------------------------------------------------For the wiggle sort, we actually have no requirments over an element's global order. for an element with odd index, we just need to meet following order principle:nums[i-1] <= nums[i] >= nums[i+1]Note: i is an odd number. Solution 1:public class Solution {    public void wiggleSort(int[] nums) {        Arrays.sort(nums);        int len = nums.length;        if (len <= 2)            return;        for (int i = 1; i < len - 1; i = i+2) {            int temp = nums[i];            nums[i] = nums[i+1];            nums[i+1] = temp;        }        return;    }}The above solution is easy. We first guarantee the elements' order befor and after nums[i]. But it needs to sort the nums array first, which we could totaly abandon.Since we only care about an odd(indexed) element's realtive order with it neighboring elements, we all just do it all the way. for (int i = 1; i < nums.length; i++) {    if (i % 2 == 1) {        if (nums[i-1] > nums[i])            swap(nums, i, i-1);    } else{        if (nums[i] > nums[i-1])            swap(nums, i, i-1);    }}The invariant:we guaratee this is no violation of wiggleSort when we reach i.Note the start of the for loop. for (int i = 1; i < nums.length; i++)

Solution:

public class Solution {    public void wiggleSort(int[] nums) {        if (nums == null || nums.length <= 0)            return;        for (int i = 1; i < nums.length; i++) {            if (i % 2 == 1) {                if (nums[i-1] > nums[i])                    swap(nums, i, i-1);            } else{                if (nums[i] > nums[i-1])                    swap(nums, i, i-1);            }        }    }            private void swap(int[] nums, int i, int j) {        int temp = nums[i];        nums[i] = nums[j];        nums[j] = temp;    }}

 

转载于:https://www.cnblogs.com/airwindow/p/4823159.html

你可能感兴趣的文章
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>