Leetcode Repeated Substring Pattern 459
来源:互联网 发布:艾弗森总决赛数据 编辑:程序博客网 时间:2024/05/05 23:17
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
Input: “abab”
Output: True
Explanation: It’s the substring “ab” twice.
Example 2:
Input: “aba”
Output: False
Example 3:
Input: “abcabcabcabc”
Output: True
Explanation: It’s the substring “abc” four times. (And the substring “abcabc” twice.)
题目链接
考虑字符串的长度不超过10000.
使用一般的算法可以求解
class Solution {public: bool repeatedSubstringPattern(string str) { if(str.length()==0) return false; int len=str.length(); for(int i=1;i<len/2+1;i++){ if(len%i==0){ int cnt=len/i; if(cnt==1) return false; string substring=""; for(int j=0;j<i;j++){ substring+=str[j]; } bool mark=true; //cout<<"cnt="<<cnt<<endl; for(int j=0;j<cnt;j++){ string temp=""; for(int k=j*i;k<i*(j+1);k++){ temp+=str[k]; } //cout<<"temp="<<temp<<" substring="<<substring<<endl; if(!judge(temp,substring)){ mark=false; break; } } if(mark) return true; } } return false; } bool judge(string t,string s){ int lent=t.length(); int lens=s.length(); if(lent!=lens) return false; for(int i=0;i<lent;i++){ if(s[i]==t[i]) continue; else{ return false; } } return true; }};
ac之后
觉得可以提高的两点:1.stl中很多算法可以直接使用,避免类似judge()的使用2.kmp算法可以更高效求解 补一下
0 0
- Leetcode 459 Repeated Substring Pattern
- Leetcode Repeated Substring Pattern 459
- leetcode-459-Repeated Substring Pattern
- 【LeetCode 459】 Repeated Substring Pattern
- Leetcode Repeated Substring Pattern
- LEETCODE--Repeated Substring Pattern
- LeetCode : Repeated Substring Pattern
- LeetCode 459: Repeated Substring Pattern (c++)
- LeetCode 459. Repeated Substring Pattern
- Leetcode-459. Repeated Substring Pattern
- leetcode 459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- [LeetCode]Repeated Substring Pattern(Java)
- 【LeetCode】 459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- Leetcode 459. Repeated Substring Pattern
- LeetCode-459. Repeated Substring Pattern
- Leetcode 459. Repeated Substring Pattern
- 神舟战神K660E(D)-I7(D8)(战斗版)(tn15s)拆机清灰教程
- EventBus的使用
- object references an unsaved transient instance - save the transient instance before flushing:
- 安卓cordove插件开发指导(android plugin development guide)
- jquery 打开小窗口
- Leetcode Repeated Substring Pattern 459
- 第16周项目三--归并排序算法的改进
- weblogic部署脚本
- NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区+NFC_电子发烧友网【申明:来源于网络】
- 帝国CMS商城代码解析1 – 购物车cookie
- 解决 第一次安装执行pod setup 慢的问题
- 慎用GetOpenFileName
- 基于 OpenFlow 的 SDN 技术 (论文笔记)
- 关于jvm内存分配的问题heap size [268435456] not equal to maximum heap size [2147483648],需要修改的jvm配置