[codewars] TripleDouble

这篇博客讨论了 Codewars 上的一个编程挑战 'TripleDouble'。内容涉及到编写一个函数,该函数检查输入的两个数字数组中是否存在连续的三个相同数字(triple)以及连续的两个相同数字(double),并且这两个序列包含相同的数字。文章提供了作者的实现代码以及一个更巧妙的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Write a function

TripleDouble(long num1, long num2)

which takes numbers num1 and num2 and returns 1 if there is a straight triple of a number at any place in num1 and also a straight double of the same number in num2.

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;
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值