【CodeWars】Sort the odd

本文介绍了一个有趣的编程挑战,要求在不改变偶数位置的情况下,仅对数组中的奇数进行升序排序。通过几种不同的实现方式,包括直接交换、分离排序奇数后再合并,以及使用算法库函数,展示了多种解决问题的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近发现国外一个好玩的网站,听说会刷题刷上瘾!今天试了试,做了一道题。

Description:

You have an array of numbers.
Your task is to sort ascending odd numbers but even numbers must be on their places.

Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.

Example

sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]

题意大概就是给数组里的所有奇数从小到大排序,同时偶数的位置保持不变。

写了个循规蹈矩的算法,过了:

 1 std::vector<int> sortArray(std::vector<int> array)
 2     {
 3         if(array.size()==0)
 4           return array;
 5         for(int i = 0;i<array.size();i++){
 6           for(int j=i+1;j<array.size();j++){
 7             if(array[i]%2==0) continue;
 8             if(array[j]%2==0) continue;
 9             else if(array[j]<array[i]) 
10                 std::swap(array[i],array[j]);
11           }
12         }
13         return array;
14     }

下面放几个能看懂的网友的solution:

 1 class Kata
 2 {
 3 public:
 4     std::vector<int> sortArray(std::vector<int> array)
 5     {
 6       for (int i=0; i<array.size(); i++) {
 7           if(array[i] &1){
 8             for (int j=i+1; j<array.size(); j++) {
 9                 if(array[j] & 1 && array[j] < array[i]){
10                     std::swap(array[i], array[j]);
11                 }
12             }
13           }
14         }
15         return array;
16     }
17 };

这个也循规蹈矩,和我的差不多。

 1 class Kata
 2 {
 3 public:
 4     std::vector<int> sortArray(std::vector<int> array)
 5     {
 6         std::vector<int> a;
 7         for (int i: array) {
 8             if (i & 1) {
 9                 a.push_back(i);
10             }
11         }
12         std::sort(a.begin(), a.end());
13         for (size_t i = 0, j = 0; i < array.size(); i++) {
14             if (array[i] & 1) {
15                 array[i] = a[j++];
16             }
17         }
18         return array;
19     }
20 };

这个是先把所有的奇数放到另一个vector里进行排序,再把排好序的插入原来的数组。

 1 #define ODD(x) (x % 2 != 0)
 2 
 3 class Kata
 4 {
 5 public:
 6     std::vector<int> sortArray(std::vector<int> array)
 7     {
 8         for (auto i = array.begin(); i != array.end(); ++i) {
 9           for (auto j = i; j != array.end(); ++j) {
10             if (ODD(*j) && ODD(*i) && (*j < *i)) {
11               *j ^= *i;
12               *i ^= *j;
13               *j ^= *i;
14             }
15           }
16         }
17         return array;
18     }
19 };

这个用到了按位与和auto,大概思路还是差不多的。

最后再放一个我不太看得懂但是赞数最高的,似乎是用到了algorithm库里的函数来做:

 1 #include <algorithm>
 2 
 3 class Kata
 4 {
 5 public:
 6     std::vector<int> sortArray(std::vector<int> array)
 7     {
 8         std::vector<int> odds;
 9         std::copy_if(array.begin(), array.end(), std::back_inserter(odds), [] (int x) {return x % 2;});
10         std::sort(odds.begin(), odds.end());
11         for (int i = 0, j = 0; i < array.size(); i++) if (array[i] % 2) array[i] = odds[j++];
12         return array;
13     }
14 };

感觉思路和前面一个是差不多的,把奇数单独放到一个vector里排序,然后再用排好序的替换原来数组中的奇数。

转载于:https://www.cnblogs.com/Aikoin/p/10478379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值