problem

686. Repeated String Match

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的更多相关文章

  1. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode_easy】942. DI String Match

    problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完

  3. 【刷题笔记】686. Repeated String Match

    题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 686. Repeated String Match

    方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...

  8. 【Leetcode_easy】844. Backspace String Compare

    problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...

  9. 【Leetcode_easy】796. Rotate String

    problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...

随机推荐

  1. 2019-2020-1 20199312《Linux内核原理与分析》第三周作业

    计算机的三大法宝:程序存储计算机.函数调用.中断 堆栈的作用:记录函数调用框架.传递函数参数.保存返回值地址.提供函数内部局部便量的存储空间. 堆栈相关的寄存器 ESP:堆栈指针,指向堆栈栈顶 EBP ...

  2. The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation

    链接: https://codeforces.com/gym/102394/problem/I 题意: DreamGrid has an interesting permutation of 1,2, ...

  3. emit传多个参数

    https://blog.csdn.net/lxy123456780/article/details/87811113 子组件: this.$emit('closeChange',false,true ...

  4. Python-sokect 示例

    server: #coding=utf-8 import socket _sokect =socket.socket() #创建sokect _host =socket.gethostname() # ...

  5. 学到了林海峰,武沛齐讲的Day22-完 os sys json pickle shelve XML re

    __ file__    =====   文件路径 os.path.dirname( 路径 )=======到上一层目录 os sys

  6. qsing

    qsing1 1.低仿机器人 一道大模拟 2.放爆竹 小辉原本想让小明告诉他,如果同时点燃n串雷,最多会有多长的时间至少有两串雷爆炸的声音是一样的. 但是小辉觉得这个问题真是太简单了,所以决定问小明, ...

  7. Java 多线程示例

    /** * 多线程案例 两种方式 模拟买票程序(不考虑线程安全问题) */ public class ThreadTest { public static void main(String[] arg ...

  8. 提高十连测day3

    提高十连测day3 A 我们可以枚举两个 $ 1 $ 之间的相隔距离,然后计算形如 $ 00100100 \cdots $ 的串在原串中最⻓⼦序列匹配即可,复杂度 $ O(n^2) $ .寻找 $ S ...

  9. Hadoop Aggregate Resource Allocation解释

    1.在hadoop里面运行程序的时候,查看某个任务的具体信息如下: [hadoop@master monitor]$ yarn application -list 如上图,这里面的Aggregate ...

  10. meshing-八分之一圆球

    ​原视频下载地址:https://yunpan.cn/cqwiFDCg6PbFj  访问密码 d1c8