小思路,突然短路了

zly361814478 2012-10-08 10:21:07
输入一个二进制串0111000111100 打印出1或者0出现最大次数 比如结果为 4;
来个实现
...全文
121 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zly361814478 2012-10-08
  • 打赏
  • 举报
回复
其实您这全都复杂了
wiksys 2012-10-08
  • 打赏
  • 举报
回复
高手!
[Quote=引用 3 楼 的回复:]

Java code

public class Find01
{
public static void main(String[] args)
{
String str="0111000111100";
String str1=str.replaceAll("11*"," ");
String str2=str.replaceAl……
[/Quote]
yufenghua 2012-10-08
  • 打赏
  • 举报
回复
额 没看清楚需求。。。我检讨[Quote=引用 4 楼 的回复:]
貌似这样也是可以的。。。。

Java code
package com.fenghua;

public class FindZeroOne {
/**
* 此方法为仅有两个字符(可多次出现)返回出现次数最多的字符的次数
* @param str
* @return
*/
public static int max01……
[/Quote]
yufenghua 2012-10-08
  • 打赏
  • 举报
回复
貌似这样也是可以的。。。。
package com.fenghua;

public class FindZeroOne {
/**
* 此方法为仅有两个字符(可多次出现)返回出现次数最多的字符的次数
* @param str
* @return
*/
public static int max01(String str){
int b1=1;
int a1=0;
char[] a=str.toCharArray();
for(int i=0;i<a.length-1;i++){
if(a[i]==a[i+1]){
b1++;
}else{
a1=a1+b1;
b1=a1-b1;
a1=a1-b1;
b1++;
}
}
return Math.max(a1, b1);
}
public static void main(String[] args){
String str="01010101000000000111111";
System.out.println(max01(str));
}

}
nmyangym 2012-10-08
  • 打赏
  • 举报
回复

public class Find01
{
public static void main(String[] args)
{
String str="0111000111100";
String str1=str.replaceAll("11*"," ");
String str2=str.replaceAll("00*"," ");
String[] s0=str1.split(" ");
String[] s1=str2.split(" ");
int num0=0;
int num1=0;
for(int i=0;i<s0.length;i++)
{
if(num0<s0[i].length())
{
num0=s0[i].length();
}
}
System.out.println("num0 is "+num0);
for(int i=0;i<s1.length;i++)
{
if(num1<s1[i].length())
{
num1=s1[i].length();
}
}
System.out.println("num1 is "+num1);

}
}
zly361814478 2012-10-08
  • 打赏
  • 举报
回复
诶,我刚自己做出来了。。脑子小小短路了下 谢谢谢啊
xiars123 2012-10-08
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String s = "0111000111100";
char[] c = s.toCharArray();
int num = 1;
int max = 1;
for (int i = 1; i < c.length; i++) {
if (c[i] == c[i - 1]) {
num++;
} else {
max = Math.max(max, num);
num = 1;
}
}
System.out.println(max);
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧