博客园  :: 首页  :: 新随笔  :: 管理

8.字符串

Posted on 2021-03-29 21:44  wsg_blog  阅读(163)  评论(0编辑  收藏  举报

Index LeetCode

字符串可以看成是字符组成的数组。一般将把字符的ASII码作为标示来使用。
string s; 长度判断为s.length();

BM83.字符串变形[easy]

输入:"This is a sample",16
输出:"SAMPLE A IS tHIS"
字符大小写转换+栈

string trans(string s, int n){
  string res;
  stack<string> stk;
  for(int i=0; i<n; i++){
    if(s[i] >= 'A' && s[i] <= 'Z')
      res+=s[i]-('A'-'a'); //大写转小写
    else if(s[i] >= 'a' && s[i] <= 'z')
      res+=s[i]+('A'-'a');
    else{
      stk.push(res);
      res="";
    }
  }
  while(!stk.empty()){
    res+=" "+stk.top();
    stk.pop();
  }
  return res;
}
BM84.最长公共前缀(14.最长公共前缀)[easy]

输入:strs = ["flower","flow","flight"]
输出:"fl"
遍历

string longestCommonPrefix(vector<string>& strs){
  if(strs.size()==0) return "";
  string res;
  for(int i=0; i<strs[0].size(); i++){
    for(int j=1; j<strs.size(); j++){
      if(i == strs[j].size() || strs[0][i] != strs[j][i]){
        res=strs[0].substr(0, i);
        return res;
      }
    }
  }
  return strs[0];
}
BM85.验证IP地址(468.验证IP地址)[medium]

输入:queryIP = "172.16.254.1";queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334";queryIP = "256.256.256.256"
输出:"IPv4";"IPv6";"Neither"
字符串分割比较

string vaildIPAddress(string queryIP){
  if(queryIP.size()==0)
    return "Neither";
  if(isIPv4(queryIP))
    return "IPv4";
  else if(isIPv6(queryIP))
    return "IPv6";
  return "Neither";
}
bool isIPv4(string queryIP){
  vector<string> s=split(queryIP, ".");
  if(s.size() != 4)     //IPv4必定为4组
    return false;
  for(int i=0; i<s.size(); i++){
    if(s[i].size() == 0)  //不可缺省,有一个分割为0,说明两点相连
      return false;
    //比较数字位及不为零时不能有前缀零
    if(s[i].size()<0 || s[i].size()>3 || (s[i][0]=='0' && s[i].size()!=1))
      return false;
    for(int j=0; j<s[i].size(); j++)  //遍历每个分割字符串必须为数字
      if(!isdigit(s[i][j])) return false;
    int num=stoi(s[i]);  //转化为数字比较,0-255
    if(num<0 || num>255)
      return false;
  }
  return true;
}
bool isIPv6(string queryIP){
  vector<string> s=split(queryIP, ":");
  if(s.size() != 8)  //IPv6必定为8组
    return false;
  for(int i=0; i<s.size(); i++){
    if(s[i].size()==0 || s[i].size()>4)  //每个分割不能缺省,不能超过4位
      return false;
    for(int j=0; j<s[i].size(); j++){
      //不能出现a-f A-F以外的大小写字符
      if(! (isdigit(s[i][j]) || (s[i][j]>='a' && s[i][j]<='f') || (s[i][j]>='A' && s[i][j]<='F')))
        return false;
    }
  }
  return true;
}
vector<string> split(string s, string spliter){  //将字符串从.或者:分割开
  vector<string> res;
  int i;
  while((i=s.find(spliter)) && i!=s.npos){  //遍历字符串查找spliter
    res.push_back(s.substr(0, i));
    s=s.substr(i+1);
  }
  res.push_back(s);
  return res;
}
BM86.大数加法(415.字符串相加)[easy]

输入:num1 = "11", num2 = "123"
输出:"134"
模拟,尾指针

string addString(string num1, string num2){
  int l1=num1.size()-1, l2=num2.size()-1, carry=0;
  string res;
  while(l1>=0 || l2>=0 || carry!=0){
    int x= l1>=0? num1[l1]-'0':0;
    int y= l2>=0? num2[l2]-'0':0;
    int sum=x+y+carry;
    carry=sum/10;
    res.push_back('0'+sum%10);
    l1--;
    l2--;
  }
  reverse(res.begin(), res.end());
  return res;
}
28.实现strStr()

字符串匹配 人人都能看懂的kmp算法

题目描述:判断一个字符串是不是另一个字符串的子字符串,并返回其位置。
输入输出样例:输入一个母字符串,输出一个整数,表示子字符串在母字符串的位置,若不存在则返回-1。
Input:haystack="hello", needle="ll"
Output:2
题解:使用著名的Knuth-Morris-Pratt(KMP)算法,可以在O(m+n)时间利用动态规划完成匹配。
//主函数
int strStr(string haystack, string needle){
    int k=-1, n=haystack.length(), p=needle.length();
    if(p == 0) return 0;
    vector<int> next(p, -1);    //-1表示不存在相同的最大前缀和后缀
    calNext(needle, next);      //计算next数组
    for(int i=0; i<n; ++i){
        while(k>-1 && needle[k+1] !=haystack[i]){
            k=next[k];  //有部分匹配,往前回溯
        }
        if(needle[k+1] == haystack[i]){
            ++k;
        }
        if(k == p-1){
            return i-p+1;   //说明k移动到needle的最末端,返回相应的位置
        }
    }
    return -1;
}
//辅函数-计算next数组(前缀表)   最长相等前后缀
void calNext(const string &needle, vector<int> &next){
    for(int j=1, p=-1; j<needle.length(); ++j){
        while(p>-1 && needle[p+1]!=needle[j]){
            p=next[p];  //如果下一位不同,往前回溯
        }
        if(needle[p+1] == needle[j]){
            ++p;
        }
        next[j]=p;
    }
}
//暴力解法
int strStr(string haystack, string needle) {
    if(!needle.length()) return 0;
    for(int i=0;i<haystack.length();i++){
        for(int j=0; j<needle.length();j++){
            if(haystack[i+j]!=needle[j]) break;
            if(j==needle.length()-1) return i;
        }
    }
    return -1;
}