Leetcode Wiggle Sort II
来源:互联网 发布:鼎域名邸房价走势 编辑:程序博客网 时间:2024/05/16 11:00
Leetcode Wiggle Sort II,一种比较高效的解法,但是在一些情况下会出现为o(n^2)的时间复杂度。
#include<iostream>#include<vector>#include<algorithm>using namespace std;class Solution {public: void wiggleSort(vector<int>& nums) { int sorted = 0; int cur = 1; // 1: next one is greater than current sorted last element // -1: next one is smaller than the current sorted last element int state = 1; int pos; while (sorted < nums.size() - 1) { if (cur == nums.size()) { cur = sorted + 1; } // if there many elements with the same value between cur and sorted if (cur != sorted + 1) { // skip all element with the same value as sorted while (nums[cur] == nums[sorted]) { cur ++; } // if cur get the end of the array, let it go back to sorted + 1 if (cur == nums.size()) { continue; } // if cur does not equals to nums[sorted], we can insert it into // the sorted sequence. if (state == 1) { // the required next one is greater // example 1: 2 5 3 3 4 // example 2: 2 5 3 3 2 if (nums[cur] > nums[sorted]) { swap(nums[sorted + 1], nums[cur]); sorted += 2; } else { swap(nums[sorted], nums[cur]); sorted ++; state = -1; } } else { // the required next one is smaller // example 1: 2 5 2 4 4 5 // example 2: 2 5 2 4 4 1 if (nums[cur] > nums[sorted]) { swap(nums[sorted], nums[cur]); sorted ++; state = 1; } else { swap(nums[sorted + 1], nums[cur]); sorted += 2; } } cur ++; } else { // cur = sorted + 1 if (state == 1) { // the required next one is greater // example 1: 3 5 2 4 // example 2: 3 5 2 1 // example 3: 3 5 2 2 if (nums[cur] > nums[sorted]) { sorted ++; cur ++; state = -1; } else if (nums[cur] < nums[sorted]) { swap(nums[sorted], nums[cur]); sorted ++; cur ++; state = -1; } else { pos = search(nums, sorted - 2, state, nums[cur]); if (pos != -1) { swap(nums[pos], nums[cur]); } else { // if cannot find the appropriate position, just // increase cur. cur ++; } } } else { // example 1: 2 5 4 6 1 // example 2: 2 5 4 6 7 // example 3: 2 5 4 6 6 if (nums[cur] < nums[sorted]) { sorted ++; cur ++; state = 1; } else if (nums[cur] > nums[sorted]) { swap(nums[sorted], nums[cur]); sorted ++; cur ++; state = 1; } else { pos = search(nums, sorted - 2, state, nums[cur]); if (pos != -1) { swap(nums[pos], nums[cur]); } else { // if cannot find the appropriate position, just // increase cur. cur ++; } } } // end of state = -1 } // end of cur = sorted + 1; cout<<"current: "; for (int i = 0; i < nums.size(); i ++) { cout<<nums[i]<<" "; } cout<<endl; } } int search(vector<int>& nums, int pos, int flag, int value) { // find the appropriate pos for condition: cur = sorte + 1, and // nums[cur] = num[sorted] while (pos >= 0) { if (nums[pos] == value) { pos -= 2; } else { if (flag == 1) { if (pos == 0 || value < nums[pos - 1]) { return pos; } else { pos -= 1; flag = -1; } } else { if (value > nums[pos - 1]) { return pos; } else { pos -= 1; flag = 1; } } } } return -1; }};// Sample input: ./a.out argv1 argv2....int main(int argc, char* argv[]) { Solution so; vector<int> vec(atoi(argv[1]), 0); for (int i = 0; i < atoi(argv[1]); i ++) { vec[i] = 1 - i % 2; } // cout<<"begin: "; // for (int i = 0; i < vec.size(); i ++) { // cout<<vec[i]<<" "; // } // cout<<endl; so.wiggleSort(vec); cout<<"result: "; for (int i = 0; i < vec.size(); i ++) { cout<<vec[i]<<" "; } cout<<endl; return 0;}
测试用例:
代码中给出的一种情况就是o(n^2)的情况,其它的基本没有问题。
./a.out 10
current: 0 1 1 0 1 0 1 0 1 0
current: 0 1 1 0 1 0 1 0 1 0
current: 0 1 0 1 1 0 1 0 1 0
current: 0 1 0 1 1 0 1 0 1 0
current: 0 1 0 1 0 1 1 0 1 0
current: 0 1 0 1 0 1 1 0 1 0
current: 0 1 0 1 0 1 0 1 1 0
current: 0 1 0 1 0 1 0 1 1 0
current: 0 1 0 1 0 1 0 1 0 1
result: 0 1 0 1 0 1 0 1 0 1
0 0
- leetcode Wiggle Sort II
- LeetCode Wiggle Sort II
- leetcode Wiggle Sort II
- Leetcode Wiggle Sort II
- Leetcode Wiggle Sort II
- LeetCode: Wiggle Sort II
- LeetCode -- Wiggle Sort II
- Leetcode Wiggle Sort and Wiggle Sort II
- leetcode之Wiggle Sort II
- [LeetCode 324] Wiggle Sort II
- [leetcode] - 324. Wiggle Sort II
- [LeetCode]324. Wiggle Sort II
- leetcode 324. Wiggle Sort II
- LeetCode 324. Wiggle Sort II
- leetcode wiggle-sort-ii(未完成)
- leetcode-Wiggle Sort II-324
- LeetCode 324. Wiggle Sort II
- LeetCode[324] Wiggle Sort II
- 对文件进行操作
- 天才第一步,点亮人生中的第一个发光二极管
- C++学习笔记46——友元与继承
- POJ 3267 The Cow Lexicon DP 字符串匹配 一主串n个子串最少在主串删除多少字母,可以匹配到n 个单词序列中的一些完整单词
- 为什么要用DLL文件
- Leetcode Wiggle Sort II
- 1005. 继续(3n+1)猜想 (25)
- 1035. DNA matching
- opencart安装
- 92. php 命名空间(2)
- android 微信支付 问题解决
- 密码学概论(1)
- 2014年第五届蓝桥杯C/C++程序设计本科B组省赛 地宫取宝(编程大题)
- nagios分支Icinga安装部署配置