通过递归的方法去写会相对简单,但运行时的耗时及内存占用会更多。
非递归的写法会要考虑很多的场景,需要额外的数据结构辅助,代码量偏多,但是运行耗时少,空间占用少。
1. 矩阵中的路径
矩阵中的路径_牛客题霸_牛客网
递归写法:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix char字符型二维数组
* @param word string字符串
* @return bool布尔型
*/
public boolean hasPath (char[][] matrix, String word) {
if(word.length() == 0){
return true;
}
char[] w = word.toCharArray();
int m = matrix.length;
int n = matrix[0].length;
int k = 0;
for(int i = 0; i < m ; i++){
for(int j = 0; j < n; j++){
if(dfs(matrix, w, i, j, k)){
return true;
}
}
}
return false;
}
public boolean dfs(char[][] matrix, char[] w, int i, int j, int k){
if(i > matrix.length - 1 || i < 0 || j > matrix[0].length -1 || j < 0 || matrix[i][j] != w[k]) {
return false;
}
if(k == w.length - 1){
return true;
}
char t = matrix[i][j];
matrix[i][j] = '\0';
boolean re = dfs(matrix, w, i-1, j, k + 1)
|| dfs(matrix, w, i+1, j, k + 1)
|| dfs(matrix, w, i, j-1, k + 1)
|| dfs(matrix, w, i, j+1, k + 1);
matrix[i][j] = t;
return re;
}
}
非递归:
public boolean hasPath (char[][] matrix, String word) {
int len = word.length();
if(len == 0){
return true;
}
int m = matrix.length;
int n = matrix[0].length;
if(m == 0 && n == 0){
return false;
}
Stack<Node> s = new Stack();
int count;
boolean re;
Node node;
for(int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if(word.charAt(0) == matrix[i][j]){
Node no = new Node(i, j);
s.add(no);
count = 1;
node = null;
while(count < len){
re = this.next(node, s, matrix, word.charAt(count));
if(re){
count++;
if(count == len){
return true;
}
node = null;
}else{
if(!s.empty()){
node = s.pop();
count--;
}else{
break;
}
}
}
if (count == len){
return true;
}
}
}
}
return false;
}
public boolean next(Node node, Stack<Node> s, char[][] matrix, char c){
if(s.empty()){
return false;
}
Node o = s.peek();
int m = matrix.length;
int n = matrix[0].length;
Node pre = null;
if(s.size() >= 2){
pre = s.get(s.size() - 2);
}
int nX = 0;
int nY = 0;
boolean re = false;
//上, 左, 下, 右顺序检查
int location = 1;
if(node != null){
if(node.x + 1 == o.x){
location = 2;
}
else if(node.y + 1 == o.y){
location = 3;
}else if(node.x - 1 == o.x){
location = 4;
}else{
return false;
}
}
while(location <= 4){
re = true;
switch(location) {
case 1:
//特别注意这时的等式容易写反
nX = o.x - 1;
nY = o.y;
if (nX < 0) {
re = false;
}
break;
case 2:
nX = o.x;
nY = o.y - 1;
if (nY < 0) {
re = false;
}
break;
case 3:
nX = o.x + 1;
nY = o.y;
if (nX >= m) {
re = false;
}
break;
case 4:
nX = o.x;
nY = o.y + 1;
if (nY >= n) {
re = false;
}
break;
}
location++;
if(re) {
if(matrix[nX][nY] == c){
if((pre != null && (nX != pre.x || nY != pre.y))) {
s.add(new Node(nX, nY));
return true;
}else if (pre == null) {
s.add(new Node(nX, nY));
return true;
}
}
}
}
return false;
}
public class Node{
int x = 0;
int y = 0;
public Node(int x, int y){
this.x = x;
this.y = y;
}
}
2.
电话号码的字母组合
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
import java.util.*;
public class LetterCon {
public static void main(String[] args){
LetterCon l = new LetterCon();
ArrayList<String> li = l.getCon("25");
System.out.println(li);
}
public ArrayList<String> getCon(String digits){
String[] str = {"", "", "adc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ArrayList<String> li = new ArrayList<>();
String s = new String();
if (digits.length() == 0) {
return li;
}else{
for(int i = 0; i < str[Integer.valueOf(digits.substring(0, 1))].length(); i++){
get(str, digits, 0, i, s, li);
}
}
return li;
}
public void get(String[] str, String digits, int k, int i, String s, ArrayList<String> li){
s = s + str[Integer.valueOf(digits.substring(k, k+1))].charAt(i);
if(k == digits.length() - 1){
li.add(s);
return;
}
for(int j = 0; j < str[Integer.valueOf(digits.substring(k + 1, k + 2))].length(); j++){
get(str, digits, k+1, j, s, li);
}
}
}