一道面试题!!求解

一个8位的字符串例如:abcdefgh, 截取他末尾成对出现的0,例如

10000000 截取后的结果:10

12345600 截取后的结果:123456

12340000 截取后的结果是:1234

10000001:则不做截取操作

10101010:则不做截取操作
问题补充

litianyu0815 写道
litianyu0815 写道
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "1234570000";
        String str1 = "12340001";
        String str2 = "12342000";
        System.out.println(subDoubleZero(str));
        System.out.println(subDoubleZero(str1));
        System.out.println(subDoubleZero(str2));

    }

    public static String subDoubleZero(String str) {
        for (int i = str.length() - 1; i > 0; i--) {
            char c1 = str.charAt(i - 1);
            char c2 = str.charAt(i);
            if (c1 == '0' && c2 == '0') {
                String testStr = str.substring(0, i - 1);
                return subDoubleZero(testStr);
            }
            return str;
        }
        return str;
    }

 


我做的很粗糙,不过应该能帮助你。


真是太感谢你了!!

18个回答

[quote="kidneyball"][code="java"]
s = s.replaceAll("(00)*$","");
[/code][/quote]

貌似没做到楼主的要求嘛

[code="java"]
s = s.replaceAll("(00)*$","");
[/code]

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "1234570000";
        String str1 = "12340001";
        String str2 = "12342000";
        System.out.println(subDoubleZero(str));
        System.out.println(subDoubleZero(str1));
        System.out.println(subDoubleZero(str2));

    }

    public static String subDoubleZero(String str) {
        for (int i = str.length() - 1; i > 0; i--) {
            char c1 = str.charAt(i - 1);
            char c2 = str.charAt(i);
            if (c1 == '0' && c2 == '0') {
                String testStr = str.substring(0, i - 1);
                return subDoubleZero(testStr);
            }
            return str;
        }
        return str;
    }

 

litianyu0815 写道
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1234570000";
String str1 = "12340001";
String str2 = "12342000";
System.out.println(subDoubleZero(str));
System.out.println(subDoubleZero(str1));
System.out.println(subDoubleZero(str2));
}

public static String subDoubleZero(String str) {
    for (int i = str.length() - 1; i > 0; i--) {
        char c1 = str.charAt(i - 1);
        char c2 = str.charAt(i);
        if (c1 == '0' && c2 == '0') {
            String testStr = str.substring(0, i - 1);
            return subDoubleZero(testStr);
        }
        return str;
    }
    return str;
}</pre>

 


我做的很粗糙,不过应该能帮助你。

这个应该不难,去查下api,感觉类似2楼的正则表达式会比较好

2楼的好简洁呀

[quote="kidneyball"][code="java"]
s = s.replaceAll("(00)*$","");
[/code][/quote]
:idea:

不用正则的方案 :D :

[code="java"]
public class RemoveDoubleZero {

public static String removeDoubleZero(String s) {
    int i;
    for (i = s.length(); i > 2 && s.charAt(i - 1) == '0' && s.charAt(i - 2) == '0'; i -= 2);
    return s.substring(0, i);
}

public static void main(String[] args) {
    System.out.println(removeDoubleZero("1"));
    System.out.println(removeDoubleZero("10"));
    System.out.println(removeDoubleZero("100"));
    System.out.println(removeDoubleZero("1000"));
    System.out.println(removeDoubleZero("10000"));
}

}
[/code]

[quote="kidneyball"]不用正则的方案 :D :

[code="java"]
public class RemoveDoubleZero {

public static String removeDoubleZero(String s) {
    int i;
    for (i = s.length(); i > 2 && s.charAt(i - 1) == '0' && s.charAt(i - 2) == '0'; i -= 2);
    return s.substring(0, i);
}

public static void main(String[] args) {
    System.out.println(removeDoubleZero("1"));
    System.out.println(removeDoubleZero("10"));
    System.out.println(removeDoubleZero("100"));
    System.out.println(removeDoubleZero("1000"));
    System.out.println(removeDoubleZero("10000"));
}

}
[/code][/quote]
上面那个比我的牛啊 连递归都没用啊

while(str.endwith("00")){
str = str.substring(0,str.length()-2);
}

共18条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!