【Leetcode_easy】686. Repeated String Match
problem
solution1:
使用string类的find函数;
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int n1 = A.size(), n2 = B.size(), cnt = ;
string t = A;
while(t.size() < n2)
{
t += A;
cnt++;
}
if(t.find(B) != string::npos) return cnt;//err.
t += A;
return (t.find(B) != string::npos) ? cnt+ : -;//
}
};
solution2:
其实可以直接算出最多需要增加的个数,即B的长度除以A的长度再加上2,当B小于A的时候,那么可能需要两个A,所以i就是小于等于2,这样每次在t中找B,如果找到了,就返回i,没找到,就增加一个A,循环推出后返回-1即可。
参考
1. Leetcode_easy_686. Repeated String Match;
2. Grandyang;
完
【Leetcode_easy】686. Repeated String Match的更多相关文章
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode_easy】942. DI String Match
problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完
- 【刷题笔记】686. Repeated String Match
题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...
- Leetcode 686 Repeated String Match
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- 686. Repeated String Match 字符串重复后的子字符串查找
[抄题]: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...
- [LeetCode] 686. Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- 686. Repeated String Match
方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
随机推荐
- 2019牛客暑期多校训练营(第九场)All men are brothers——并查集&&组合数
题意 最初有 $n$ 个人且互不认识,接下来 $m$ 行,每行有 $x,y$,表示 $x$ 和 $y$ 交朋友,朋友关系满足自反性和传递性,每次输出当前选取4个人且互不认识的方案数. 分析 并查集维护 ...
- python中的函数、生成器的工作原理
1.python中函数的工作原理 def foo(): bar() def bar(): pass python的解释器,也就是python.exe(c编写)会用PyEval_EvalFramEx(c ...
- js 实现多文件批量下载
关于兼容性问题: <a href="xxx.docx" target='_blank'></a> 下载文件时,这种写法是没有兼容性问题:但是下载图片时,IE ...
- 51nod 1434
首先可以得出一个性质:LCM(1,2,3,4,...,N-1,N) 中质因子k的出现的次数为t,则有k^t<=n 根据这个性质我们先筛出素数,然后枚举每个质数,求出对应的k和t,然后找出倍数j( ...
- tsnr--基于vpp+dpdk的高性能防火墙
tsnr--基于vpp+dpdk的高性能防火墙 2019年01月31日 12:06:00 网络安全研发随想 阅读数:508 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...
- P1613 跑路——倍增思想,floyd
https://www.luogu.org/problemnew/show/P1613 他有一个跑路机器,每次只能跑2k (单位)路程,每相邻两个点的路程为1,也就是说如果连边1——>2—— ...
- BZOJ1856[Scoi2010]字符串——组合数学+容斥
题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足 ...
- NOIP2014提高组 题解报告
D1 T1 无线网路发射器选址 题目大意:找一个矩形,使其覆盖的目标点最大. 题目过水,直接暴力搞过去,代码就不贴了. 但我TM居然有个地方SB了,调了半天才发现输入有问题: scanf(" ...
- 使用 Nexus3 Repository Manager 搭建 npm 私服
公司里一般都有自己的私服,用于管理封装的工具插件等,Nexus2主要是用于maven/gralde仓库的统一管理,Nexus3则添加了npm插件,可以对npm提供支持,其实用于npm仓库管理的还有一个 ...
- TCP和UDP并实现socket的简单通信
http://www.cnblogs.com/IPrograming/archive/2012/10/15/CSharp_Socket_4.html http://www.cnblogs.com/do ...