| 失效链接处理 | 
| 
      LeetCode题解(java语言实现) PDF 下载 
	本站整理下载: 
	相关截图: 
![]() 
	主要内容: 
		1.3 Solution 3 - Reversal 
	
		Can we do this in O(1) space and in O(n) time? The following solution does. 
	
		Assuming we are given 1,2,3,4,5,6 and order 2. The basic idea is: 
	
		1. Divide the array two parts: 1,2,3,4 and 5, 6 
	
		2. Rotate first part: 4,3,2,1,5,6 
	
		3. Rotate second part: 4,3,2,1,6,5 
	
		4. Rotate the whole array: 5,6,1,2,3,4 
	
		public static void rotate(int[] arr, int order) { 
	
		order = order % arr.length; 
	
		if (arr == null || order < 0) { 
	
		throw new IllegalArgumentException("Illegal argument!"); 
	
		} 
	
		//length of first part 
	
		int a = arr.length - order; 
	
		reverse(arr, 0, a-1); 
	
		reverse(arr, a, arr.length-1); 
	
		reverse(arr, 0, arr.length-1); 
	
		} 
	
		public static void reverse(int[] arr, int left, int right){ 
	
		if(arr == null || arr.length == 1) 
	
		return; 
	
		while(left < right){ 
	
		int temp = arr[left]; 
	
		arr[left] = arr[right]; 
	
		arr[right] = temp; 
	
		left++; 
	
		right--; 
	
		8 | 181 Program Creek 
	
		} }2 Evaluate Reverse Polish Notation 
	
		The problem: 
	
		Evaluate the value of an arithmetic expression in Reverse Polish Notation. 
	
		Valid operators are +, -, *, /. Each operand may be an integer or another 
	
		expression. 
	
		Some examples: 
	
		["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 
	
		["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 
	
		2.1 Naive Approach 
	
		This problem is simple. After understanding the problem, we should quickly realize 
	
		that this problem can be solved by using a stack. We can loop through each element 
	
		in the given array. When it is a number, push it to the stack. When it is an operator, 
	
		pop two numbers from the stack, do the calculation, and push back the result. 
 | 
    




    
苏公网安备 32061202001004号


    