随笔分类 - Matrix
摘要:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]生成的过程为 (1) 横向生成 1,2 (2) 纵向生成 3,4 (3) 横向生成 5,6 (4) 纵向生成 7,8 (5) 生成 9public class ...
阅读全文
摘要:The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and
阅读全文
摘要:Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return[1,2,3,6,9,8,7,4,5].相当于一圈一圈的剥掉矩阵,剥掉一圈,row-2, col-2 1 public class Solution { 2 public ArrayList spiralO...
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?参考:水中的鱼: [LeetCode]Rotate Image解题报告[解题思路]如下图,首先沿逆对角线翻转一次,然后按x轴中线翻转一次。 1 public class Solution { 2 public void rotate(int[][] matrix) { 3 if(matrix.length == ...
阅读全文
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.二维DP。设数组A[row][col],Min[i][j] = min(Min[i-1][j], Min[i][j-1]) +A[i][j];注意初始条件即可。public class
阅读全文
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1,
阅读全文
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be only one unique solution.A sudoku puzzle......and its solution numbers marked in red. 1 public class Solution { 2 public void solveSudoku(char[]
阅读全文
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially filled sudoku which is valid.rules:1. 同一行中1-9出现次数不重复2. 同一列中1-9出现次数不重复3. 9宫格中1-9出现次数不重复九宫格 block的顺序0 1 23 4 56 7 8
阅读全文
摘要:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni
阅读全文
摘要:public class Solution { public void setZeroes(int[][] matrix) { if(matrix.length == 0) { return; } int r = matrix.length; int c = matrix[0].length; boolean rZeros = false; boolean cZeros = false; for(int i = 0 ; i < r ; i++){...
阅读全文

浙公网安备 33010602011771号