【编程练习】数组篇

145 阅读1分钟

1. 螺旋矩阵

image.png

思路:

  1. 通过四个指针控制遍历的范围,top,bottom控制上下范围,left,right控制左右范围。初始值:top = 0, left = 0, bottom = matrix.length - 1, right = matrix[0].length - 1;

  2. 设置遍历条件:top <= bottom && left <= right

  3. 从左到右遍历,如下图:

image.png

  1. 从上到下遍历,从top+1开始遍历,避免重复

image.png

  1. 从右到左遍历,注意:从right-1开始遍历的同时,还要设置 left != right的条件,避免重复遍历

image.png

  1. 从下到上遍历,注意:停止位置为top+1,避免与以一次的遍历重复,同时要限制 top!=bottom
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        if (matrix.length < 1) {
            return res;
        }
        //设置初始值
        int top = 0;
        int bottom = matrix.length - 1;
        int left = 0;
        int right = matrix[0].length - 1;

        while (top <= bottom && left <= right) {
            //1. 从左到右遍历
            for (int i = left; i <= right; i++) {
                res.add(matrix[top][i]);
            }
            //2. 从上到下遍历
            for (int i = top + 1; i <= bottom; i++) {
                res.add(matrix[i][right]);
            }
            //3. 从右到左遍历
            for (int i = right - 1; i >= left && top != bottom; i--) {
                res.add(matrix[bottom][i]);
            }
            //4. 从下到上遍历
            for (int i = bottom - 1; i >= top + 1 && left != right; i--) {
                res.add(matrix[i][left]);
            }
            top++;
            bottom--;
            left++;
            right--;
        }
        return res;
    }
}

2. 顺时针旋转矩阵

image.png 顺时针旋转后,列变成了行,依次原本数组的列遍历即可;

image.png

import java.util.*;

public class Solution {
    public int[][] rotateMatrix(int[][] mat, int n) {
        // write code here
        if(n < 1){
            return new int[0][0];
        }
        int[][] res = new int[n][n];
        for(int i=0;i<n;i++){
            int[] arr = new int[n];
            for(int j=n-1;j>=0;j--){
                arr[n-1-j] = mat[j][i];
            }
            res[i] = arr;
        }
        return res;
    }
}

3. 矩阵乘法

image.png

image.png

  1. i控制行,j控制列,m控制相乘的索引
  2. 使用三层嵌套循环得出结果
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param a int整型二维数组 第一个矩阵
     * @param b int整型二维数组 第二个矩阵
     * @return int整型二维数组
     */
    public int[][] solve (int[][] a, int[][] b) {
        // write code here
        int n = a.length;
        if(n<1){
            return new int[0][0];
        }
        int[][] res = new int[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                int sum = 0;
                for(int m = 0;m<n;m++){
                // i控制行,j控制列,m控制相乘的索引
                    sum += a[i][m]*b[m][j];
                }
                res[i][j] = sum;
            }
        }
        return res;
    }
}

4. 两数之和

image.png

设置一个map用于存储已经遍历过的信息,key为target-numbers[i],value为索引值

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        // 设置一个map用于存储已经遍历过的信息,key为target-numbers[i],value为索引值
        Map<Integer,Integer> diffMap = new HashMap<Integer,Integer>();
        int[] res = new int[2];
        for(int i=0;i<numbers.length;i++){
            int diff = target - numbers[i];
            if(diffMap.containsKey(numbers[i])){
                res[0] = diffMap.get(numbers[i])+1;
                res[1] = i+1;
            }else{
                diffMap.put(diff,i);
            }
        }
        return res;
    }
}