repeated-substring-pattern
2024-08-26 16:36:28
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(); } }
最新文章
- 实战p12文件转pem文件
- 【代码笔记】iOS-根据size截取屏幕中间矩形区域
- KVC与KVO的实现原理
- TCL校园招聘——软件开发工程师(java) 只招5个。。。
- Java关于md5+salt盐加密验证
- activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示
- 每天一道LeetCode--371. Sum of Two Integers
- c++ string用法
- 往github上传demo
- 随机数(random)
- Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程
- HDU 5815 - Golden Week
- hdu 3954 Level up(线段树)
- sql的基本查询语句
- JQuery打造下拉框联动效果
- Servlet 基础知识
- mysql 删匿名帐户
- Javascript实现继承
- ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解
- python中的2、8、16、10进制之间的转换
热门文章
- springboot-vue-前后端数据交互
- VMware Workstation 14 PRO 下安装Ubuntu 16.04 LTS教程
- [uiautomator篇][8] 增加应用读取内置存储卡的权限
- Good Bye 2017
- 矩阵快速幂在ACM中的应用
- [整理]菜鸟教程:docker使用笔记
- Welcome-to-Swift-16自动引用计数(Automatic Reference Counting)
- 【Luogu】P3389高斯消元模板(矩阵高斯消元)
- 面试题之redis的过期时间原理
- Linux运维打怪升级篇,从苦逼到牛逼的必备装备(转)