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 ...
Kth Largest Element in an Array 桶排序 First Missing Positive 计数排序 H-Index 基数排序 Maximum Gap 其他 Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted ...
// can create from an arrayvar DYNAMIC_LANGS = Enum ( [ "JavaScript" , "Python" , "Ruby" ] ) ;DYNAMIC_LANGS . JavaScript // => "JavaScript"delete DYNAMIC_LANGS . JavaScript // => throws errorDYNAMIC_...
-- 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:数组中的逆序对) ...
JavaScript权威指南 犀牛书 Chapter 1. Introduction to JavaScript Section 1.1. JavaScript Myths Section 1.2.... Section 1.3.... Array.reverse( ) Array.shift( ) Array.slice( ) Array.sort( ...
an element occurs more than n/3 times in array. 5.后缀波兰表达式STRING转换为中缀表达式的STRING。 这题本来很简单,但我可能算错了。纠结的地方是a,b,+,c,/ 到底是 (c/(a+b)) 还是 ((a+b)/c)Reverse Polish ...
MESSAGE Uses an expression to suppress a feature. SYNONYMS hide, hold back, remove, conditional, blank HINT This command is available in history modeling mode. HELP DSN_feature_edit BITMAP ...
Tabs Function Declarations and Definitions Function Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean Expressions Return Values Variable and Array ...
elements (Element, Array) : 用于封装成jQuery对象的DOM元素 示例 设置页面背景色。 jQuery 代码: $(document.body).css( "background", "black" ); -------------------------------------------------------...
prerequisite patches in an unstable way, which has been updated to compute in a way that is compatible with "git patch-id --stable". * The "git log" command by default behaves as if the --mailmap ...