大雪菜 — LeetCode刷题打卡活动第三期——week2 DFS专题(部分代码)

转自   https://www.bilibili.com/video/av34962180?t=1435&p=2

77,给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

vector<vector<int> >ans;
    vector<vector<int>> combine(int n, int k) {
        vector<int>way;
        dfs(way,1,n,k);
        return ans;
    }
    void dfs(vector<int>&way,int start,int n,int k)
    {
        if(k==0)
        {
            ans.push_back(way);
            return;
        }

        for(int i=start;i<=n;i++)
        {
            way.push_back(i);
            dfs(way,i+1,n,k-1);
            way.pop_back();
        }
    }

784,给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。

class Solution {
public:
    vector<string> ans;
    vector<string> letterCasePermutation(string S) {
        dfs(S,0);

        return ans;
    }
    void dfs(string S,int u)
    {
        if(u==S.size())
        {
            ans.push_back(S);
            return;
        }
        dfs(S,u+1);

        if(S[u]>=‘A‘)
        {
            S[u] ^=32;
            dfs(S , u + 1);
        }
    }
};

这一题 有一个挺秀的操作, S[u] ^=32    ,实现大小写的互换

这两题我觉得还不错,愿能记住!!!

========== ========== ========= ======= ======== ====== ===== ==== == =

求上进的人,不要总想着靠谁,人都是自私的,自己才是最靠得住的人。

原文地址:https://www.cnblogs.com/asdfknjhu/p/12535636.html

时间: 2024-08-30 10:22:09

大雪菜 — LeetCode刷题打卡活动第三期——week2 DFS专题(部分代码)的相关文章

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

leetcode 刷题之路 94 N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

【leetcode刷题笔记】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetcode 刷题之路 77 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Permutations 的升级版,依旧是全排列问题,但是序列中可能会出现重复数字. 思路:采用字典序的非递归方

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

【leetcode刷题笔记】3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

【leetcode刷题笔记】Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

LeetCode刷题(一):Two Sum

今天开始在LeetCode刷题,第一题为"两数之和(Two Sum)",整体来讲这一题难度是比较低的,但还是在这个过程中遇到一些问题,以下主要记录出现的一些问题. 这个题目比较容易想到的方法便是穷举了,很暴力但也很直接,需要注意的一点便是向量库Vector,自己也是前一阵子开始学数据结构才知道有这个库(有的也称为容器),定义计算数组长度用的方法是size()而不是length(),官方解答给的是length(),不知道是不是没有注意到还是因为他用的代码是Java(本人不了解Java),

LeetCode刷题总结之双指针法

Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链表里是两个指针,一般能实现O(n)的时间解决问题,两个指针的位置一般在第一个元素和第二个元素或者第一个元素和最后一个元素,快指针在前“探路”,当符合某种条件时慢指针向前挪 盛最多水的容器 这道题其实是求最大面积,最大面积取决于较小值.初始时两指针分别位于第一和最后一个元素处,那么明确指针应该向什么方