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的更多相关文章

  1. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  2. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  3. LeetCode_459. Repeated Substring Pattern

    459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...

  4. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  5. *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 ...

  6. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  7. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  8. Repeated Substring Pattern Leetcode

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  10. [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 ...

随机推荐

  1. CentOS-文件操作

    centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...

  2. 关于freetype在安装中的遇到的问题

    本人电脑配置的是Anconda环境+pycharm2017.2.2 comuniity,每次安装什么包就是直接pip install 的,但是这次在安装freetype的安装中却遇到了麻烦. 具体是在 ...

  3. luogu1972 [SDOI2009]HH的项链

    莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...

  4. 淘宝的TProfile分析

    TProfile是一个用来抓取性能数据的工具.大概是去年的时候对其分析了一下,并将它改造成了用于分析学习开源产品时的一个trace工具(不是很完善,自己用够用).现在将之前的笔记翻出来,记录一下. 1 ...

  5. js中的事件委托和事件代理详解

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...

  6. hdu 2665 划分树模板题(可作为模板)

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. JDBC 学习笔记(一)—— JDBC 基础

    1. 什么是 JDBC JDBC,Java Database Connectivity(Java 数据库连接),是一组执行 SQL 语句的 Java API. JDBC,是 Java SE(Java ...

  8. 【Luogu】P3047附近的牛(树形DP)

    题目链接 树形DP,设f[i][j]是当前在i点,j步之内有多少牛.从相邻点to的f[to][j-1]转移而来,减去重复计算即可. #include<cstdio> #include< ...

  9. Bash Command 1: find

    GNU find searches the directory tree rooted at each given starting-point by evaluating the given exp ...

  10. HDU——2723Electronic Document Security(STL map嵌套set做法)

    Electronic Document Security Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...