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.)
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么抽出合适长度的重复单元
[一句话思路]:
小于i/2的长度,一个一个地试能否被整除
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
手生还是有影响的:for j 又写成了i
[总结]:
不知道长度就一个个数除着来试吧
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean repeatedSubstringPattern(String s) {
//cc
if (s.length() == 0) {
return true;
}
//for loop
for (int i = s.length() / 2; i >= 1; i--) {
if (s.length() % i == 0) {
int m = s.length() / i;
String sub = s.substring(0, i);
StringBuilder sb = new StringBuilder();
for (int j = 0; j < m; j++) {
sb.append(sub);
}
if(sb.toString().equals(s)) {
return true;
}
}
}
return false;
}
}
459. Repeated Substring Pattern 判断数组是否由重复单元构成的更多相关文章
- 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 ...
- *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 ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- KMP - LeetCode #459 Repeated Substring Pattern
复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...
- [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 ...
- LeetCode 459 Repeated Substring Pattern
Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
- 【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 ...
随机推荐
- np基本函数大全
Numpy是科学计算库,是一个强大的N维数组对象ndarray,是广播功能函数.其整合C/C++.fortran代码的工具 ,更是Scipy.Pandas等的基础 .ndim :维度 .shape : ...
- model里面字段choices的values值的选择
代码如下: Model: class Person(models.Model): name = models.CharField(max_length=200) CATEGORY_CHOICES = ...
- OpenWRT mt7620n 系统升级引起的问题
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qianguozheng/article/details/27237175 OpenWRT系统升级採用 ...
- 10 结构体和类 - —— 《Swift3.0 从入门到出家》
Swift中的面向对象5个要素:枚举.结构体.类.协议.扩展 面向对象研究的是对象,完成一件事情需要多个对象参与,是生活的映射 Swift中结构体和类非常相似,也就是结构体能完成类的所有功能.结构体是 ...
- gen already exists but is not a source folder
遇到android项目导入出现后重复空包等错误,往往是导入的java编译级别有关,点击项目properties-> java Compiler ->修改Compiler complianc ...
- MVC匿名类传值学习
刚接触MVC+EF框架不久,但一直很困惑的就是控制器能否及如何向视图传递匿名类数据.宝宝表示很讨厌去新建实体类啦,查询稍有不同就去建一个实体类不是很麻烦吗,故趁阳光正好,周末睡到自然醒后起来尝试了之前 ...
- (转)Oracle存储过程基本语法
本文转载自:http://www.cnblogs.com/hero4china/articles/base_rule_oracle_procedure.html 存储过程 1 CREATE OR R ...
- 转: SQL中的where条件,在数据库中提取与应用浅析
SQL中的where条件,在数据库中提取与应用浅析 http://hedengcheng.com/?p=577 1问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...
- 高并发场景下System.currentTimeMillis()的性能问题的优化 以及SnowFlakeIdWorker高性能ID生成器
package xxx; import java.sql.Timestamp; import java.util.concurrent.*; import java.util.concurrent.a ...
- Unity3D版本之我见
关心Unity版本的变化以及了解未来版本的内容是专业做Unity的同学必备的功课,下面我来说一下我对4.0以后版本的一些见解. v4.0: 这个版本比3.5有较大的跳跃,首先最大卖点是新的动作系统Me ...