substring,subsequence,charAt执行效率的不同

Posted monster-world

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了substring,subsequence,charAt执行效率的不同相关的知识,希望对你有一定的参考价值。

 1 package com.java.tencent;
 2 
 3 public class T_2_longestPalindrome {
 4 
 5     public String test1(String s){
 6         long startTime=System.currentTimeMillis();
 7         int len = s.length();
 8         int tmp = 0;
 9         String result = "";
10         for(int i=0;i<len;i++){
11             if(tmp+i-len>0){
12                 break;
13             }
14             int lenj = len-1;
15             for(int j=lenj;j>=i;j--){
16                 if(tmp>j-i+1){
17                     break;
18                 }
19                 String ch = s.substring(i,i+1);
20                 String chj = s.substring(j,j+1);
21                 if(ch.equals(chj)){
22                     String str = s.substring(i,j+1);
23                     if(tmp>str.length()){
24                         continue;
25                     }
26                     boolean bl = true;
27                     int ln = str.length();
28                     int lenm = ln/2+1;
29                     for(int m=0;m<lenm;m++){
30                         CharSequence start = str.subSequence(m, m+1);
31                         CharSequence end = str.subSequence(ln-m-1,ln-m);
32                         if(!start.equals(end)){
33                             bl = false;
34                             break;
35                         }
36                     }
37                     if(bl && ln>tmp){
38                         result = str;
39                         tmp = ln;
40                     }
41                 }
42             }
43         }
44         System.out.println(result);
45         long endTime=System.currentTimeMillis();
46         System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
47         return result;
48     }
49 
50     public String test2(String s){
51         long startTime=System.currentTimeMillis();
52         int len = s.length();
53         int tmp = 0;
54         String result = "";
55         for(int i=0;i<len;i++){
56             if(tmp+i-len>0){
57                 break;
58             }
59             for(int j=len-1;j>=i;j--){
60                 if(tmp>j-i+1){
61                     break;
62                 }
63                 String str = s.substring(i,j+1);
64                 boolean bl = true;
65                 for(int m=0;m<(str.length()/2+1);m++){
66                     Character start = str.charAt(m);
67                     Character end = str.charAt(str.length()-m-1);
68                     if(!start.equals(end)){
69                         bl = false;
70                         break;
71                     }
72                 }
73                 if(bl && str.length()>tmp){
74                     result = str;
75                     tmp = str.length();
76                     break;
77                 }
78             }
79         }
80         System.out.println(result);
81         long endTime=System.currentTimeMillis();
82         System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
83         return result;
84     }
85     
86     
87     public static void main(String[] args) {
88         T_2_longestPalindrome lp = new T_2_longestPalindrome();
89         String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
90         lp.test2(s);
91     }
92 
93 }

以上写了一个返回最长回文子串的程序(1000个a,回文字串就是如abcddcba、bcb,左右两边相同,当然a一个字符也是),之前使用的test1总是500以上的执行时间,时间总是无法降低,最后问题落到了两个截取字串位置上,经过修改,test2成功降低执行时间。

test1 使用 sub 截取字符串,执行时间500+ms

test2 使用 charAt,执行时间180ms

以上可以明显看出执行效率的不同。

以上是关于substring,subsequence,charAt执行效率的不同的主要内容,如果未能解决你的问题,请参考以下文章

substring,subsequence,charAt执行效率的不同

Codeforces 163A Substring and Subsequence

Longest Palindromic Substring & Longest Palindromic Subsequence

CodeForces 163A Substring and Subsequence

Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

*187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)