Reverse Array Order

前端 未结 8 1146
独厮守ぢ
独厮守ぢ 2020-12-01 15:50

I am trying to reverse the order of an Array in java.
What is the most efficient way to do so in O(n) with the least amount of memory used.
No need to answer with co

8条回答
  •  萌比男神i
    2020-12-01 16:46

    Use a single temp element.

    int array[SIZE];
    int temp;
    
    for (int i = 0; i < SIZE/2; i++)
      {
         temp = array[i];
         array[i] = array[SIZE-1 - i];
         array[SIZE-1 - i] = temp;
      }
    

提交回复
热议问题