`
niwtsew
  • 浏览: 69087 次
  • 性别: 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 ...

    cpp-算法精粹

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

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

    黄油:用于构建高效软件的美味语言。 在制品

    -- 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:数组中的逆序对) ...

    JavaScript权威指南

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

    leetcode双人赛-preparing:准备

    an element occurs more than n/3 times in array. 5.后缀波兰表达式STRING转换为中缀表达式的STRING。 这题本来很简单,但我可能算错了。纠结的地方是a,b,+,c,/ 到底是 (c/(a+b)) 还是 ((a+b)/c)Reverse Polish ...

    UG6.0快捷键大全

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

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Tabs Function Declarations and Definitions Function Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean Expressions Return Values Variable and Array ...

    jQuery完全实例.rar

    elements (Element, Array) : 用于封装成jQuery对象的DOM元素 示例 设置页面背景色。 jQuery 代码: $(document.body).css( "background", "black" ); -------------------------------------------------------...

    Git-2.21.0-64-bit.zip

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

Global site tag (gtag.js) - Google Analytics