您的位置:首页 > 其它

leetcode wiggle-sort-ii(未完成)

2016-05-14 21:04 302 查看
题目链接

我给出了一个错误的算法。因为这个算法不能不能处理相等的情况。

不过个人感觉时间空间复杂度都很不错

public class Solution {
public void wiggleSort(int[] nums) {
int i=0;
for(i=0;i<=nums.length-3;i+=2)
{
int a=nums[i],b=nums[i+1],c=nums[i+2];
int temp=a;
if(a>b)
{
temp=a;
a=b;
b=temp;
}

if(b<c)
{
temp=c;
c=b;
b=temp;
}

nums[i]=a;
nums[i+1]=b;
nums[i+2]=c;

}

if(i+2==nums.length)//结尾有两个元素
{
int a=nums[i],b=nums[i+1];
int temp;
if(a>b)
{
temp=a;
a=b;
b=temp;
}
nums[i]=a;
nums[i+1]=b;
}
}
}


错误用例

Input:
[1,2,2,1,2,1,1,1,1,2,2,2]
Output:
[1,2,1,2,1,2,1,1,1,2,2,2]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: