【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 ...
随机推荐
- elasticsearch基本概念
NRT(近实时搜索) Elasticsearch是一个NRT平台.这意味着当你索引一个文件时,在细微的延迟(通常1s)之后,该文件才能被搜索到. Cluster(集群) cluster是在所有节点中保 ...
- 20170114 - Mac 向上一级文件夹快捷键
以前使用XtraFinder来实现向上跳转文件夹,Mac其实自带的向上一级文件夹,只是没有那么明显, Mac下跳转上一级文件夹的快捷键是 Command + Up Arrow,即: ⌘ ↑
- C++ unordered_map 在key为string类型和char*类型时测试时间性能差异
测试系统liunx centos6.5 代码如下 #include <string.h> #include <sstream> #include <list> #i ...
- redis服务器安装-SuSE Linux Enterprise Server 11 SP3
一.下载 官网下载,可自选版本,点击进入下载,这里下载了redis-3.2.4 放到 /root/usr/local/redis/ 目录下 二.编译 1. 执行make编译redis tar -zxz ...
- Linux双网卡NAT共享上网
linux双网卡NAT共享上网 术语字汇 私有IP地址(路由不可达地址)是一个被用于本地局域网的IP地址(在互联网中不可见). 公用IP地址(路由可达地址)是一个在互联网中可见的IP地址. IP伪装是 ...
- python3 数据类型
Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Number(数字) Py ...
- java程序基础
- 浅谈javascript中stopImmediatePropagation函数和stopPropagation函数的区别
在事件处理程序中,每个事件处理程序中间都会有一个event对象,而这个event对象有两个方法,一个是stopPropagation方法,一个是stopImmediatePropagation方法,两 ...
- 新Mac 开机启动MySQL/MongoDB/Redis 等服务
在Mac上我们使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)来安装和管理开发工具包,例如:mysql.php.redis.只需要一个命令 brew ...
- VR应用向导,全球Top10 VR应用排行榜
2016年国际知名产商索尼.三星.HTC.Oculus.YouTube等等都推出了自己的VR设备,与此同时还有自有的VR应用平台,供各位玩家下载应用体验沉浸式VR,当然每个平台的VR应用下载量各不相同 ...