问题描述
一个8位的字符串例如:abcdefgh, 截取他末尾成对出现的0,例如10000000 截取后的结果:1012345600 截取后的结果:123456 12340000 截取后的结果是:123410000001:则不做截取操作10101010:则不做截取操作问题补充litianyu0815 写道
解决方案
kidneyball 写道s = s.replaceAll("(00)*$","");貌似没做到楼主的要求嘛
解决方案二:
public static void main(String[] args) throws Exception {System.out.println(trimTailCoupleZero("100000"));System.out.println(trimTailCoupleZero("00000"));System.out.println(trimTailCoupleZero("0000"));System.out.println(trimTailCoupleZero("10000000"));System.out.println(trimTailCoupleZero("12345600"));System.out.println(trimTailCoupleZero("12340000"));System.out.println(trimTailCoupleZero("10000001"));System.out.println(trimTailCoupleZero("10101010"));}public static String trimTailCoupleZero(String str) {int n = str.length() - 1;int index = n;for (; index >= 0; index--) {if (str.charAt(index) != '0')break;}if (((n - index) & 1) != 0) {index++;}if (n == index)return str;elsereturn str.substring(0, index + 1);}不用递归,substring方法最多也只要调用一次
解决方案三:
bf2624 你的代码也太那个啥了吧?你知道你这样子会产生多少个String对象吗?而且subString会进行native方法调用,JNI查找本地方法时的查找要用时间的,性能的话还是要考虑的。还是kidneyball的指针移动比较好。
解决方案四:
……public static void main(String[] args){String a = "1000000";for(int i=0;i<a.length();i++){if(a.endsWith("00")){a = a.substring(0,a.length()-2);System.out.println(a);}else{break;}}}
解决方案五:
这算法不用这么罗嗦吧~public static void main(String[] args){String a = "werwr00000";for(int i=0;i<a.length()/2;i++){if(a.endsWith("00")){a = a.substring(0,a.length()-2);}else{break;}System.out.println(a);}}
解决方案六:
你们都是刚工作啊?
解决方案七:
zkl_1987 写道kidneyball 写道s = s.replaceAll("(00)*$","");貌似没做到楼主的要求嘛符合要求,没错啊。去掉末尾成对的0。
解决方案八:
zkl_1987 写道kidneyball 写道s = s.replaceAll("(00)*$","");貌似没做到楼主的要求嘛例如哪个输入数据?
解决方案九:
kidneyball 写道不用正则的方案 :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"));}}向你学习了。
解决方案十:
while(str.endwith("00")){ str = str.substring(0,str.length()-2);}
解决方案十一:
kidneyball 写道不用正则的方案 :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"));}}上面那个比我的牛啊 连递归都没用啊
解决方案十二:
不用正则的方案 :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"));}}
解决方案十三:
kidneyball 写道s = s.replaceAll("(00)*$","");
解决方案十四:
2楼的好简洁呀
解决方案十五:
这个应该不难,去查下api,感觉类似2楼的正则表达式会比较好
其他方案:
litianyu0815 写道public static void main(String[] args) {// TODO Auto-generated method stubString 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;}
其他方案:
public static void main(String[] args) {// TODO Auto-generated method stubString 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;}
其他方案:
s = s.replaceAll("(00)*$","");