Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"

Output: True

Explanation: It's the substring "ab" twice.

Example 2:

Input: "aba"

Output: False

Example 3:

Input: "abcabcabcabc"

Output: True

Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

题意:

查看一个字符串是否可以由子字符串重复组成,可以的话返回真,否则返回假

思路:

改变扫描步长,二重循环

1.假定长度为p的字符串可以重复组成,1=<p<=len(s)

2.重复多次循环,每个循环中不断验证str[i]!=str[i+p],如果为真,break;

3.如果有一次可以扫描到结尾,也就是i+p=l,而且i%p==0,那么返回真

 bool repeatedSubstringPattern(char* str) {
int p,i;
int l=strlen(str);
int flag=;
for(p=;p<=l/;p++)
{
for(i=;i<l-p;i++)
{
flag=;
if(str[i]!=str[i+p])
{
flag=;
break;
}
}
if(==flag&&i%p==)
return true;
}
return false;
}

这种解法的时间复杂度为O(n^2),还有一种利用KMP算法的,时间复杂度为O(n),我还没做,待补充

【LeetCode】459. Repeated Substring Pattern的更多相关文章

  1. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  2. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  3. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  4. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  7. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  8. *459. Repeated Substring Pattern (O(n^2)) two pointers could be better?

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. [LeetCode] 459. Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

随机推荐

  1. 舒适的路线 (code[vs] 1001)

    传送门 :code[vs]  1001 思路:拿到这题的首先的思路 , 就是跑一遍最短路. 可是在尝试了一会后发现不会写,于是果断弃 尝试另外的算法.. 于是就有的以下的算法.并查集 + 乱搞(有点像 ...

  2. C#-实验3

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. windows server 2008系统VPN服务配置

    转自:http://www.softxp.net/article/win2008-vpn/,,仅作自己的笔记用 Windows sever 2008 R2的NPS(network policy ser ...

  4. attr 和 prop 区别

    jquery 中 attr 和 prop 都表示 "属性",同样是属性为啥还要弄两个! attr 适用于自定义属性 如 定义一个懒加载用的src 栗子 <img class= ...

  5. webservices(一)

    ---完全摘自网络 什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebSer ...

  6. Navicat连接不上MySQL

    [root@localhost init.d]# pwd /etc/init.d [root@localhost init.d]# mysql -u root -p Enter password: E ...

  7. css 小知识

    <!-- IE下消除点击图片文字后出现的虚线框代码 --> <style type="text/css">a {blr:expression(this.on ...

  8. webstorm之js,css文件压缩

    不说废话了,代码压缩是每个网站上线前的必备过程,但由于有时候小的项目或者加急的项目每次都构建一次类似gulp或者grunt等前端部署,实在是大题小做,所以才有了今天这个帖子: 我们会用到yui com ...

  9. HDOJ-1052 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述: Here is a famous story in Chinese history. "That was ...

  10. 反射机制(reflection)动态相关机制

    功能:动态获取类的信息以及动态调用对象的方法. Java反射机制主要提供了以下功能: 1.在运行时判断任意一个对象所属的类. 2.在运行时构造任意一个类的对象. 3.在运行时判断任意一个类所具有的成员 ...