[C++] [Java] Clean Code 4-liner


  • 44

    if you have n number, the maximum k can be n - 1;
    if n is 9, max k is 8.
    This can be done by picking numbers interleavingly from head and tail,

    // start from i = 1, j = n;
    // i++, j--, i++, j--, i++, j--
    
    1   2   3   4   5
      9   8   7   6
    out: 1 9 2 8 3 7 6 4 5
    dif:  8 7 6 5 4 3 2 1
    

    Above is a case where k is exactly n - 1
    When k is less than that, simply lay out the rest (i, j) in incremental
    order(all diff is 1). Say if k is 5:

         i++ j-- i++ j--  i++ i++ i++ ...
    out: 1   9   2   8    3   4   5   6   7
    dif:   8   7   6   5    1   1   1   1 
    

    C++

    class Solution {
    public:
        vector<int> constructArray(int n, int k) {
            vector<int> res;
            for (int i = 1, j = n; i <= j; ) {
                if (k > 1) {
                    res.push_back(k-- % 2 ? i++ : j--);
                }
                else {
                    res.push_back(i++);
                }
            }
    
            return res;
        }
    };
    

    C++ Compact

    class Solution {
    public:
        vector<int> constructArray(int n, int k) {
            vector<int> res;
            for (int i = 1, j = n; i <= j; )
                res.push_back(k > 1 ? (k-- % 2 ? i++ : j--) : i++;
            return res;
        }
    };
    

    Java

    class Solution {
        public int[] constructArray(int n, int k) {
            int[] res = new int[n];
            for (int i = 0, l = 1, r = n; l <= r; i++)
                res[i] = k > 1 ? (k-- % 2 != 0 ? l++ : r--) : l++;
            return res;
        }
    }
    

  • 1
    D

    with diff in range [1, k]

        public int[] constructArray(int n, int k) {
            int[] res = new int[n];
            int idx = 0, start = 0;
            for (int i = 1, j = n; i <= j; ) {
                if (idx < n - k - 1) {
                    res[idx++] = i++;
                } else {
                    res[idx++] = start++ % 2 == 0 ? i++ : j--;
                }
            }
            return res;
        }
    

  • 0
    Q

    @alexander said in [C++] [Java] Clean Code 4-liner:

    res[i] = k > 1 ? (k-- % 2 != 0 ? l++ : r--) : (k % 2 != 0? l++ : r--);

    Hi, alexander. Thank you for you nice solution!
    May I ask whether I could use res[i] = k > 1 ? (k-- % 2 != 0 ? l++ : r--) : l++ to replace res[i] = k > 1 ? (k-- % 2 != 0 ? l++ : r--) : (k % 2 != 0? l++ : r--). If not, why?


  • 11
    M

    Thanks for sharing.
    Here is my java version based on yours.

    class Solution {
        public int[] constructArray(int n, int k) {
            int[] result = new int[n];
            int left=1, right=n;
            for(int i=0; i<n; i++){
                result[i] = k%2 !=0 ? left++ : right--;
                if(k>1)
                    k--;
            }
                    
            return result; 
        }
    }
    

  • 1
    G
    class Solution {
        public int[] constructArray(int n, int k) {
            int[] res = new int[n];
            for (int i = 0, l = 1, r = n; i < n; i++)
                res[i] = k > 1 && k-- % 2 == 0 ? r-- : l++;
            return res;        
        }
    }
    

  • 0
    C

    Modified your solution so that it produces the 'out' in your example.

    vector<int> constructArray(int n, int k) {
            vector<int> res;
            for (int i = 1, j = n; i <= j; ) {
                if (k > 1) {
                    res.push_back(k%2 == 0 ? i++ : j--);
                    k--;
                }
                else
                    res.push_back(j--);
            }
            return res;
    }
    

  • 4

    Come on! You don't have to save lines. It totally doesn't matter if you spend more lines to make your code more readable. I am sure this code will meet trouble when you are taking interview or under code review in company.


  • 0

    @BHC but it's cool. :) And he had been written a more readable solution


  • 0
    F
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.