【CodeWars】Sort the odd

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

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里排序,然后再用排好序的替换原来数组中的奇数。

posted @   Aikoin  阅读(392)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示