• 《程序员面试金典》之像素翻转


    1、题目描述

    有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度。给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于500,图像元素小于等于256。

    测试样例:
    [[1,2,3],[4,5,6],[7,8,9]],3
    返回:[[7,4,1],[8,5,2],[9,6,3]]
     

    2、代码实现

     1 import java.util.*;
     2 
     3 public class Transform {
     4     
     5     private static int num = 0; // 记录阶数
     6     
     7     /**
     8      * 将图像顺时针旋转90度
     9      * @param mat 原来的二维数组
    10      * @param n 阶数
    11      * @return 旋转90度后二维数组
    12      */
    13     public int[][] transformImage(int[][] mat, int n) {
    14         int[][] temp = new int[n][n];
    15         
    16         for(int i = 0; i < n; i++){
    17             int tempNum = 0;
    18             for(int j = n-1; j >= 0; j--){
    19                 temp[i][tempNum] = mat[j][i];
    20                 tempNum++;
    21             }
    22         }
    23         return temp;
    24     }
    25     
    26     /**
    27      * 通过给定的字符串,获取新的二维数组
    28      * @param str 给定的字符串
    29      * @return 新的二维数组
    30      */
    31     public static int[][] getIntArray(String str){
    32         String strResult = "";
    33 
    34         // 提取无'['和']'的子字符串
    35         for(int i = 0; i < str.length(); i++){
    36             if(str.charAt(i) == '[' || str.charAt(i) == ']'){
    37                 continue;
    38             }
    39             strResult += str.charAt(i);
    40         }
    41         
    42         String[] strings = strResult.split(",");    // 分离字符串,存入字符数组里面
    43         num = Integer.parseInt(strings[strings.length-1]);
    44         int[][] arr = new int[num][num];
    45         int temp = 0;
    46         // 将字符数组转化成整型数组
    47         for(int i = 0; i < num; i++){
    48             for(int j = 0; j < num; j++){
    49                 arr[i][j] = Integer.parseInt(strings[temp]);
    50                 temp++;
    51             }
    52         }
    53         return arr;
    54     }
    55     
    56     /**
    57      * 打印出结果
    58      * @param arr 旋转后的二维数组
    59      */
    60     public void print(int[][] arr){
    61         System.out.print("[");
    62         for(int i = 0; i < arr.length; i++){
    63             System.out.print(Arrays.toString(arr[i]));
    64             if(i != arr.length - 1){
    65                 System.out.print(",");
    66             }
    67         }
    68         System.out.println("]");
    69     }
    70     
    71     /**
    72      * 用户页面测试
    73      * @param args
    74      */
    75     public static void main(String[] args) {
    76         
    77         Transform t = new Transform();
    78         String str = "[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],4"; // 给定待测试的字符串
    79         int[][] mat = Transform.getIntArray(str);                           // 由字符串提取二维数组
    80         int[][] arr = t.transformImage(mat, num);                          // 获取旋转90度后的二维数组
    81         t.print(arr);                                                      // 打印出旋转90度后的结果
    82     }
    83 }
  • 相关阅读:
    年末反思
    Flink运行时架构
    Phoenix 启动报错:Error: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.
    Clickhouse学习
    Flink简单认识
    IDEA无法pull代码到本地,Can't Update No tracked branch configured for branch master or the branch doesn't exist.
    第1章 计算机系统漫游
    简单的 Shell 脚本入门教程
    开源≠免费 常见开源协议介绍
    MySQL 视图
  • 原文地址:https://www.cnblogs.com/wangchaoyuan/p/5965355.html
Copyright © 2020-2023  润新知