【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 ...
随机推荐
- python 2.7 字符串处理
python 2 字符串处理小结: 字符替换:new_str = old_str.replace(char_old, char_new)--可用于清除字符串中所有的空格 字符分割(正/反):str_n ...
- 创建ListView控件
// 创建List控件 HWND hListView = CreateWindow(WC_LISTVIEW ,/*listview 宏的名字*/ L"" ,/*窗口标题*/ WS_ ...
- lzo压缩格式文件查看
使用lzop命令解压并查看 :lzop -cd xxx.lzo |more 附压缩命令:lzop xxx.log (生成xxx.log.lzo) 其它参数: # lzop -v test # 创建te ...
- iOS 电商购物车倒计时时间计算
/** * 倒计时 * * @param endTime 截止的时间戳 * * @return 返回的剩余时间 */ - (NSString*)remainingTimeMethodAction:(l ...
- iOS 手势识别
首先给大家解释一下为什么要学习手势识别? 如果想监听一个UIView上面的触摸事件,之前的做法是: 自定义一个UIView : 实现UIView的touches方法,在方法里面实现具体功能 透过tou ...
- ios_swift开发资源整理
目录 1.苹果官方资源 2.国内外视频网站推荐 3.中文文档 4.demo网站 5.开发工具推荐 6.国内外开发网站论坛 7.技术博客推荐 8.书籍推荐 9.第三方框架推荐 10.第三方发布平台 11 ...
- nohup及/dev/null使用
1.nohup ----后台执行,执行记录默认输出到当前目录下的nohup.out文件 nohup find /etc -name passwd 2./dev/null介绍 把/ ...
- Hololens生成与安装(旁加载)应用
Hololens生成应用的几种方式: 一:HoloToolkit编辑器生成appx应用 二:Vistul Studio 2015 创建应用 旁加载概述: 你可以将应用旁加载到你的设备,而无需将它们提交 ...
- 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群(转载-2)
原文:http://www.cnblogs.com/PurpleDream/p/4510279.html 分布式存储 CentOS6.5虚拟机环境搭建FastDFS-5.0.5集群 前言: ...
- 1.webpack-----模块加载器兼打包工具
一.webpack的优势 1. 能模块化 JS . 2. 开发便捷,能替代部分 grunt/gulp 的工作,比如打包.压缩混淆.图片转base64等. 3. 扩展性强,插件机制完善,特别是支持 Re ...