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 ...
随机推荐
- TOJ3448: 小学生的作业
Python字符串的插入操作 传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3448 ...
- Fraction to Recurring Decimal(STRING-TYPE CONVERTION)
QUESTION Given two integers representing the numerator and denominator of a fraction, return the fra ...
- vue-webpack项目本地开发环境设置代理解决跨域问题
前言: 一般跨域问题只要后端配置好的话,是不需要前端做处理的,但也不能保证你遇到的所有后端都能很好的处理这个问题,这个时候可能就需要前端设置代理解决这个问题了. 配置方法: 1. config/ind ...
- java bean validation 参数验证
一.前言 二.几种解决方案 三.使用bean validation 自带的注解验证 四.自定义bean validation 注解验证 一.前言 在后台开发过程中,对参数的校验成为开发环境不可缺少的一 ...
- 截图原理(一)——Android自动化测试学习历程
把两节的内容汇总起来,第一节讲的是如何在apk中直接进行截屏,用到了Robotium的Solo类的takeScreenShot方法,有一个小的demo,以及从方法一直往里钻,知道它具体是怎么进行截屏的 ...
- swift4.2 打印devicetoken
import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicati ...
- centos7下的glusterfs的安装与使用
环境说明: 两台虚拟主机,IP分别为192.168.1.222和192.168.1.233,分别装上glusterfs. 一.下载glusterfs源码包 https://download.glust ...
- [Z]sql优化
前言:平常写的SQL可能主要以实现查询出结果为主,但如果数据量一大,就会突出SQL查询语句优化的性能独特之处.一般的数据库设计都会建索引查询,这样较全盘扫描查询的确快了不少.下面总结下SQL查询语句的 ...
- 正则表达式 re sys os random time 模块
今天学习内容如下: 1.正则表达式 百度正则表达式在线测试,可以练习 正则表达式本身也和python没有什么关系,就是匹配字符串内容的一种规则.官方定义:正则表达式是对字符串操作的一种逻辑公式,就是用 ...
- String 练习
package com.hanqi; import java.util.Random; public class Text { public static void main(String[] arg ...