矩阵归零
来源:互联网 发布:java年度工作总结 编辑:程序博客网 时间:2024/04/30 22:16
给定一个m×n矩阵,如果一个元素是0,则将其所在行和列全部元素变成0。
需要在原矩阵上完成操作。
样例
给出一个矩阵
[
[1, 2],
[0, 3]
]
返回
[
[0, 2],
[0, 0]
需要在原矩阵上完成操作。
样例
给出一个矩阵
[
[1, 2],
[0, 3]
]
返回
[
[0, 2],
[0, 0]
]
import java.util.Scanner;/** * 给定一个m×n矩阵,如果一个元素是0,则将其所在行和列全部元素变成0。需要在原矩阵上完成操作。样例给出一个矩阵[ [1, 2], [0, 3]]返回[ [0, 2], [0, 0]] * @author Dell * */public class Test162 { public static void setZeroes(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; int[][] temp=new int[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(matrix[i][j]==0) { for(int k=0;k<n;k++) { if(temp[i][k]!=(-1)*matrix[i][k]) temp[i][k]=-1*matrix[i][k]; } for(int t=0;t<m;t++) { if( temp[t][j]!=-1*matrix[t][j]) temp[t][j]=-1*matrix[t][j]; } } } } for(int i=0;i<m;i++){for(int j=0;j<n;j++){ matrix[i][j]=matrix[i][j]+temp[i][j];}} }public static void main(String[] args) {Scanner sc=new Scanner(System.in);int m=sc.nextInt();int n=sc.nextInt(); int[][] matrix=new int[m][n]; for(int i=0;i<m;i++){for(int j=0;j<n;j++){ matrix[i][j]=sc.nextInt();}}/*for(int i=0;i<m;i++){for(int j=0;j<n;j++){ System.out.print(matrix[i][j]+" ");}System.out.println();}*/setZeroes(matrix);for(int i=0;i<m;i++){for(int j=0;j<n;j++){ System.out.print(matrix[i][j]+" ");}System.out.println();}}}
阅读全文
0 0
- 矩阵归零
- 矩阵归零
- 矩阵归零
- LintCode-矩阵归零
- LintCode: 矩阵归零
- 矩阵归零
- 矩阵归零
- 矩阵归零
- 矩阵归零-LintCode
- lintcode-矩阵归零
- 矩阵归零
- lintcode-矩阵归零-162
- LintCode 162-矩阵归零
- Java实现-矩阵归零
- 矩阵归零消减序列和
- 【Openjudge】矩阵归零消减序列和
- [Lintcode] Set Matrix Zeroes矩阵归零
- 矩阵归零消减序列和
- ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 43. Created with MySQL 5
- 使用Java的对UTF8URL进行编码方式
- ASP.NET MVC4在部署IIS后,运行时显示的是整个Web的目录列表
- grails项目中的java类中调用service方法
- 搜索框(历史记录)cookie
- 矩阵归零
- android dialog 的超简单使用
- Effective Modern C++: Item 4 -> 知道如何查看推断类型
- 对象作用域
- pytorch-cycleGAN网络文件的理解
- Session过期,如何跳出iframe框架页的问题
- 安卓实现listview联动效果且选项多选
- centos7 k8s1.5.2(kubernetes) etcd flannel dns dashboard nginx tomcat slb haproxy keepalived
- 【教程】如何在C#中创建PDF417条码