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 ...
随机推荐
- xcode加载静态链接库.a文件总是失败
明明项目是对的,代码没有问题,并且把项目作为库项目引入到新项目中没问题,可是一旦把项目编译出.a文件,引入到新项目中不知为何会有几率出现一大堆错误,其实是xcode的缓存机制在作怪,去这个目录: /U ...
- 关键的OOP概念
OOP的好处 1.封装, 2继承, 3多态. 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结果.不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性. <? ...
- django中的@login_required
转:http://www.cnblogs.com/ccorz/p/Django-zhong-loginrequired-yong-fa-jian-jie.html 1.网站开发时的登录需求: ===用 ...
- 十四、python沉淀之路--文件操作
一.文件操作b模式 1. # f = open('test11.py','rb',encoding='utf-8') # 这种情况会报错 f = open('test11.py','rb') # b ...
- PHP Tools for VS2017 key/破解 [搬运]
看看结果 搬运地址 : (自己敲吧...) 这里面破解的只有一年 时间可以在文中提供的 ------------------------------------------------------- ...
- chrome瀏覽器去掉緩存的方法
方法一: 1.開發說打開開發者工具 勾選這個訪問可以 方法二: command+shift+R
- 【转】使用JMeter进行负载测试——终极指南
使用JMeter进行负载测试——终极指南 这篇教程讨论的是JMeter,它是一款基于Java的.集合了几个应用程序.具有特定用途的负载和性能测试工具. 本篇主要涉及的内容: 解释一下JMeter的用途 ...
- mysql实战优化之二:limit优化(大表翻页查询时) sql优化
mysql的表test中有20105119行数据.建立索引:data_status,place_cargo_status 场景1: SELECT id, resource_id, resource_t ...
- Java-Maven-Runoob:Maven 项目文档
ylbtech-Java-Maven-Runoob:Maven 项目文档 1.返回顶部 1. Maven 项目文档 本章节我们主要学习如何创建 Maven 项目文档. 比如我们在 C:/MVN 目录下 ...
- python开发模块基础:time&random
一,time模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块 常用方法1.(线程)推迟指定的时间运行.单位为秒. time.sleep(1) #括号内为整数 2.获取当前 ...