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

猜你喜欢

转载自www.cnblogs.com/Aikoin/p/10478379.html