
LeetCode&算法
记录刷过的LeetCode题目及心得
房东丢的猫
之前研究计算机视觉算法,现转战Java开发领域的Coder
展开
-
求链表的中间节点(Java语言实现)
1. 方法一 (1)算法思想:遍历一遍整个链表,求得链表的长度len后,再从头遍历链表len/2遍,即可到达链表的中点处。 (2)代码: public ListNode findMid(ListNode head) { int len = 0; ListNode curr = head; while (curr != null) { len++; curr = curr.next; } curr = head; for (int i = 0; i < len / 2; i++)原创 2021-03-04 16:33:57 · 593 阅读 · 4 评论 -
Problem#155 Min Stack
Problem Solution # 法一:差值法 class MinStack: def __init__(self): """ initialize your data structure here. """ self.min_v = float('inf') self.stack = [] ...原创 2020-03-06 18:30:40 · 121 阅读 · 0 评论 -
Problem#136 Single Number
Problem Solution class Solution: def singleNumber(self, nums: List[int]) -> int: single_num = 0 for num in nums: single_num = single_num ^ num return single...原创 2020-03-06 18:28:57 · 147 阅读 · 0 评论 -
Problem#122 Best Time to Buy and Sell Stock II
Problem Solution class Solution: def maxProfit(self, prices: List[int]) -> int: max_profit = 0 for i in range(len(prices) - 1): if prices[i+1] - prices[i] > 0: ...原创 2020-03-06 18:26:15 · 113 阅读 · 0 评论 -
Problem#121 Best Time to Buy and Sell Stock
Problem Solution class Solution: def maxProfit(self, prices: List[int]) -> int: min_price = float('inf') max_profit = 0 for price in prices: # 在最小点买入,在最高点卖出...原创 2020-03-06 18:24:26 · 105 阅读 · 0 评论 -
Problem#104 Maximum Depth of Binary Tree
Problem Solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # 法一:递归方式 class Solu...原创 2020-03-06 18:22:20 · 103 阅读 · 0 评论 -
Problem#88 Merge Sorted Array
Problem Solution # 从后向前填充 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...原创 2020-03-06 18:20:11 · 110 阅读 · 0 评论 -
Problem#53 Maximum Subarray
Problem Solution # 法一:优化前缀和 class Solution: def maxSubArray(self, nums: List[int]) -> int: n = len(nums) max_sum = nums[0] min_sum = sum = 0 sum = 0 for...原创 2020-03-06 18:18:33 · 346 阅读 · 0 评论 -
Problem#26 Remove Duplicates from Sorted Array
Problem Solution class Solution: def removeDuplicates(self, nums: List[int]) -> int: if nums: slow = 0 for fast in range(1, len(nums)): if nums[...原创 2020-02-26 11:56:46 · 148 阅读 · 0 评论 -
Problem#20 Valid Parentheses
Problem Solution class Solution: def isValid(self, s: str) -> bool: stack = [] map = { ')': '(', '}': '{', ']': '[' } if len...原创 2020-02-26 11:26:29 · 121 阅读 · 0 评论