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(); } }

最新文章

  1. 实战p12文件转pem文件
  2. 【代码笔记】iOS-根据size截取屏幕中间矩形区域
  3. KVC与KVO的实现原理
  4. TCL校园招聘——软件开发工程师(java) 只招5个。。。
  5. Java关于md5+salt盐加密验证
  6. activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示
  7. 每天一道LeetCode--371. Sum of Two Integers
  8. c++ string用法
  9. 往github上传demo
  10. 随机数(random)
  11. Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程
  12. HDU 5815 - Golden Week
  13. hdu 3954 Level up(线段树)
  14. sql的基本查询语句
  15. JQuery打造下拉框联动效果
  16. Servlet 基础知识
  17. mysql 删匿名帐户
  18. Javascript实现继承
  19. ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解
  20. python中的2、8、16、10进制之间的转换

热门文章

  1. springboot-vue-前后端数据交互
  2. VMware Workstation 14 PRO 下安装Ubuntu 16.04 LTS教程
  3. [uiautomator篇][8] 增加应用读取内置存储卡的权限
  4. Good Bye 2017
  5. 矩阵快速幂在ACM中的应用
  6. [整理]菜鸟教程:docker使用笔记
  7. Welcome-to-Swift-16自动引用计数(Automatic Reference Counting)
  8. 【Luogu】P3389高斯消元模板(矩阵高斯消元)
  9. 面试题之redis的过期时间原理
  10. Linux运维打怪升级篇,从苦逼到牛逼的必备装备(转)