프로그래머스 - 로또의 최고 순위와 최저 순위
·
Algorithm/programmers
import Foundationfunc solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] { let rank = [6,5,4,3,2] let lottoSet: Set = Set(lottos) let win_numSet: Set = Set(win_nums) let inter = lottoSet.intersection(win_numSet) var first = inter.count + lottos.filter { $0 == 0 }.count first = (first >= 2) ? rank.firstIndex(of: first)!+1 : 6 var last = inter.count last = (last >= 2) ? (r..
프로그래머스 - 바탕화면 정리
·
Algorithm/programmers
import Foundationfunc solution(_ wallpaper:[String]) -> [Int] { var wallpapers: [(Int,Int)] = [] var minX = Int.max var minY = Int.max var maxX = Int.min var maxY = Int.min for (y, value1) in wallpaper.enumerated() { for (x, value2) in value1.enumerated() { if value2 == "#" { wallpapers.append((y, x)) minX = min(minX, ..
프로그래머스 - 공원 산책
·
Algorithm/programmers
1.import Foundationfunc solution(_ park:[String], _ routes:[String]) -> [Int] { var currentY = 0 var currentX = 0 var obstacles: [(Int,Int)] = [] for (y,value) in park.enumerated() { for (x, value2) in value.map({ String($0) }).enumerated() { if value2 == "S" { currentX = x currentY = y } else if value2 == "X" { ..
프로그래머스 - 약수의 개수와 덧셈
·
Algorithm/programmers
import Foundationfunc solution(_ left:Int, _ right:Int) -> Int { func solution2(num: Int) -> Int { var count = 0 for i in 1...num { if num % i == 0 { count += 1 } } return count } return Array(left...right).map { solution2(num: $0)%2==0 ? $0 : -$0 }.reduce(0, +)}
프로그래머스 - 숫자 짝꿍
·
Algorithm/programmers
import Foundationfunc solution(_ X:String, _ Y:String) -> String { let s: Set = Set(X) var arr: [String] = [] for i in s { let xcount = X.filter { $0 == i }.count let ycount = Y.filter { $0 == i }.count arr.append(String(repeating: i, count: xcount
프로그래머스 - 369게임 feat. Regex 지원안될 때 정규표현식
·
Algorithm/programmers
import Foundationfunc solution(_ order: Int) -> Int { let orderString = String(order) // 정수 order를 문자열로 변환 let pattern = "[369]" // 3, 6, 9 중 하나에 해당하는 문자 패턴 do { let regex = try NSRegularExpression(pattern: pattern) let matches = regex.matches(in: orderString, range: NSRange(orderString.startIndex..., in: orderString)) return matches.count } catch { prin..
ytw_developer
'Algorithm/programmers' 카테고리의 글 목록 (7 Page)