剑指offer 11,12
方法1:
1 public static int method2(int[] numbers){ 2 if(numbers.length == 1) return numbers[0]; 3 else{ 4 int firstNum,secondNum ,min = numbers[0]; 5 for(int i = 0; i < numbers.length - 1; i++) { 6 firstNum = numbers[i]; 7 secondNum = numbers[i+1]; 8 if(firstNum>secondNum) return secondNum; 9 10 } 11 return min; 12 } 13 }
2.矩阵中的路径
方法1:
1 class Solution { 2 3 4 public boolean exist(char[][] board,String word){ 5 6 for (int i = 0; i < board.length; i++) { 7 for (int j = 0; j < board[0].length; j++) { 8 if(board[i][j] == word.charAt(0)){ 9 int[][] indexState = new int[board.length][board[0].length]; 10 int wordIndex = 0; 11 indexState[i][j] = 1; 12 wordIndex ++; 13 int x = i,y=j; 14 // 从第一个符合word单词第一个字的位置向周边搜索 15 while(true){ 16 if(wordIndex == word.length()) return true; 17 else if(isRight(board,indexState,x-1,y,word.charAt(wordIndex))) x--; 18 else if(isRight(board,indexState,x,y-1,word.charAt(wordIndex))) y--; 19 else if(isRight(board,indexState,x+1,y,word.charAt(wordIndex))) x++; 20 else if(isRight(board,indexState,x,y+1,word.charAt(wordIndex))) y++; 21 else break; 22 wordIndex++; 23 } 24 25 } 26 } 27 } 28 29 return false; 30 } 31 32 public static boolean isRight(char[][] board,int[][] indexState,int x,int y,char target){ 33 if(x>=0 && x <board.length && y >=0 && y < board[0].length && indexState[x][y] != 1 && board[x][y] == target) 34 return true; 35 return false; 36 } 37 }