lintcode(507)摆动排序 II
来源:互联网 发布:数据对账 英文怎么说 编辑:程序博客网 时间:2024/05/29 02:28
描述:
给你一个数组nums
,将它重排列如下形式
nums[0] < nums[1] > nums[2] < nums[3]....
注意事项
你可以认为每个输入都有合法解
样例:
给出 nums = [1, 5, 1, 1, 6, 4]
,一种方案为 [1, 4, 1, 5, 1, 6]
.
给出 nums = [1, 3, 2, 2, 3, 1]
,一种方案为 [2, 3, 1, 3, 1, 2]
.
思路:
复制数组并排序,按照降序依次插入到nums中
采用降序的目的在于讲sort后的数组,排列在中间位置的,也就是值比较接近的,分别放置在数组两边,以此来避免相等的情况出现
public class Solution { /** * @param nums a list of integer * @return void */ public void wiggleSort(int[] nums) { // Write your code here int[] temp = nums.clone(); Arrays.sort(temp); for(int i = (nu ms.length - 1)/2 , j = 0 ; i>=0;i-- , j += 2){ nums[j] = temp[i]; } for(int i = nums.length -1, j = 1;i>(nums.length - 1)/2;i-- , j+=2){ nums[j] = temp[i]; } }}
0 0
- lintcode(507)摆动排序 II
- LintCode:摆动排序 II
- LintCode:摆动排序 II
- 摆动排序 II-LintCode
- Lintcode 摆动排序
- 摆动排序-LintCode
- *[Lintcode]Wiggle Sort摆动排序
- lintcode 摆动排序(Wiggle Sort) (Java)
- 324. Wiggle Sort II 摆动排序
- 【LintCode】整数排序II
- 整数排序II-lintcode
- LintCode-整数排序 II
- LintCode 整数排序 II
- 整数排序 II -LintCode
- 摆动排序
- Google 2016 面试题 2 | 摆动排序 II
- Google 2016 面试题3 | 摆动排序 II
- LintCode 合并排序数组 II
- mysql 锁测试
- mysql加索引和常用sql语句
- exception异常
- MySQL笔记(一):事物与Innodb存储引擎
- MongoDB的索引
- lintcode(507)摆动排序 II
- Android中图片加载框架Glide解析2----从源码的角度理解Glide的执行流程
- c#中foreach的使用
- Qt Creator 窗体控件自适应窗口大小布局
- 经典进程同步问题:吸烟者问题
- 多个so文件引用起的坑
- 面试常考的常用数据结构与算法
- 随笔
- 慕课网地址