string_palindrome_string


title: string-1
data: 2020-7-31
categories:

  • 算法题
  • String

Leetcode125验证回文串

bool check(char c){
        if( c >= 'a' && c <= 'z' || c >= '0' && c <='9' || c >='A' && c <= 'Z')
            return true;
        return false;
    }
    bool isPalindrome(string s) {
        int i, j;
        for(i = 0, j = s.size(); i < j ; i++, j--){
            while(i < j && !check(s[i]))i++;
            while(i < j && !check(s[j]))j--;
            if(i < j && tolower(s[i]) != tolower(s[j]))
                return false;
        }
        cout<<"i="<<i<<" j="<<j;
        return true;
    }

找出最长回文串

  1. 思路:枚举每个字符为回文串的中点位置,向两边散开

  2. 代码

     string longestPalindrome(string s) {
         if(s.size() == 0 || s.size() == 1)return s;
         string res = "";
         for(int i = 0;i < s.size(); i++){
             int j = i+1, k = i;
             while(k >= 0&& j<s.size() && s[k] == s[j]){k--,j++;}
             if(j-k-1 > res.size())res = s.substr(k+1, j-k-1);
             j = i+1 ,k = i-1;
             while(k >= 0 && j<s.size() && s[k] == s[j]){k--,j++;}
             if(j-k-1 > res.size())res = s.substr(k+1, j-k+1);
         }
         return res;
     }
    
    

解决办法2

Leetcode131分割回文串

vector<vector<bool>> dp;//i到j是否是回文串, i <= j
    vector<vector<string>> res;
    vector<string> path;
    vector<vector<string>> partition(string s) {
        int n = s.size();
        dp = vector<vector<bool>>(n, vector<bool>(n));
        for(int j = 0; j < n; j++){//枚举结束位置,正三角(矩形的下半部分)
            for(int i = 0; i <= j; i++){
                if(i == j)dp[i][j] = true;
                else if(s[i] == s[j]){
                    //只有两个字符        //通常情况
                    if(i + 1 > j - 1 || dp[i + 1][j - 1])dp[i][j] = true;
                }
            }
        }
        dfs(s, 0);
        return res;
    }
    void dfs(string& s, int index){
        if(index == s.size())res.push_back(path);
        else{
            for(int i = index; i < s.size(); i++){
                if(dp[index][i]){ // 枚举每个结束位置
                    path.push_back(s.substr(index, i - index + 1));
                    dfs(s, i + 1);
                    path.pop_back();
                }
            }
        }
    }
  1. 字符串哈希加上二分查找 思路:

前缀后缀中间缀之类

最长公共前缀

取第一个字符串为标杆,后面依次做比较,如果完全相同,则返回第一个字符

 string longestCommonPrefix(vector<string>strs) {
       string res ="" ;
       if ( strs.size() == 0 )
            return res;
        for(int i = 0; i < strs[0].size(); i++){
            char c = strs[0][i];
            for(int j=1 ; j < strs.size(); j++){
                if(i==strs[j].size() ||c != strs[j][i])
                    return strs[0].substr(0,i);
            }
        }
        return strs[0];
    }


model small .stack 100h .data msg db 'The string is a palindrome.$' msg1 db 'The string is a palindrome.$' msg2 db 'The string is not a palindrome.$' str1 db 'abccba$' str2 db '1234321$' str3 db 'sdsfds$' .code main proc mov ax, @data mov ds, ax ; 判断字符串1 mov si, 0 mov cx, 0 mov cl, 6 dec cx mov di, cx shr cx, 1 mov bx, 0 cmp cx, bx jle palindrome1 mov bx, 1 outer_loop1: mov al, [str1+si+1] mov ah, [str1+di+1] cmp al, ah jne not_palindrome1 inc si dec di cmp si, di jg palindrome1 inner_loop1: mov al, [str1+si+1] mov ah, [str1+di+1] cmp al, ah jne not_palindrome1 inc si dec di cmp si, di jle inner_loop1 palindrome1: lea dx, msg1 mov ah, 09h int 21h jmp check_next not_palindrome1: lea dx, msg2 mov ah, 09h int 21h check_next: ; 判断字符串2 mov si, 0 mov cx, 0 mov cl, 7 dec cx mov di, cx shr cx, 1 mov bx, 0 cmp cx, bx jle palindrome2 mov bx, 1 outer_loop2: mov al, [str2+si+1] mov ah, [str2+di+1] cmp al, ah jne not_palindrome2 inc si dec di cmp si, di jg palindrome2 inner_loop2: mov al, [str2+si+1] mov ah, [str2+di+1] cmp al, ah jne not_palindrome2 inc si dec di cmp si, di jle inner_loop2 palindrome2: lea dx, msg1 mov ah, 09h int 21h jmp check_next2 not_palindrome2: lea dx, msg2 mov ah, 09h int 21h check_next2: ; 判断字符串3 mov si, 0 mov cx, 0 mov cl, 6 dec cx mov di, cx shr cx, 1 mov bx, 0 cmp cx, bx jle palindrome3 mov bx, 1 outer_loop3: mov al, [str3+si+1] mov ah, [str3+di+1] cmp al, ah jne not_palindrome3 inc si dec di cmp si, di jg palindrome3 inner_loop3: mov al, [str3+si+1] mov ah, [str3+di+1] cmp al, ah jne not_palindrome3 inc si dec di cmp si, di jle inner_loop3 palindrome3: lea dx, msg1 mov ah, 09h int 21h jmp exit_program not_palindrome3: lea dx, msg2 mov ah, 09h int 21h jmp exit_program exit_program: mov ah, 4ch int 21h main endp end main能不能将代码优化用将带判断字符串看为二维数组
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值