zoukankan      html  css  js  c++  java
  • 【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里排序,然后再用排好序的替换原来数组中的奇数。

  • 相关阅读:
    mysql表结构同步
    关于Java8中lambda约简函数reduce的一个计算问题
    激烈的歌曲有助于编程
    今天刷了数据解构与算法这门课 感觉略有收获
    我有一个好朋友 他的名字叫刘洋 他的ID是北极的大企鹅 他的技术不错 他渴望成为架构师 猎头们路过可以去他的博客看看
    缓存雪崩,缓存击穿,缓存穿透
    celery
    Redis
    django 缓存的使用
    base64 加密
  • 原文地址:https://www.cnblogs.com/Aikoin/p/10478379.html
Copyright © 2011-2022 走看看