LeetCode练习-字符串


一、palindrome-partitioning

题目描述
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s ="aab",
Return
  [
    ["aa","b"],
    ["a","a","b"]
  ]

思路:动态规划+深度优先遍历(DFS)

1、用动态规范算法划分子回文串

2、用DFS遍历可能的情况

代码:

class Solution {
public:
    vector > partition(string s){
        vector > result;
        vector array;
        if(s.length()==0)
            return result;
        vector > flag = dp(s);

        dfs(s, 0, flag, array, result);
        return result;
    }

    void dfs(string s, int begin, vector > flag, vector array, vector > &result) {  
        if(begin==s.length()) {
            result.push_back(array); 
            //array.clear();
            return;  
        }  

        for(int i=begin;i temp(array);
                temp.push_back(s.substr(begin,i+1-begin));
                //cout << "begin=" << begin << "; i+1=" << (i+1) << endl;
                //cout << array.back() << endl;
                dfs(s, i+1, flag, temp, result);  
            }  
        }  
    }
    vector > dp(string s){
        int len = s.length();
        vector > flag(len, vector(len, 0));
        int i, j;
        for(i=len-1; i>=0; --i){
            for(j=i; j

本地测试代码:

vector > dp(string s);//动态规划,划分子串
void dfs(string s, int begin, vector > flag, vector array, vector > &result);//深度优先遍历
vector > partition(string s);
int main(){
	string s = "abcbad";
	//cout << s.substr(1, 2) << endl;

	vector > flag = dp(s);
	int i, j;
	for(i=0; i > result;
	//vector array;
	//dfs(s, 0, flag, array, result);
	vector > result = partition(s);
	
	for(i=0; i

二、add-binary

题目描述
Given two binary strings, return their sum (also a binary string).
For example,
a ="11"
b ="1"
Return"100"


代码:

class Solution {
public:
    string addBinary(string a, string b) {
        string sum;//输出
        int up=0;//进位
        int i=a.length()-1;
        int j=b.length()-1;
        while(i>=0 && j>=0){
            int temp = a[i]+b[j]-'0'-'0' + up;//当前位上 数字的和
            if(temp == 3){
                up = 1;
                sum.insert(sum.begin(), '1');
            }
            else if(temp == 2){
                up = 1;
                sum.insert(sum.begin(), '0');
            }
            else if(temp == 1){
                up = 0;
                sum.insert(sum.begin(), '1');
            }
            else if(temp == 0){
                up = 0;
                sum.insert(sum.begin(), '0');
            }
            --i;
            --j;
        }

        while(i>=0){//此时如果i>=0,说明a长,对a余下的位数进行操作
            int temp = a[i] - '0' +up; 
            if(temp == 2){
                up = 1;
                sum.insert(sum.begin(), '0');
            }
            else if(temp == 1){
                up = 0;
                sum.insert(sum.begin(), '1');
            }
            else if(temp == 0){
                up = 0;
                sum.insert(sum.begin(), '0');
            }
            --i;
        }	
        while(j>=0){//此时如果j>=0,说明b长,对b余下的位数进行操作
            int temp = b[j] - '0' + up;
            if(temp == 2){
                up = 1;
                sum.insert(sum.begin(), '0');
            }
            else if(temp == 1){
                up = 0;
                sum.insert(sum.begin(), '1');
            }
            else if(temp == 0){
                up = 0;
                sum.insert(sum.begin(), '0');
            }
            --j;
        }
        if(up == 1)//判断最高位是否有进位
            sum.insert(sum.begin(), '1');
        return sum;
    }
};

本地测试代码:

int main(){
	string a;
	string b;
	
	cout << "a: ";
	getline(cin, a);
	cout << "b: ";
	getline(cin, b);
	
	string s = addBinary(a, b);
	//string s = "ss";
	cout << s << endl;
	return 0;
}








你可能感兴趣的:(C++,C语言,LeetCode练习题)