您好,欢迎来到微智科技网。
搜索
您的当前位置:首页leetcode:sort:Wiggle Sort II(324)

leetcode:sort:Wiggle Sort II(324)

来源:微智科技网

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]….

Example:
(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
(2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].



class Solution {  
public:  
    void wiggleSort(vector<int>& nums) {  
        int n = nums.size();  
        if (n < 1)  
        {  
            return;  
        }  
        sort(nums.begin(), nums.end());  

        vector<int> temp(n, 0);  
        int j = n-1;  
        for (int i = 1; i < n; i += 2, j--)  
        {  
            temp[i] = nums[j];  
        }  
        for (int i = 0; i < n; i += 2, j--)  
        {  
            temp[i] = nums[j];  
        }  

        nums = temp;  
    }  
};  

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务