
算法
文章平均质量分 62
PKU_Jade
这个作者很懒,什么都没留下…
展开
-
leetcode(142). Linked List Cycle II
problem Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using ext原创 2017-08-06 21:25:53 · 279 阅读 · 0 评论 -
leetcode(337). House Robber III
problem The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent hous原创 2017-09-08 22:18:08 · 218 阅读 · 0 评论 -
[python]leetcode(76). Minimum Window Substring
problem Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = “ADOBECODEBANC” T = “ABC” Minimum window原创 2017-09-17 17:12:08 · 518 阅读 · 0 评论 -
[python]leetcode(438). Find All Anagrams in a String
problem Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and原创 2017-09-17 16:49:30 · 1287 阅读 · 0 评论 -
[python]回溯法模板
什么是回溯法回溯法(探索与回溯法)是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。无重复元素全排列问题给定一个所有元素都不同的list,要求返回list元素的全排列。设n = len(list),那么这个问题可以考虑为n叉树,对这个树原创 2017-09-18 16:36:46 · 13315 阅读 · 4 评论 -
[python]leetcode(23). Merge k Sorted Lists
problem Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.solution对于这个问题,有这样两种主要的思路:从这k个链表中找出最小的连接到dummy node上,知道所有链表为空。把merge k简化成merge 2,也就是每次只原创 2017-09-26 19:15:59 · 1086 阅读 · 0 评论 -
[python]leetcode(128). Longest Consecutive Sequence
problem Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements seque原创 2017-09-10 21:53:52 · 1340 阅读 · 0 评论 -
[python]leetcode(148). Sort List
problem Sort a linked list in O(n log n) time using constant space complexity.solution因为常见的排序算法时间复杂度描述的都是顺序表,而链表和顺序表的主要区别就是顺序表可以随机存取,而链表的查找效率为O(n)O(n),插入、删除操作链表的效率要更高,但是这建立在已知插入位置的情况下。class Solution原创 2017-09-27 14:06:15 · 497 阅读 · 0 评论 -
leetcode(543). Diameter of Binary Tree
problem Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This pa原创 2017-08-02 11:03:58 · 401 阅读 · 0 评论 -
剑指offer 第二章 基础知识
基础知识编程语言知识直接问语法解决一个实际的问题python常考点: pass数据结构常考 数组,字符串(连续内存存储), 链表,树(需要操作指针), 栈(与递归相关)和 队列(广度优先遍历)。算法和数据操作查找和排序 递归和循环 位运算原创 2017-09-08 11:02:00 · 264 阅读 · 0 评论 -
leetcode(240). Search a 2D Matrix II
problem Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right.原创 2017-09-06 23:00:51 · 237 阅读 · 0 评论 -
leetcode(279). Perfect Squares
problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; g原创 2017-08-23 23:52:04 · 264 阅读 · 0 评论 -
leetcode(98). Validate Binary Search Tree
problem Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than t原创 2017-08-06 23:18:37 · 321 阅读 · 0 评论 -
leetcode(380). Insert Delete GetRandom O(1)
problem Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an i原创 2017-08-22 10:15:25 · 300 阅读 · 0 评论 -
HTML与css
什么是HTML超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言。它负责网页的内容和结构,例如在一篇博客中可以用它来展示博客的内容以及标记博客的标题、正文等。后来为了更好的展示内容HTML中也加入了一些新的标签,例如font,i等,但是这与程序设计理念(单一职责原则)不符,这样导致的问题就是可读性降低(内容和格式混合到一起),维原创 2017-08-21 23:15:59 · 341 阅读 · 0 评论 -
leetcode(56). Merge Intervals
problem Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].solution# Definition for an interval.# c原创 2017-08-22 12:24:55 · 269 阅读 · 0 评论 -
从python的列表推导式说起
引言之前在刷leetcode时用到了数组中元素都是0的情况,看到两种方式生成这样的list:[0]*n[0 for _ in range(n)]通过本地测试发现两种方式实现的效率并不一样,可偏偏这是一个二维dp问题,要生成一个二维数组,自然代码就要写成下面这样的形式了:[[0]*n]*m[[0 for _ in range(n)] for _ in range(m)]结果提交时就发现正确原创 2017-08-04 01:09:41 · 518 阅读 · 0 评论 -
leetcode(416). Partition Equal Subset Sum
problem Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each原创 2017-08-23 14:05:46 · 316 阅读 · 0 评论 -
图的最短路径算法
最短路径问题是图论研究中的一个经典算法问题,旨在寻找图(由结点和路径组成的)中两结点之间的最短路径。算法具体的形式包括:确定起点的最短路径问题 - 即已知起始结点,求最短路径的问题。适合使用Dijkstra算法。确定终点的最短路径问题 - 与确定起点的问题相反,该问题是已知终结结点,求最短路径的问题。在无向图中该问题与确定起点的问题完全等同,在有向图中该问题等同于把所有路径方向反转的确定起点的问原创 2017-08-23 17:01:14 · 1164 阅读 · 0 评论 -
leetcode(310). Minimum Height Trees
problem For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height原创 2017-08-23 20:11:06 · 299 阅读 · 0 评论 -
leetcode(617). Merge Two Binary Trees
problem Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.原创 2017-08-01 13:07:24 · 275 阅读 · 0 评论 -
leetcode(34). Search for a Range
problem Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log)原创 2017-07-26 21:29:56 · 310 阅读 · 0 评论 -
[python]leetcode(75). Sort Colors
problem Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will原创 2017-10-06 18:59:01 · 470 阅读 · 0 评论 -
[python]leetcode(309). Best Time to Buy and Sell Stock with Cooldown
problem Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you原创 2017-10-06 21:52:44 · 713 阅读 · 0 评论 -
[python]leetcode(72). Edit Distance AND (583). Delete Operation for Two Strings
problem Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations per原创 2017-09-13 22:51:30 · 306 阅读 · 0 评论 -
leetcode(338). Counting Bits
problem Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array. Exampl原创 2017-08-07 15:47:04 · 362 阅读 · 0 评论 -
[python]leetcode(632). Smallest Range
problem You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k lists. We define the range [a,b] is smaller tha原创 2017-09-21 12:10:43 · 632 阅读 · 0 评论 -
42. Trapping Rain Water
problem Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,原创 2017-09-22 11:33:10 · 239 阅读 · 0 评论 -
leetcode(547). Friend Circles
problem There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct fri原创 2017-08-01 22:32:02 · 296 阅读 · 0 评论 -
leetcode(15). 3Sum
problem Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.原创 2017-07-23 23:36:35 · 245 阅读 · 0 评论 -
[python]leetcode(450). Delete Node in a BST
problem Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion原创 2017-09-20 12:55:23 · 734 阅读 · 0 评论 -
剑指offer 第五章 优化时间和空间效率
时间效率编程习惯:例如c++中尽量使用引用或指针传递复杂参数等,这些需要在实际中积累,多看看别人的代码实现思路,也许时间复杂度相同,但是常数上可能会有差别循环和递归的选择,因为递归在子函数调用中会有一些开销,所以通常递归会慢一些,还有就是通常可以写成循环使用动态优化选择合适的数据结构和算法,例如在查找中,顺序查找、二分查找,hash表查找,时间相差很多,还有在例如max sliding win原创 2017-09-12 10:12:41 · 504 阅读 · 0 评论 -
[python]leetcode(79). Word Search
problem Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally原创 2017-09-19 22:00:21 · 1999 阅读 · 0 评论 -
leetcode(3) Longest Substring Without Repeating Characters
problem Given a string, find the length of the longest substring without repeating characters.原创 2017-07-21 17:16:14 · 235 阅读 · 0 评论 -
leetcode(121). Best Time to Buy and Sell Stock
problem Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one sha原创 2017-08-06 09:45:51 · 227 阅读 · 0 评论 -
[python]leetcode(437). Path Sum III
problem You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root o原创 2017-09-15 09:55:43 · 1634 阅读 · 4 评论 -
[python]leetcode(315). Count of Smaller Numbers After Self
problem You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i原创 2017-09-11 13:51:08 · 900 阅读 · 0 评论 -
[python]leetcode(239). Sliding Window Maximum
problem Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the s原创 2017-09-11 16:30:06 · 1331 阅读 · 0 评论 -
[python]leetcode(22). Generate Parentheses
problem Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.solution1-回溯法参考这篇博客的模板,我们可以把括号生成的问题考虑成一颗二叉树遍历问题,对这棵树进行遍历回溯。class Solution(object):原创 2017-09-27 20:57:31 · 391 阅读 · 0 评论 -
[python]leetcode(49). Group Anagrams
problem Given an array of strings, group anagrams together.solution这个问题的关键就在于如何判别两个字符串是不是anagrams,比较容易想到的是把字符串排序之后在进行比较,只不过这样的复杂度是O(nlogn)O(n\log n),n为字符串长度,我们要利用的信息就是不管怎么样排列都是anagram,所以要找到一个函数使得变换次原创 2017-09-27 22:38:34 · 565 阅读 · 0 评论