Reverse Array Order

前端 未结 8 1102
独厮守ぢ
独厮守ぢ 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条回答
  • 2020-12-01 16:53

    pseudocode, assuming 0-based-index arrays:

    for i in range(0, len(array)/2):
         swap(array[i], array[(len(array)-1)-i])
    
    0 讨论(0)
  • 2020-12-01 16:55

    Here's two solutions:

        loop to N/2
          swap each element at i with element at N - i
    

    Another solution is (depending on your circumstances) fake reversing the array by indexing:

        GetValueAt(int i){return array[N - i];}
    
    0 讨论(0)
提交回复
热议问题