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. 一个TensorFlow例子

    一个TensorFlow的例子 import tensorflow as tf x = tf.constant(1.0, name='input') w = tf.Variable(0.8, name ...

  2. JAVA-STRUTS-2x的项目配置

    首先是web.xml的配置,这个是项目加载的开始. <display-name></display-name> <!--struts2配置开始--> <fil ...

  3. C#-SqlServer连接

    C#连接数据库在类方面没有java通用,不同数据库有不同的类库.在这里只做了SqlServer的连接类. public class DbLink { private string config = C ...

  4. 服务器端查看图片库 eog

    eog 命令 eye of gnome,用来在服务器端查看图片. sudo apt-get install eog eog picname.jpg

  5. Timer和TimerTask详解

    1.概览 Timer是一种定时器工具,用来在一个后台线程计划执行指定任务.它可以计划执行一个任务一次或反复多次.TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务. 简单的一个例 ...

  6. java EE技术体系——CLF平台API开发注意事项(2)——后端测试

    前言:上篇博客说到了关于开发中的一些情况,这篇博客主要说明一些关于测试的内容. 一.宏观说明 要求:每一个API都必须经过测试.   备注:如果涉及到服务间调用(如权限和基础数据),而对方服务不可用时 ...

  7. Bootstrap-datepicker 用法

    <div class="input-group input-daterange"> <input type="text" id="s ...

  8. IE6 IE7下li间距、高度不一致问题(转)

    http://www.phpddt.com/dhtml/926.html 问题描述:li的高度在IE6 IE7间距高度和其他浏览器不一致,即便设定了高度,IE6,7中,仍比其他浏览器要高. 解决方法: ...

  9. [LOJ#2255][BZOJ5017][Snoi2017]炸弹

    [LOJ#2255][BZOJ5017][Snoi2017]炸弹 试题描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足: ...

  10. Jersey实现Client端:GET/PUT/POST/DELETE

    原文地址:Jersey客户端API调用REST风格的Web服务 [节选] 要开始使用 Jersey 客户端 API,你首先需要创建一个 com.sun.jersey .api.client.Clien ...