dictionary:subscirpt 를 array의 contains 보다 이용하자.

hankyulee·2021년 12월 10일
0

Swift coding test 준비

목록 보기
48/57

time complexity가 O(1)므로.
import Foundation

var arr = (readLine()!).split(separator: " ").map{Int(String($0))!}
let a = arr[0]
let b = arr[1]
var set = Dictionary<String,Bool>()
var set2 = Set()
var dividen:Double = 0
for indexn in 1...(a) {
set.updateValue(false, forKey: readLine()!)
}
for indexn in 1...(b) {
let given = readLine()!

if let _ = set[given] {
    set2.insert(given)
}

}

var result = set2.sorted{$0<$1}
print(result.count)
for i in result{
print(i)
}

                            가 아래보다 좋다.
                            
                            
                            

import Foundation

var arr = (readLine()!).split(separator: " ").map{Int(String($0))!}
let a = arr[0]
let b = arr[1]
var set = Set()
var set2 = Set()
var dividen:Double = 0
for indexn in 1...(a) {
set.insert(readLine()!)
}
for indexn in 1...(b) {
let given = readLine()!
if set.contains(given){
set2.insert(given)
}
}

var result = set2.sorted{$0<$1}
print(result.count)
for i in result{
print(i)
}

0개의 댓글