https://leetcode.com/problems/valid-parentheses/description/ 주어진 문자열(s)가 유효한지 확인 모든 열려있는 괄호는 같은 타입의 괄호로 닫혀야 함 내 풀이 class Solution { func isValid(_ s: String) -> Bool { guard s.count > 1 else { return false } var stack = [Character]() for char in s { switch char { case "(", "{", "[": stack.append(char) case ")": if stack.last == "(" { stack.removeLast() } else { return false } case "}": if stack...
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 일별로 주식 가격이 주어졌을 때, 최대 이익을 구하기 내 풀이 class Solution { func maxProfit(_ prices: [Int]) -> Int { guard prices.count > 1 else { return 0 } var result: [Int] = [] for price in prices { for j in 1.. 0 { var profit = prices[j] - price print(prices[j], "-", price, "=", prices[j] - price) result.append(profit) } } } return result.max() ?? 0 } ..
https://leetcode.com/problems/valid-palindrome/description/ 주어진 문장(s)을 소문자로 바꾼후, 영문/숫자가 아닌걸 제거하고, 주어진 문장과 거꾸로 읽어도 똑같다면 true를 리턴, 다르다면 false를 리턴 내 풀이 class Solution { func isPalindrome(_ s: String) -> Bool { let convertedStr = s .lowercased() .filter { $0.isNumber || $0.isLetter } let reversedStr = String(convertedStr.reversed()) return convertedStr == reversedStr ? true : false } } 소문자로 바꿔주는 메서드..
https://leetcode.com/problems/two-sum/ 정수의 배열로 이루어진 nums로 각각 더해서 target의 값과 같으면 해당 index들을 리턴 내 풀이 class Solution { func twoSum(_ nums: [Int], _ target: Int) -> [Int] { var result: [Int] = [] for (idx, num) in nums.enumerated() { for idx2 in (idx + 1) ..< nums.count { if num + nums[idx2] == target { return [idx, idx2] } } } return result } } Solution().twoSum([2, 7, 11, 15], 9) enumerated()를 이용해..
https://leetcode.com/problems/valid-anagram/ 내 풀이 class Solution { func isAnagram(_ s: String, _ t: String) -> Bool { var sortedS = s.sorted() var sortedT = t.sorted() return sortedS == sortedT ? true : false } } Solution().isAnagram("anagram", "nagaram") s, t를 정렬해서 두 string의 순서들을 똑같이 만듬. `==` (비교연산자)를 사용해 s, t를 비교하여 같으면 true를, 다르면 false를 리턴 다른사람의 풀이 class Solution { func isAnagram(_ s: String, ..
https://leetcode.com/problems/contains-duplicate/description/ 주어진 배열에서 중복되는 요소가 있으면 `true`를 리턴, 없다면 `false`를 리턴 문제 사진 내 풀이 class Solution { func containsDuplicate(_ nums: [Int]) -> Bool { let numbers = Set(nums) if nums.count != numbers.count { return true } else { return false } } } Solution().containsDuplicate([1,2,3,1,3]) - 새로운 배열 numbers에 Set을 사용해서 중복되는 요소들을 걸러냄 - 주어진 배열과 numbers 배열의 길이(coun..
https://school.programmers.co.kr/learn/courses/30/lessons/120894 문제 사진 내 풀이 import Foundation // 1 var dic = ["zero" : "0", "one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9"] func solution(_ numbers:String) -> Int { // 2 var result = numbers // 3 for (key, value) in dic { result = String(result.replacingOccurrences(of: key, with: ..
https://school.programmers.co.kr/learn/courses/30/lessons/120912 문제 사진 내 풀이 import Foundation func solution(_ array:[Int]) -> Int { return array.map { String($0) }.joined(separator: "").filter { $0 == "7" }.count } 1. array 요소들을 String으로 변환 2. 변환된 String을 하나의 String으로 합침 (joined) 3. String으로 변환된 결과값들 중 "7"이 들어간요소들의 갯수만 걸러냄 (filter) 다른사람의 풀이 import Foundation func solution(_ array:[Int]) -> Int {..
https://school.programmers.co.kr/learn/courses/30/lessons/120892 문제 사진 내 풀이 import Foundation func solution(_ cipher:String, _ code:Int) -> String { return (1...cipher.count) // 1 .filter { $0.isMultiple(of: code) } // 2 .map { String(Array(cipher)[$0 - 1]) } // 3 .joined(separator: "") } solution("pfqallllabwaoclk", 2) 1. isMultiple(of:) 메서드로 매개변수 code 만큼의 배수들만 걸러냅니다. print [2, 4, 6, 8, 10, 12,..
https://school.programmers.co.kr/learn/courses/30/lessons/120887 문제 사진 내 풀이 import Foundation func solution(_ i:Int, _ j:Int, _ k:Int) -> Int { return Array(i...j).flatMap { String($0).compactMap { Int(String($0)) } }.filter { $0 == k }.count } solution(1, 13, 1) 1. i ~ j 까지의 숫자를 배열로 만든다. 2. 그 사이 요소들중 10의자리 수 이상인 요소들을 분리 시킨다. flatMap { String($0).compactMap { Int(String($0)) } } 예) 32 → 3, 2 / 1..
- Total
- Today
- Yesterday
- 호텔리어 개발자
- swift protocol
- swift reduce
- Swift Leetcode
- Swift 프로그래머스
- CS 네트워크
- Swift inout
- Swift joined()
- Swift while
- 원티드 프리온보딩
- Combine: Asynchronous Programming with Swift
- Swift Error Handling
- Swift ModernRIBs
- Swift joined
- Swift final
- swift 고차함수
- iOS GCD
- Swift RIBs
- Class
- RIBs tutorial
- swift property
- Swift 내림차순
- Swift
- Swift 프로퍼티
- 2023년 회고
- Swift init
- Uber Architecture
- swift function
- ios
- iOS error
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |