最近の記事

LeetCode 26. Remove Duplicates from Sorted Array

class Solution: def removeDuplicates(self, nums: List[int]) -> int: count = 0 before = -1000 for i in range(len(nums)): if nums[i] != before: nums[count] = nums[i] count += 1

    • LeetCode 217. Contains Duplicate

      class Solution: def containsDuplicate(self, nums: List[int]) -> bool: dic = dict() for num in nums: if num not in dic.keys(): dic[num] = True else: return True retu

      • LeetCode 5. Longest Palindromic Substring

        class Solution: def longestPalindrome(self, s: str) -> str: ans = "" # odd for i in range(len(s)): front = i-1 end = i + 1 while front>=0 and end<len(s) and s[front] == s[end]:

        • LeetCode 3. Longest Substring Without Repeating Characters

          class Solution: def lengthOfLongestSubstring(self, s: str) -> int: tmp_max = 0 tmp_head = 0 if len(s) == 0: return 0 dic = dict() for i in range(len(s)): if s[i] not in dic:

        LeetCode 26. Remove Duplicates from Sorted Array

          LeetCode 2. Add Two Numbers(連結リスト)

          # Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) ->

          LeetCode 2. Add Two Numbers(連結リスト)

          LeetCode 242. Valid Anagram

          class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False dic = dict() for elm in s: if elm not in dic: dic[elm] = 1 el

          LeetCode 242. Valid Anagram

          LeetCode 290. Word Pattern

          class Solution: def wordPattern(self, pattern: str, s: str) -> bool: s_list = s.split() if len(s_list) != len(pattern): return False for i in range(len(pattern)): if pattern.find(pattern[i]) !=

          LeetCode 290. Word Pattern

          LeetCode 205. Isomorphic Strings

          class Solution: def isIsomorphic(self, s: str, t: str) -> bool: dic1 = dict() dic2 = dict() for i in range(len(s)): if s[i] not in dic1: dic1[s[i]] = t[i] elif dic1[s[i]] != t[i]:

          LeetCode 205. Isomorphic Strings

          LeetCode 383. Ransom Note

          class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: if len(ransomNote) > len(magazine): return False dic = dict() for elm in magazine: if elm not in dic.keys():

          LeetCode 383. Ransom Note

          LeetCode 392. Is Subsequence

          class Solution: def isSubsequence(self, s: str, t: str) -> bool: t_index = -1 flag = False if len(s) == 0: return True elif len(t) == 0 or len(s)>len(t): return False for s_index i

          LeetCode 392. Is Subsequence

          LeetCode 125. Valid Palindrome

          class Solution: def isPalindrome(self, s: str) -> bool: new_s = s.lower() head = 0 tail = len(s) - 1 while head < tail: if new_s[head].isalnum() and new_s[tail].isalnum(): if new_s[he

          LeetCode 125. Valid Palindrome

          LeetCode 28. Find the Index of the First Occurrence in a String

          class Solution: def strStr(self, haystack: str, needle: str) -> int: len_needle = len(needle) for i in range(len(haystack)): if haystack[i] == needle[0]: if len(haystack)-i < len_needle:

          LeetCode 28. Find the Index of the First Occurrence in a String

          LeetCode 58. Length of Last Word

          class Solution: def lengthOfLastWord(self, s: str) -> int: lst = s.split() print(lst) return len(lst[-1]) この問題のポイントはsplit split(" ")という風に指定してしまうと、空白が複数続いた場合に、分割後のリストの中に" "が含まれてしまう。 これを解決するためにsplit()と書けば、複数空白が連続しても

          LeetCode 58. Length of Last Word

          LeetCode 169. Majority Element

          class Solution: def majorityElement(self, nums: List[int]) -> int: majority = nums[0] count = 0 for elm in nums: if majority == elm: count += 1 else: if count > 0:

          LeetCode 169. Majority Element

          LeetCode 94. Binary Tree Inorder Traversal

          # Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def inorderTraversal(self, root: Op

          LeetCode 94. Binary Tree Inorder Traversal

          LeetCode 637. Average of Levels in Binary Tree

          # Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def averageOfLevels(self, root: Opt

          LeetCode 637. Average of Levels in Binary Tree