`
niwtsew
  • 浏览: 71948 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Reverse an array

Go 
阅读更多
	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);
	}
0
0
分享到:
评论

相关推荐

    Algorithm-Concepts:此存储库包含计算机科学领域中使用的一些核心算法和数据结构的代码

    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

    leetcode中国-DP:DP

    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 ...

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    *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 ...

    Coding Interview In Java

    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 ...

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    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, ...

    lrucacheleetcode-leetcode:记录自己的leetcode解题历程~Welcomeeveryonetocomment:grinning_face:~

    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 ...

    判断链表是否为回文链表leetcode-Competitive-Programming:我对竞争性编程问题的解决方案

    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 ...

    Sortable前端框架

    * 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 ...

    mune:基于 ES6 代理的枚举。 'mune' === 'enum'.split('').reverse().join('');

    // can create from an arrayvar DYNAMIC_LANGS = Enum ( [ "JavaScript" , "Python" , "Ruby" ] ) ;DYNAMIC_LANGS . JavaScript // =&gt; "JavaScript"delete DYNAMIC_LANGS . JavaScript // =&gt; throws errorDYNAMIC_...

    PDF添加书签javascript代码

    // 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 != ...

    Coding Interview in Java

    10. 深入理解堆和优先队列:在问题如Kth Largest Element in an Array(数组中第K大的元素)中会用到堆和优先队列的知识。 11. 栈与单调栈问题:例如Valid Parentheses(有效的括号)、Largest Rectangle in ...

    CORE JAVA总结_沈红喜

    - `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) =&gt; { for i in [0.&lt; arr.len // 2] { opposite = arr.len - i - 1; arr[i], arr[opposite] &lt;- arr[opposite], arr[i]; } } 设计原理 黄油旨在 友好,适合经验...

    LeetCode最全代码

    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/...

    leetcode双人赛-java_leetcode:java打印letcode

    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 ...

    javalruleetcode-reverie:找工作

    Array(方法1:堆 方法二:快速排序(推荐)) (面试题40:最小的k个数) LeetCode 347 Top K Frequent Elements(堆排序、桶排序) LintCode 532 Reverse Pairs(归并排序的应用)(面试题51:数组中的逆序对) ...

    大学英语四级写作关键词与短语汇总.docx

    an array of —— 表示“各种各样的”或“一系列的”。 - 例如:There is a variety of options for students to choose from in their extracurricular activities. 2. 时间相关: - 过时的:outdated;...

    C++深度遍历

    - 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 ...

    Broadcom Product Brief BCM7403

    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 ...

Global site tag (gtag.js) - Google Analytics