首页 > 其他分享 >代码随想录day10 ● 459.重复的子字符串 ● 字符串总结 ● 双指针回顾

代码随想录day10 ● 459.重复的子字符串 ● 字符串总结 ● 双指针回顾

时间:2022-10-03 11:13:51浏览次数:72  
标签:459 string int 随想录 npos 字符串 find

459. 重复的子字符串

 1 class Solution {
 2 public:
 3     bool repeatedSubstringPattern(string s) {
 4         string t = s + s;
 5         //掐头去尾
 6         t.erase(t.begin());
 7         t.erase(t.end() - 1);
 8         if (t.find(s) != std::string::npos){
 9             return true;
10         }
11         return false;
12     }
13 };

npos的用法,npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。

例子如下:

 1 #include <iostream>  
 2 #include <string>  
 3 using namespace std;  
 4 int main()  
 5 {  
 6     string b;  
 7     getline(cin,b);  
 8     int count=0;  
 9     for(int i=0;i<=127;i++)  
10         if(b.find(i)!=string::npos)  
11         count++;  
12     cout<<count;  
13 } 

 

标签:459,string,int,随想录,npos,字符串,find
From: https://www.cnblogs.com/zsqy/p/16750161.html

相关文章