Are they the "same"?

难题
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 = [121, 144, 19, 161, 19, 144, 19, 11]
b = [1111, 121121, 144144, 1919, 161161, 1919, 144144, 1919]

Invalid arrays

If we change the first number to something else, comp may not return true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.
Remarks

a or b might be [] (all languages except R, Shell). a or b might be nil or null or None (except in Haskell, Elixir, C++, Rust, R, Shell).

If a or b are nil (or null or None), the problem doesn't make sense so return false.

If a or b are empty the result is evident by itself.
Note for C

The two arrays have the same size (> 0) given as parameter in function comp.

Good Solution1:

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

Good Solution2:

import java.util.*;
import java.io.*;
import java.util.Arrays;

public class AreSame {
  
  public static boolean comp(int[] a, int[] b) {
    if ((a == null) || (b == null)){
          return false;
    }
    int[] aa = Arrays.stream(a).map(n -> n * n).toArray();
    Arrays.sort(aa);
    Arrays.sort(b);
    return (Arrays.equals(aa, b));
    
  }
}

My Solution:

import java.util.*;
public class AreSame {
  
  public static boolean comp(int[] a, int[] b) {    
    int flag=0;
    List<Integer> list=new ArrayList<Integer>();
    if(a==null||b==null){
        return false;
    }
//     System.out.println(Arrays.stream(a).distinct().toArray().length);
//     System.out.println(Arrays.stream(b).distinct().toArray().length);
    if(a.length!=b.length||Arrays.stream(a).distinct().toArray().length!=Arrays.stream(b).distinct().toArray().length){
        
        return false;
    }
    for(int i:a){
        System.out.print(" "+i);
    }
    System.out.println("====================");
    for(int i:b){
        System.out.print(" "+i);
    }
    if(a.length==0&&b.length==0){
        return true;
    }
    for(int i=0;i<a.length;i++){
        for(int j=0;j<b.length;j++){
            if(a[i]*a[i]==b[j]||a[i]==b[j]||a[i]==b[j]*b[j]){
                flag=1;
                break;
            }
        }
        list.add(flag);
    }

    return !(list.contains(0));
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容