記事一覧
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[cou
もっとみる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
もっとみる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 e
もっとみる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)
もっとみる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: Optiona
もっとみる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:
もっとみる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)):
もっとみる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]] =
もっとみる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:
もっとみる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):
もっとみる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[tai
もっとみる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
もっとみる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(" ")という風に指定してしまうと、空白が複数続いた場合に、分割後のリストの中に" "が含ま
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
もっとみる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
もっとみる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
もっとみる