A로 B 만들기
| func solution(_ before:String, _ after:String) -> Int { |
| return String(before.sorted()) == String(after.sorted()) ? 1 : 0 |
| } |
가까운 수
| func solution(_ array:[Int], _ n:Int) -> Int { |
| |
| let sortedArray = array.sorted() |
| |
| var resultNum = sortedArray[0] |
| var tempNum = abs(sortedArray[0] - n) |
| |
| for i in sortedArray { |
| if abs(i - n) < tempNum { |
| resultNum = i |
| tempNum = abs(i - n) |
| } |
| } |
| |
| return resultNum |
| } |
k의 개수
| func solution(_ i:Int, _ j:Int, _ k:Int) -> Int { |
| |
| var count = 0 |
| |
| for num in i...j { |
| let _ = String(num).map { |
| if String($0) == String(k) { |
| count += 1 |
| } |
| } |
| } |
| |
| return count |
| } |
배열의 유사도
| func solution(_ s1:[String], _ s2:[String]) -> Int { |
| |
| var count = 0 |
| |
| let _ = s1.map { s1Num in |
| s2.map { s2Num in |
| if s1Num == s2Num { count += 1 } |
| } |
| } |
| |
| return count |
| } |
아이스 아메리카노
| func solution(_ money:Int) -> [Int] { |
| var resultArray = [Int]() |
| |
| if money < 5500 { |
| return [0, money] |
| } |
| |
| resultArray.append(money / 5500) |
| resultArray.append(money % 5500) |
| |
| return resultArray |
| } |
옷가게 할인 받기
| func solution(_ price:Int) -> Int { |
| |
| if price >= 500000 { |
| return Int(Double(price) * 0.80) |
| } else if price >= 300000 { |
| return Int(Double(price) * 0.90) |
| } else if price >= 100000 { |
| return Int(Double(price) * 0.95) |
| } else { |
| return price |
| } |
| |
| } |
중앙값 구하기
| func solution(_ array:[Int]) -> Int { |
| |
| let sortedArray = array.sorted() |
| let center = Int(sortedArray.count / 2) |
| |
| return sortedArray[center] |
| } |
공 던지기
| func solution(_ numbers:[Int], _ k:Int) -> Int { |
| |
| var ball = 0 |
| |
| for _ in 1...k-1 { |
| ball += 2 |
| if ball >= numbers.count { |
| ball -= numbers.count |
| } |
| } |
| |
| return numbers[ball] |
| } |
로그인 성공?
| func solution(_ id_pw:[String], _ db:[[String]]) -> String { |
| |
| var result = "fail" |
| |
| for data in db { |
| if id_pw[0] == data[0] { |
| result = "wrong pw" |
| if id_pw[1] == data[1] { |
| result = "login" |
| } |
| } |
| } |
| |
| return result |
| } |
치킨 쿠폰
| func solution(_ chicken:Int) -> Int { |
| |
| var coupon = chicken |
| var serviceChicken = 0 |
| |
| while coupon >= 10 { |
| serviceChicken += coupon / 10 |
| coupon = (coupon % 10) + (coupon / 10) |
| } |
| |
| return serviceChicken |
| } |
등수 매기기
| func solution(_ score:[[Int]]) -> [Int] { |
| |
| var resultArray = [Int]() |
| var rank = 1 |
| |
| let averageArray = score.map { ($0[0] + $0[1]) } |
| for i in averageArray { |
| rank = 1 |
| for j in averageArray { |
| if i < j { |
| rank += 1 |
| } |
| } |
| resultArray.append(rank) |
| } |
| |
| return resultArray |
| } |
배열 만들기 6
| func solution(_ arr:[Int]) -> [Int] { |
| var stk = [Int]() |
| |
| var i = 0 |
| while i < arr.count { |
| if stk.isEmpty { |
| stk.append(arr[i]) |
| i += 1 |
| } else { |
| if stk.last! == arr[i] { |
| stk.removeLast() |
| i += 1 |
| } else { |
| stk.append(arr[i]) |
| i += 1 |
| } |
| } |
| } |
| |
| return stk.isEmpty ? [-1] : stk |
| } |
대소문자 바꿔서 출력하기
| var s1 = readLine()! |
| |
| for i in s1 { |
| if i.isUppercase { |
| print(i.lowercased(), terminator: "") |
| } else { |
| print(i.uppercased(), terminator: "") |
| } |
| } |
저주의 숫자 3
| func solution(_ n:Int) -> Int { |
| var result = 0 |
| |
| for _ in 1 ... n { |
| result += 1 |
| while (result % 3 == 0) || String(result).contains("3") { |
| result += 1 |
| } |
| } |
| |
| return result |
| } |
그림 확대
| func solution(_ picture:[String], _ k:Int) -> [String] { |
| let array = picture.map{ $0.map{ String(repeating: String($0), count: k) }.joined(separator: "") } |
| |
| var result: [String] = [] |
| for i in array { |
| for _ in 1 ... k { |
| result.append(i) |
| } |
| } |
| |
| return result |
| } |
특이한 정렬
| func solution(_ numlist:[Int], _ n:Int) -> [Int] { |
| let sortedList = numlist.sorted { num1, num2 in |
| if abs(num1 - n) == abs(num2 - n) { |
| return num1 > num2 |
| } else { |
| return abs(num1 - n) < abs(num2 - n) |
| } |
| } |
| return sortedList |
| } |
문자열 밀기
| func solution(_ A:String, _ B:String) -> Int { |
| |
| var pushString = A |
| var pushCount = 0 |
| |
| while true { |
| if pushCount == pushString.count { return -1 } |
| |
| if pushString == B { |
| return pushCount |
| } |
| pushCount += 1 |
| |
| pushString = String(pushString.suffix(1)) + String(pushString.prefix(pushString.count - 1)) |
| } |
| } |
전국 대회 선발 고사
| func solution(_ rank:[Int], _ attendance:[Bool]) -> Int { |
| |
| var attendArray = Array<Int>(repeating: rank.count + 1, count: rank.count) |
| |
| for i in 0 ..< rank.count { |
| if attendance[i] == true { |
| attendArray[i] = rank[i] |
| } |
| } |
| |
| let numArray = [10000, 100, 1] |
| var result = 0 |
| |
| for i in 0 ..< 3 { |
| if let minElement = attendArray.enumerated().min(by: { $0.element < $1.element }) { |
| result += numArray[i] * minElement.offset |
| attendArray[minElement.offset] = rank.count + 1 |
| } |
| } |
| |
| return result |
| } |
다항식 더하기
| func solution(_ polynomial:String) -> String { |
| |
| let components = polynomial.components(separatedBy: " + ") |
| |
| var xNum = 0 |
| var num = 0 |
| |
| var resultString = "" |
| |
| for i in components { |
| if i.contains("x") { |
| var removeX = "0"+i |
| removeX.removeLast() |
| let intX = Int(String(removeX))! |
| if intX == 0 || intX == 1 { |
| xNum += 1 |
| } else { |
| xNum += intX |
| } |
| } else { |
| num += Int(i)! |
| } |
| } |
| |
| if xNum > 0 { |
| if xNum == 1 { |
| resultString += "x" |
| } else { |
| resultString += "\(xNum)x" |
| } |
| |
| if num > 0 { |
| resultString += " + " |
| } |
| } |
| |
| if num > 0 { |
| resultString += "\(num)" |
| } |
| |
| return resultString |
| } |
배열 만들기 2
| func solution(_ l:Int, _ r:Int) -> [Int] { |
| var isValid = true |
| var num = l |
| var result = [Int]() |
| |
| while num <= r { |
| for i in String(num) { |
| if (i == "0" || i == "5") { |
| continue |
| } else { |
| isValid = false |
| break |
| } |
| } |
| |
| if isValid { |
| result.append(num) |
| } |
| |
| isValid = true |
| if num % 5 == 0 { |
| num += 5 |
| } else { |
| num += 1 |
| } |
| } |
| |
| return result.isEmpty ? [-1] : result |
| } |
최빈값 구하기
| func solution(_ array:[Int]) -> Int { |
| var countArray = Array<Int>(repeating: 0, count: 1000) |
| |
| for i in array { |
| countArray[i] += 1 |
| } |
| |
| let maxValue = countArray.max() |
| let maxArray = countArray.filter { $0 == maxValue } |
| |
| return maxArray.count > 1 ? -1 : countArray.firstIndex(of: maxValue!)! |
| } |
코드 처리하기
| func solution(_ code:String) -> String { |
| var idx = 0 |
| var mode = 0 |
| var ret = "" |
| |
| for char in code { |
| if mode == 0 { |
| if char == "1" { |
| mode = 1 |
| } else { |
| if idx % 2 == 0 { |
| ret.append(char) |
| } |
| } |
| } else { |
| if char == "1" { |
| mode = 0 |
| } else { |
| if idx % 2 != 0 { |
| ret.append(char) |
| } |
| } |
| } |
| idx += 1 |
| } |
| |
| return ret.isEmpty ? "EMPTY" : ret |
| } |
문자열 출력하기
| let s1 = readLine()! |
| print(s1) |
OX퀴즈
| func solution(_ quiz:[String]) -> [String] { |
| |
| var result = [String]() |
| |
| for i in quiz { |
| let components = i.components(separatedBy: " ") |
| if components[1] == "+" { |
| if Int(components[0])! + Int(components[2])! == Int(components[4])! { |
| result.append("O") |
| } else { |
| result.append("X") |
| } |
| } else { |
| if Int(components[0])! - Int(components[2])! == Int(components[4])! { |
| result.append("O") |
| } else { |
| result.append("X") |
| } |
| } |
| } |
| |
| return result |
| } |
분수의 덧셈
| func solution(_ numer1:Int, _ denom1:Int, _ numer2:Int, _ denom2:Int) -> [Int] { |
| |
| |
| let number = (numer1 * denom2) + (numer2 * denom1) |
| let denom = denom1 * denom2 |
| |
| |
| return [number / gcd(number, denom), denom / gcd(number, denom)] |
| } |
| |
| func gcd(_ num1: Int, _ num2: Int) -> Int { |
| return num1 % num2 == 0 ? num2 : gcd(num2, num1 % num2) |
| } |
다음에 올 숫자
| func solution(_ common:[Int]) -> Int { |
| |
| if common[1] - common[0] == common[2] - common[1] { |
| |
| let diff = common[1] - common[0] |
| return common.last! + diff |
| } else { |
| |
| let diff = common[1] / common[0] |
| return common.last! * diff |
| } |
| |
| } |
연속된 수의 합
| func solution(_ num:Int, _ total:Int) -> [Int] { |
| |
| let mid = num % 2 == 0 ? Int(total / num) + 1 : Int(total / num) |
| let first = mid - Int(num / 2) |
| |
| var result = [Int]() |
| |
| for i in 0 ..< num { |
| result.append(i + first) |
| } |
| |
| return result |
| } |
안전지대
| func solution(_ board:[[Int]]) -> Int { |
| |
| var newBoard = board |
| for index in 0 ..< newBoard.count { |
| newBoard[index].insert(2, at: 0) |
| newBoard[index].append(2) |
| } |
| newBoard.insert(Array(repeating: 2, count: newBoard.count + 2), at: 0) |
| newBoard.append(Array(repeating: 2, count: newBoard.count + 2)) |
| |
| |
| for xIndex in 0 ..< newBoard.count { |
| for yIndex in 0 ..< newBoard.count { |
| if newBoard[xIndex][yIndex] == 1 { |
| |
| for xRange in xIndex-1 ... xIndex+1 { |
| for yRange in yIndex-1 ... yIndex+1 { |
| if newBoard[xRange][yRange] != 1 { newBoard[xRange][yRange] = 2 } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| return newBoard.flatMap{ $0.filter{ $0 == 0 } }.count |
| } |
배열 조각하기
| func solution(_ arr:[Int], _ query:[Int]) -> [Int] { |
| |
| var result = arr |
| |
| for i in 0 ..< query.count { |
| if i % 2 == 0{ |
| |
| let index = query[i] |
| result = Array(result.prefix(upTo: index + 1)) |
| } else { |
| |
| let index = query[i] |
| result = Array(result.suffix(from: index)) |
| } |
| } |
| |
| return result |
| } |
| |
| |
| func solution(_ arr:[Int], _ query:[Int]) -> [Int] { |
| var result = arr |
| for i in 0 ..< query.count { |
| let index = query[i] |
| result = i % 2 == 0 ? Array(result.prefix(upTo: index + 1)) : Array(result.suffix(from: index)) |
| } |
| return result |
| } |
주사위 게임 3
| func solution(_ a:Int, _ b:Int, _ c:Int, _ d:Int) -> Int { |
| var countArray = Array(repeating: 0, count: 6) |
| countArray[a-1] += 1 |
| countArray[b-1] += 1 |
| countArray[c-1] += 1 |
| countArray[d-1] += 1 |
| |
| var result = 0 |
| |
| if countArray.contains(4) { |
| result = 1111 * (countArray.firstIndex(of: 4)! + 1) |
| } else if countArray.contains(3) { |
| let p = Double(countArray.firstIndex(of: 3)! + 1) |
| let q = Double(countArray.firstIndex(of: 1)! + 1) |
| result = Int(pow(10 * p + q, 2)) |
| } else if countArray.filter({$0 == 2}).count == 2 { |
| let p = countArray.firstIndex(of: 2)! + 1 |
| let q = countArray.lastIndex(of: 2)! + 1 |
| result = (p + q) * abs(p - q) |
| } else if countArray.filter({$0 == 2}).count == 1 && countArray.filter({$0 == 1}).count == 2 { |
| let q = countArray.firstIndex(of: 1)! + 1 |
| let r = countArray.lastIndex(of: 1)! + 1 |
| result = q * r |
| } else { |
| result = countArray.firstIndex(of: 1)! + 1 |
| } |
| |
| return result |
| } |
업뎃예정 ~