Leetcode-686. Repeated String Match(string)
来源:互联网 发布:鲜活的数据 编辑:程序博客网 时间:2024/05/22 05:25
题目链接:点击打开链接
题目大意:给你两个字符串A和B,求A要最少重复几次才能让B成为它的子串。(这里有个坑,就是A本身已经包含了1次)
解题思路:用C++的string就好
解题代码:
class Solution {public: int repeatedStringMatch(string A, string B) { string ss=A; int ans=1; if(ss.length()>=B.length()) if(ss.find(B)==0) return ans; while(ss.length()<B.length()) { ss+=A; ans++; } if(ss.find(B)!=-1) return ans; ss+=A; ans++; if(ss.find(B)!=-1) return ans; return -1; }};
1.find():查找第一次出现的目标字符串,如果查找成功则输出查找到的第一个位置,否则返回-1;
#include<iostream>#include<csdtio>using namespace std;int main(){string s1 = "abcdef";string s2 = "de";int ans = s1.find(s2) ; //在S1中查找子串S2cout<<ans<<endl;}
查找从指定位置开始的第一次出现的目标字符串:
#include<iostream>#include<csdtio>using namespace std;int main(){string s1 = "abcdef";string s2 = "de";int ans = s1.find(s2, 2) ; //从S1的第二个字符开始查找子串S2cout<<ans<<endl;}
2.find_first_of():查找子串中的某个字符最先出现的位置。find_first_of()不是全匹配,而find()是全匹配
#include<iostream>#include<csdtio>using namespace std;int main(){string s1 = "adedef";string s2 = "dek";int ans = s1.find_first_of(s2) ; //在S1中查找子串S2cout<<ans<<endl;}
其中find_first_of()也可以约定初始查找的位置:s1.find_first_of(s2, 2) ;
3.find_last_of():这个函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。
5.find_first_not_of():找到第一个不与子串匹配的位置。
~step by step
阅读全文
2 0
- leetcode 686. Repeated String Match
- leetcode 686. Repeated String Match
- leetcode 686. Repeated String Match
- 【LeetCode】686. Repeated String Match
- leetcode 686. Repeated String Match
- Leetcode-686. Repeated String Match(string)
- Leetcode | Repeated String Match
- 686. Repeated String Match
- 686. Repeated String Match
- leetcode 686 Repeated String Match
- [LC]686. Repeated String Match
- leetcode题解-686. Repeated String Match && 38. Count and Say
- LeetCode Weekly Contest 52 Repeated String Match(string)
- LWC 52:686. Repeated String Match
- Leetcode挑战题——Repeated String Match
- Repeated String Match问题及解法
- leetcode_686.Repeated String Match ? 待解决
- String:459. Repeated Substring Pattern
- Java for Web学习笔记(八三):RESTful(3)Controller
- webpack超详细配置, 使用教程(图文)
- 神经网络训练细节(二)
- 程序调试的总结
- 主机安装全命令
- Leetcode-686. Repeated String Match(string)
- 基于MATLAB的m序列产生函数及其调用方法
- redis学习笔记
- CF Unbearable Controversy of Being 489D 暴力
- gradle之ssh插件
- 文章标题
- kali 基本命令语言
- 第二讲,计算机网络概述(下)
- Linux中makefile过程(尚未学完)