2024/9/11 LeetCode Hot100 backtracking以及上周周赛

This commit is contained in:
Cool 2024-09-11 23:53:17 +08:00
parent 0b1434aacb
commit ecf2f98b85
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package com.cool.hot100.backtracking;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/11/21:21
* DayNumber 1
* Hard 2
* Level 4
*/
public class Num79 {
public boolean exist(char[][] board, String word) {
if (board.length < 1) {
return false;
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
if (dfs(board, word, i, j, 0)) {
return true;
}
}
}
}
return false;
}
private boolean dfs(char[][] chars, String word, int x, int y, int index) {
if (index == word.length()) {
return true;
}
if (x < 0 || x >= chars.length || y < 0 || y >= chars[0].length) {
return false;
}
if (chars[x][y] == '1') {
return false;
}
boolean is = false;
if (word.charAt(index) == chars[x][y]) {
char temp = chars[x][y];
chars[x][y] = '1';
is =dfs(chars, word, x + 1, y, index + 1) || dfs(chars, word, x, y + 1, index + 1)
|| dfs(chars, word, x - 1, y, index + 1) || dfs(chars, word, x, y - 1, index + 1);
chars[x][y] = temp;
return is;
} else {
return false;
}
}
}

View File

@ -0,0 +1,36 @@
package com.cool.week414;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/08/11:28
* ProblemNumber
*/
public class Problem1 {
public String convertDateToBinary(String date) {
String[] dates=date.split("-");
String res="";
for(int i=0;i<dates.length;i++){
int dateInt=Integer.parseInt(dates[i]);
res+=trans(dateInt);
if(i!=dates.length-1){
res+="-";
}
}
return res;
}
private String trans(int date){
if(date==0){
return "0";
}
String res="";
while(date!=0){
int num=date%2;
date=date/2;
res=String.valueOf(num)+res;
}
return res;
}
}