lintcode 508.Wiggle Sort
题目:
LintCode 508. 摆动排序
给你一个没有排序的数组,请将原数组就地重新排列满足如下性质
nums[0] <= nums[1] >= nums[2] <= nums[3]....
方案:
1 class Solution { 2 public: 3 /* 4 * @param nums: A list of integers 5 * @return: nothing 6 */ 7 void wiggleSort(vector<int> &nums) const { 8 const int len = nums.size(); 9 10 if (len < 2) 11 { 12 return; 13 } 14 15 bool flag_query_min = true; 16 for (int i = 0; i < len; ++i) 17 { 18 int curr_idx = i; 19 for (int j = i+1; j < i + 3 && j < len; ++j) 20 { 21 if (flag_query_min) 22 { 23 if (nums[j] < nums[curr_idx]) 24 { 25 curr_idx = j; 26 } 27 } 28 else 29 { 30 if (nums[j] > nums[curr_idx]) 31 { 32 curr_idx = j; 33 } 34 } 35 } 36 37 if (curr_idx != i) 38 { 39 std::swap(nums[i], nums[curr_idx]); 40 } 41 42 flag_query_min = !flag_query_min; 43 } 44 } 45 };
//z 2018-01-16 23:59:30 L.349'30 T2283528791.K.F2338414085[T1,L15,R1,V2]
@IS2120#CNBLOGS.T2169364049[T1,L65,R1,V259]:备忘
$ € ₤ ₭ ₪ ₩ ₮ ₦ ₱ ฿ ₡ ₫ ﷼ ¥ ﷼ ₫ ₡ ฿ ₱ ₦ ₮ ₩ ₪ ₭ ₤ € $