From 6cdab826c73aa3b121d6d3aa8aeb9aa44fe00eb2 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 03:58:16 +0800 Subject: [PATCH 01/13] finish 27 --- leetcode/27.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 leetcode/27.py diff --git a/leetcode/27.py b/leetcode/27.py new file mode 100644 index 0000000..9521ddc --- /dev/null +++ b/leetcode/27.py @@ -0,0 +1,28 @@ +from typing import List +''' +Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. +The order of the elements may be changed. +Then return the number of elements in nums which are not equal to val. + +Consider the number of elements in nums which are not equal to val be k, +to get accepted, you need to do the following things: + +Change the array nums such that the first k elements of nums +contain the elements which are not equal to val. +The remaining elements of nums are not important as well as the size of nums. + +Return k. +Constraints: + +0 <= nums.length <= 100 +0 <= nums[i] <= 50 +0 <= val <= 100 +''' +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + k = len(nums) + while val in nums: + nums.remove(val) + k = k - 1 + return k + From fe4a2064e86102146f1b78bbc79337fab5c84a0a Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 04:02:31 +0800 Subject: [PATCH 02/13] finish 28 --- leetcode/28.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 leetcode/28.py diff --git a/leetcode/28.py b/leetcode/28.py new file mode 100644 index 0000000..0df4336 --- /dev/null +++ b/leetcode/28.py @@ -0,0 +1,12 @@ +""" +Given two strings needle and haystack, +return the index of the first occurrence of needle in haystack, +or -1 if needle is not part of haystack. +""" +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if needle in haystack: + return haystack.find(needle) + else: + return -1 + \ No newline at end of file From d0633f929254b0aa8c604e64af80ec8fafd318ec Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 04:15:57 +0800 Subject: [PATCH 03/13] finish 35 --- leetcode/35.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 leetcode/35.py diff --git a/leetcode/35.py b/leetcode/35.py new file mode 100644 index 0000000..ce944c1 --- /dev/null +++ b/leetcode/35.py @@ -0,0 +1,31 @@ +""" +Given a sorted array of distinct integers and a target value, +return the index if the target is found. +If not, return the index where it would be if it were inserted in order. + +You must write an algorithm with O(log n) runtime complexity. + +Constraints: + +1 <= nums.length <= 10^4 +-10^4 <= nums[i] <= 10^4 +nums contains distinct values sorted in ascending order. +-10^4 <= target <= 10^4 +""" + +from typing import List + + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + low = 0 + high = len(nums) - 1 + while low <= high: # Changed the condition to low <= high + mid = (high + low) // 2 + if target == nums[mid]: + return mid + elif target < nums[mid]: + high = mid - 1 + else: + low = mid + 1 + return low From 211fcd1dfa51de068a1f869ed4061907d992e4fa Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 04:24:15 +0800 Subject: [PATCH 04/13] finish 58 --- leetcode/58.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 leetcode/58.py diff --git a/leetcode/58.py b/leetcode/58.py new file mode 100644 index 0000000..2b3edb1 --- /dev/null +++ b/leetcode/58.py @@ -0,0 +1,11 @@ +""" +Given a string s consisting of words and spaces, +return the length of the last word in the string. + +A word is a maximal substring consisting of non-space characters only. +""" +class Solution: + def lengthOfLastWord(self, s: str) -> int: + s = s.strip() + s = s.split(" ") + return len(s[-1]) From 8d26ef13f237c626f4af60031ad3f32fcbf5e844 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 04:34:38 +0800 Subject: [PATCH 05/13] finish 66 --- leetcode/66.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 leetcode/66.py diff --git a/leetcode/66.py b/leetcode/66.py new file mode 100644 index 0000000..b57fec6 --- /dev/null +++ b/leetcode/66.py @@ -0,0 +1,22 @@ +"""You are given a large integer represented as an integer array digits, +where each digits[i] is the ith digit of the integer. +The digits are ordered from most significant to least significant in left-to-right order. +The large integer does not contain any leading 0's. +""" + +from typing import List +class Solution: + def plusOne(self, digits: List[int]) -> List[int]: + n = len(digits) + # Start from the rightmost digit + for i in range(n - 1, -1, -1): + # If the current digit is less than 9, just increment it by 1 and return + if digits[i] < 9: + digits[i] += 1 + return digits + # Otherwise, set the current digit to 0 and continue with the next digit + else: + digits[i] = 0 + # If we reach here, it means all digits were 9, so we need to add a new leading 1 + digits.insert(0, 1) + return digits From ca45976f263080968b53f041a777d092a238e362 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 15:07:09 +0800 Subject: [PATCH 06/13] unfinish --- luogu/p3717.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 luogu/p3717.py diff --git a/luogu/p3717.py b/luogu/p3717.py new file mode 100644 index 0000000..ff2823d --- /dev/null +++ b/luogu/p3717.py @@ -0,0 +1,27 @@ +# Unfinish +from typing import Tuple, List +import math + +n, m, r = input().split(" ") +locations: List[Tuple[str, str]] = [] +for i in range(int(m)): + location: Tuple[str, str] = tuple(input().split(" ")) + locations.append(location) + +map = [[0 for _ in range(int(n))] for _ in range(int(n))] +count = 0 + +for i in range(int(n)): + for j in range(int(n)): + if map[i][j] == 0: + for k in range(int(m)): + distance = math.sqrt( + (int(locations[k][0]) - i) ** 2 + (int(locations[k][1]) - j) ** 2 + ) + if distance <= float(r): + map[i][j] = 1 + count = count + 1 + break + +print(count) +print(map) \ No newline at end of file From 89e23e765454fe7aded7f82a69f1891c1de2a381 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 15:13:12 +0800 Subject: [PATCH 07/13] finish 67 --- leetcode/67.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 leetcode/67.py diff --git a/leetcode/67.py b/leetcode/67.py new file mode 100644 index 0000000..33850cc --- /dev/null +++ b/leetcode/67.py @@ -0,0 +1,9 @@ +# Given two binary strings a and b, return their sum as a binary string. + + +class Solution: + def addBinary(self, a: str, b: str) -> str: + int_a = int("0b" + a, 2) + int_b = int("0b" + b, 2) + int_c = int_a + int_b + return bin(int_c)[2:] From e02270f6bd277b26c7fd72f2c2664575809557b9 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 15:35:05 +0800 Subject: [PATCH 08/13] finish 83 --- leetcode/83.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/83.py diff --git a/leetcode/83.py b/leetcode/83.py new file mode 100644 index 0000000..df01fcb --- /dev/null +++ b/leetcode/83.py @@ -0,0 +1,23 @@ +# Given the head of a sorted linked list, +# delete all duplicates such that each element appears only once. +# Return the linked list sorted as well. + +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +from typing import Optional +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head is None: + return head + pointer = head + while pointer.next is not None: + if pointer.val == pointer.next.val: + pointer.next = pointer.next.next + else: + pointer = pointer.next + + return head \ No newline at end of file From 449729bf9d860f436d52ee8a26162a8a7c7ffe63 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 15:35:11 +0800 Subject: [PATCH 09/13] finish 88 --- leetcode/88.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 leetcode/88.py diff --git a/leetcode/88.py b/leetcode/88.py new file mode 100644 index 0000000..4246a38 --- /dev/null +++ b/leetcode/88.py @@ -0,0 +1,37 @@ +# You are given two integer arrays nums1 and nums2, +# sorted in non-decreasing order, +# and two integers m and n, +# representing the number of elements in nums1 and nums2 respectively. + +# Merge nums1 and nums2 into a single array sorted in non-decreasing order. + + +# The final sorted array should not be returned by the function, +# but instead be stored inside the array nums1. +# To accommodate this, nums1 has a length of m + n, +# where the first m elements denote the elements that should be merged, +# and the last n elements are set to 0 and should be ignored. nums2 has a length of n. + +# Constraints: + +# nums1.length == m + n +# nums2.length == n +# 0 <= m, n <= 200 +# 1 <= m + n <= 200 +# -10^9 <= nums1[i], nums2[j] <= 10^9 + +from typing import List + +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + if n == 0: + pass + else: + for i in range(n): + nums1[m+i] = nums2[i] + nums1.sort() + + From efde31ecc7ec873d2d155ba003bc0a6fa3b0f83e Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 15:47:15 +0800 Subject: [PATCH 10/13] finish 657 --- leetcode/657.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 leetcode/657.py diff --git a/leetcode/657.py b/leetcode/657.py new file mode 100644 index 0000000..383e520 --- /dev/null +++ b/leetcode/657.py @@ -0,0 +1,45 @@ +# There is a robot starting at the position (0, 0), the origin, +# on a 2D plane. Given a sequence of its moves, +# judge if this robot ends up at (0, 0) after it completes its moves. + +# You are given a string moves that represents +# the move sequence of the robot where moves[i] +# represents its ith move. Valid moves are 'R' (right), +# 'L' (left), 'U' (up), and 'D' (down). + +# Return true if the robot returns to the origin after it finishes all of its moves, +# or false otherwise. + +# Note: The way that the robot is "facing" is irrelevant. +# 'R' will always make the robot move to the right once, +# 'L' will always make it move left, etc. Also, +# assume that the magnitude of the robot's movement is the same for each move. + + +# Constraints: + +# 1 <= moves.length <= 2 * 10^4 +# moves only contains the characters 'U', 'D', 'L' and 'R'. + + +class Solution: + def judgeCircle1(self, moves: str) -> bool: + horizontal = 0 + vertical = 0 + for move in moves: + if move == "U": + vertical += 1 + if move == "D": + vertical -= 1 + if move == "L": + horizontal -= 1 + if move == "R": + horizontal += 1 + return horizontal == 0 and vertical == 0 + + def judgeCircle(self, moves: str) -> bool: + if moves.count("U")!=moves.count("D"): + return False + if moves.count("L")!=moves.count("R"): + return False + return True From c7da6a6c4b046b9f2ef67f124e2cc21474d90ac5 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 22:51:13 +0800 Subject: [PATCH 11/13] finish 461 --- leetcode/461.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 leetcode/461.py diff --git a/leetcode/461.py b/leetcode/461.py new file mode 100644 index 0000000..9ad4327 --- /dev/null +++ b/leetcode/461.py @@ -0,0 +1,5 @@ +class Solution: + def hammingDistance(self, x: int, y: int) -> int: + x_y = x^y + distance = bin(x_y)[2:].count("1") + return distance \ No newline at end of file From 813dca291067a34a458395d2ea5696dccc592978 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 22:51:20 +0800 Subject: [PATCH 12/13] finish 645 --- leetcode/645.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 leetcode/645.py diff --git a/leetcode/645.py b/leetcode/645.py new file mode 100644 index 0000000..54641ba --- /dev/null +++ b/leetcode/645.py @@ -0,0 +1,26 @@ +from typing import List + + +class Solution: + + def findErrorNums(self, nums: List[int]) -> List[int]: + sumSetNums = sum(set(nums)) + sumNums = sum(nums) + return [sumNums - sumSetNums, ((len(nums) * (len(nums) + 1)) // 2) - sumSetNums] + + def findErrorNums1(self, nums: List[int]) -> List[int]: + n = len(nums) + return_list = [0, 0] + for i in range(1, n + 1): + if i not in nums: + return_list[1] = i + break + + for i in range(1, n + 1): + if i in nums: + nums.remove(i) + if i in nums: + return_list[0] = i + break + + return return_list From 29d718861a79fef567e8d3e8449f96893a15c828 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Tue, 12 Mar 2024 22:51:38 +0800 Subject: [PATCH 13/13] update format --- leetcode/461.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/leetcode/461.py b/leetcode/461.py index 9ad4327..4bcf519 100644 --- a/leetcode/461.py +++ b/leetcode/461.py @@ -1,5 +1,5 @@ class Solution: def hammingDistance(self, x: int, y: int) -> int: - x_y = x^y + x_y = x ^ y distance = bin(x_y)[2:].count("1") - return distance \ No newline at end of file + return distance