๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ

(159)
๋งค์ผ LeetCode ํ’€๊ธฐ | Maximum Product Subarray Given an integer array nums, find a subarray that has the largest product, and return the product.The test cases are generated so that the answer will fit in a 32-bit integer. Example 1:Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2:Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.  Constraints:1 ..
๋งค์ผ LeetCode ํ’€๊ธฐ | Maximum Subarray Given an integer array nums, find the subarray with the largest sum, and return its sum. Example 1:Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2:Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3:Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the large..
๋งค์ผ LeetCode ํ’€๊ธฐ | 3Sums Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.Notice that the solution set must not contain duplicate triplets. Example 1:Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = ..
๋งค์ผ LeetCode ํ’€๊ธฐ | Number of 1 bits Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight). Example 1:Input: n = 11Output: 3Explanation:The input binary string 1011 has a total of three set bits.Example 2:Input: n = 128Output: 1Explanation:The input binary string 10000000 has a total of one set bit.Example 3:Input: n = 2147483645Outpu..
๋งค์ผ LeetCode ํ’€๊ธฐ | Sum of Two Integers Given two integers a and b, return the sum of the two integers without using the operators + and -. Example 1:Input: a = 1, b = 2 Output: 3 Example 2:Input: a = 2, b = 3 Output: 5  Constraints:-1000  ์ •๋ง ์ ‘๊ทผ์กฐ์ฐจ ์‰ฝ์ง€ ์•Š์•˜๋˜ ๋ฌธ์ œ์˜€๋‹ค.  class Solution { func getSum(_ a: Int, _ b: Int) -> Int { var a = a var b = b while b != 0 { let carry = a & b a = a ^ b ..
๋งค์ผ LeetCode ํ’€๊ธฐ | Product of Array Except Self Product of Array Except SelfGiven an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in O(n) time and without using the division operation. Example 1:Input: nums = [1,2,3,4] Output: [24,12,8,6]..
๋งค์ผ LeetCode ํ’€๊ธฐ | Contains Duplicate Contains DuplicateGiven an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example 1:Input: nums = [1,2,3,1] Output: true Example 2:Input: nums = [1,2,3,4] Output: false Example 3:Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true  Constraints:1  class Solution { func containsDuplicate(_ nums: [Int]) -> Bool { ..
์• ํ”Œ ๋””๋ฒจ๋กœํผ ์•„์นด๋ฐ๋ฏธ 23์ฃผ์ฐจ ํšŒ๊ณ  MC3์š” ๊ทผ๋ž˜ ํšŒ๊ณ ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŠนํžˆ ๋” ์งง์€ ๊ธฐ๊ฐ„ ์•ˆ์— ๊ฐœ๋ฐœ์„ ํ•ด์•ผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋ฃจํ•˜๋ฃจ ๊ฐœ๋ฐœํ•˜๊ธฐ์— ๋ฐ”๋นด๋‹ค๊ณ  ํ•‘๊ณ„ ์•„๋‹Œ ํ•‘๊ณ„๋ฅผ ๋Œ€๋ณธ๋‹ค. ๊ทธ๋ž˜๋„ ํŒ€์›๋“ค๊ณผ ํ•จ๊ป˜ ๋ชฐ์ž…ํ•ด์„œ ๊ฐœ๋ฐœ์„ ํ•œ ๋•๋ถ„์—, ๊ธฐ๊ฐ„ ์•ˆ์— ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.์ด๋ฒˆ์ฃผ์— ๋Œ€๊ตฌ์— ๊ฐ€์„œ UT๋„ ์ง„ํ–‰ํ•˜๊ณ  ์™”๋Š”๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์•ฑ์„ ์ง์ ‘ ์‚ฌ์šฉ์ž๊ฐ€ ์“ฐ๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋ฉด์„œ ํ”ผ๋“œ๋ฐฑ์„ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ์–ด๋–ป๊ฒŒ UT๊ฐ€ ์–ด๋–ค ํ”Œ๋กœ์šฐ๋กœ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์ธ์ง€, ์‚ฌ์šฉ์ž์˜ ์ •ํ™•ํ•œ ํ”ผ๋“œ๋ฐฑ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์งˆ๋ฌธ์„ ํ•ด์•ผ ํ•˜๋Š” ์ง€๋ฅผ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” UT๊ฐ€ ์ฒ˜์Œ์ด๋ผ, UT์— ์ง์ ‘์ ์ธ ๊ธฐ์—ฌ๋ฅผ ํ•œ ๋ถ€๋ถ„์€ ๋งŽ์ด ์—†์—ˆ์ง€๋งŒ ํŒ€์›๋“ค์ด UT๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋ชจ์Šต๊ณผ ์งˆ๋ฌธ๋“ค์„ ํ†ตํ•ด UT์— ๋Œ€ํ•œ ํฐ ํ๋ฆ„์„ ์žก์„ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.์ฝ”๋กœ๋‚˜์—..