gpt4 book ai didi

java - "If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0."

转载 作者:行者123 更新时间:2023-12-04 08:34:08 25 4
gpt4 key购买 nike

我想让代码只添加我试图读取的数字的完全唯一版本(即 111 中的 1 给出 3,但 333 中的 33 仅给出 33。),但是当我尝试输出结果,像后一个示例这样的实例每个间隔计数一次并添加每个计数的实例(例如,333 中的 33 给出 66 而不是 33)。
到目前为止,这是我的代码:

public static int sumRepeat(int num, String text)
{
int sum = 0;
String calc = Integer.toString(num);
int value = text.indexOf(calc);
for (int i = 0; i < text.length() - calc.length() + 1; i++) {
if (text.substring(i, i + calc.length()).equals(calc))
sum += num;
}
return sum;
}
有人可以帮我把它弄到我不会遇到这个错误并且代码按预期运行的地方吗?谢谢你。
编辑:抱歉,我的测试用例比我最初说的要多。这是我所有的测试用例:
sumRepeat(1, "I love CSCE111") returns 3
sumRepeat(12, "The bill is $12.97") returns 0
sumRepeat(33, "333 bananas for 33 monkeys.") returns 66
sumRepeat(333, "My number is (333)-333-3333") returns 999
sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!") returns 0
sumRepeat(41, "41414141") returns 164
sumRepeat(0, "") returns 0

最佳答案

更新(基于更新的问题):
您可以使用 Java Regex API来解决它。使用正则表达式,"(?<!\\$)(" + String.valueOf(num) + ")"并将每个匹配项添加到 sum .请注意 ?<!用于 negative lookbehind .这里的意思是num前面不应加上 $ .

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
System.out.println(sumRepeat(1, "111"));
System.out.println(sumRepeat(33, "333"));
System.out.println(sumRepeat(1, "I love CSCE111"));
System.out.println(sumRepeat(12, "The bill is $12.97"));
System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
System.out.println(sumRepeat(41, "41414141"));
System.out.println(sumRepeat(0, ""));
}

public static int sumRepeat(int num, String text) {
int sum = 0;
Matcher matcher = Pattern.compile("(?<!\\$)(" + String.valueOf(num) + ")").matcher(text);
while (matcher.find()) {
sum += num;
}
return sum;
}
}
输出:
3
33
3
0
66
999
0
164
0
非正则表达式解决方案:
您可以使用 String#indexOf(String, int) 找到 calc 的索引从给定的索引开始。每次找到该索引时,添加 numsum并将此索引移动 num 的长度;否则,继续将此索引移动 1 .
演示:
public class Main {
public static void main(String[] args) {
System.out.println(sumRepeat(1, "111"));
System.out.println(sumRepeat(33, "333"));
System.out.println(sumRepeat(1, "I love CSCE111"));
System.out.println(sumRepeat(12, "The bill is $12.97"));
System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
System.out.println(sumRepeat(41, "41414141"));
System.out.println(sumRepeat(0, ""));
}

public static int sumRepeat(int num, String text) {
int sum = 0;
String calc = Integer.toString(num);
int len = calc.length();
int i = 0;
while (i < text.length()) {
int index = text.indexOf(calc, i);
if (index != -1) {
if (index == 0 || text.charAt(index - 1) != '$') {// Check to exclude num preceded by $
sum += num;
i = index + len;
} else {
i++;
}
} else {
i++;
}
}
return sum;
}
}
输出:
3
33
3
0
66
999
0
164
0
原解决方案:
修改循环计数器以终止于 i < text.length()并继续步值 num .
public class Main {
public static void main(String[] args) {
System.out.println(sumRepeat(1, "111"));
System.out.println(sumRepeat(33, "333"));
}

public static int sumRepeat(int num, String text) {
int sum = 0;
String calc = Integer.toString(num);
for (int i = 0; i < text.length(); i += num) {
if (text.substring(i, i + calc.length()).equals(calc)) {
sum += num;
}
}
return sum;
}
}
输出:
3
33

关于java - "If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64884316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com