This commit is contained in:
sangge-redmi 2024-05-27 15:54:36 +08:00
commit c4ddb1d696
12 changed files with 276 additions and 0 deletions

28
leetcode/27.py Normal file
View File

@ -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

12
leetcode/28.py Normal file
View File

@ -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

31
leetcode/35.py Normal file
View File

@ -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

5
leetcode/461.py Normal file
View File

@ -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

11
leetcode/58.py Normal file
View File

@ -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])

26
leetcode/645.py Normal file
View File

@ -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

45
leetcode/657.py Normal file
View File

@ -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

22
leetcode/66.py Normal file
View File

@ -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

9
leetcode/67.py Normal file
View File

@ -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:]

23
leetcode/83.py Normal file
View File

@ -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

37
leetcode/88.py Normal file
View File

@ -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()

27
luogu/p3717.py Normal file
View File

@ -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)