想了近半个多小时,然后看视频对答案,结果说我这个方法还是高效的{:3_53:} 原来都是先看答案再自己敲,现在是先敲再看答案,感觉比前者好
- class str{
- public static void main(String[] args){
- String s = "abkkcdkkefkkskksdjhfgkjkkkk";
- sop("("+s+")");
- sop("("+getString(s,"kk")+")");
- }
- public static int getString(String str,String key){
- int count =0;
- int x =0;
- while(true){
- int y = str.indexOf(key,x);
- if(y==-1)
- break;
- count++;
- x = str.indexOf(key,x)+key.length();
-
- }
- return count;
- }
- public static void sop(Object obj){
- System.out.println(obj);
- }
- }
复制代码 |
|