본문 바로가기

반응형

hacking or software engineering skills/leetcode

(9)
[Leetcode] Longest Substring Without Repeating Characters (python) python class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s: return 0 answer = 1 max_num = len(set(s)) n = len(list(s)) for group_size in range(2,max_num+1): for start_idx in range(0,n-group_size+1): if len(set(s[start_idx:start_idx+group_size])) == group_size: answer = group_size break return answer그냥 brute force로 다 구했다. dynamic programming 형식으로 우선 group_size는 logN의 사이즈이고..
[leetcode] reverse_integer (python3) String → list → reverse 이용해서 풀기 첫번째 풀이, 28ms python class Solution: def __init__(self): self.min = -2**31 self.max = 2**31 def reverse(self, x:int) -> int: reverse_list = list(str(x)) reverse_list.reverse() if x self.max: return 0 else: return i..
7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer ov..
6. ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s = "PAY..
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd" Output: "bb" class Solution: def longestPalindrome(self, s: str) -> str: length = len(s) if length == 0: return '' if length == 1: return s if length == 2: if s[0] == s[-1]: return ..
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 class Solution(object): def findMedianSortedAr..
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a su..
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 4..

728x90
반응형