栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构【字符串】相关代码(数据结构笔试、复测、Leecode、牛客)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构【字符串】相关代码(数据结构笔试、复测、Leecode、牛客)

提示:这些是自己整理 可以借鉴 也可能存在错误 欢迎指正

字符串
  • 基础知识
  • 题目
    • 简单
      • 反转字符串
    • 中等
      • 1.大数加法
      • 2.最长公共子串
      • 4. 最长回文子串


基础知识 题目 简单 反转字符串

描述

写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

解题:

  • 左右指针开始交换数据
class Solution {
public:
    
    string solve(string str) {
        // write code here
        int left = 0;
        int right = str.length()-1;
        while(left <= right)
            swap(str[left++], str[right--]);
        return str;
        
    }
};
中等 1.大数加法

描述

以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回。

数据范围:s.length,t.length≤100000,字符串仅由’0’~‘9’构成
要求:时间复杂度 O(n)

思路:

  • 大整数相加,就可以按照整数相加的方式,从个位开始,逐渐往上累加,换到字符串中就是从两个字符串的末尾开始相加。
class Solution {
public:
    
    string solve(string s, string t) {
        // write code here
        if(s.empty())
            return t;
        if(t.empty())
            return s;
        //s记录比较大的数---保存结果
        if(s.length() < t.length())
            swap(s, t);
        //进位标志 flag
        int flag = 0;
        //从最后一位开始相加
        for(int i=s.length()-1; i>=0; i--){
            int temp = s[i] -'0'+flag;//转数字
            int j = i-s.length()+t.length(); //t的最后一位开始
            if(j >= 0){
                temp = temp + t[j] - '0';
            }
            flag = temp/10;
            temp = temp % 10;
            s[i] = temp + '0';
        }
        if(flag == 1)
            s = '1' + s;
        return s;
    }
};
2.最长公共子串

描述

给定两个字符串str1和str2,输出两个字符串的最长公共子串
题目保证str1和str2的最长公共子串存在且唯一。 

解题思路

  • 动态规划
  • dp[i][j] = dp[i-1][j-1] +1; else dp[i][j] =0
class Solution {
public:
    
    string LCS(string str1, string str2) {
        // write code here
        vector> dp(str1.length()+1, vector(str2.length()+1,0));
        dp[0][0] = 0;
        int max=0, pos=0;
        for(int i=1; i<=str1.length(); i++){
            for(int j=1; j<=str2.length(); j++){
                if(str1[i-1] == str2[j-1])
                    dp[i][j] = dp[i-1][j-1] +1;
                else
                    dp[i][j] = 0;
                if(dp[i][j] > max){
                    max = dp[i][j];
                    pos = i-1;
                }
            } 
        }
        return str1.substr(pos-max+1, max);
    }
};
4. 最长回文子串

描述

对于长度为n的一个字符串A(仅包含数字,大小写英文字母),请设计一个高效算法,计算其中最长回文子串的长度。
输入:"ababc"
返回值:3
说明:最长的回文子串为"aba"与"bab",长度都为3  

解题思路:

class Solution {
public:
    
    int getLongestPalindrome(string A) {
        // write code here
        if(A.size() < 2)
            return A.size();
        //最大长度
        int res = 0;
        //i为中心点
        for(int i=0; i
            int len = 0;
            if(helper(A,i,i) > helper(A,i,i+1))
                len = helper(A,i,i);
            else
                len = helper(A,i,i+1);
            res = max(res, len);
        }
        return res;
    }
    
    int helper(string A, int left, int right){
        //从左右开始扩散
        while(left>=0 && right
            if(A[left] == A[right]){
                left--;
                right++;
            }else
                break;
        }
        return right-left+1-2; 
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号