摆动排序 II · Wiggle Sort II

news/2024/7/4 8:49:35

 

链接:

题解:

1.先用partition函数,求得n/2的位置的排序

2.然后选取首尾指针(奇数选择1和length-1,偶数选择为1和length-2),进行swap交换

3.每次首指针每次+2,尾指针每次-2

九章算法 - 帮助更多程序员找到好工作,硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧

 

 

 

 

 

这段代码没有通过

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @return: nothing
     */
    void wiggleSort(vector<int> &nums) {
        // write your code here
        if (nums.size() <= 1) {
            return;
        }
        int len = nums.size();
        kthLargestElement(len/2, nums);
        if (len % 2 == 0) {
            int left = 1;
            int right = nums.size()-2;
            while (left < right) {
                swap(nums[left], nums[right]);
                left += 2;
                right -= 2;
            }
        } else {
            int left = 1;
            int right = nums.size() - 1;
            while (left < right) {
                swap(nums[left], nums[right]);
                left += 2;
                right -= 2;
            }
        }
    }

private:
    int kthLargestElement(int k, vector<int> &nums) {
        cout << "k=" << k << endl;
        // write your code here
        if (nums.size() <= 0) {
            return 0;
        }
        int left = 0;
        int right = nums.size()-1;
        int mid = partition(nums, left, right);
        //cout << "mid=" << mid << endl;
        while (mid != k-1) {
            //cout << "mid=====" << mid << endl;
            if (mid > k) {
                right = mid;
            } else {
                left = mid;
            }
            mid = partition(nums, left, right);
        }
        return nums[mid];
    }
   
    int partition(std::vector<int>& nums, int left, int right) {
        int index = random() % (right-left+1);
        swap(nums[right], nums[left+index]);
        int nSmall = left - 1;
        for (; left < right; ++left) {
            if (nums[left] <= nums[right]) {
                ++nSmall;
                swap(nums[left], nums[nSmall]);
            }
        }
        ++nSmall;
        swap(nums[right], nums[nSmall]);
        cout << nSmall << " val=" << nums[nSmall] << endl;  
        return nSmall;
    }
};


http://www.niftyadmin.cn/n/3649267.html

相关文章

使用Create React App v2和TypeScript

介绍 (Introduction) Create React App v2 introduced official TypeScript support, allowing JavaScript users to use TypeScript with the React front-end framework. TypeScript is a powerful tool that helps write safer, self-documenting code, allowing developers…

6个值得推荐的Android开源框架简介

1、volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JSON&#xff0c;图像等的异步下载&#xff1b; (2) 网络请求的排序&#xff08;scheduling&#xff09; (3) 网络请求的优先级处理 (4) 缓存 (5) 多级别取消请求 (6) 和Activity和生命周期…

谁说QTP不能多线程 - 当Python遇上QTP

谁说QTP不能多线程 - 当Python遇上QTP 作者&#xff1a;Wally Yu (微博&#xff1a;http://weibo.com/quicktest) 经常有人问我一个问题&#xff1a;QTP可以同时做多个项目的自动化吗&#xff1f;我每次都回答说“不行&#xff0c;QTP不支持多线程&#xff0c;VBS本身就不是…

验证手机号码工具类

/*** 验证手机号码&#xff08;支持国际格式&#xff0c;86135xxxx...&#xff08;中国内地&#xff09;&#xff0c;00852137xxxx...&#xff08;中国香港&#xff09;&#xff09;* param mobile 移动、联通、电信运营商的号码段*<p>移动的号段&#xff1a;134(0-8)、1…

Android中使用隐藏API图文解析

Android SDK的很多API是隐藏的&#xff0c;我无法直接使用。但是我们通过编译Android系统源码可以得到完整的API。编译Android系统源码后可以在out\target\common\obj\JAVA_LIBRARIES目录可以看到它的所有API。当然对于一般情况&#xff0c;out\target\common\obj\JAVA_LIBRARI…

QTP测试框架之_报表

QTP测试框架之_报表 作者&#xff1a;Wally Yu (微博&#xff1a;http://weibo.com/quicktest) 自己在开发QTP测试框架的时候一些对于报表的经验&#xff1a; Excel报表&#xff1a; 下载Report Manager:http://www.advancedqtp.com/knowl ... ut-reportermanager/ 修改…

git克隆github_如何使用Git和Github分叉,克隆和推送更改

git克隆github介绍 (Introduction) Github is an excellent resource for finding useful code. In order to contribute changes back to a project, you’ll need to make a local copy and then upload your changes. In this tutorial, you’ll fork and clone an existing…

LBS模块架构的封装

对地图进行一个封装 public interface ILbsLayer {/*** 获取地图*/View getMapView();/*** 设置位置变化监听*/void setLocationChangeListener(CommonLocationChangeListener locationChangeListener);/*** 设置定位图标*/void setLocationRes(int res);/*** 添加&#xf…