Chi*_*mar -8 java algorithm alphanumeric increment
我需要实现字母数字增量算法,如 AAA001 应该变成 AAA002 AAA999 应该变成 AAB000 等等。
所有字母均为大写,字母为 0-9。它可以在字母数字字符串的任何位置包含字母或字母。
但是有一些规则,比如一些 000 或 666 不应该出现在一个系列中。这可以稍后完成,但我需要基本逻辑来实现算法。
我看到很多人没有理解我的问题。想象一下车辆的车牌号,它只是一个字母数字系列,可以包含一些排除的字符,例如 BB6660 -> 666,中间不允许出现三重 6。
它应该支持不同的格式,例如-
@@@##
@#@@##
1@#@@##
@@@@####
##@@#@
@ means alphabet A-Z
# means numbers 0-9
Run Code Online (Sandbox Code Playgroud)
例子:
AFG99 + 1= AFH00
A2GF23 + 1 = A2GF24
1A9AU99 + 1 = 1A9AV00
AAZZ9999 + 1 = ABAA0000
11AA9Z + 1 = 11AB0A
Run Code Online (Sandbox Code Playgroud)
我需要某种数学解决方案,以便我可以在不使用字符增量的情况下进行数学运算并轻松增加它。
我还需要两个范围之间的计数,例如 AAA003 和 AA010 之间有多少计数?
AAA010 - AAA003 = 7
Run Code Online (Sandbox Code Playgroud)
我会很感激的帮助..
这里有 3 个解决方案:前两个在某种程度上是算术增量,而第三个则更多是字符操作。
这 3 个实现都通过了相同的单元测试:
assertEquals("1DDA01A", MyClass.increment("1DDA00Z"));
assertEquals("1A9AV00", MyClass.increment("1A9AU99"));
assertEquals("AFH00", MyClass.increment("AFG99"));
assertEquals("A2GF24", MyClass.increment("A2GF23"));
assertEquals("ABAA0000", MyClass.increment("AAZZ9999"));
assertEquals("11AB0A", MyClass.increment("11AA9Z"));
Run Code Online (Sandbox Code Playgroud)
第一的:
public static String increment(String number) {
Pattern compile = Pattern.compile("^(.*?)([9Z]*)$");
Matcher matcher = compile.matcher(number);
String left="";
String right="";
if(matcher.matches()){
left = matcher.group(1);
right = matcher.group(2);
}
number = !left.isEmpty() ? Long.toString(Long.parseLong(left, 36) + 1,36):"";
number += right.replace("Z", "A").replace("9", "0");
return number.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)
第二:
public static String increment(String number) {
Pattern compile = Pattern.compile("^(.*?)([0-9]*|[A-Z]*)$");
Matcher matcher = compile.matcher(number);
String remaining = number;
String currentGroup = "";
String result = "";
boolean continueToNext = true;
while (matcher.matches() && continueToNext) {
remaining = matcher.group(1);
currentGroup = matcher.group(2);
int currentGroupLength = currentGroup.length();
int base = currentGroup.matches("[0-9]*") ? 10 : 36;
currentGroup = Long.toString(Long.parseLong("1" + currentGroup, base) + 1, base); // The "1" if just to ensure that "000" doesn't become 0 (and thus losing the original string length)
currentGroup = currentGroup.substring(currentGroup.length() - currentGroupLength, currentGroup.length());
continueToNext = Long.valueOf(currentGroup, base) == 0;
if (base == 36) {
currentGroup = currentGroup.replace("0", "A");
}
result = currentGroup + result;
matcher = compile.matcher(remaining);
}
result = remaining + result;
return result.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)
第三:
这适用于您当前的“请求”。比起一开始问的问题,这不仅仅是“左边由字母组成”+“右边由数字组成”。现在,它是“任何事情”,字母从 A 到 Z 到 A,而数字从 0 到 9 到 0。当一个字母到达 Z 时,它被重置为 A,然后它左边的数字/字母递增。
如果所有数字都递增,则不会在左侧添加新数字。你没有在你的问题中提到这一点,但我相信你可以从这里弄明白:
public static String increment(String number) {
char[] cars = number.toUpperCase().toCharArray();
for (int i = cars.length - 1; i >= 0; i--) {
if (cars[i] == 'Z') {
cars[i] = 'A';
} else if (cars[i] == '9') {
cars[i] = '0';
} else {
cars[i]++;
break;
}
}
return String.valueOf(cars);
}
Run Code Online (Sandbox Code Playgroud)
至于“计数”,您的示例不足以掌握逻辑。它只计算数字吗?字母呢?它是否遵循 baseXx ?
AA010-AAA003 = 7,3 A 与 2 A 怎么会无关紧要?我觉得这取决于您了解您的要求是什么(即:家庭作业..)
从技术上讲,这回答了最初提出的问题(在此过程中进行了许多修改)。