Java simple solution


  • 0
    G
    class Solution {
        public int[] constructArray(int n, int k) {
            List<Integer> ls = new ArrayList<>();
            
            ls.add(1);
            
            boolean flag = true;
            
            for(int i=k; i>=1; i--){
                
                int tail = ls.get(ls.size()-1);
                
                if(flag){
                    int next = tail + i;
                    ls.add(next);
                    flag = false;
                } else{
                    int next = tail - i;
                    ls.add(next);
                    flag  = true;  
                }
            }
            System.out.println(ls);
            
            for(int i=k+2; i<=n; i++){
                ls.add(i);            
            }
            
            int [] arr = new int[n];
            for(int i=0; i<n;i++){
            	arr[i] = ls.get(i);
            	
            }
            return arr;
        }
    }
    

Log in to reply
 

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