顺时针旋转数组

2024-03-08

我有一个二维数组,需要顺时针旋转 90 度,但是我不断收到 arrayindexoutofbounds...

public int[][] rotateArray(int[][] arr) {
    // first change the dimensions vertical length
    // for horizontal length and vice versa
    int[][] newArray = new int[arr[0].length][arr.length];

    // invert values 90 degrees clockwise by starting
    // from button of array to top and from left to right
    int ii = 0;
    int jj = 0;
    for (int i = 0; i < arr[0].length; i++) {
        for (int j = arr.length - 1; j >= 0; j--) {
            newArray[ii][jj] = arr[i][j];
            jj++;
        }
        ii++;
    }
    return newArray;
}

这是一个标准矩阵顺时针旋转代码:

static int[][] rotateCW(int[][] mat) {
    final int M = mat.length;
    final int N = mat[0].length;
    int[][] ret = new int[N][M];
    for (int r = 0; r < M; r++) {
        for (int c = 0; c < N; c++) {
            ret[c][M-1-r] = mat[r][c];
        }
    }
    return ret;
}

请注意以下几点:

  • 它提高了引用尺寸的可读性MxN矩阵为M and N
  • 这是传统的使用方式r, c代替i, j索引矩阵的行和列
  • This is not the most robust implementation:
    • 并不能确保mat是一个有效的MxN矩阵,M>0, N>0
  • Use an explicit mapping formula instead of extraneous local variables
    • 使程序不那么复杂并且更具可读性

这是一个测试工具:

import java.util.Arrays;
//...

static void printMatrix(int[][] mat) {
    System.out.println("Matrix = ");
    for (int[] row : mat) {
        System.out.println(Arrays.toString(row));
    }
}
public static void main(String[] args){
    int[][] mat = {
        { 1, 2, 3 },
        { 4, 5, 6 }
    };
    printMatrix(mat);
    // Matrix = 
    // [1, 2, 3]
    // [4, 5, 6]

    int[][] matCW = rotateCW(mat);
    printMatrix(matCW);
    // Matrix = 
    // [4, 1]
    // [5, 2]
    // [6, 3]
}

注意使用for-each http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html循环和java.util.Arrays http://java.sun.com/javase/6/docs/api/java/util/Arrays.html in printMatrix。如果您经常在 Java 中使用数组,那么您绝对应该熟悉它们。

Java 矩阵库的链接

如果您经常使用矩阵,您可能需要考虑使用专门的矩阵库。

  • JAMA: http://math.nist.gov/javanumerics/jama/ http://math.nist.gov/javanumerics/jama/
  • UJMP: http://www.ujmp.org/ http://www.ujmp.org/

相关问题

从技术上讲,Java 有数组的数组。确保您了解所有含义。

  • 数组数组与多维数组的性能比较 https://stackoverflow.com/questions/2368761/performance-comparison-of-array-of-arrays-vs-multidimensional-arrays
  • Java Arrays.equals()回报false对于二维数组。 https://stackoverflow.com/questions/2721033/java-arrays-equals-returns-false-for-two-dimensional-arrays
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

顺时针旋转数组 的相关文章

随机推荐

  • 检测 Swing 中单击生成的 MouseEvent 上的 Shift 修饰符

    我正在使用 Java Swing 处理 GUI 应用程序中的一些 MouseEvent 从现在开始我正在分析 mousePressed 方法中的鼠标事件 只是为了确定是否 发生左键或右键单击 我的代码是 public void mouseP
  • React,未捕获的引用错误:ReactDOM 未定义

    我在做这个路由器教程 http www tutorialspoint com reactjs reactjs router htm 我的 App jsx 文件 import React from react import ReactDOM
  • int 和 char 数组有什么区别?

    下面 int 和 char 数组有什么区别 int main int numbers 2 1 3 char letter a b 0 cout lt lt numbers lt
  • C语言中的大整数?

    在 C 中处理大量数字的最简单方法是什么 我需要在区域中存储值1000 900 或者以更人类可读的形式10 2700 有谁知道一个简单的方法来做到这一点 任何帮助将不胜感激 Use libgmp http gmplib org GMP 是一
  • 从 ADO.NET 执行 T-Sql 视图

    是否可以从 C 代码执行视图 如果是的话我想知道参数化视图是否存在以及我应该如何使用它们 参数化意味着我们在存储过程中使用相同的参数来声明 where 条件 不执行视图 一个执行查询 如果查询从视图或表中选择行 ADO NET 不知道也不关
  • Ruby on Rails / Paperclip / AWS::S3::NoSuchBucket 错误

    我安装了回形针插件并能够在本地使用它 当我将其配置为与 amazon S3 一起使用时 我不断收到 NoSuchBucket 指定的存储桶不存在 错误 回形针文档指出 如果存储桶不存在 则会创建该存储桶 但很明显 我的情况出了问题 我首先安
  • Mysql中如何将数据合并到临时表中

    我有一张非常大的桌子 叫做paypal ipn orders 在这个表中 我有 2 个重要的信息 一行称为item name和一行称为sort num 我想使用某些参数从中提取记录paypal ipn orders并将它们放入一个名为的临时
  • MS Access SQL 中是否有与 SUBSTRING 函数等效的函数?

    我想在 MS Access 查询中执行类似的操作 但 SUBSTRING 是一个未定义的函数 SELECT DISTINCT SUBSTRING LastName 1 1 FROM Authors 您可以使用 VBA 字符串函数 正如 on
  • Django静态媒体不显示图片

    在寻找解决方案几个小时后未能解决我的问题后 我发布了此内容 我的媒体根目录中的图像没有显示在我的 html 上 在 chrome 的控制台中我得到一个404 file not found 尽管图像就在那里 我在 Pycharm 中使用 Py
  • Google 日历 feed timeMin timeMax 不起作用

    我从搜索中推断 限制日期范围的 Google 日历提要的 URI 应包括 timeMin 和 timeMax 还应包括 singleEvents 和 orderBy 这是我构建的 URI 无论我在投影值后放置什么查询参数 我仍然会获取从 8
  • 以编程方式使下载停靠栏图标弹起

    如何以编程方式使 Dock 下载 图标弹起 请注意 我不希望我的应用程序图标弹起 而只希望下载图标弹起 特别是 我正在将文件从我的应用程序下载到 下载 文件夹 这没问题 但我希望下载图标在下载完成时弹起 就像 Safari 完成下载时发生的
  • 通过右值引用返回是否更有效?

    例如 Beta ab Beta toAB const return move Beta ab 1 1 Beta ab Beta toAB const return move Beta ab 1 1 这会返回一个悬空引用 就像左值引用的情况一
  • 多个鼠标/鼠标/光标?

    如何为多个鼠标显示另一个光标 我有两个 TMemo 两个可以输入各自 TMemo 的键盘 2 个鼠标 我需要 2 个光标 如果假设的话 我已经可以检测出哪只老鼠是哪只 我怎样才能让我自己的光标跟着它一起走 使用德尔福 可能沿着多点 http
  • 使用 bootstrap-datepicker 禁用日期范围?

    如何禁用多个日期范围 使用bootstrap datepicker 目前 这是我关于如何专门禁用日期的代码 div class input group input daterange div
  • Linux shell 编程字符串比较语法

    有什么区别 and 在Linux shell编程中比较字符串 也许下面的代码可以工作 if NAME user then echo your name is user fi 但我认为这不是正确的语法 它将用于比较字符串 陈述 什么是正确的
  • webpack --env.product 和 --mode="product" 之间有什么区别

    如果我错了 请纠正我 但据我从文档中了解到 env option https webpack js org guides environment variables 用来ONLY为了能够在webpack config js如果它导出一个函数
  • 插入表视图并添加按钮或空行时最好的是什么?

    当呈现一个简单的表格视图 或者我想甚至是列表视图 时 您输入新数据的首选方法是什么 With add delete buttons like this Or with a blank line indicating a new record
  • UIAlertViewDelegate 方法 didDismissWithButtonIndex 在手机睡眠/锁定时被调用

    我有一个 UIAlertView 它的 didDismissWithButtonIndex 委托方法调用会弹出视图控制器 同一类 它是 AlertView 委托和视图控制器 以使用户返回到上一个屏幕 问题是 当您在 警报显示 之前锁定手机时
  • Docker 化 Spring boot 应用程序以进行 Kubernetes 部署

    我有一个 Spring Boot 应用程序 在我的 application properties 中具有如下一些属性 server ssl keyStore users admin certs appcert jks server ssl
  • 顺时针旋转数组

    我有一个二维数组 需要顺时针旋转 90 度 但是我不断收到 arrayindexoutofbounds public int rotateArray int arr first change the dimensions vertical