去掉字符串中连续出现k个0的子串
2017-12-26 16:44
274 查看
//去掉字符串中连续出现k个0的子串
public class RemoveKZeros{
//返回去掉k个0后的字符串
public static String removeKZeros(String str,int k)
{
if(str==null||k<1)
{
return str;
}
//字符串转换成数组
char[]ch=str.toCharArray();
int count=0;//记录0出现的次数
int start=-1; //记录0出现的位置
for(int i=0;i!=ch.length;i++)
{
if(ch[i]=='0')
{
count++;
start=start==-1?i:start;
}else{
if(count==k)
{
while(count--!=0)
{
ch[start++]=0; //null的ascci码为0
}
}
count=0;
start=-1;
}
}
if(count==k)
{
while(count--!=0)
{
ch[start++]=0;
}
}
//数组转换成字符串
return String.valueOf(ch);
}
public static void main(String[]args)
{
//System.out.println("Hello");
String str="A00B"; //原字符串
String str2="A0000B000";
System.out.println(removeKZeros(str,2));
System.out.println(removeKZeros(str2,3));
}
}
相关文章推荐
- 去掉字符串中连续出现k个0的子串
- [算法]去掉字符串中连续出现的k个0子串
- 去掉字符串中首次连续出现k个0的子串。
- 去掉字符串中连续出现k个0的子串
- 字符串问题---去掉字符串中连续出现k个0的子串
- 去掉字符串中连续出现k个0的子串
- 去掉字符串中连续出现的K个0的子串
- 经典笔试题——去掉字符串中连续出现K个0的子串
- 去掉字符串末尾连续出现k个0的子串
- 5.3 去掉字符串中连续出现k个0的子串
- 去掉字符串中连续出现k个0的字串
- 去掉字符串中出现k个0的子串
- C++实现去掉字符串中连续相同K个0子串
- 求一个字符串中连续出现次数最多的子串
- POJ 3294 Life Forms (后缀数组,求出现在不少于k个字符串的最长子串)
- 程序员面试宝典:求一个字符串中连续出现次数最多的子串
- 求一个字符串中连续出现次数最多的子串
- 求一个字符串中连续出现次数最多的子串
- 字符串中连续出现最多的子串 & 字符串中最长反复子串
- 求一个字符串中连续出现次数最多的子串