private void reverse(Object[] anArray){
if (anArray == null){
return;
}
reverseArgrithmTwo(anArray);
}
// private void reverseArgrithmOne(Object[] anArray){
// int maxIndex = anArray.length - 1;
// for (int index=(maxIndex-1)>>1;index>=0;index--) {
// Object temp = anArray[index];
// anArray[index] = anArray[maxIndex - index];
// anArray[maxIndex-index] = temp;
// }
// }
private void reverseArgrithmTwo(Object[] anArray){
for (int index = 0; index<anArray.length/2;index++)
{
Object temp = anArray[index];
anArray[index]= anArray[anArray.length-1-index];
anArray[anArray.length-1-index] = temp;
}
}
The test cases go below:
@Test
public void reverseAnArray(){
Integer[] original = {3,5,1,-2,0};
reverse(original);
assertArrayEquals(new Integer[]{0,-2,1,5,3}, original);
}
@Test
public void reverseAnOnlyOneElementArray(){
Integer[] oneElementArray = {5};
reverse(oneElementArray);
assertArrayEquals(new Integer[]{5}, oneElementArray);
}
@Test public void reverseArrayHasZeroElement(){
Integer[] zeroElementArray = new Integer[0];
reverse(zeroElementArray);
assertArrayEquals(new Integer[0], zeroElementArray);
}
@Test
public void reverseNullArray(){
Integer[] nullArray = null;
reverse(nullArray);
assertNull(nullArray);
}
分享到:
相关推荐
Algorithm to reverse an array5. Algorithm to rotate array of size 'n' by 'd' elements6. Algorithm to segregate 0's and 1's in an array7. Find the maximum difference between two elemen
Reverse the array 2. Find the maximum and minimum element in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1 and 2. Sort the array without ...
*Reverse the array *Find the maximum and minimum element in an array *Find the "Kth" max and min element of an array *Given an array which consists of only 0, 1 and 2. Sort the array with *Move all ...
8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 ...
Reverse the order of bits in an unsigned integer Chapter 20. Convert an integer to a string and a string to an integer Chapter 21. Convert a number from base b1 to base b2 Chapter 22. Given a set S, ...
Reverse String 414. Third Maximum Number 448. Find All Numbers Disappeared in an Array 66. Plus One 238. Product of Array Except Self 697. Degree of an Array 849. Maximize Distance to Closest ...
array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element ...
* put: `true|false|["foo", "bar"]|function` — whether elements can be added from other lists, or an array of group names from which elements can be taken. * revertClone: `boolean` — revert cloned ...
// can create from an arrayvar DYNAMIC_LANGS = Enum ( [ "JavaScript" , "Python" , "Ruby" ] ) ;DYNAMIC_LANGS . JavaScript // => "JavaScript"delete DYNAMIC_LANGS . JavaScript // => throws errorDYNAMIC_...
// data is an array of arrays that needs // to be serialized and stored into a persistent // global string var ds = ''; for (ii = 0; ii ; ++ii) { for (jj = 0; jj ; ++jj) { if (ii != 0 || jj != ...
10. 深入理解堆和优先队列:在问题如Kth Largest Element in an Array(数组中第K大的元素)中会用到堆和优先队列的知识。 11. 栈与单调栈问题:例如Valid Parentheses(有效的括号)、Largest Rectangle in ...
- `equals(Object anObject)` - `equalsIgnoreCase(String anotherString)` - `indexOf(int ch)` - `indexOf(String str)` - `replace(char oldChar, char newChar)` - `replaceAll(String regex, String ...
-- reverses an array in place reverse(arr) => { for i in [0.< arr.len // 2] { opposite = arr.len - i - 1; arr[i], arr[opposite] <- arr[opposite], arr[i]; } } 设计原理 黄油旨在 友好,适合经验...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
an array of integers, find two numbers such that they add up to a specific target number. 向后遍历数组,直到获得两个数的和是给定的值 You are given two linked lists representing two non-negative ...
Array(方法1:堆 方法二:快速排序(推荐)) (面试题40:最小的k个数) LeetCode 347 Top K Frequent Elements(堆排序、桶排序) LintCode 532 Reverse Pairs(归并排序的应用)(面试题51:数组中的逆序对) ...
an array of —— 表示“各种各样的”或“一系列的”。 - 例如:There is a variety of options for students to choose from in their extracurricular activities. 2. 时间相关: - 过时的:outdated;...
- Links the edges in reverse order to ensure the first inserted edge is the first one in the list. ### Conclusion This C++ code demonstrates the implementation of depth-first search using an ...
This comprehensive solution offers an array of features that support high-quality video and audio processing, making it ideal for multimedia devices requiring robust capabilities. #### Key Features ...