数据结构笔记--字符串经典高频题

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值