这个题的思想很重要,两种方法

第一种,回溯法

 class Solution
{
public:
int sz;
vector<string> letterCasePermutation(string S)
{
vector<string> res;
sz=S.size();
backtrack(S,,res);
return res;
} void backtrack(string &s,int i,vector<string> &res)
{
if(i==sz)
{
res.push_back(s);
return ;
}
backtrack(s,i+,res);
if(isalpha(s[i]))
{
s[i]^=(<<);
backtrack(s,i+,res);
}
}
};

这个方法是先扫到末尾,再往回走,往回时遇到一个字母,改变其大小写,然后再把这个序列扫到末尾,改变之后的字母大小写。往回扫到第一个字符时,结束运行,输出结果。

第二种,顺序法

 class Solution
{
public:
int sz;
vector<string> letterCasePermutation(string S)
{
vector<string> res;
sz=S.size();
letterCasePermutation(S,,res);
return res;
} void letterCasePermutation(string &s,int idx,vector<string> &res)
{
res.push_back(s);
for(int i=idx;i<sz;i++)
{
char temp=s[i];
if(isalpha(s[i]))
{
s[i]^=(<<);
letterCasePermutation(s,i+,res);
}
s[i]=temp;
}
}
};

这个方法和回溯法有点类似,只是这个是从头扫到尾,扫到第一个字符,改变大小写,然后递归,扫之后的字符。递归完成后,说明改变这个字符之后,右边字符序列改变的组合已经完成了。然后把字符变回去,继续向右扫,直到扫描完成。

784. Letter Case Permutation的更多相关文章

  1. 【Leetcode_easy】784. Letter Case Permutation

    problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...

  2. 784. Letter Case Permutation 字符串中字母的大小写组合

    [抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...

  3. leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  4. LeetCode 784 Letter Case Permutation 解题报告

    题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...

  5. [LeetCode&Python] Problem 784. Letter Case Permutation

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  6. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  7. 【easy】784. Letter Case Permutation

    Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...

  8. 784. Letter Case Permutation C++字母大小写全排列

    网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...

  9. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

随机推荐

  1. PAT1103

    1103. Integer Factorization (30) 时间限制 1200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. 学习Auxre记录

    1.下载地址:https://www.axure.com.cn/3510/(可以免费试用30天) 2.视频教程:https://huke88.com/course/6455.html 3.文字教程:h ...

  3. 如何彻底卸载mysql(xp)

    如何彻底卸载mysql 完整的卸载MySQL 5.x 的方法: 1.控制面板里的增加删除程序内进行删除 2.删除MySQL的安装文件夹C:\Program Files\MySQL,如果备份好,可以直接 ...

  4. How to set the bash display to not show the vim text after exit?

    Xshell客户端在vim编辑文件保存退出,仍然显示文本内容,而不是回到shell terminal终端. 解决办法如下: User1 is using TERM=xterm, in this cas ...

  5. 【Linux 线程】常用线程函数复习《四》

    1.线程属性的设置 /************************************************************************* > File Name: ...

  6. Flexible variants in STVARV

    DATA: lv_time TYPE TVARV_VAL, lv_tvarvc(25) . CONSTANTS lv_prefix(25) VALUE 'ZZXXS_'. CONCATENATE lv ...

  7. C++ 计算直线的交点数(动态规划)

    问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 Problem Description 平面上有n条直线,且无三线共点,问这些直线能有多少种不同 ...

  8. android 下拉刷新框架PullToRefreshScrollView(com.handmark.pulltorefresh)

    很简单,实现OnRefreshListener这个监听器. mPullRefreshScrollView .setOnRefreshListener(new OnRefreshListener< ...

  9. 1.maven安装配置

    这段时间在做项目构建管理方面的工作,以前很多项目都是通过ant去构建的,虽然很早就接触过mavan,但是从没有系统的去学习过, 现在项目需要用maven来构建,我结合自己的心得整理一下放在博客上作为自 ...

  10. 4C - 七夕节

    七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" 人们纷纷来到告示前,都想知道谁才是自己 ...