写一个方法,2) 返回一个字符串中字符连续出现次数最多的字符,3) 及其出现的次数,4) 如果多个字符出现的次数同样最多,返回第一个字符。
例如:aaafffxxxxxx 返回结果为 ( x , 6)
Aaabbbccc 返回结果为 (b , 3)
例如:aaafffxxxxxx 返回结果为 ( x , 6)
Aaabbbccc 返回结果为 (b , 3)
解决方案 »
- 谁了解,做Mac os 应用软件(objective-c)开发前景如何
- JAVA 官方网站,找不到 JDK 6 的API文档下载地址?给我把地址发上来,谢谢、。要官方地址。
- 大家有空都来看看!!!
- SWT,请问,怎么把Label设置为透明
- try问题..................
- 怎样在java中实现:在同一本程序中实现取2个数据库的数据,而且这2个数据库在2台不同机器上,分别是sqlserver2000和sqlserver2005。
- 关于JAVA的强转问题
- 急求jspsmartupload.zip 能否给发一个 谢谢! (在线等)急啊!
- 关于类之间的调用
- 能否实现运行在两台不同服务器上的Applet之间的方法调用?
- 请教如何在一个java程序中启动另一个java程序
- 如何将16进制的ucs码转换成utf-8
,如果该字符与下一个字符不等,输出COUNT值与该字符,并将COUNT置0
public class ChongDie {
public String getMost(String str){
List list = new ArrayList();
StringBuffer temp = new StringBuffer(""+str.charAt(0));
//将每一组连续的数字写入集合中
for(int i=1;i<str.length();i++){
if(str.charAt(i)==str.charAt(i-1)){
temp.append(str.charAt(i));
}else{
list.add(temp.toString());
temp = new StringBuffer(""+str.charAt(i));
}
if(i==str.length()-1){
list.add(temp.toString());
}
}
//检查集合中数量最多的一组
Iterator it = list.iterator();
int count=0;
String fin = null;
while(it.hasNext()){
String c=(String)it.next();
if(c.length()>count){
count = c.length();
fin = c;
}
}
return fin.toString();
}
public static void main(String[] args){
ChongDie cd = new ChongDie();
String fin = cd.getMost("aaabbbbbcadefffffffxxxxxxxgg");
System.out.println(""+fin.charAt(0)+","+fin.length());
} //result: f,7
}
public class Test {
public static void main(String[] args)
{
Test test = new Test();
test.test("a");
}
public void test(String str)
{
byte[] barray = str.getBytes();
byte b=0;
int count = 0;
int tempCount = 0;
for (int i = 0; i < barray.length; i++) {
if(i>0)
{
if(barray[i]==barray[i-1])
{
++tempCount;
}
else
{
tempCount = 1;
}
}
else
{
++tempCount;
}
if(count<tempCount)
{
count = tempCount;
b = barray[i];
}
}
System.out.println("char-->"+(char)b+"\ncount-->"+count);
}
}
boolean flag=true;
int n=1,m=0;
char c='\0';
for(int i=1;i<s.length();i++){
if(s.charAt(i)==s.charAt(i-1)){
n++;
}else{
flag=false;
if(m<n){
m=n;
c=s.charAt(i-1);
n=1;
}else{
n=1;
}
}
}
if(flag==true){
System.out.println(s.charAt(0)+" "+n);
}else{
if(n>m){
System.out.println(s.charAt(s.length()-1)+" "+n);
}else{
System.out.println(c+" "+m);
}
}
}