【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) ...
随机推荐
- 沙箱模式以及其使用到的IIFE
//沙箱 //与外界隔绝的一个环境,外界无法修改该环境内任何信息,沙箱内的东西单独属于一个世界 //360沙箱模式 //将软件和操作系统进行隔离,以达到安全的目的 //苹果手的app使用的就是沙箱模式 ...
- 【VS Code】中node.js代码自动补全的方法
原文链接: https://blog.csdn.net/qq_39189819/article/details/91347484
- 删除TOMCAT服务器上的文件
deleteFile(文件名称,路径名称); public boolean deleteFile(String filename,String filepath){ String path =Ser ...
- Linux centOS 6 和 centOS 7一些简单的区别
最明显的是,获取网络端口的命令和IP信息的命令不同了 所以刚安装的centos 7 使用ifconfig查看ip时有可能出现-bash: ifconfig: 未找到命令
- springcloud系列
1.使用Spring Cloud搭建服务注册中心2.使用Spring Cloud搭建高可用服务注册中心3.Spring Cloud中服务的发现与消费4.Eureka中的核心概念5.什么是客户端负载均衡 ...
- 【编程语言】Kotlin之扩展函数
简介: 平时Android开发中会使用各种各样的工具类函数,大部分工具类都是对原有对象的一种扩展,例如: public static void startActivity(Activity act, ...
- &和&&,|和||的用法区别
&和&&的区别是,&会执行两边,不管第一个是否成立&&只会执行一边,如果第一个条件为假,则不会走第二个条件举例public class Test2{ p ...
- 007_硬件基础电路_RC复位电路中二极管的作用
--------------------- 作者:碎碎思 来源:CSDN 原文:https://blog.csdn.net/Pieces_thinking/article/details/781110 ...
- 学到了武沛齐讲的Day14完
& 交 | 并 ^ 并-交 --------------------- 格式化 %s 字符串,数字,一切 %.4s 留前面4位 %d 数字 %f 小数保留6位 四舍五入 %0. ...
- 发布VS源码
发布VS源码步奏 先将Web.config设置修改一下 IP设置成点 文件名称设置成文件夹的名称,右键点击项目,点发布 勾选删除现有文件,点击发布 打开文件加 将文件解压成压缩包, 打 ...