記事一覧

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 nu…

May
11か月前

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(): …

May
11か月前

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…

May
1年前

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 d…

May
1年前

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…

May
1年前

LeetCode 242. Valid Anagram

class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False dic = dict() for e…

May
1年前

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…

May
1年前

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] n…

May
1年前

LeetCode 383. Ransom Note

class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: if len(ransomNote) > len(magazine): return False dic…

May
1年前

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 …

May
1年前

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: …

May
1年前

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 hays…

May
1年前

LeetCode 58. Length of Last Word

class Solution: def lengthOfLastWord(self, s: str) -> int: lst = s.split() print(lst) return len(lst[-1]) この問題のポイントはsplit sp…

May
1年前

LeetCode 169. Majority Element

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

May
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# …

May
1年前
1

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# …

May
1年前

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

もっとみる