php算法在线刷题,c,算法_每日一道算法:leetcode 刷题碰到的问题。,c,算法 - phpStudy...

每日一道算法:leetcode 刷题碰到的问题。

这是题目:

Given an unsorted array nums, reorder it such that nums[0] < nums[1] >nums[2] < nums[3]....

Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is

[1, 4, 1, 5, 1, 6]. (2) Given nums = [1, 3, 2, 2, 3, 1], one possible

answer is [2, 3, 1, 3, 1, 2].

Note: You may assume all input has valid answer.

Follow Up: Can you do it in O(n) time and/or in-place with O(1) extraspace?

我的思路比较简单虽然通过了但是用了1081ms所以其实无论是时间复杂度还是空间复杂度都达不到要求,希望各位谁感兴趣的话试着用C做一下。下面是我的代码,如果有大神能帮我想到改进的方法也可以啊。(大体上是先快排之后然后再把后面一半的数插入前面一半的数):

void quicksort(int *nums, int numsSize);

void wiggleSort(int *nums, int numsSize){

quicksort(nums, numsSize);

int counter = 0;

int left = (numsSize - 1) / 2;

int right = numsSize - 1;

int cache[numsSize];

for(counter=1; counter <= numsSize; counter++){

if((counter & 1) == 1){

cache[counter - 1] = nums[left--];

}

else{

cache[counter - 1] = nums[right--];

}

}

for(counter=0; counter

nums[counter] = cache[counter];

}

}

void quicksort(int *nums, int numsSize){

if(numsSize <= 1){

return;

}

int left = 0;

int right = numsSize - 1;

int key = nums[left];

while(left < right){

while(nums[right] >= key && left < right){

right--;

}

nums[left] = nums[right];

while(left < right && nums[left] <= key){

left++;

}

nums[right] = nums[left];

}

nums[left] = key;

quicksort(nums, left);

quicksort(nums+left+1, numsSize - 1 - left);

}

另外在网上看到有些人的思路是先找出中指点(据说只需要O(n)的时间复杂度,我不知道为什么只要O(n)的时间复杂度就能找到中指点,希望有知道的大神顺便也帮我解释一下)。

相关阅读:

MSSQL如何用脚本导出创建表结构的脚本?

elasticsearch能作为主数据库使用吗?

android中mvc架构问题

ThinkPHP 怎么获取前台JS动态生成的js数组

github上pull request时出现的问题。

view 里面的 view 怎么设置frame

css可以设置图片以最短边为依据展示在父级中吗?

针对密码泄漏事件,都有哪些相对安全的密码加密规则?

PS 如何在一张图中截出同样大小的圆

PHP paypal ipn 返回 INVALID,但是 payment_status=Completed,这是为什么呢?

iptables的nat表中 -j redirect 与-dnat --to-destnation的区别。

Github中Gist的embed和raw功能是如何实现的?

gitbook上写好的书怎么导入github中

simple_html_dom执行出现死循环如何解决?

网易云音乐的通讯加密方式是什么?

请问在一个多层的JAVA企业项目中,大家是如何设计统一异常处理的?

moment日期比较

想用Java实现客户端的智能联想提示,应该选择Lucene之类的全文检索还是选择客户端缓存?

php pdo 缺省错误模式 和 例外模式,差不多,为什么弄两个?

如何在Ubuntu上安装内核对应的源码来编译驱动?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值