算法题
天灵狐
GO GO的说
展开
-
【LeetCode】2.Add Two Numbers 两数相加
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807class ListNode: def __init__(self, x): self.val = x self.next = None解题思路:本题的难点主要在于输入为指定...原创 2018-11-06 17:59:09 · 155 阅读 · 0 评论 -
【LeetCode】3.Longest Substring Without Repeating Characters 无重复字符的最长子串
输入: "pwwkew"输出: 3解释: 无重复字符的最长子串是 "wke",其长度为 3。 请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串。解题思路:本题没什么难点。lenth = 0prevLoc = 0i = 0maxI = len(s)while i < maxI: if s[i] in s[prevLoc:i]:...原创 2018-11-07 11:53:08 · 171 阅读 · 0 评论 -
【LeetCode】1.Two Sum 两数之和
给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]解题思路1:两个循环,轻松搞定:for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j...原创 2018-11-05 17:24:43 · 184 阅读 · 0 评论 -
【LeetCode】4.Median of Two Sorted Arrays 两个排序数组的中位数
示例 1:nums1 = [1, 3]nums2 = [2]中位数是 2.0示例 2:nums1 = [1, 2]nums2 = [3, 4]中位数是 (2 + 3)/2 = 2.5解题思路:糟糕- -没理解题意,首先需要知道“中位数”的含义,其次,题目对时间复杂度做了要求,必须为O(log (m+n))。先写理解的代码,猜测题意:nums = s...原创 2018-11-16 18:23:01 · 228 阅读 · 0 评论 -
【LeetCode】6.ZigZag Conversion Z字形变换
示例1:Input: s = "PAYPALISHIRING", numRows = 3Output: "PAHNAPLSIIGYIR"Explanation:P A H NA P L S I I GY I R示例2:Input: s = "PAYPALISHIRING", numRows = 4Output: "PINALSIGYAHRPI"E...原创 2018-11-27 21:40:16 · 165 阅读 · 0 评论 -
【LeetCode】7.Reverse Integer 整数反转
示例1:Input: 123Output: 321注意:假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。解题思路:以后过于简单的题不写进博客了。if x < 0: res = - int(str(x)[: 0: -1]) if res <...原创 2018-11-28 14:09:45 · 137 阅读 · 0 评论