본문 바로가기

Valid Sudoku Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. A partially filled sudoku which is valid.The Sudoku board could be partially .. 더보기
Rotate Image You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.Example 1:Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6.. 더보기
Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.Example:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. My solution: class Solution: def twoSum(self, nums, target): """ :type nums: List[i.. 더보기
Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12] Output: [1,3,12,0,0]Note:You must do this in-place without making a copy of the array.Minimize the total number of operations. My solution: class Solution: def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return .. 더보기
Plus One Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.You may assume the integer does not contain any leading zero, except the number 0 itself.Example 1:Input: [1,2,3] Output: [1,2,4] Explanation: The array represents.. 더보기
Intersection of Two Arrays II Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9]Note:Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.Follow up:What if the given array is already sorted? How would you optimize your alg.. 더보기
Single Number Given a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Example 1:Input: [2,2,1] Output: 1 Example 2:Input: [4,1,2,1,2] Output: 4 My solution:class Solution: def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ distinct = .. 더보기
Contains Duplicate Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.Example 1:Input: [1,2,3,1] Output: trueExample 2:Input: [1,2,3,4] Output: falseExample 3:Input: [1,1,1,3,3,4,3,2,4,2] Output: true My solution:class Solution: def containsDuplicate(self, nu.. 더보기
Rotate Array Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2:Input: [-1,-100,3,99] and k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the r.. 더보기
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).Example 1:Input: [7,1,5,3,6,4].. 더보기