1--打印字符串的全部子序列
题目:
返回一个字符串的全部子序列(包括空串);
主要思路:
暴力枚举是否加入当前字符;
#include <iostream>
#include <vector>
#include <string>
class Solution{
public:
std::vector<std::string> Printstr(std::string str){
if(str.length() == 0) return res;
process(str, 0, "");
return res;
}
void process(std::string str, int i, std::string cur_str){
if(i == str.length()){
res.push_back(cur_str);
return;
}
process(str, i+1, cur_str+str[i]); // 加入当前字符
process(str, i+1, cur_str); // 不加入当前字符
}
private:
std::vector<std::string> res;
};
int main(int argc, char *argv[]){
Solution S1;
std::string test = "abc";
std::vector<std::string> res = S1.Printstr(test);
for(std::string str : res) std::cout << str << std::endl;
}
2--字符串的全排列
主要思路:
利用全排列遍历字符串,注意重复字符在树层上的剪枝(但要保留重复字符在树枝上的排列)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Solution {
public:
std::vector<std::string> permutation(std::string s){
if(s.length() == 0) return res;
std::vector<bool> used(s.length(), false);
std::sort(s.begin(), s.end());
std::string tmp = "";
process(s, 0, tmp, used);
return res;
}
void process(std::string str, int idx, std::string tmp, std::vector<bool> used){
if(idx == str.length()){
res.push_back(tmp);
return;
}
for(int i = 0; i < str.length(); i++){
if(used[i] == true) continue; // 该字符已使用
// 去除树层重复字符
if(i > 0 && str[i] == str[i-1] && used[i-1] == true) continue;
used[i] = true;
process(str, idx + 1, tmp + str[i], used);
// 回溯
used[i] = false;
}
}
private:
std::vector<std::string> res;
};
int main(int argc, char *argv[]){
std::string test = "aab";
Solution S1;
std::vector<std::string> res = S1.permutation(test);
for(std::string str : res) std::cout << str << std::endl;
return 0;
}
3--N字形变换
主要思路:
老实人做法,按规律顺序填入 board 中;
#include <iostream>
#include <vector>
#include <string>
class Solution {
public:
std::string convert(std::string s, int numRows) {
int i = 0, j = 0, idx = 0;
std::vector<std::vector<char>> board(numRows, std::vector<char>(s.length(), '#'));
while(idx < s.length()){
while(idx < s.length() && i < numRows){
board[i][j] = s[idx];
i++;
idx++;
}
i = std::max(0, i - 2); // 防止 i 越界
j += 1;
while(idx < s.length() && i > 0){
board[i][j] = s[idx];
i--;
j++;
idx++;
}
}
std::string res;
for(int i = 0; i < numRows; i++){
for(int j = 0; j < board[0].size(); j++){
if(board[i][j] != '#') res.push_back(board[i][j]);
}
}
return res;
}
};
int main(int argc, char *argv[]) {
// s = "PAYPALISHIRING", numRows = 3
std::string test = "PAYPALISHIRING";
int rows = 3;
Solution S1;
std::string res = S1.convert(test, rows);
std::cout << res << std::endl;
return 0;
}
主要思路:
巧妙做法:路飞
#include <iostream>
#include <vector>
#include <string>
class Solution {
public:
std::string convert(std::string s, int numRows) {
if (numRows < 2)
return s;
std::vector<std::string> rows(numRows);
int i = 0, flag = -1;
for (char c : s) {
rows[i].push_back(c);
if (i == 0 || i == numRows -1)
flag = - flag;
i += flag;
}
std::string res;
for (const std::string &row : rows)
res += row;
return res;
}
};
int main(int argc, char *argv[]) {
// s = "PAYPALISHIRING", numRows = 3
std::string test = "PAYPALISHIRING";
int rows = 3;
Solution S1;
std::string res = S1.convert(test, rows);
std::cout << res << std::endl;
return 0;
}