算法与数据结构进阶课第八节笔记

打表技巧和矩阵处理技巧

打表法

1)问题如果返回值不太多,可以用hardcode的方式列出,作为程序的一部分

2)一个大问题解决时底层频繁使用规模不大的小问题的解,如果小问题的返回值满足条件1),可以把小问题的解列成一张表,作为程序的一部分

3)打表找规律(本节课重点),有关1)和2)内容欢迎关注后序课程

打表找规律

1)某个面试题,输入参数类型简单,并且只有一个实际参数

2)要求的返回值类型也简单,并且只有一个

3)用暴力方法,把输入参数对应的返回值,打印出来看看,进而优化code

 

例1:

小虎去买苹果,商店只提供两种类型的塑料袋,每种类型都有任意数量。
1)能装下6个苹果的袋子
2)能装下8个苹果的袋子
小虎可以自由使用两种袋子来装苹果,但是小虎有强迫症,他要求自己使用的袋子数量必须最少,且使用的每个袋子必须装满。
给定一个正整数N,返回至少使用多少袋子。如果N无法让使用的每个袋子必须装满,返回-1

暴力方法,先看所有用8号袋装能剩几个苹果,剩下的用6号袋能不能装下,装不下,就少用一个8号袋,看看能不能用6号袋装

打印出来结果,会发现超过18个苹果,8个为一组的话,奇数返回-1,偶数是(N-18)/8+3

直接写代码

       // 暴力方法     
       public static int minBags(int apple) {
		if (apple < 0) {
			return -1;
		}
		int bag6 = -1;
		int bag8 = apple / 8;
		int rest = apple - 8 * bag8;
		while (bag8 >= 0 && rest < 24) {
			int restUse6 = minBagBase6(rest);
			if (restUse6 != -1) {
				bag6 = restUse6;
				break;
			}
			rest = apple - 8 * (--bag8);
		}
		return bag6 == -1 ? -1 : bag6 + bag8;
	}

	// 如果剩余苹果rest可以被装6个苹果的袋子搞定,返回袋子数量
	// 不能搞定返回-1
	public static int minBagBase6(int rest) {
		return rest % 6 == 0 ? (rest / 6) : -1;
	}

	public static int minBagAwesome(int apple) {
		if ((apple & 1) != 0) { // 如果是奇数,返回-1
			return -1;
		}
		if (apple < 18) {
			return apple == 0 ? 0 : (apple == 6 || apple == 8) ? 1
					: (apple == 12 || apple == 14 || apple == 16) ? 2 : -1;
		}
		return (apple - 18) / 8 + 3;
	}

例2:

给定一个正整数N,表示有N份青草统一堆放在仓库里, 有一只牛和一只羊,牛先吃,羊后吃,它俩轮流吃草, 不管是牛还是羊,每一轮能吃的草量必须是:
1,4,16,64…(4的某次方)
谁最先把草吃完,谁获胜, 假设牛和羊都绝顶聪明,都想赢,都会做出理性的决定,根据唯一的参数N,返回谁会赢

        // n份青草放在一堆
	// 先手后手都绝顶聪明
	// string "先手" "后手"
	public static String winner1(int n) {
		// 0  1  2  3  4
		// 后 先 后 先 先
		if (n < 5) { // base case
			return (n == 0 || n == 2) ? "后手" : "先手";
		}
		// n >= 5 时
		int base = 1; // 当前先手决定吃的草数
		// 当前是先手在选
		while (base <= n) {
			// 当前一共n份草,先手吃掉的是base份,n - base 是留给后手的草
			// 母过程 先手 在子过程里是 后手
			if (winner1(n - base).equals("后手")) {
				return "先手";
			}
			if (base > n / 4) { // 防止base*4之后溢出
				break;
			}
			base *= 4;
		}
		return "后手";
	}

	public static String winner2(int n) {
		if (n % 5 == 0 || n % 5 == 2) {
			return "后手";
		} else {
			return "先手";
		}
	}

例3:

定义一种数:可以表示成若干(数量>1)连续正数和的数
比如: 5 = 2+3,5就是这样的数,12 = 3+4+5,12就是这样的数,1不是这样的数,因为要求数量大于1个、连续正数和,2 = 1 + 1,2也不是,因为等号右边不是连续正数
给定一个参数N,返回是不是可以表示成若干连续正数和的数

      public static boolean build(int N){
		if (N == 1){
			return false;
		}
		int start = 1;
		int sum = 0;
		int count = 1;

		while (start < N){
			if (sum == N){
				return true;
			}else if (sum > N){
				start ++;
				sum = 0;
				count = start;
			}
			sum += count;
			count ++;
		}
		return false;
	}

N 是2的次幂就是false

       public static boolean isMSum2(int num) {
		if (num < 3) {
			return false;
		}
                // (num & (num - 1)) == 0 是二的某次幂
		return (num & (num - 1)) != 0;
	}

一排灯,在i位置开关操作,可以改变后面灯的状态,比如i位置后面的灯是开,i操作后,后面的灯会变成关,所有灯变成开的状态就算赢,给一排灯,那么先手和后手谁会赢

只看最后一个灯的状态,如果最后是开的,先手一定不会赢,因为先手每次操作都会让最后变成开

矩阵处理技巧

1)zigzag打印矩阵

设置两个点A,B都是左上角的点,规定A从左往右走,到右边界时往下走,规定B从上往下走,到下边界时往右走,这样每次AB一起移动一个位置,就可以发现,AB之间的连线覆盖了整个矩阵,那么只要按照从B到A打印,再从A到B打印,这样交替进行,就是zigzag打印矩阵

      public static void printMatrixZigZag(int[][] matrix) {
		int tR = 0;
		int tC = 0;
		int dR = 0;
		int dC = 0;
		int endR = matrix.length - 1;
		int endC = matrix[0].length - 1;
		boolean fromUp = false;
		while (tR != endR + 1) {
			printLevel(matrix, tR, tC, dR, dC, fromUp);
			tR = tC == endC ? tR + 1 : tR;
			tC = tC == endC ? tC : tC + 1;
			dC = dR == endR ? dC + 1 : dC;
			dR = dR == endR ? dR : dR + 1;
			fromUp = !fromUp;
		}
		System.out.println();
	}

	public static void printLevel(int[][] m, int tR, int tC, int dR, int dC,
			boolean f) {
		if (f) {
			while (tR != dR + 1) {
				System.out.print(m[tR++][tC--] + " ");
			}
		} else {
			while (dR != tR - 1) {
				System.out.print(m[dR--][dC++] + " ");
			}
		}
	}

2)转圈打印矩阵

顺时针打印矩阵

      public static void spiralOrderPrint(int[][] matrix) {
		int tR = 0;
		int tC = 0;
		int dR = matrix.length - 1;
		int dC = matrix[0].length - 1;
		while (tR <= dR && tC <= dC) {
			printEdge(matrix, tR++, tC++, dR--, dC--);
		}
	}

	public static void printEdge(int[][] m, int tR, int tC, int dR, int dC) {
		// 只剩一条横线
		if (tR == dR) {
			for (int i = tC; i <= dC; i++) {
				System.out.print(m[tR][i] + " ");
			}
		} else if (tC == dC) { // 只剩一条竖线
			for (int i = tR; i <= dR; i++) {
				System.out.print(m[i][tC] + " ");
			}
		} else {
			int curC = tC;
			int curR = tR;
			while (curC != dC) {
				System.out.print(m[tR][curC] + " ");
				curC++;
			}
			while (curR != dR) {
				System.out.print(m[curR][dC] + " ");
				curR++;
			}
			while (curC != tC) {
				System.out.print(m[dR][curC] + " ");
				curC--;
			}
			while (curR != tR) {
				System.out.print(m[curR][tC] + " ");
				curR--;
			}
		}
	}

自己版本:

public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length < 1){
            return new int[0];
        }
        int row = matrix.length;
        int column = matrix[0].length;
        int[] arr = new int[row * column];
        int index = 0;
        int top = 0;
        int left = 0;
        int bottom = row - 1;
        int right = column - 1;
        while (top != bottom && left != right){
            index = leftToRight(matrix,top,left,right,arr,index);
            index = upToDown(matrix,right,top,bottom,arr,index);
            index = rightToLeft(matrix,bottom,right,left,arr,index);
            index = downToUp(matrix,left,bottom,top,arr,index);
            top++;
            left++;
            bottom--;
            right--;
            if (top > bottom || left > right){
                break;
            }
        }

        if (top == bottom){
            for (int i = left; i <= right; i++){
                arr[index++] = matrix[top][i];
            }
        }else if (left == right){
            for (int i = top; i <= bottom; i++){
                arr[index++] = matrix[i][left];
            }
        }

        

        return arr;

 
    }

    public int leftToRight(int[][] matrix, int row, int columnStart, int columnEnd, int[] arr, int index){
        for (int i = columnStart; i < columnEnd; i++){
            arr[index++] = matrix[row][i];
        }
        return index;

    }

    public int upToDown(int[][] matrix, int column, int rowStart, int rowEnd, int[] arr, int index){
        for (int i = rowStart; i < rowEnd; i++){
            arr[index++] = matrix[i][column];
        }
        return index;

    }

    public int rightToLeft(int[][] matrix, int row, int columnStart, int columnEnd, int[] arr, int index){
        for (int i = columnStart; i > columnEnd; i--){
            arr[index++] = matrix[row][i];
        }
        return index;

    }

    public int downToUp(int[][] matrix, int column, int rowStart, int rowEnd, int[] arr, int index){

        for (int i = rowStart; i > rowEnd; i--){
            arr[index++] = matrix[i][column];
        }
        return index;

    }

3)原地旋转正方形矩阵

旋转矩阵

       public static void rotate(int[][] matrix) {
		int a = 0;
		int b = 0;
		int c = matrix.length - 1;
		int d = matrix[0].length - 1;
		while (a < c) {
			rotateEdge(matrix, a++, b++, c--, d--);
		}
	}

	public static void rotateEdge(int[][] m, int a, int b, int c, int d) {
		int tmp = 0;
		for (int i = 0; i < d - b; i++) {
			tmp = m[a][b + i];
			m[a][b + i] = m[c - i][b];
			m[c - i][b] = m[c][d - i];
			m[c][d - i] = m[a + i][d];
			m[a + i][d] = tmp;
		}
	}

核心技巧:找到coding上的宏观调度

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值