Sort by

recency

|

2174 Discussions

|

  • + 0 comments

    Here's an in-place solution (C#):

        public static List<int> reverseArray(List<int> a)
        {
            for (int i = 0; i < a.Count / 2; i++)
            {
                a[i] = a[i]^a[a.Count - i - 1];
                a[a.Count-i - 1] = a[i]^a[a.Count - i - 1];
                a[i] = a[i]^a[a.Count - i - 1];
            }
            return a;
        }
    
  • + 0 comments

    Sorry I didn't have a good idea that the next 8 months ago when

  • + 0 comments

    Reversing an array is built into Python!

    def reverseArray(a):
        return a[::-1]
    
  • + 0 comments

    Here is my c++ solution, you can watch video explanation here : https://youtu.be/Z_CYzW_sQAE

    Solution 1

    vector<int> reverseArray(vector<int> a) {
      reverse(a.begin(), a.end());
      return a;
    }
    

    Solution 2

    vector<int> reverseArray(vector<int> a) {
       vector<int> result; 
        for(int i = a.size() - 1; i >= 0; i--){
            result.push_back(a[i]);
        }
        return result;
    }
    
  • + 2 comments

    who can help me with the code using java

    • + 1 comment

      there is two solutions with different times Space and Time.

       public static List<Integer> reverseArray(List<Integer> list) {
           //two pointers technique
           int left = 0, right = list.size() - 1;
      
           while(left < right) {
               int tempValue = list.get(left);
               list.set(left, list.get(right));
               list.set(right, tempValue);
      
               left++;
               right--;
           }
         return list;
      }
      
      • + 1 comment
         public static List<Integer> reverseArray(List<Integer> toReversList) {
        
         if(toReversList.isEmpty()) return Collections.empty(); 
         List<Integer> reversed = new ArrayList<>();
        
         for(int i = toReversList.size() -1; i >= 0; i--){
             reversed.add(toReversList.get(i));
            }
            return reversed;
         }
        return list;
        

        }

        • + 0 comments

          First solution change the list in-place, so Time: 0(n) Space: 0(1)

          Second solution one create another list, so: Time: 0(n) Space: 0(n)