排列组合
文章平均质量分 95
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 1079. Letter Tile Possibilities【哈希表,回溯,动态规划,排列组合】中等
根据上面的讨论,定义fif[i]fi表示用前iii种字符构造长为jjj的序列的方案数。设第iii种字符有cntcnt如果一个也不选,那么fijfi−1jfijfi−1j。如果选kkk个,那么需要从jjj个位置中选kkk个放第iii种字符,其余位置就是用前i−1i−1i−1种字符构造长为j−kj-kj−k的序列的方案数,所以有fijfi−1j−k⋅jkfijfi−1j−k⋅k。原创 2023-05-19 15:43:07 · 977 阅读 · 0 评论 -
LeetCode 2437. Number of Valid Clock Times【分类讨论,乘法原理,枚举】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-05-11 17:25:46 · 464 阅读 · 0 评论 -
LeetCode 1617. Count Subtrees With Max Distance Between Cities【数学,二进制枚举,位操作,状压,树型DP,BFS,DFS】困难
There aren1tonedgesof sizen-1, whereuiandviAFor eachdfrom1ton-1dReturnn-1where thedthelementdthat the(ui, vi)题意:给你n个城市,编号为从1到n。给出一个大小为n-1的数组edges,其中表示城市ui和vi之间有一条双向边。题目保证任意城市之间只有唯一的一条路径。即所有城市形成了一棵。一棵是城市集合的一个子集,且子集中任意城市都可以通过子集中的其他城市和边到达。原创 2023-03-13 02:11:41 · 75 阅读 · 0 评论 -
LeetCode 62. Unique Paths【动态规划;数学,排列组合】中等
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the dia原创 2020-06-28 16:11:20 · 344 阅读 · 1 评论 -
LeetCode C++ 面试题 08.07. Permutation I LCCI【Backtracking/Recursion】中等
Write a method to compute all permutations of a string of unique characters.Example1:Input: S = "qwe"Output: ["qwe", "qew", "wqe", "weq", "ewq", "eqw"]Example2:Input: S = "ab"Output: ["ab", "ba"]Note:All charaters are English letters.1 <= S.原创 2020-11-30 15:10:49 · 240 阅读 · 0 评论 -
LeetCode C++ 面试题 08.04. Power Set LCCI【Bit Manipulation/Recursion】中等
Write a method to return all subsets of a set. The elements in a set are pairwise distinct.Note: The result set should not contain duplicated subsets.Example:Input: nums = [1,2,3]Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]题意原创 2020-11-28 15:32:05 · 212 阅读 · 0 评论 -
LeetCode C++ 961. N-Repeated Element in Size 2N Array【哈希表/数学】简单
In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times.Example 1:Input: [1,2,3,3]Output: 3 Example 2:Input: [2,1,2,5,3,2]Output: 2Example 3:Input: [5,原创 2020-09-26 23:14:36 · 133 阅读 · 0 评论 -
LeetCode C++ 78. Subsets【位操作/回溯】中等
Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]题意:给原创 2020-09-20 09:54:13 · 138 阅读 · 1 评论 -
LeetCode C++ 31. Next Permutation【Array】中等
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement mus原创 2020-06-03 20:46:59 · 187 阅读 · 0 评论 -
【算法学习】搜索专题 排列、组合和子集生成
文章目录一、递归和排列二、子集生成三、组合生成搜索是基础的编程技术,是基础中的基础,非常非常重要,而且也有难度。我们必须认真学习。搜索体现了暴力法的思想,有很多用途:很多问题只能用暴力搜索解决,如猜密码。对小规模的数据,完全足够,而且简单不易出错。暴力法往往用于参照,可以从它出发,逐步思考更高级的算法,而且在生成测试数据进行对拍上面很有用。暴力法往往可以进行优化,使用剪枝跳过不符合要...原创 2020-11-30 11:50:12 · 410 阅读 · 0 评论 -
【算法学习】康托展开(全排列中某一排列的字典序计算问题)
文章目录一、康托展开介绍二、手动计算Cantor展开三、代码模板这也是排列组合中的一个问题,不是【搜索】的技巧,但也有一定的关系,可以简化搜索状态的复杂度。如果,在某一个问题中,我们想知道一个排列是否出现过,由于全排列有N!个,我们一一比对,要O(N!)的复杂度。这就会拖累整体算法。下面我们运用一种方法——康托展开,用O(N!)个bool的空间,得到O(N2)的复杂度。一、康托展开介绍康...原创 2020-02-28 15:52:46 · 454 阅读 · 0 评论 -
STL next_permutation和prev_permutation 算法原理和自行实现
文章目录一、本文目标二、next_permutation算法和思想三、next_permutation具体实现四、prev_permutation算法和思想五、prev_permutation具体实现六、总结和分析一、本文目标在【其一 排列组合和子集生成】这篇文章中,我列举了多种实现排列和组合的方法。但是,那是以求出全部的结果为目标的函数。如果,要求给出某个排列的前/下一个排列(字典序),那...原创 2020-03-11 22:45:48 · 1444 阅读 · 0 评论 -
POJ 程序设计与算法(二)第02周测验(2020春季) 003:全排列
总时间限制: 1000ms内存限制: 65536kB描述给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列。 我们假设对于小写字母有’a’ < ‘b’ < … < ‘y’ < ‘z’,而且给定的字符串中的字母已经按照从小到大的顺序排列。输入输入只有一行,是一个由不同的小写字母组成的字符串,已知字符串的长度在1到6之间。输出输出这个...原创 2020-03-12 13:28:29 · 232 阅读 · 0 评论 -
Leetcode C++ 第24场双周赛——5372. 逐步求和得到正数的最小值 | 5373. 和为K的最少斐波那契数字数目 | 5374. 长度为n的开心字符串中字典序第k小的字符串
文章目录5372. 逐步求和得到正数的最小值(1) 思路(2) 我的代码(3) 更好的代码5373. 和为 K 的最少斐波那契数字数目(1) 思路(2) 我的代码(3) 更好的代码5374. 长度为n的开心字符串中字典序第k小的字符串(DFS)(1) 思路(2) 我的代码(3) 更好的代码5372. 逐步求和得到正数的最小值给你一个整数数组 nums 。你可以选定任意的 正数 startVal...原创 2020-04-19 01:37:21 · 390 阅读 · 0 评论