686. Repeated String Match
方法一、算是暴力解法吧,拼一段找一下
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int repeatedStringMatch(string A, string B)
{
string as=A;
int count=B.size()/A.size()+;
for(int i=;i<count;i++,as+=A)
{
if(as.find(B)!=string::npos)
return i;
}
return -;
}
};
方法二、稍微有点技术含量了,运行速度快些
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int repeatedStringMatch(string A, string B)
{
string tmp=A+A;
size_t pos=tmp.find(B.substr(,A.size()));
if(pos==string::npos)
return -;
int count=;
size_t i=;
while(i<B.size())
{
if(pos==A.size())
{
pos=;
++count;
}
if(A[pos++]!=B[i++])
return -;
}
return count;
}
};
先把两个A拼起来成为tmp,在tmp中找B的前面一截,若没找到,则B必然不能成为A拼接序列的子串
若找到了,再进行下面的判定,一个字符一个字符来,扫到A末尾则回到A首部并增加计数器,直到找到B的所有元素
686. Repeated String Match的更多相关文章
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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判断字符串重复几次可以包含另外一个
public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...
- 【刷题笔记】686. Repeated String Match
题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- LeetCode686——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 ...
随机推荐
- FormData的使用
var formData = new FormData(); <form id="coords" class="coords" onsubmit=&quo ...
- 200. Number of Islands (Graph)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...
- 【nginx】大文件下载
nginx自带文件读取功能,而且实现地很好. 比如直接读取txt文件,png图片等,用chrome可以直接获取到内容. 但是对于很大的文件,比如有2个G的视频,nginx如何吐出2G的内容呢? 实验: ...
- Django的视图函数和路由系统中一些没有用过的小点
1.request对象 print("返回用户访问的url,但是不包括域名",request.path_info) print("返回请求的方法,全大写",re ...
- swift - 启动APP 黑屏
https://blog.csdn.net/chengkaizone/article/details/50478045
- C语言可以开发哪些项目?(转)
原文地址:https://www.cnblogs.com/shiyanlou/p/6098661.html 知乎:https://www.zhihu.com/question/20564904 C语言 ...
- TabLayout+ViewPager的简单使用
1. build.gradle文件中加入 compile 'com.android.support:design:22.2.0' 2.写Xml文件,注意TabLayout的三个属性 app:tab ...
- YII2中使用控制台命令
有些时候我们需要通过crontab在后台跑一些定时脚本,这时候就需要用到控制台命令了. 我们在commands目录下创建TestController.php,当然脚本的位置是可以随意指定的,只需要在c ...
- netcore webapi 用户 'IIS APPPOOL\无托管代码' 登录失败
配置在iis上,除了环境配置错误的原因还有一种可能是连接字符串的问题,iis要求使用sql server的sa或者其他登录用户. ps:连接字符串: "Default": &qu ...