Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
PingZ7 committed Feb 15, 2020
0 parents commit b54fc31
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 125.验证回文串.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# @lc app=leetcode.cn id=125 lang=python3
#
# [125] 验证回文串
#
# https://leetcode-cn.com/problems/valid-palindrome/description/
#
# algorithms
# Easy (42.34%)
# Likes: 154
# Dislikes: 0
# Total Accepted: 77.6K
# Total Submissions: 183.3K
# Testcase Example: '"A man, a plan, a canal: Panama"'
#
# 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
#
# 说明:本题中,我们将空字符串定义为有效的回文串。
#
# 示例 1:
#
# 输入: "A man, a plan, a canal: Panama"
# 输出: true
#
#
# 示例 2:
#
# 输入: "race a car"
# 输出: false
#
#
#

# @lc code=start
class Solution:
def isPalindrome(self, s: str) -> bool:
# 双指针
l, r = 0, len(s) - 1
while l < r:
while l < r and not s[l].isalnum():
l += 1
while l < r and not s[r].isalnum():
r -= 1
if s[l].lower() != s[r].lower():
return False
l += 1
r -= 1
return True



# @lc code=end

27 changes: 27 additions & 0 deletions 189.旋转数组.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# @lc app=leetcode.cn id=189 lang=python3
#
# [189] 旋转数组
#

# @lc code=start
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# 三次反转
def swap(i, j, nums):
while i < j:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1

n = len(nums)
k %= n
swap(0, n-1, nums)
swap(0, k-1, nums)
swap(k, n-1, nums)

# @lc code=end

47 changes: 47 additions & 0 deletions 21.合并两个有序链表.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# @lc app=leetcode.cn id=21 lang=python3
#
# [21] 合并两个有序链表
#
# https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
#
# algorithms
# Easy (59.59%)
# Likes: 846
# Dislikes: 0
# Total Accepted: 180.1K
# Total Submissions: 302.3K
# Testcase Example: '[1,2,4]\n[1,3,4]'
#
# 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
#
# 示例:
#
# 输入:1->2->4, 1->3->4
# 输出:1->1->2->3->4->4
#
#
#

# @lc code=start
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is None:
return l2
elif l2 is None:
return l1
elif l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l2.next, l1)
return l2

# @lc code=end

69 changes: 69 additions & 0 deletions 26.删除排序数组中的重复项.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# @lc app=leetcode.cn id=26 lang=python3
#
# [26] 删除排序数组中的重复项
#
# https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/
#
# algorithms
# Easy (48.46%)
# Likes: 1324
# Dislikes: 0
# Total Accepted: 244.2K
# Total Submissions: 503.9K
# Testcase Example: '[1,1,2]'
#
# 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
#
# 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
#
# 示例 1:
#
# 给定数组 nums = [1,1,2],
#
# 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
#
# 你不需要考虑数组中超出新长度后面的元素。
#
# 示例 2:
#
# 给定 nums = [0,0,1,1,1,2,2,3,3,4],
#
# 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
#
# 你不需要考虑数组中超出新长度后面的元素。
#
#
# 说明:
#
# 为什么返回数值是整数,但输出的答案是数组呢?
#
# 请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
#
# 你可以想象内部操作如下:
#
# // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
# int len = removeDuplicates(nums);
#
# // 在函数里修改输入数组对于调用者是可见的。
# // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
# for (int i = 0; i < len; i++) {
# print(nums[i]);
# }
#
#
#

# @lc code=start
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
# 双指针法
i = 0
for j in range(len(nums)):
if nums[i] != nums[j]:
i += 1
nums[i] = nums[j]
return i + 1

# @lc code=end

45 changes: 45 additions & 0 deletions 283.移动零.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# @lc app=leetcode.cn id=283 lang=python3
#
# [283] 移动零
#
# https://leetcode-cn.com/problems/move-zeroes/description/
#
# algorithms
# Easy (59.28%)
# Likes: 494
# Dislikes: 0
# Total Accepted: 106.6K
# Total Submissions: 179.8K
# Testcase Example: '[0,1,0,3,12]'
#
# 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
#
# 示例:
#
# 输入: [0,1,0,3,12]
# 输出: [1,3,12,0,0]
#
# 说明:
#
#
# 必须在原数组上操作,不能拷贝额外的数组。
# 尽量减少操作次数。
#
#
#

# @lc code=start
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# 双指针
i = 0
for j in range(len(nums)):
if nums[j] != 0:
nums[i], nums[j] = nums[j], nums[i]
i += 1
# @lc code=end

58 changes: 58 additions & 0 deletions 88.合并两个有序数组.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# @lc app=leetcode.cn id=88 lang=python3
#
# [88] 合并两个有序数组
#
# https://leetcode-cn.com/problems/merge-sorted-array/description/
#
# algorithms
# Easy (46.57%)
# Likes: 414
# Dislikes: 0
# Total Accepted: 107.9K
# Total Submissions: 231.7K
# Testcase Example: '[1,2,3,0,0,0]\n3\n[2,5,6]\n3'
#
# 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
#
# 说明:
#
#
# 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
# 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
#
#
# 示例:
#
# 输入:
# nums1 = [1,2,3,0,0,0], m = 3
# nums2 = [2,5,6], n = 3
#
# 输出: [1,2,2,3,5,6]
#
#

# @lc code=start
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.
"""
# nums1[:] = sorted(nums1[:m] + nums2)

# 双指针/从后往前
p1 = m - 1
p2 = n - 1
p = m + n -1
while p1 >= 0 and p2 >= 0:
if nums1[p1] < nums2[p2]:
nums1[p] = nums2[p2]
p2 -= 1
else:
nums1[p] = nums1[p1]
p1 -= 1
p -= 1
nums1[:p2 + 1] = nums2[:p2 + 1]

# @lc code=end

0 comments on commit b54fc31

Please sign in to comment.