Leetcode

· One step
LeetCode-36 Valid Sudoku : A Sudoku board (partially filled) could be valid but is not necessarily solvable. note : Only the filled cells need to be validated according to the mentioned rules.Answer :::python class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: def valid_row(i, j, row): if row==1: item = board[i] else: item = [board[n][j] for n in range(0,9)] p = [] box = [bo..
· One step
LeetCode-706 Design HashMap : Design a HashMap without using any built-in hash table libraries. note : At most 104 calls will be made to put, get, and remove.Answer :::python class ListNode: def init(self, key=None, value=None): self.key = key self.value = value self.next = None class MyHashMap: def init(self): self.size = 10**4 self.table = collections.defaultdict(ListNode) def put(self, key: i..
· One step
LeetCode-38 Count and Say : The count-and-say sequence is a sequence of digit strings defined by the recursive formula: note : To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.Answer :::py..
· One step
LeetCode-347 Top K Frequent Elements : Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. note : time complexity must be better than O(n log n), where n is the array s size.Over * :::python class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: if len(set(nums))==1: return [nums[0]] answer = [] count =..
· One step
LeetCode-43 Multiply Strings : Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. note : Note: You must not use any built-in BigInteger library or convert the inputs to integer directly. :::python class Solution: def multiply(self, num1: str, num2: str) -> str: if (num1=='0') or (num2=='0'): ret..
· One step
LeetCode-14 Longest Common Prefix : Write a function to find the longest common prefix string amongst an array of strings. note : If there is no common prefix, return an empty string "".Answer :::python class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs)==1: return strs[0] answer = '' i = 0 min_p = min([len(s) for s in strs]) if min_p==0: return "" while i
· One step
LeetCode-12 Integer to Roman : Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. note : Given an integer, convert it to a roman numeral.Answer :::python class Solution: def intToRoman(self, num: int) -> str: roman_dict = { 0 : "V" ,1 : "L" ,2 : "D" } d_dict= { 0 : "I" ,1 : "X" ,2 : "C" ,3 : "M" } answer = '' set_num = 3 while set_num >= 0: remain = num //..
· One step
LeetCode-10 Regular Expression Matching : Given an input string s and a pattern p, implement regular expression matching note : The matching should cover the entire input string (not partial).Answer :::python class Solution: def isMatch(self, s: str, p: str) -> bool: if len(re.findall(p,s))>0: return s == re.findall(p,s)[0] else: return False Result : 201ms Memory: 13.9mbSolve :::python class So..
다했다
'Leetcode' 태그의 글 목록