repeated-substring-pattern
https://leetcode.com/problems/repeated-substring-pattern/
下面这个方法,开始我觉得挺好。可惜还是超时了。后来我就加了一个剪枝策略,只有长度能够整除总长度的子串,才需要进行比较。
package com.company; import java.util.*; class Solution {
public boolean repeatedSubstringPattern(String str) { for (int i=; i<=str.length()/; i++) {
if (str.length() % i != ) {
continue;
}
StringBuilder sb = new StringBuilder();
sb.append(str.substring(i));
sb.append(str.substring(, i));
if (str.equals(sb.toString())) {
return true;
}
}
return false;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
String str = "abcabcabcc";
boolean ret = solution.repeatedSubstringPattern(str);
System.out.printf("ret:%b\n", ret); System.out.println(); } }
下面是开始超时的方法,少了一个剪枝条件。
package com.company; import java.util.*; class Solution {
public boolean repeatedSubstringPattern(String str) { for (int i=; i<=str.length()/; i++) {
StringBuilder sb = new StringBuilder();
sb.append(str.substring(i));
sb.append(str.substring(, i));
if (str.equals(sb.toString())) {
return true;
}
}
return false;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
String str = "abcabcabcc";
boolean ret = solution.repeatedSubstringPattern(str);
System.out.printf("ret:%b\n", ret); System.out.println(); } }
repeated-substring-pattern的更多相关文章
- 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【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- LeetCode_459. Repeated Substring Pattern
459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
- *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 ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- Repeated Substring Pattern Leetcode
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- LeetCode算法题-Repeated Substring Pattern(Java实现)
这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...
- [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 ...
随机推荐
- 在Ubuntu中打开pycharm步骤:
在pycharm的bin文件夹中打开终端,包含pycharm.sh文件的,输入“sh pycharm.sh",如下图所示: 创建工程和windows环境下相同. 结束关掉pycharm 终端 ...
- Mac 之 STF 搭建(淘宝源安装)
参考链接:https://www.jianshu.com/p/5fe8cb7d214f (MAC直接安装STF)https://www.jianshu.com/p/c5c298486dbd(homeb ...
- 【第一章第一回】BootStrap 简介
Twitter Bootstrap 是目前最受欢迎的前端框架,它简洁.直观.移动优先.强悍的前端开发框架,让web开发更迅速.简单.基于HTML.CSS和Javascript. 为什么使用Bootst ...
- python-高级编程-06-长连接&连接池
我们都知道tcp是基于连接的协议,其实这个连接只是一个逻辑上面的概念,在ip层来看,tcp和udp仅仅是内容上稍有差别而已. tcp 的连接仅仅是连接两端对于四元组和sequence号的一种约定而已 ...
- getattr、setattr、hasattr
写一个演示类 class test(): title="验证getattr.setattr.hasattr方法" def run(self): return "run方法 ...
- EOJ Monthly 2018.4
A. ultmaster 的小迷妹们 Time limit per test: 2.0 seconds Memory limit: 256 megabytes ultmaster 男神和他的小迷妹们准 ...
- oracle中的dual表
dual表是和Oracle数据字典一起创建的.它实际上只包含dummy这一个column,并且只有一条记录,这条记录的值是X. X dual表的owner是SYS,但所有用户都可以访问它.Althou ...
- 九度oj 题目1459:Prime ring problem
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
- 学习 JSP:第三步 JSP基础(未完)
因为之前学过也用过JSP,这里只列出笔记,初学者请移步其他教程. JSP隐含对象 JSP支持九个自动定义的变量,江湖人称隐含对象.这九个隐含对象的简介见下表: 对象 描述 request HttpSe ...
- jdk1.7升级到1.8遇到的问题
1.修改project structure 里面的Project , Modules , SDKs jdk的版本 2.修改Java Compiler 里面java的jdk版本 3.tomcat 里面j ...