If I enter a=12435
and b=45 as they are in the sequence return true
a=12435
b=54 as they are not in sequence return false.
I think i can accept an input number as a string and split it into char array but I wanted to use only INT arrays.That is what the problem i am facing.Thanks in advance.
在确定a是超级数后,如何判断数字b是否与a的顺序相同?
答案 0 :(得分:0)
public static boolean verify(String a, String b){
if("".equals(b)){
return true;
}
int locate = a.indexOf(String.valueOf(b.charAt(0)));
if(locate == -1){
return false;
}else{
return verify(a.substring(locate + 1), b.substring(1));
}
}
答案 1 :(得分:0)
使用变量来跟踪匹配字符的序列。 找到匹配项后,将删除该字符以防止在处理包含重复字符的序列时出现潜在错误。
public bool IsInSequence(string a, string b)
{
int lowestIndex = -1;
for (int i = 0; i < a.Length; i++)
{
var index = b.IndexOf(a[i]);
if (index != -1)
{
if (index >= lowestIndex)
{
lowestIndex = index;
b = b.Remove(index,1);
--i;
}
else
return false;
}
}
return lowestIndex > -1;
}
答案 2 :(得分:0)
您不需要像其他答案所示转换为String
:只需检查超级号码的最后一位数字(a
)是否与正常号码的最后一位数字相匹配( b
)。
a
除以10,因为该数字不是您正在寻找的下一个数字。重复此操作直到任一数字达到零;如果a
与b
匹配,则b
现在将为零。
while (a != 0 && b != 0) {
if (a % 10 == b % 10) {
b /= 10;
}
a /= 10;
}
return b == 0;