5月10日

1 37  Sudoku Slover

 public void solveSudoku(char[][] board) {
if(board == null || board.length == 0)
return;
slove(board);
}
public boolean slove(char[][] board){
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[0].length; j++)
{
if (board[i][j] == '.')
{
for (char c = '1'; c <= '9'; c++)
{
if (isValue(board, i, j, c))
{
board[i][j] = c;
if (slove(board))
return true;
else
board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
public boolean isValue(char[][] board, int row, int col, char c)
{
for (int i = 0; i < 9; i++)
{
if (board[i][col] != '.' && board[i][col] == c) return false;
if (board[row][i] != '.' && board[row][i] == c) return false;
if (board[3 * (row/3) + i/3][3 *(col/3) + i % 3] != '.' &&
board[3 * (row/3) + i/3][3 *(col/3) + i % 3] == c) return false;
}
return true;
}

2 51 N-Queens

  public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
help(n, 0, new int[n], res);
return res;
}
public void help(int n, int row, int[] colforrow, List<List<String>> res)
{
if (row == n)
{
ArrayList<String> item = new ArrayList<>();
for (int i = 0; i < n; i++)
{
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++)
{
if (colforrow[i] == j)
sb.append('Q');
else
sb.append('.');
}
item.add(sb.toString());
}
res.add(item);
return;
}
for (int i = 0; i < n; i++)
{
colforrow[row] = i;
if (check(row, colforrow))
{
help(n, row + 1, colforrow, res);
}
}
}

3 52 N-Queens II

    public int totalNQueens(int n) {
ArrayList<Integer> res = new ArrayList<>();
res.add(0);
help(n, 0, new int[n], res);
return res.get(0);
}
private void help(int n, int row, int[] columnForRow, ArrayList<Integer> res)
{
if(row==n)
{
res.set(0,res.get(0)+1);
return;
}
for(int i=0;i<n;i++)
{
columnForRow[row]=i;
if(check(row,columnForRow))
{
help(n,row+1,columnForRow,res);
}
}
}
private boolean check(int row, int[] columnForRow)
{
for(int i=0;i<row;i++)
{
if(columnForRow[i]==columnForRow[row] || Math.abs(columnForRow[row]-columnForRow[i])==row-i)
return false;
}
return true;
}

4 77  combinations  分子问题

    public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if (n <= 0 || n < k) return res;
help(n, k, 1, new ArrayList<Integer>(), res);
return res;
}
public void help(int n, int k, int start, ArrayList<Integer> item, List<List<Integer>> res)
{
if (item.size() == k)
{
res.add(new ArrayList<Integer>(item));
return;
}
for (int i = start; i <= n; i++)
{
item.add(i);
help(n, k, i + 1, item, res);
item.remove(item.size() - 1);
}
}

5 89 Gray Code

    public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
res.add(0);
for (int i = 0; i < n; i++)
{
int size = res.size();
for (int k = size - 1; k >= 0; k--)
{
res.add(res.get(k)|1<<i);
}
}
return res;
}

6 211 Add and search Word

public class WordDictionary {

    public class TrieNode{
public TrieNode[] children = new TrieNode[26];
public String item = "";
}
private TrieNode root = new TrieNode();
/** Initialize your data structure here. */
public WordDictionary() {
} /** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray())
{
if (node.children[c - 'a'] == null)
{
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.item = word;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return match(word.toCharArray(), 0, root);
}
private boolean match(char[] chs, int k, TrieNode node)
{
if (k == chs.length) return !node.item.equals("");
if (chs[k] != '.')
{
return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k]-'a']);
}
else
{
for (int i = 0; i < node.children.length; i++)
{
if (node.children[i] != null)
{
if (match(chs, k + 1, node.children[i]))
return true;
}
}
}
return false;
}
}

7 212 Word Search II

    Set<String> res = new HashSet<>();
public List<String> findWords(char[][] board, String[] words) {
Trie trie = new Trie();
for (String str : words)
{
trie.insert(str);
}
int m = board.length, n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
dfs(board, visited, "", i, j, trie);
}
}
return new ArrayList<String>(res);
}
public void dfs(char[][] board, boolean[][] visited, int i, int j, String item, Trie trie)
{
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return;
if (visited[i][j]) return;
item += board[i][j];
if (!trie.startsWith(item)) return;
if (trie.search(item)) res.add(item);
visited[i][j] = true;
dfs(board, visited, i, j - 1, item, trie);
dfs(board, visited, i, j + 1, item, trie);
dfs(board, visited, i + 1, j, item, trie);
dfs(board, visited, i - 1, j, item, trie);
visited[i][j] = false;
}

backtracing的更多相关文章

  1. 关于Backtracing中有重复元素的处理办法

    backtracing是一个常用的解法.之前遇到一个题目,求一个集合的子集, 例如给定{1,2,3,4,5},求其大小为3的子集. 利用backtracing可以较快的给出答案. 然而,该题还有一个变 ...

  2. net-force.nl/programming writeup

    从 wechall.net 到 net-force.nl 网站,发现网站的内容不错,里面也有不同类型的挑战题目:Javascript / Java Applets / Cryptography / E ...

  3. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  4. POJ #1015 - Jury Compromise - TODO: POJ website issue

    (poj.org issue. Not submitted yet) This is a 2D DP problem, very classic too. Since I'm just learnin ...

  5. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. redis的管理工具

    phpredisadmin工具 rdbtools管理工具 saltstack管理redis 通过codis完成redis管理 一:phpredisadmin工具:类似于mysqladmin管理mysq ...

  9. Redis【第一篇】安装

    第一步:准备 1. 操作系统 CentOS-7-x86_64-Everything-1511 2. redis 版本 redis-3.2.8 3. 修改内核参数 有三种方式: 1)编辑/etc/sys ...

随机推荐

  1. SecureCRT的安装以及破破解(内含安装包)

    1.百度网盘连接:链接:https://pan.baidu.com/s/13i8sblGthYtj2SbUTrbmsg  提取码:8cw1 2.解压前先关闭电脑防护软件,否则会杀掉破解软件的 3.压缩 ...

  2. TCP UDP socket http webSocket 之间的关系

    ---恢复内容开始--- OSI&TCP/IP模型 要弄清tcp udp socket http websocket之间的关系,首先要知道经典的OSI七层模型,与之对应的是TCP/IP的四层模 ...

  3. Java学习笔记——设计模式之六.原型模式(浅克隆和深克隆)

    That there's some good in this world, Mr. Frodo. And it's worth fighting for. 原型模式(prototype),用原型实例指 ...

  4. JS中的闭包 详细解析大全(面试避必考题)

    JS中闭包的介绍   闭包的概念 闭包就是能够读取其他函数内部变量的函数. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变 ...

  5. 机器学习中K-means聚类算法原理及C语言实现

    本人以前主要focus在传统音频的软件开发,接触到的算法主要是音频信号处理相关的,如各种编解码算法和回声消除算法等.最近切到语音识别上,接触到的算法就变成了各种机器学习算法,如GMM等.K-means ...

  6. 使用NLog记录业务日志到数据库

    项目中很多时候要记录业务日志,其实是可以直接用日志框架计入数据库的. 使用NLog并不是只能将日志主体插入数据库,而是可以根据实际情况自定义任意列记入.非常方便.而且很容易实现 下面是用NLog记录业 ...

  7. ajax 前端发含有列表的数据

    在前端页面也可以给后端发送一个包含列表的数据 html <body> <h3>index页面 </h3> <input type="text&quo ...

  8. 图片去水印工具:Inpaint 7.2中文专业破解版下载及使用方法

    下载地址: 点我 Inpaint 是一款可以从图片上去除不必要的物体,让您轻松摆脱照片上的水印.划痕.污渍.标志等瑕疵的实用型软件:简单说来,Inpaint 就是一款强大实用的图片去水印软件,您的图片 ...

  9. Django rest framework(4)----版本

    目录 Django组件库之(一) APIView源码 Django restframework (1) ----认证 Django rest framework(2)----权限 Django res ...

  10. mybatis基础配置

    我这个写的比较简略,是自己短时间记录的可能只适合自己看,新手或者不懂的建议看看下面大神这篇: https://www.cnblogs.com/homejim/p/9613205.html 1.MyBa ...