06 2014 档案
摘要:最简单的不相交集的实现,来自MAW的《数据结构与算法分析》。代码:class DisjSet: def __init__(self, NumSets): self.S = [0 for i in range(NumSets+1)] def SetUnion(self, S,...
阅读全文
摘要:二叉树的插入与删除,来自Mark Allen Weiss的《数据结构与算法分析》。# Definition for a binary tree nodeclass TreeNode: def __init__(self, x): self.val = x self...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/add-two-numbers/题意:You are given two linked lists representing two non-negative numbers. The digits are stored i...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/题意:Given a string, find the length of the longest substring witho...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/reverse-nodes-in-k-group/题意:Given a linked list, reverse the nodes of a linked listkat a time and return its mod...
阅读全文
摘要:原题地址:https://oj.leetcode.com/submissions/detail/5341904/题意:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/merge-two-sorted-lists/题意:Merge two sorted linked lists and return it as a new list. The new list should be made...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/rotate-list/题意:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/minimum-window-substring/题意:Given a string S and a string T, find the minimum window in S which will contain all...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/unique-paths-ii/题意:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/unique-paths/题意:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/maximal-rectangle/题意:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/题意:Givennnon-negative integers representing the histogram's bar height where the ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/decode-ways/题意:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/gray-code/题意:The gray code is a binary numeral system where two successive values differ in only one bit.Given a...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/题意:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/题意:Given a sorted linked list, delete all nodes that have duplicate number...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/题意:Given a sorted linked list, delete all duplicates such that each element a...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/题意:Follow up for "Remove Duplicates":What if duplicates are allowed at mo...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/题意:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Gi...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/partition-list/题意:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/题意:Given a binary tree, find its maximum depth.The maximum depth is the number of n...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/text-justification/题意:Given an array of words and a lengthL, format the text such that each line has exactlyLcha...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/insert-interval/题意:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if ne...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/merge-intervals/题意:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6]...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/maximum-subarray/题意:Find the contiguous subarray within an array (containing at least one number) which has the ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/jump-game-ii/题意:Given an array of non-negative integers, you are initially positioned at the first index of the ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/jump-game/题意:Given an array of non-negative integers, you are initially positioned at the first index of the arr...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/wildcard-matching/题意:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single charac...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/regular-expression-matching/题意:Implement regular expression matching with support for'.'and'*'.'.' Matches any s...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/multiply-strings/题意:Given two numbers represented as strings, return multiplication of the numbers as a string.N...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/trapping-rain-water/题意:Givennnon-negative integers representing an elevation map where the width of each bar is ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/count-and-say/题意:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 11...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/longest-valid-parentheses/题意:Given a string containing just the characters'('and')', find the length of the long...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/next-permutation/题意:Implement next permutation, which rearranges numbers into the lexicographically next greater...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/题意:You are given a string,S, and a list of words,L, that are all of th...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/remove-element/题意:Given an array and a value, remove all instances of that value in place and return the new len...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/题意:Given a sorted array, remove the duplicates in place such that each eleme...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/generate-parentheses/题意:Givennpairs of parentheses, write a function to generate all combinations of well-formed...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/valid-parentheses/题意:Given a string containing just the characters'(',')','{','}','['and']', determine if the in...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/题意:Given a digit string, return all possible letter combinations that the ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/longest-common-prefix/题意:Write a function to find the longest common prefix string amongst an array of strings.解...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/roman-to-integer/题意:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range f...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/integer-to-roman/题意:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range f...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/container-with-most-water/题意:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordina...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/palindrome-number/题意:Determine whether an integer is a palindrome. Do this without extra space.click to show spo...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/divide-two-integers/题意:Divide two integers without using multiplication, division and mod operator.解题思路:不许用乘、除和求...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/string-to-integer-atoi/题意:Implementatoito convert a string to an integer.Hint:Carefully consider all possible in...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/zigzag-conversion/题意:The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like thi...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/reverse-integer/题意:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321解题思路:翻...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/longest-palindromic-substring/题意:Given a stringS, find the longest palindromic substring inS. You may assume tha...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/combination-sum-ii/题意:Given a collection of candidate numbers (C) and a target number (T), find all unique combi...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/combination-sum/题意:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/first-missing-positive/题意:Given an unsorted integer array, find the first missing positive integer.For example,G...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array/题意:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/scramble-string/题意:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty su...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/simplify-path/题意:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/add-binary/题意:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/search-for-a-range/题意:Given a sorted array of integers, find the starting and ending position of a given target ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/sqrtx/题意:Implementint sqrt(int x).Compute and return the square root ofx.解题思路:实现开平方函数。这里要注意的一点是返回的时一个整数。通过这一点我们可...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/sort-colors/题意:Given an array withnobjects colored red, white or blue, sort them so that objects of the same col...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/powx-n/题意:Implement pow(x,n).解题思路:求幂函数的实现。使用递归,类似于二分的思路,解法来自Mark Allen Weiss的《数据结构与算法分析》。正确代码:class Solution: ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/edit-distance/题意:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2....
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/rotate-image/题意:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Fo...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/length-of-last-word/题意:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', retu...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/plus-one/题意:Given a non-negative number represented as an array of digits, plus one to the number.The digits are...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/search-insert-position/题意:Given a sorted array and a target value, return the index if the target is found. If n...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/题意:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its com...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/sudoku-solver/题意:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated b...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/valid-sudoku/题意:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could b...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/题意:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/anagrams/题意:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be i...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/题意:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order....
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/spiral-matrix/题意:Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral orde...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/word-search/题意:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/merge-sorted-array/题意:Given two sorted integer arrays A and B, merge B into A as one sorted array.解题思路:归并排序的归并这一...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/题意:Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.解题...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/题意:Given a string containing only digits, restore it by returning all possible valid IP add...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/interleaving-string/题意:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/distinct-subsequences/题意:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subseq...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/题意:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/pascals-triangle/题意:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Ret...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/triangle/题意:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n...
阅读全文
摘要:算法导论上面快速排序的实现。代码:def partition(array, left, right): i = left-1 for j in range(left, right): if array[j] <= array[right]: i += ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/题意:Say you have an array for which theithelement is the price of a given sto...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/题意:Say you have an array for which theithelement is the price of a given stoc...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/题意:Say you have an array for which theithelement is the price of a given stock o...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/valid-palindrome/题意:Given a string, determine if it is a palindrome, considering only alphanumeric characters an...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/word-ladder/题意:Given two words (startandend), and a dictionary, find the length of shortest transformation seque...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/题意:Given an unsorted array of integers, find the length of the longest consecutive ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/surrounded-regions/题意:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is cap...
阅读全文