题目:
Write a function
TripleDouble(long num1, long num2)
which takes numbers
num1
andnum2
and returns1
if there is a straight triple of a number at any place innum1
and also a straight double of the same number innum2
.If this isn't the case, return
0
Examples
TripleDouble(451999277, 41177722899) == 1 // num1 has straight triple 999s and // num2 has straight double 99s TripleDouble(1222345, 12345) == 0 // num1 has straight triple 2s but num2 has only a single 2 TripleDouble(12345, 12345) == 0 TripleDouble(666789, 12345667) == 1
my code:
package my;
public class Kata
{
private static String temp ;
public static int TripleDouble(long num1, long num2)
{
//code me ^^
boolean flag1 = false;
boolean flag2 = false;
String[] list1 = String.valueOf(num1).split("");
String[] list2 = String.valueOf(num2).split("");
for(int i =0; i<list1.length-2;i++)
{
if(list1[i].equals(list1[i+1]) && list1[i].equals(list1[i+2]))
{
temp = list1[i];
flag1 = true;
}
}
for(int k =0; k<list2.length-1;k++)
{
if(list2[k].equals(list2[k+1] )&& list2[k].equals( temp))
{
flag2 = true;
}
}
if(flag1 == true && flag2 == true)
{
System.out.println("1");
return 1;
}
else
{
System.out.println("0");
return 0;
}
// print(list1);
}
clever code:
public class Kata
{
public static int TripleDouble(long num1, long num2)
{
String n1str = String.valueOf(num1);
String n2str = String.valueOf(num2);
for(int i=0;i<10;i++) {
String n = String.valueOf(i);
if( n1str.contains(n+n+n) && n2str.contains(n+n) ) return 1;
}
return 0;
}
}