我在美国要面试一家软件公司的实习机会, 原题是这样的: How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
没有限制,我只是想了解下编程思想,呵呵 我在美国要面试一家软件公司的实习机会, 原题是这样的: How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
原题是这样的: How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
For Examplelong l = 1230000001; String test = l+""; System.out.println(test.length()-test.replaceAll("0*", "").length());
方法N多
先获得总的长度replace ""
在获得长度相减
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
希望这两个方法能够帮助你我觉得用循环方便。
Pattern pattern = Pattern.compile("0+");
Matcher matcher = pattern.matcher("123424000000123434");
if (matcher.find()) { // 123424000000123434000123100如果这样的,每一个都要求出来,还得用循环。
System.out.println(matcher.group() + ": " + matcher.group().length());
}
原题是这样的:
How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
我在美国要面试一家软件公司的实习机会,
原题是这样的:
How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
原题是这样的:
How can you tell efficiently how many consecutive zero's are in a number without looping through its digits?
String test = l+"";
System.out.println(test.length()-test.replaceAll("0*", "").length());
(6 2 5 3)
String b = a+"";
String[] c = b.split("0");为什么用这个截取不到连续的0,只能截取一个