Article Directory
66. plus one
Title Description
- Given a non-negative integer nonempty array of integers represented, on the basis of the number plus one.
- Most significant digit stored in the first array, each element of the array stores only a single digit.
- You may assume that in addition to the integer 0, the integer does not begin with a zero.
Example:
输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
Links: https://leetcode-cn.com/problems/plus-one
Ideas analysis
Reference clever solution to a problem the god of solution: the Java math problem solving
a number with a storage array, then add only two cases after 1
- 9 is not the last one, last one added directly to the array after the array returns 1.
- 9 is the last case, it is necessary to determine the cycle, because it's in front of a possibly 9, let 9 to 0, then the former plus 1.
- If the highest level 9, you can create a new array, the array length than the original length freshman, the first position 0. To
/**
* 将一个用表示数字的数组传入,加上1之后,返回新数组
* @param digits 传入的数组
* @return 返回加一之后的新数组
*/
public static int[] plusOne(int[] digits){
int len = digits.length;
for(int i = len-1;i>=0;i--){
digits[i]++;
digits[i] = digits[i]%10;
//末位不是9,直接返回
if(digits[i]!=0) return digits;
}
//最高位为9的情况,创建一个比原数组长1的数组
int[] newDigits = new int[len+1];
//首位置1,其余初始化为0
newDigits[0] = 1;
return newDigits;
}
88. merge two ordered arrays
Title Description
- Given two ordered arrays of integers and nums2 nums1, incorporated into the nums2 nums1 in an ordered array such as a num1.
Description:
- And initializing the number of elements nums1 nums2 of m and n.
- You can assume nums1 sufficient space (space equal to or greater than m + n) to save the elements of nums2.
Example:
输入:
nums1 =[1,2,3,0,0,0],m =3
nums2 =[2,5,6],n =3
输出: [1,2,2,3,5,6]
Links: https://leetcode-cn.com/problems/merge-sorted-array
Ideas analysis
Reference Solution: painting solution algorithm
- Since there is enough space nums1 support under two arrays, the processing from back to front merge sort added .
- Set point two arrays of digital tail pointers p1 and p2, and p is a pointer to a pointer to merge just the last bit of the array.
- Comparing two values from back to front in the array, back into the large nums1 sequentially fill forward.
- When no time nums1 number , all copies of the nums2 nums1 the front.
/**
* 合并并排序到第一个数组
*
* @param nums1 传入的第一个数组
* @param m 第一个数组的长度
* @param nums2 传入的第二个数组
* @param n 第二个数组的长度
*/
public static void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1;//指向nums1的有数字的最后一位
int p2 = n - 1;//指向nums2的有数字的最后一位
int p = m + n - 1;
//从有数字的最后一位向前,一一对比,两者较大的数存到nums1的p位置,分别向前
while (p1 >= 0 && p2 >= 0) {
nums1[p--] = nums1[p1] > nums2[p2] ? nums1[p1--] : nums2[p2--];
}
//由于是存到nums1上的,所以保证最后p1无元素可指的时候,把nums2上的元素按顺序拷贝到nums1
System.arraycopy(nums2, 0, nums1, 0, p2 + 1);
}
167. The two numbers and enter an ordered array Ⅱ-
Title Description
-
Has been given a follow ascending ordered array to find the number of two numbers such that their sum is equal to the target sum.
-
Function should return the two index values and index1 index2, which must be less than index1 index2.
Description:
Return values of the index (index1, index2 and) is not zero .
You can assume that each input corresponding to only the only answer, but you can not reuse the same elements.
Example:
输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
Links: https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted
Ideas analysis
Reference Solution: Dual Pointer solving two numbers
- Note that the array is sorted, you can use it to solve.
- Solving double pointer , a pointer to the front, a pointer to the rear.
- If two pointer corresponds exactly and target, two return pointer index corresponding to +1.
- If greater than target, large numbers pointer will move forward . Conversely, the decimal pointer moves rearwardly.
/**
* 得到传入数组中两个元素和刚好是目标数字的位置,用数组返回
* @param numbers 传入的目标数组
* @param target 传入的目标数字
* @return 返回两元素和为目标数字的对应位置
*/
public static int[] twoSum(int[] numbers,int target){
int i = 0;
int j = numbers.length-1;
if(numbers == null) return null;
while(i<j){
int sum = numbers[i]+numbers[j];
if(sum==target) return new int[]{i+1,j+1};
else if(sum>target){
j--;
}else{
i++;
}
}
return null;
}