backtracing

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);
        }
    }

时间: 2024-11-06 13:55:47

backtracing的相关文章

[leetcode summary] Backtracing

主线是 res helper(res):dfs(): helper(res,num,curr, index, resultOne, visited..) { if(curr==num) 1 res.add(resultOne); 2 res.add(new resultOne); else { for() { if() 1: temp = new resultOne helper(temp...) 2 result.add(); visited; helper(); result.remove(

[leetcode] Word Search

题目:(Backtrancing) Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter

make

http://futeng.iteye.com/blog/2071867 http://zhou123.blog.51cto.com/4355617/1196415 w [email protected]52-248-ubuntu:/# wget http://download.redis.io/releases/redis-3.2.8.tar.gz --2017-04-28 21:43:43-- http://download.redis.io/releases/redis-3.2.8.tar

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

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

[leetcode] Word Break2

题目:(DP, Backtracing) Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat&qu

redis集群 部署操作流程

/************************操作说明************************************/ 1.本操作过程是在centOS7_64的环境下进行: 2.登录用户为root(管理员账号): 3.本流程中使用的redis.conf是自己已经配好的文件,进行细微修改即可(文件下载见:参考文献及资料下载路径): 4.使用的虚拟机为vmware 12.0.0 build-2985596 /***************************************

Dancing Links - nuclear weapon for Exact Cover problems

http://www.cnblogs.com/grenet/p/3145800.htmlBest elaboration on dancing list I found. in Chinese. Traditional backtracing is a heuristic-like procedure. Say, there are N items to try with one by one, when N[i] is picked, all rest N[i + 1, N-1] items

Redis【第一篇】安装

第一步:准备 1. 操作系统 CentOS-7-x86_64-Everything-1511 2. redis 版本 redis-3.2.8 3. 修改内核参数 有三种方式: 1)编辑/etc/sysctl.conf ,改vm.overcommit_memory=1,然后sysctl -p 使配置文件生效 2)sysctl vm.overcommit_memory=1 3)echo 1 > /proc/sys/vm/overcommit_memory 附:内核参数 overcommit_memo

Redis集群方案,Codis安装测试

1,关于豌豆荚开源的Codis Codis是豌豆荚使用Go和C语言开发.以代理的方式实现的一个Redis分布式集群解决方案,且完全兼容Twemproxy.Twemproxy对于上一层的应用来说, 连接Codis Proxy(Redis代理服务)和连接原生的Redis服务器没有明显的区别,上一层应用能够像使用单机的 Redis一样对待.Codis底层会处理请求的转发.不停机的数据迁移等工作, 所有底层的一切处理, 对于客户端来说是透明的.总之,可以简单的认为后台连接的是一个内存无限大的Redis服