Codewars解题Are they the "same"?

来源:互联网 发布:程序员进公司职业规划 编辑:程序博客网 时间:2024/06/05 10:36

题目
Given two arrays a and b write a function comp(a, b) (compSame(a, b)
in Clojure) that checks whether the two arrays have the “same”
elements, with the same multiplicities. “Same” means, here, that the
elements in b are the elements in a squared, regardless of the order.

Examples

Valid arrays

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [121, 14641, 20736,
361, 25921, 361, 20736, 361] comp(a, b) returns true because in b 121
is the square of 11, 14641 is the square of 121, 20736 the square of
144, 361 the square of 19, 25921 the square of 161, and so on. It gets
obvious if we write b’s elements in terms of squares:

谷歌翻译:

给定两个数组a并b写一个函数comp(a, b)(compSame(a,
b)在Clojure中),检查两个数组是否具有相同的元素,具有相同的多重性。在这里,“相同”是指无论顺序如何,其中的元素b都是a平方的元素。

例子

有效的数组

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [121, 14641, 20736,
361, 25921, 361, 20736, 361] comp(a,
b)因为b121是11的平方,14641是121的平方,144是平方,361是19的平方,25921是161的平方,依此类推。如果我们b按照正方形编写元素,这是显而易见的:

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [11*11, 121*121,
144*144, 19*19, 161*161, 19*19, 144*144, 19*19] 无效的数组

如果我们把第一个数字改成其他的数字,comp可能不会再变成真的了:

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [132, 14641, 20736,
361, 25921, 361, 20736, 361] comp(a,b)返回假,因为在b132不是任何数量的平方a。

a = [121, 144, 19, 161, 19, 144, 19, 11] b = [121, 14641, 20736,
36100, 25921, 361, 20736, 361] comp(a,b)返回false,因为在b36100中不是任何数量的平方a。

备注

a或者b可能是[](R,Shell以外的所有语言)。 a或者b可能是nil或null或None(除了在Haskell,酏剂,C
++,锈病,R,壳牌)。

如果a或b有nil(或null或None),这个问题就没有意义了那么返回false。

如果a或者b是空的,结果本身就是明显的。

C的注意事项

这两个数组的大小(> 0)与函数中的参数大小相同comp。

大意就是如果数组b中的数据与数组a中的数据的平方一一对应则返回true 反之返回false

以下为我的代码

public class AreSame {    public static boolean comp(int[] a, int[] b) {     //判断数组是否为null 不判断会报错    if(a==null||b==null){        return false;    }    //变量求数组a中所有数的平方的和    int sum1 = 0;    //求数组b中所有数的和    int sum2 = 0;    //遍历数组b求和    for(int j = 0;j<b.length;j++){      sum2+=b[j];    }    //遍历数组a    for(int i = 0 ;i<a.length;i++){       //定义标记      int tem =  0 ;      //获得平方      int aa = a[i]*a[i];      //将平方累加      sum1+=aa;      for(int j = 0;j<b.length;j++){          //判断是否存在符合条件的数据        if(aa==b[j]){            //判断标记确保执行一次          if(tem==0){             //标记            tem++;            //将符合条件的数据置为无效,避免重复            b[j]=b[j]+1;          }        }      }      //判断标记,若没有标记返回false      if(tem==0){        return false;      }    }    //判断数据总体的合法性    if(sum1!=sum2){      return false;    }    //以上条件都通过则返回true    return true;  }}

该方法已经通过校验

以下代码为评分较高的解决方案
stream为jdk1.8中的方法

import java.util.Arrays;public class AreSame {  public static boolean comp(final int[] a, final int[] b) {    return a != null && b != null && a.length == b.length && Arrays.equals(Arrays.stream(a).map(i -> i * i).sorted().toArray(), Arrays.stream(b).sorted().toArray());  }}

说实话我看不懂

原创粉丝点击