【leetcode刷刷】39. 组合总和、40.组合总和II、131.分割回文串

2024-02-02 18:52

本文主要是介绍【leetcode刷刷】39. 组合总和、40.组合总和II、131.分割回文串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

39. 组合总和

  1. 一开始写的时候没注意到可以重复,注意到可以重复之后就去掉了start_index,但是出现了类似[2,2,3][2,3,2]这种重复。看了题解之后,发现加上start_index,但是进for循环的时候start_index还是i,这样就是既可以重复也不会重新取之前的数。
class Solution:def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:self.res = []self.backtracking(candidates, target, [], 0)return self.resdef backtracking(self, candidates, target, path, start_index):if target == 0:# print(path)self.res.append(path[:])return if target < 0: return for i in range(start_index, len(candidates)):if target-candidates[i] < 0:continuepath.append(candidates[i])self.backtracking(candidates, target-candidates[i], path, i)path.pop()

40.组合总和II

  1. 相比于之前这个题的区别,就是这个在candidates里可以重复,但是最后res里不能有重复的list。想要去重还挺难想的,不过题解里给了一个思路,就是在树的同一层进行操作,即可进行去重操作。递归的时候不在树的同一层,因此不需要有去重操作。
class Solution:def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:self.res = []candidates.sort()self.backtracking(candidates, target, 0, [])return self.resdef backtracking(self, candidates, target, start_index, path):if target == 0:self.res.append(path[:])returnfor i in range(start_index, len(candidates)):if target-candidates[i] < 0: continue   # 剪枝# 树的同一层,如果有重复的,跳过if candidates[i] == candidates[i-1] and i > start_index: continuepath.append(candidates[i])self.backtracking(candidates, target-candidates[i], i+1, path)path.pop()

131.分割回文串

  1. 这题好难。。没有思路。。根本想不到分割和回溯又什么关系。所以直接看的题解。可能需要复习复习。
  2. 题解给的解释:
    - 递归用于纵向遍历
    - for循环用于横向遍历
    - 当切割线迭代至字符串末尾,说明找到一种方法
    - 类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线)
    - 关于模拟切割线,其实就是index是上一层已经确定了的分割线,i是这一层试图寻找的新分割线
class Solution:def partition(self, s: str) -> List[List[str]]:self.res = []self.backtracking(s, 0, [])return self.resdef backtracking(self, s, start_index, path):if start_index == len(s):self.res.append(path[:])return for i in range(start_index, len(s)):  # 用start_index作为分割点if self.is_palindrome(s, start_index, i):path.append(s[start_index:i+1])self.backtracking(s, i+1, path)path.pop()def is_palindrome(self, s, start, end):i = startj = endwhile(i < j):if s[i] != s[j]:return Falsei += 1j -= 1return True
# 若反序和正序相同,意味着这是回文串
if s[start_index: i + 1] == s[start_index: i + 1][::-1]:

这篇关于【leetcode刷刷】39. 组合总和、40.组合总和II、131.分割回文串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/671638

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ

csu1328(近似回文串)

题意:求近似回文串的最大长度,串长度为1000。 解题思路:以某点为中心,向左右两边扩展,注意奇偶分开讨论,暴力解即可。时间复杂度O(n^2); 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>#include<string>#inclu

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

从0到1,AI我来了- (7)AI应用-ComfyUI-II(进阶)

上篇comfyUI 入门 ,了解了TA是个啥,这篇,我们通过ComfyUI 及其相关Lora 模型,生成一些更惊艳的图片。这篇主要了解这些内容:         1、哪里获取模型?         2、实践如何画一个美女?         3、附录:               1)相关SD(稳定扩散模型的组成部分)               2)模型放置目录(重要)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点