본문 바로가기

Pascal's Triangle Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it.Example:Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] My solution:class Solution: def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ result = list() if numRows > 0: result.append([1.. 더보기
Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.Roman numerals are usually written largest to smallest from left to.. 더보기
Power of Three Given an integer, write a function to determine if it is a power of three.Example 1:Input: 27 Output: true Example 2:Input: 0 Output: falseExample 3:Input: 9 Output: trueExample 4:Input: 45 Output: falseFollow up: Could you do it without using any loop / recursion? My solution:class Solution: def isPowerOfThree(self, n): """ :type n: int :rtype: bool """ if n == 0: return False while n % 3 == 0:.. 더보기
Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.Example:n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ].. 더보기
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.. 더보기