-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def largestNumber(self, nums: List[int]) -> str: | ||
|
||
for i in range(len(nums) - 1): | ||
for j in range(i + 1, len(nums)): | ||
if str(nums[i]) + str(nums[j]) < str(nums[j]) + str(nums[i]): | ||
nums[i], nums[j] = nums[j], nums[i] | ||
s = "" | ||
for num in nums: | ||
s += str(num) | ||
return str(int(s)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Definition for singly-linked list. | ||
from typing import Optional | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
class Solution: | ||
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: | ||
dummy = ListNode() | ||
p = dummy | ||
while True: | ||
count = k | ||
stack = [] | ||
tmp = head | ||
while count and tmp: | ||
stack.append(tmp) | ||
tmp = tmp.next | ||
count -= 1 | ||
if count: | ||
p.next = head | ||
break | ||
while stack: | ||
p.next = stack.pop() | ||
p = p.next | ||
p.next = tmp | ||
head = tmp | ||
return dummy.next | ||
|
||
|
||
if __name__ == '__main__': | ||
print(Solution().reverseKGroup([1, 2, 3, 4, 5], 2)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from itertools import combinations | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
L = [] | ||
for i in combinations(nums, 3): | ||
if sum(list(i)) == 0: | ||
L.append(list(i)) | ||
L1 = [] | ||
for j in L: | ||
j.sort() | ||
L1.append(j) | ||
b = list(set([tuple(j) for j in L1])) | ||
return [list(i) for i in b] | ||
|
||
|
||
nums = [-1, 0, 1, 2, -1, -4] | ||
print(Solution().threeSum(nums)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if not head or not head.next: | ||
return head | ||
|
||
tail = head.next | ||
head.next = self.swapPairs(tail.next) | ||
tail.next = head | ||
return tail |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
|
||
@staticmethod | ||
def twoSum(nums: List[int], target: int) -> List[int]: | ||
length = len(nums) | ||
if length < 2: | ||
return [] | ||
for k, v in enumerate(nums): | ||
|
||
for j in range(k + 1, length): | ||
if v + nums[j] == target: | ||
return [k, j] | ||
return [] | ||
|
||
|
||
if __name__ == '__main__': | ||
nums = [3, 2, 4] | ||
target = 6 | ||
print(Solution.twoSum(nums, target)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Definition for singly-linked list. | ||
|
||
from typing import Optional | ||
|
||
|
||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
class Solution: | ||
def addTwoNumbers(self, l1, l2) -> Optional[ListNode]: | ||
l1.val += l2.val | ||
if l1.val > 10: | ||
l1.next = self.addTwoNumbers(ListNode(l1.val // 10), l1.next) | ||
l1.val %= 10 | ||
l1.next = self.addTwoNumbers(l1.next, l2.next) | ||
return l1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def bubbleSort(data: list): | ||
length = len(data) | ||
for i in range(length - 1): | ||
for j in range(length - i - 1): | ||
if data[j] > data[j + 1]: | ||
data[j], data[j + 1] = data[j + 1], data[j] | ||
return data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from PyPDF2 import PdfReader, PdfWriter | ||
|
||
output = PdfWriter() | ||
|
||
reader = PdfReader(r"D:\CU_76a18490a1c94c20bda9ccfc81acabd6 (1).pdf") | ||
# 获取总页数 | ||
page_count = reader.getNumPages() | ||
|
||
# with open(,'rb') as pf: | ||
# pin = PdfReader(pf) | ||
# for i in range(2): | ||
# page = pin.getPage(pin.pages[0]) | ||
# print(page) | ||
# # output.addPage(page) | ||
# # with open("XXXX/out.pdf",'wb') as ouf: | ||
# # output.write(ouf) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
class Solution: | ||
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||
heap = [] | ||
for sub_list in lists: | ||
while sub_list: | ||
heap.append(sub_list.val) | ||
sub_list = sub_list.next | ||
heap.sort(reverse=True) | ||
|
||
head = ListNode(None) | ||
curr_list = head | ||
while heap: | ||
# 从heap列表的尾部取最小数加入到链表中 | ||
temp_list = ListNode(heap.pop()) | ||
curr_list.next = temp_list | ||
curr_list = curr_list.next | ||
return head.next |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def isPalindrome(self, x: int) -> bool: | ||
if x < 0: | ||
return False | ||
x1 = str(x)[::-1] | ||
if int(x1) == x: | ||
return True | ||
return False | ||
|
||
|
||
if __name__ == '__main__': | ||
x = -123 | ||
print(str(x)[::-1].isalnum()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import re | ||
|
||
|
||
class Solution: | ||
def myAtoi(self, s: str) -> int: | ||
INT_MAX = 2147483647 | ||
INT_MIN = -2147483648 | ||
str = s.lstrip() # 清除左边多余的空格 | ||
num_re = re.compile(r'^[\+\-]?\d+') # 设置正则规则 | ||
num = num_re.findall(str) # 查找匹配的内容 | ||
num = int(*num) # 由于返回的是个列表,解包并且转换成整数 | ||
return max(min(num, INT_MAX), INT_MIN) # 返回值 |