본문 바로가기

반응형

코딩 테스트/LeetCode(swift)

(28)
[LeetCode] 322. Coin Change 문제 leetcode.com/problems/coin-change/ Coin Change - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 0부터 코인들을 더해볼까요? 아니면, 목표 값에서 코인들을 하나씩 빼볼까요? 코드 22년 2월 17일 코드 class Solution { func coinChange(_ coins: [Int], _ amount: Int) -> Int { if amount == 0 { return 0 } var result = Arra..
[LeetCode] 70. Climbing Stairs 문제 leetcode.com/problems/climbing-stairs/ Climbing Stairs - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 코드 class Solution { func climbStairs(_ n: Int) -> Int { var before2 = 0 var before1 = 1 for i in 1...n { var current = before2 + before1 before2 = before1 before1 = current }..
[LeetCode] 190. Reverse Bits 문제 leetcode.com/problems/reverse-bits/ Reverse Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 "If this function is called many times, how would you optimize it?" 문제는 풀어도 최적화가 관건이네요~ 코드 21년 4월 7일 코드 class Solution { func reverseBits(_ n: Int) -> Int { var calN = n var re..
[LeetCode] 268. Missing Number 문제 leetcode.com/problems/missing-number/ Missing Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 손으로 계산하면서 패턴을 찾아보세요 코드 class Solution { func missingNumber(_ nums: [Int]) -> Int { var totalSum = 0 var numSum = 0 for i in 0.. Int { var missingNum = 0 var addNum = 1 nums..
[LeetCode] 338. Counting Bits 문제 leetcode.com/problems/counting-bits/ Counting Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 DP입니다 코드 22년 2월 16일 작성 코드 class Solution { func countBits(_ n: Int) -> [Int] { var oneCount: [Int] = Array(repeating: 0, count: n+1) if oneCount.count > 1 { oneCount[1] = 1 }..
[LeetCode] 191. Number of 1 Bits 문제 leetcode.com/problems/number-of-1-bits/ Number of 1 Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 비트 연산으로 풀어보세요 코드 class Solution { func hammingWeight(_ n: Int) -> Int { var targetNum = n var count = 0 for i in 0..> 1 } return count } } 설명 비트 연산을 통해, 1의 개수를 계산했습니다.
[LeetCode] 371. Sum of Two Integers 문제 leetcode.com/problems/sum-of-two-integers/ Sum of Two Integers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 비트 연산인데, 아이디어가 없으면 코드 바로 보세요~ 코드 class Solution { func getSum(_ a: Int, _ b: Int) -> Int { var tempB = (a & b)
[LeetCode] 11. Container With Most Water 문제 leetcode.com/problems/container-with-most-water/ Container With Most Water - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 코드 class Solution { func maxArea(_ height: [Int]) -> Int { var left = 0 var right = height.count-1 var maxVolume = 0 while left < right { let tempVolume =..
[LeetCode] 15. 3Sum 문제 leetcode.com/problems/3sum/ 3Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 코드 class Solution { func threeSum(_ nums: [Int]) -> [[Int]] { var arraySet = Set() var sortedNums = nums.sorted() var maxIndex = sortedNums.count-1 for y in 0.. 0, sortedNums[y] == sortedNums[y-1]..
[LeetCode] 33. Search in Rotated Sorted Array 문제 leetcode.com/problems/search-in-rotated-sorted-array/ Search in Rotated Sorted Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 잠깐만 단순 포문으로 풀수 있겠지만, 문제 하단에 이런 문장이 있네요. Follow up: Can you achieve this in O(log n) time complexity? 코드 class Solution { func search(_ nums: [In..

반응형