题目:
输入1个整数数组,实现1个函数来调剂该数组中数组的顺序,使得所有奇数位于数组的前半部份,所有偶数位于数组的后半部份。
解法1:O(n^2)
从头扫描数组,每碰到1个偶数,拿出这个数字,并把位于这个数字后面的所有数字往前移动1位。挪完以后在数组的末尾有1个空位,这时候把该偶数放入这个空位中。
由于每碰到1个偶数就需要移动O(n)个数字,因此总的时间复杂度是O(n^2).
解法2:O(n)
类似快排,两个指针,1个指向数组头,只向后移动,1个指向数组尾,只向前移动。如果第1个指针指向的数字是偶数,并且第2个指针指向的数字是奇数,交换两数。
void foo(int a[],int left,int right)
{
if(left>right)
return;
int i,j;
i=left;
j=right;
while(i!=j)
{
while(a[j]%2==0&&i<j) j–;
while(a[i]%2!=0&&i<j) i++;
{
if(left>right)
return;
int i,j;
i=left;
j=right;
while(i!=j)
{
while(a[j]%2==0&&i<j) j–;
while(a[i]%2!=0&&i<j) i++;
if(i<j)
swap(a[i],a[j]);//交换
}
}
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » 剑指offer 面试题14―调整数组顺序使奇数位于偶数前面
波比源码 » 剑指offer 面试题14―调整数组顺序使奇数位于偶数前面
purchase levofloxacin pills order levaquin 250mg for sale
generic sildenafil 100mg nolvadex 20mg pill methocarbamol 500mg pill
order lamotrigine prazosin for sale online tretinoin tablet
acheter 5mg tadalafil vrai cialis 20mg prix acheter 50mg du viagra
order terazosin 5mg pills brand azulfidine 500 mg buy azulfidine 500 mg generic
imuran 100mcg canada order pantoprazole generic viagra mail order us
purchase dapsone online perindopril price aceon 4mg over the counter
buy accutane generic accutane buy online buy deltasone 10mg
azithromycin 250mg tablet neurontin 600mg tablet order neurontin online
viagra cialis 50mg viagra overnight delivery for viagra
order triamcinolone 10mg online cheap order generic aristocort 10mg order clarinex pill
buy domperidone 10mg motilium pills cheap flexeril 15mg
order sumatriptan 25mg pills order levofloxacin 500mg online avodart over the counter
ranitidine drug buy celebrex 100mg pills celebrex online order
real money online casino real money slots blackjack online real money
order generic metronidazole 400mg augmentin online order bactrim for sale
buy viagra 50mg for sale order tadalafil 10mg pill tadalafil 10mg usa
order tadacip diclofenac for sale online indocin 50mg cost
buy lamisil 250mg without prescription buy cefixime pill order amoxicillin 500mg sale
buy clarithromycin 500mg pills biaxin 250mg over the counter buy antivert generic
proventil 100mcg drug protonix 40mg pill buy ciprofloxacin 500mg generic
actos uk buy viagra 100mg without prescription viagra australia
buy tadalafil 40mg pills online casinos real money online casino for real cash
free welcome spins no deposit real money slots roulette games
roulette online affordable essay writing professional research paper writers
order doxycycline generic doxycycline 100mg pill cleocin order online
brand olmesartan 10mg calan 120mg us divalproex sale
buy diamox 250mg online cheap buy imdur 20mg for sale imuran generic
pamelor 25 mg ca panadol 500 mg canada order paxil 20mg for sale
indomethacin order online buy flomax 0.4mg generic buy cenforce pills
doxycycline for sale online order generic doxycycline medrol canada
purchase ropinirole online buy calcitriol online cheap oral trandate 100 mg
buy tadacip for sale trimox uk trimox cost
fenofibrate 160mg usa sildenafil professional viagra mail order us
esomeprazole usa buy clarithromycin pills buy furosemide 100mg generic
buy minocin 100mg capsules order terazosin 1mg online buy generic terazosin
order glycomet 1000mg for sale verapamil 240mg tablet buy tamoxifen
purchase deltasone without prescription buy prednisone 10mg generic buy amoxicillin 1000mg without prescription
accutane 20mg price buy ampicillin online cheap ampicillin price
ivermectin 12 mg tablet order deltasone sale buy deltasone 5mg sale
order zofran 4mg online order bactrim generic bactrim 480mg us
brand ventolin inhalator synthroid 100mcg price augmentin 1000mg cost
order modafinil for sale provigil over the counter metoprolol where to buy