Leetcode

· One step
LeetCode-8 String to Integer (atoi) : Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). note : integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.Answer :::python class Solution: def myAtoi(self, s: str) -> int: left = 0 k = '' p = '' for l ..
· One step
LeetCode-6 Zigzag Conversion : The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows note : like this: (you may want to display this pattern in a fixed font for better legibility)Answer :::python class Solution: def convert(self, s: str, numRows: int) -> str: if numRows==1: return s answer = '' interval = numRows + (numRows-2) ls = [[''] for _ in ra..
· One step
LeetCode-7 Reverse Integer : Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. note : Assume the environment does not allow you to store 64-bit integers (signed or unsigned).Answer :::python class Solution: def reverse(self, x: int) -> int: if x > 0: x = int(str(x)[::-1..
· One step
LeetCode-9 PalindromeNumber : Given an integer x, return true if x is a palindrome, and false otherwise. note : From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Answer :::python class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1] Result : 57ms Memory: 13.7mb PalindromeNumber PalindromeNumber는 앞으로 읽으나 뒤로 읽으나..
· One step
LeetCode-4 Median of Two Sorted Arrays : Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. note : The overall run time complexity should be O(log (m+n)).Answer :::python class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: p = nums1 + nums2 p.sort() if len(p)%2!=0: median = p[len(p)//2] else..
· One step
LeetCode-3 Longest Substring Without Repeating Characters : Given a string s, find the length of the longest substring without repeating characters. note : s consists of English letters, digits, symbols and spaces.Answer :::python class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s)==1: return 1 left, right, answer_max = 0, 1, 0 for _ in range(len(s)): while (left
· One step
LeetCode-23 Merge k Sorted Lists : You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. note : Merge all the linked-lists into one sorted linked-list and return it.MyAnswer :::python # Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeKLists(self, lists: L..
· One step
LeetCode-739 Daily Temperature : 몇 일 후 기온이 오를까? note : Answer :::python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: out = [] left, right = 0, 0 while left != len(temperatures)-1: day = 0 check = 0 while right temperatures[stack[-1]]: last = stack.pop() answer[last] = i - last stack.append(i) return answer Result : 1352ms Memory: 28.6mb
다했다
'Leetcode' 태그의 글 목록 (2 Page)