Leetcode
山上之城
这个作者很懒,什么都没留下…
展开
-
[Leetcode with python] 187. Repeated DNA Sequences (# Hash table)
题目 https://leetcode.com/problems/repeated-dna-sequences/ 解题思路 题目意思是:找出出现超过一次的且长度为10的子字符串。 我的第一种做法是遍历一次字符串,然后用str.find()去判断子字符串是否出现第二次,但是出现Time Limit Exceeded。 第二种做法同样是遍历一次,但是改成写入字典的方式,最后筛出大于1次的子字符串即可...原创 2019-02-02 11:29:36 · 108 阅读 · 0 评论 -
[Leetcode with python] 707. Design Linked List (# Linked list)
题目 https://leetcode.com/problems/design-linked-list/ 解题 分情况讨论的思维(if-else),要养成习惯 如何去设计/定义一个class,以及class里面的变量和方法 class MyLinkedList(object): class Node(object): def __init__(self, x, ne...原创 2019-03-08 16:39:13 · 204 阅读 · 0 评论 -
[Leetcode with python] 206. Reverse Linked List (# Linked list)
题目 https://leetcode.com/problems/reverse-linked-list/ 解题 利用栈结构,将链表内容依次压入栈,再从栈依次弹出即可构造逆序。下面的代码用普通数组模拟栈。 注意最后几行代码:l = head, 遍历 l 赋值,但是最后是返回 head!(返回 l 报错!) # Definition for singly-linked list. # clas...原创 2019-03-08 15:56:31 · 176 阅读 · 0 评论 -
[Leetcode with python] 141. Linked List Cycle (# Linked list)
题目 https://leetcode.com/problems/linked-list-cycle/ 解题 快慢指针法。定义两个指针:快指针每次走一步;慢指针每次走两步。依次循环下去,如果链表存在环,那么快慢指针一定会有相等的时候。 # Definition for singly-linked list. # class ListNode(object): # def __init...原创 2019-03-08 15:46:59 · 161 阅读 · 0 评论 -
[Leetcode with python] 83. Remove Duplicates from Sorted List (# Linked list)
题目 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 解题 还是递归。 注意两点: 1,用递归前要判别head和head.next是否为空,; 2,最后要判别第一第二个值是否相等。 # Definition for singly-linked list. # class ListNode(object): # ...原创 2019-02-28 18:11:12 · 153 阅读 · 0 评论 -
[Leetcode with python] 242. Valid Anagram (# Hash table)
题目 https://leetcode.com/problems/valid-anagram/ 解题 解法一: 对字符串s,遍历字符串,统计每个字母出现次数,并存入字典里。对字符串t进行同样的操作。判断两个字符串是否相等。 class Solution(object): def isAnagram(self, s, t): """ :type s: str...原创 2019-02-13 18:12:26 · 159 阅读 · 0 评论 -
[Leetcode with python] 3. Longest Substring Without Repeating Characters (# Hash table)
题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 解题 滑动窗口法。 需要注意的是:窗口里不一定不含重复字符,而是窗口长度等于不含重复字符的最大长度。 (有点难理解,但的确是这样!) class Solution: # @return an integer def le...原创 2019-02-18 18:42:55 · 122 阅读 · 0 评论 -
[Leetcode with python] 21. Merge Two Sorted Lists (# Linked list)
题目 https://leetcode.com/problems/merge-two-sorted-lists/ 解题 题目的意思是合并成一条新的有序链表。可以用递归思想,非常简单。 链表就像是一条腿的二叉树。(从单点看) # Definition for singly-linked list. # class ListNode(object): # def __init__(self,...原创 2019-02-27 18:09:54 · 232 阅读 · 0 评论 -
[Leetcode with python] 202. Happy Number (# Hash table)
题目 https://leetcode.com/problems/happy-number/ 解题思路 用字典储存出现过的数字。若重复出现,则返回False;若等于1,则返回True。 代码 class Solution(object): def cal_square_sum(self, n): num_l = [] while n > 0: ...原创 2019-02-11 17:53:27 · 217 阅读 · 0 评论 -
[Leetcode with python] 1. Two Sum (# Hash table)
题目 https://leetcode.com/problems/two-sum/ 解题思路 第一次写技术博客,有点小兴奋! 代码 class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int ...原创 2019-01-29 18:15:43 · 174 阅读 · 0 评论 -
[Leetcode with python] 36. Valid Sudoku (# Hash table)
题目 https://leetcode.com/problems/valid-sudoku/ 解题 遍历三次矩阵,三个规则都检验一次。 class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool ...原创 2019-04-01 18:10:30 · 170 阅读 · 0 评论