文章目录 一、题目二、题解 一、题目
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answe…
文章目录 一、题目二、题解 一、题目
A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
For example, “0.1.2.201” and “192.168.1.1” are valid IP add…
问题:在一个N*N的棋盘上摆放N个“皇后”,且两两不在同一直线和斜线上,求有多少种摆法
解法:使用递归和回溯的思想求解
代码:
#include <iostream>
using namespace std;
const int N 8;// N皇后问题
int map…
文章目录 一、题目二、题解 一、题目
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Example 1:
Input: nums [1,1,2] Output: [[1,1,2], [1,2,1], [2,1,1]] Example 2:
Input: nums [1,…
剑指 Offer 12. 矩阵中的路径 - 力扣(LeetCode) (leetcode-cn.com)
目录
非递归写法(含注释)
运行结果
递归写法
运行结果 非递归写法(含注释)
class Solution {enum class direction { N, U, D, L,…
1.组合总和问题
原题:力扣39.
元素可以重复拿取,且题目的测试用例保证了组合数少于 150 个。
class CombinationSum {List<List<Integer>> res new ArrayList<>();List<Integer> path new ArrayList<>();public List…
文章目录 一、题目二、题解 一、题目
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example …
文章目录 一、题目二、题解 一、题目
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must oc…
131. 分割回文串 思想:回溯三步骤!① 传入参数 ② 回溯结束条件 ③ 单层搜索逻辑!抽象成回溯树,树枝上是每次从头部穷举切分出的子串,节点上是待切分的剩余字符串【从头开始每次往后加一】 class Solution:def partiti…
文章目录 一、题目二、题解 一、题目
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums [1,2,3] Outpu…
文章目录 一、题目二、题解 一、题目
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone bu…
今天学习kmp算法,看了好多大佬的文章,总算是解决了理解层面上的问题,现在和大家分享一下。
kmp算法是什么
KMP算法是一种改进的字符串匹配算法 主串 为:a b a c a a b a c a b a c 模式串 为:a b a c a b 目的就是为…
矩阵中的路径(回溯)/pair的学习问题分析示例代码pair学习问题
来自力扣:
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按…
//组合
//1---5里面5个数选出来3个 #include<iostream>
using namespace std;
int v[6] { 0 };
int a[4];
int sum 0;
void f(int num,int flag, int n){if (n 3){sum;for (int l 0; l < 3; l)printf("%d ", a[l]);printf("\n");}else{for (…
文章目录 一、题目二、题解 一、题目
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example 1:
I…
题目描述
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相…
递归为什么这么难?一篇文章带你了解递归
美国计算机科学家——彼得多伊奇(L Peter Deutsch)在《程序员修炼之道》(The Pragmatic Programmer)一书中提到“To Iterate is Human, to Recurse, Divine”——我理解的这句话为:人理解迭代,神理解…
文章目录 一、题目二、题解 一、题目
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
Th…
leetcode题目链接 17 216 216.组合总和III
class Solution {List<Integer> path new ArrayList();List<List<Integer>> result new ArrayList();public List<List<Integer>> combinationSum3(int k, int n) {backTrace(k,n,1,0);return resu…
文章目录 一、题目二、题解 一、题目
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in…
符号三角形问题题目信息测试样例解答想法题目信息 输入:n (1<n<23) 输出不同方案的个数
测试样例
34解答
#include<iostream>
#include<algorithm>
#define MAXN 24
using namespace std;
int n;int triangle[MAXN][MAXN];
int half;
int counts;
int ans…
93.复原 IP 地址 78. 子集 90. 子集 II 复原 IP 地址
class Solution {List<String> result new ArrayList();public List<String> restoreIpAddresses(String s) {backTrace(s,0,0);return result;}public void backTrace(String s, int startIndex,int pointSu…
题目链接 Leetcode.2698 求一个整数的惩罚数 rating : 1679 题目描述
给你一个正整数 n n n ,请你返回 n n n 的 惩罚数 。 n n n 的 惩罚数 定义为所有满足以下条件 i i i 的数的平方和: 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n i ∗ i i * i i∗i 的…