36-40题 题解

本文介绍了Sudoku的有效性验证及求解算法实现,包括通过模拟判断有效性的简单方法和使用递归回溯解决Sudoku谜题的暴力算法。此外,还涉及了计数并叙述序列的生成方法以及组合总和问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

36Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

//模拟,就是简单判断下有没有重复
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        map<char, int> mp;
        for(int i = 0; i < 9; i++)
        {
            mp.clear();
            for(int j = 0; j < 9; j++)
            {
                if(board[i][j] == '.') continue;
                mp[board[i][j]]++;
                if(mp[board[i][j]] == 2) return false;
            }
            mp.clear();
            for(int j = 0; j < 9; j++)
            {
                if(board[j][i] == '.') continue;
                mp[board[j][i]]++;
                if(mp[board[j][i]] == 2) return false;
            }
        }
        for(int i = 0; i < 9; i += 3)
        {
            for(int j = 0; j < 9; j += 3)
            {
                mp.clear();
                for(int k = i; k < i+3; k++)
                {
                    for(int kk = j; kk < j+3; kk++)
                    {
                        if(board[k][kk] == '.') continue;
                        mp[board[k][kk]]++;
                        if(mp[board[k][kk]] == 2) return false;
                    }
                }
            }
        }
        return true;
    }
};
37 Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.


A sudoku puzzle...


...and its solution numbers marked in red.


//暴力
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board, int i, int j) {
        for(int k = 0; k < 9; k++)
        {
            if(k == i) continue;
            if(board[k][j] == board[i][j]) return false;
        }
        for(int k = 0; k < 9; k++)
        {
            if(k == j) continue;
            if(board[i][k] == board[i][j]) return false;
        }
        for(int k = i/3*3; k < i/3*3+3; k++)
        {
            for(int kk = j/3*3; kk < j/3*3+3; kk++)
            {
                if(k != i && kk != j && board[k][kk] == board[i][j]) return false;
            }
        }
        return true;
    }
    bool solveSudoku(vector<vector<char>>& board) {
        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if(board[i][j] == '.')
                {
                    for(int k = 1; k <= 9; k++)
                    {
                        board[i][j] = k+'0';
                        if(isValidSudoku(board, i, j) && solveSudoku(board)) return true;
                        board[i][j] = '.';
                    }
                    return false;
                }
            }
        }
        return true;
    }

};

38 Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"
//刚开始没能理解题意,然后看别人博客,原来是统计上一个字符串数字的个数
class Solution {
public:
    string countAndSay(int n) {
        string s = "1", e;
        for(int i = 2; i <= n; i++)
        {
            int j = 0, len = s.length();
            e = "";
            while(j < len)
            {
                int k = j;
                while(s[j] == s[k]) k++;
                e += k-j+'0';
                e += s[j];
                j = k;
            }
            s = e;
        }
        return s;
    }
};
39 Combination Sum

Given a set of candidate numbers (C(without duplicates) 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 be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7
A solution set is: 

[
  [7],
  [2, 2, 3]
]
//贼暴力的方法。。。
class Solution {
public:
    void dfs(int i, int len, vector<int> &nums, int target, int sum, vector<int> x)
    {
        if(i == len)
        {
            if(sum == target) ans.push_back(x);
            return ;
        }
        if(sum > target) return ;
        dfs(i+1, len, nums, target, sum, x);
        for(int j = 1; ; j++)
        {
            if(target < sum+nums[i]*j) break;
            x.push_back(nums[i]);
            dfs(i+1, len, nums, target, sum+nums[i]*j, x);
        }
    }
    vector<vector<int>> ans;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> x;
        dfs(0, candidates.size(), candidates, target, 0, x);
        return ans;
    }
};
40 Combination Sum II

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

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8
A solution set is: 

[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
//详见39题
class Solution {
public:
    void dfs(int i, int len, vector<int> &nums, int target, int sum, vector<int> x)
    {
        if(i == len)
        {
            if(sum == target) ans.push_back(x);
            return ;
        }
        if(sum > target) return ;
        int l = i, r = i;
        while(r < len && nums[l] == nums[r]) r++;
        dfs(r, len, nums, target, sum, x);
        for(int j = 1; j <= r - l; j++)
        {
            x.push_back(nums[i]);
            dfs(r, len, nums, target, sum+nums[i]*j, x);
        }
    }
    vector<vector<int>> ans;
     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<int> x;
        dfs(0, candidates.size(), candidates, target, 0, x);
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值