随笔分类 -  leetcode-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 public class Solution { 2 public int[][] generateMatrix(int n) { 3 int []count = {1}; 4 ... 阅读全文
posted @ 2014-02-17 02:16 krunning
摘要: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 阅读全文
posted @ 2014-02-17 01:44 krunning
摘要:The Sudoku board could be partially filled, where empty cells are filled with the character'.'. 1 public class Solution { 2 public boolean isValidSudoku(char[][] board) { 3 for(int i=0;i<9;i++){ 4 int []row = new int[10]; 5 int []col = new int[10]; 6 f... 阅读全文
posted @ 2014-02-10 15:33 krunning
摘要: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]. 1 public class Solution { 2 public ArrayList spiralOrder(int[][] matrix) { 3 ... 阅读全文
posted @ 2014-02-10 15:29 krunning
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place? 1 public class Solution { 2 public void rotate(int[][] matrix) { 3 int len = matrix.length; 4 if(len<=0) return; 5 for(int i=0;i<len-1;i++){... 阅读全文
posted @ 2014-02-08 05:53 krunning
摘要:1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 boolean row = false; 4 boolean col = false; 5 for(int i=0;i<matrix.length;i++){ 6 if(matrix[i][0]==0){ 7 row =true; 8 break; 9 }10 }11 ... 阅读全文
posted @ 2014-02-06 14:43 krunning