【LeetCode】459. Repeated Substring Pattern
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的更多相关文章
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- LeetCode算法题-Repeated Substring Pattern(Java实现)
这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...
- 【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 ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- *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 ...
- [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 ...
随机推荐
- 苹果也要开发AR眼镜,正与蔡司联手打造
近日,知名博主Robert Scoble在Facebook上称,苹果正在与德国卡尔蔡司公司合作研发一副增强现实智能眼镜,而且最快将在今年发布.据Scoble称,通过和蔡司的雇员在上周的CES展会上进行 ...
- 遇到个奇怪的问题,同时开启本地和远程两个事务,远程事务是sql2000没问题,是sql2008的不报错,但是写不上数据
遇到个奇怪的问题,同时开启本地和远程两个事务,远程事务是sql2000没问题,是sql2008的不报错,但是写不上数据. 倒腾了4.5天,找到最终的解决办法:直接调用AdoConnection.exe ...
- wife信号如何传播
方法一:像哈利波特一样穿墙而出 无论是wife信号还是广播信号本质上都属于电磁波.x光穿透力强所以可以穿透人体给体内照相,但是wife信号作为电磁波虽然也可以穿透墙而过,但是他的穿透能力实在是太弱了. ...
- 循序渐进看Java web日志跟踪(1)-Tomcat 日志追踪与配置
日志,是软件运行过程中,对各类操作中重要信息的记录. 日志跟踪,不管对于怎么样的项目来说,都是非常重要的一部分,它关系到项目后期的维护和排错,起着举足轻重的作用.项目开发过程中,对日志的记录规则,也将 ...
- BOGEER博格尔YT-813码表使用说明书 (我的是YT-823)
BOGEER博格尔YT-813码表使用说明书.doc 源:http://w.gdu.me/wiki/Bike/BOGEER-YT-813.html 参数设置 首先要测量出车轮的周长,测出车轮周长后按住 ...
- SIGPIPE信号
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include &l ...
- HTML5学习之Web存储
在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...
- excel 常用函数
1.统计整列唯一值的数量 =sumproduct(1/countif(offset(A1,,,COUNTA(A:A)),OFFSET(A1,,,COUNTA(A:A))))
- CF Round #353 Div.2
http://codeforces.com/contest/675 A. Infinite Sequence 题意:给出等差数列的首项a以及公差c,问数b是不是该数列中的数,若是输出YES否则输出NO ...
- [Q]复制授权了文件但仍显示“未注册”问题(安装在非默认目录或目录包含中文)
1. 注意要将解压后的文件复制到CAD批量打图精灵安装目录,而不要复制文件夹,复制是提示是否需要覆盖,要选择覆盖. 2. 若通过第1步操作仍然显示“未注册”,则可能是由于安装目录含有中文或者不是默认目 ...