본문 바로가기

Algorithm/Leetcode

(19)
매일 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 { ..
매일 LeetCode 풀기 | Two Sum / Time to Buy and Sell Stock Two SumGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example 1:Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]..