【codewars】Are they the "same"

原貼地址
https://www.codewars.com/kata/550498447451fbbd7600041c/train/java

簡介如下:
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.

方法類

public class AreSame {
    
    public static boolean comp(int[] a, int[] b) {
    return null;
  }
}

測(cè)試類

import static org.junit.Assert.*;
import org.junit.Test;


public class AreSameTest {

    @Test
    public void test1() {
        int[] a = new int[]{121, 144, 19, 161, 19, 144, 19, 11};
        int[] b = new int[]{121, 14641, 20736, 361, 25921, 361, 20736, 361};
        assertEquals(AreSame.comp(a, b), true); 
    }
}

我的答案

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class AreSame {
    
    public static boolean comp(int[] a, int[] b) {
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        
        if (a == null || b == null || a.length != b.length ) {
            return false;
        }
        
        
        List<Integer> blist = new ArrayList<>();
        for (int i = 0; i < b.length; i++) {
            blist.add(b[i]);
        }
        
        boolean flag = true;
        for(int i = 0; i < a.length; i++){
            int tempa = a[i];
            int square = tempa * tempa;
            if (!blist.contains(square)) {
                flag = false;
                break;
            }else {
                blist.remove(Integer.valueOf(square));
            }
        }
        
        return flag;
  }
}

solution 地址
https://www.codewars.com/kata/550498447451fbbd7600041c/solutions/java

best prictice

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());
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末吧趣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌合呐,老刑警劉巖盛嘿,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝇裤,死亡現(xiàn)場離奇詭異巧勤,居然都是意外死亡惹想,警方通過查閱死者的電腦和手機(jī)拣凹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門森爽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嚣镜,你說我怎么就攤上這事爬迟。” “怎么了菊匿?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵付呕,是天一觀的道長。 經(jīng)常有香客問我跌捆,道長徽职,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任佩厚,我火速辦了婚禮姆钉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘抄瓦。我一直安慰自己潮瓶,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布闺鲸。 她就那樣靜靜地躺著筋讨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摸恍。 梳的紋絲不亂的頭發(fā)上悉罕,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音立镶,去河邊找鬼壁袄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛媚媒,可吹牛的內(nèi)容都是我干的嗜逻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼缭召,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼栈顷!你這毒婦竟也來了逆日?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤萄凤,失蹤者是張志新(化名)和其女友劉穎室抽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體靡努,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坪圾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了惑朦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兽泄。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖漾月,靈堂內(nèi)的尸體忽然破棺而出病梢,到底是詐尸還是另有隱情,我是刑警寧澤栅屏,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布飘千,位于F島的核電站,受9級(jí)特大地震影響栈雳,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缔莲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一哥纫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧痴奏,春花似錦蛀骇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至檐晕,卻和暖如春暑诸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辟灰。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工个榕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人芥喇。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓西采,卻偏偏與公主長得像,于是被迫代替她去往敵國和親继控。 傳聞我的和親對(duì)象是個(gè)殘疾皇子械馆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,476評(píng)論 0 23
  • 我相信這世界上有很多內(nèi)心不夠強(qiáng)大的孩子們 你需要人認(rèn)可胖眷,你不希望被人否定 你們有時(shí)候也會(huì)傻傻的在意不該在意的人 然...
    M小生閱讀 205評(píng)論 0 0
  • 我踏著清晨最溫婉的朝暉而來,哼著世間最清脆的樂霹崎,我會(huì)被世間一切寵愛瘦材,于是 我以為魚是愛我的,原來我只是她習(xí)慣到感覺...
    難為水_閱讀 244評(píng)論 0 2
  • 早先仿畸,正好寫過一篇食棕,總結(jié)下來其實(shí)就一句。 許多人的所謂成熟错沽,不過是被習(xí)俗磨去了棱角簿晓,變得世故而實(shí)際了。那不是成熟千埃,...
    章小白同學(xué)閱讀 271評(píng)論 0 2
  • 白色的烏鴉 披著喪服的烏鴉憔儿, 墜入漆黑的淵。 當(dāng)苦難肆虐人心放可, 我保留最后一點(diǎn)沉默谒臼, 荊棘纏繞年輕的骨骼, 綻放一...
    末世婉兒閱讀 236評(píng)論 6 0