566. Reshape the Matrix

问题概述
In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.

You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

根据数组的数学公式得出其位置,一次遍历将原数组中的数字填入。
O(r*c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {

int[][] ans = new int[r][c];

int oldR = mat.length;
int oldC = mat[0].length;


if ( oldR * oldC != r * c ){
return mat;
}
for (int i = 0; i < r*c ; i++ ){
int m = i/oldC;
int n = i%oldC;

int p = i/c;
int q = i%c;
ans[p][q] = mat[m][n];
}
return ans;
}
}

283. Move Zeroes

问题描述
Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

双指针,i指针左侧保留大于零的元素,j指针左侧保留等于零的元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public void moveZeroes(int[] nums) {
int i = 0;
int j = 0;
while ( j < nums.length ){
if ( nums[j] != 0 ){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;

i++;
}
j++;
}
}
}

121. Best Time to Buy and Sell Stock

问题描述
You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

采用dp的思想,先计算一遍盈利差,再计算一遍最大收益。
空间上还可以优化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int maxProfit(int[] prices) {
int best = 0;
int[] difference = new int[prices.length];
difference[0] = 0;
for (int i = 1; i < prices.length; i++ ){
difference[i] = prices[i] - prices[i - 1];
if ( difference[i] + difference[i - 1] > difference[i] ){
difference[i] = difference[i] + difference[i - 1];
}
if (difference[i] > best){
best = difference[i];
}
}
return best;
}
}


350. Intersection of Two Arrays II

问题描述
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

遍历一个数组,将所有元素添加到哈希表中。
遍历第二个数组,如果在哈希表中则添加到数组中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
ArrayList<Integer> arr = new ArrayList<Integer>();
int count = 0;

for ( int i = 0 ; i < nums1.length ; i++ ){
if (!map.containsKey(nums1[i])){
map.put(nums1[i],1);
}
else{
map.put(nums1[i],map.get(nums1[i])+1);
}

}

for ( int i = 0 ; i < nums2.length ; i++ ){
if (map.containsKey(nums2[i])){
if (map.get(nums2[i]) > 0){
count++;
arr.add(nums2[i]);
map.put(nums2[i],map.get(nums2[i])-1);
}
}
}
int[] ans = new int[count];

for (int i = 0 ; i < arr.size() ; i++){
ans[i] = arr.get(i);
}
return ans;
}
}

31. Next Permutation

A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

For example, for arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

For example, the next permutation of arr = [1,2,3] is [1,3,2].
Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
Given an array of integers nums, find the next permutation of nums.

The replacement must be in place and use only constant extra memory.

从数组末尾开始遍历第i个元素。
如果后一项小于前一项,则排序关系正确。
反之则将i与遍历过的部分中比i大的第一个数字交换。
然后对已遍历的部分排序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public void nextPermutation(int[] nums) {
int flag = 0; //标记,如果没有下一个排列时,排序数组。
if (nums.length != 1){
int i = nums.length -2;
while (i >= 0){
if (nums[i + 1] <= nums[i]) { //从尾部开始,比较元素是否是大到小
i--;
continue;
}
else { //排序关系不正确时
for (int j = nums.length-1;j>i;j--){
if (nums[j] <= nums[i]){
continue;
}
int temp = nums[j]; //将i元素和遍历过的元素中第一个比nums[i]大的交换。
nums[j] = nums[i];
nums[i] = temp;
Arrays.sort(nums,i+1,nums.length); //排序i之后的数组。
flag = 1;
break;
}
break;
}
}
if (flag == 0 ){ //如果全部从大到小,则排序整个数组。
Arrays.sort(nums);
}
}
}
}