
LeetCode
文章平均质量分 70
记录我这几个月做的LeetCode题,基本是Python代码,会给出简单的分析。
白熊花田
这个作者很懒,什么都没留下…
展开
-
LeetCode[BFS]----Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time Eac...原创 2018-12-12 21:06:45 · 396 阅读 · 0 评论 -
LeetCode[BFS]----Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a ti...原创 2018-12-12 20:57:58 · 379 阅读 · 0 评论 -
LeetCode[in-place]----Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]E...原创 2018-11-22 13:50:00 · 207 阅读 · 0 评论 -
LeetCode[Math]----Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only...原创 2018-11-07 10:30:10 · 343 阅读 · 0 评论 -
LeetCode[Bitwise]----Bitwise AND of Numbers Range
Bitwise AND of Numbers RangeGiven a range [m, n] where 0 For example, given the range [5, 7], you should return 4.分析:给定m和n,返回m到n闭区间内所有元素位运算&后的结果。我们知道a和b进行And操作时,在a和b同时为1的时候结果才为1.原创 2016-05-02 23:40:48 · 961 阅读 · 0 评论 -
LeetCode[Stack]----Min Stack&Stack&Queue
整理一下LeetCode上三道与栈有关的题,分别是Implement Queue using Stacks,Implement Stack using Queues和Min Stack。1.Implement Queue using StacksImplement the following operations of a queue using stacks.原创 2016-04-30 15:25:48 · 4155 阅读 · 0 评论 -
LeetCode[Design]----Peeking Iterator
Peeking IteratorGiven an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the原创 2016-04-22 21:11:02 · 2281 阅读 · 0 评论 -
LeetCode[Math]----Excel Sheet Column Title----Excel Sheet Column Number
Excel Sheet Column TitleGiven a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27原创 2016-04-22 16:28:27 · 1546 阅读 · 0 评论 -
LeetCode[Array]---- 4Sum
4SumGiven an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2016-04-19 11:28:08 · 901 阅读 · 0 评论 -
LeetCode[Array]----3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each i原创 2016-04-19 10:03:10 · 853 阅读 · 0 评论 -
LeetCode[Array]----3Sum
3SumGiven 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.Note:Elements in a tr原创 2016-04-18 21:20:11 · 814 阅读 · 0 评论 -
LeetCode----Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowes原创 2016-03-08 14:44:40 · 727 阅读 · 0 评论 -
LeetCode----Lowest Common Ancestor of a Binary Search Tree
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wiki原创 2016-03-08 14:00:40 · 602 阅读 · 0 评论 -
LeetCode----Remove Nth Node From End of List
Remove Nth Node From End of ListGiven a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After re原创 2016-02-29 09:19:46 · 603 阅读 · 0 评论 -
LeetCode----Maximum Product Subarray
Maximum Product SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous原创 2016-02-27 15:44:38 · 690 阅读 · 0 评论 -
LeetCode----Product of Array Except Self
Product of Array Except SelfGiven an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].原创 2016-02-26 21:05:36 · 678 阅读 · 0 评论 -
LeetCode----Isomorphic Strings
Isomorphic StringsGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character原创 2015-12-02 09:05:44 · 939 阅读 · 0 评论 -
LeetCode----Insertion Sort List
Insertion Sort ListSort a linked list using insertion sort.原题链接为:https://leetcode.com/problems/insertion-sort-list分析:使用插入排序对链表进行排序。可以新建一个带头节点的有序链表,每次从原链表中选择一个节点,插入到带头节点的链表中。C++代码:...原创 2015-11-14 17:16:52 · 628 阅读 · 0 评论 -
LeetCode----Sort List
Sort ListSort a linked list in O(n log n) time using constant space complexity.分析:给链表排序,要求O(nlogn)的时间,O(1)的空间。链表不是数组,给它排序不能像数组一样可以通过各种下标操作。能够想到的方法也就是(1)通过新建一个有序链表,每次从原来链表中选择一个元素到新的有序原创 2015-11-14 15:42:18 · 576 阅读 · 0 评论 -
LeetCode----Happy Number
Happy NumberWrite an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by th原创 2015-11-11 10:21:52 · 664 阅读 · 0 评论 -
LeetCode----Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for an原创 2015-11-10 18:46:35 · 584 阅读 · 0 评论 -
LeetCode----Generate Parentheses
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())原创 2015-11-10 15:59:44 · 1306 阅读 · 0 评论 -
LeetCode----Sort Colors
Sort Colors 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原创 2015-09-10 08:10:39 · 575 阅读 · 0 评论 -
LeetCode----First Missing Positive
First Missing PositiveGiven an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run原创 2015-09-09 19:50:50 · 551 阅读 · 0 评论 -
LeetCode----Merge k Sorted Lists
Merge k Sorted ListsMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.分析1:利用上一道题(合并两个有序链表)代码,进行两两合并,最后得到一个链表。代码如下,然而一直超时。超时的代码:原创 2015-09-07 23:06:15 · 592 阅读 · 0 评论 -
LeetCode----Merge Two Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.分析:合并两个有序的链表。原创 2015-09-07 15:17:21 · 582 阅读 · 0 评论 -
LeetCode----Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or原创 2015-09-06 15:30:40 · 723 阅读 · 0 评论 -
LeetCode----Search a 2D Matrix II
Search a 2D Matrix IIWrite 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 fr原创 2015-09-05 23:16:27 · 599 阅读 · 0 评论 -
LeetCode----Insert Position
Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assu原创 2015-09-05 22:43:01 · 550 阅读 · 0 评论 -
LeetCode----Search for a Range
Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).原创 2015-09-04 23:13:15 · 631 阅读 · 0 评论 -
LeetCode----Integer to Roman
Integer to RomanGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.分析:Roman to Integer的逆转换,逆着来,难度比上一道题难了一些。问题的核心是如何处理一些数比如40,90,900原创 2015-09-03 21:12:01 · 608 阅读 · 0 评论 -
LeetCode----Roman To Integer
Roman to Integer Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.分析:给定罗马字符串,输出该串对应的十进制阿拉伯数。首先我们要了解罗马数字的表示规则。从百科上我找到了:原创 2015-09-03 15:45:23 · 600 阅读 · 0 评论 -
LeetCode----String to Integer (atoi)
String to Integer (atoi)注:要开始研究僧生活了,希望能在这一年里多编码,努力提高编程水平,提高算法水平。题目链接:https://leetcode.com/problems/string-to-integer-atoi/#Implement atoi to convert a str原创 2015-09-01 16:56:27 · 634 阅读 · 0 评论 -
LeetCode----Remove Element
Remove ElementGiven an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyon原创 2015-09-10 20:04:17 · 539 阅读 · 0 评论 -
LeetCode----House Robber
House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is t原创 2015-09-13 09:55:33 · 618 阅读 · 0 评论 -
LeetCode----House RobberII
House Robber IINote: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get to原创 2015-09-15 20:39:16 · 560 阅读 · 0 评论 -
LeetCode----Climbing Stairs
Climbing StairsYou are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析:原创 2015-09-15 23:37:21 · 642 阅读 · 0 评论 -
LeetCode----Triangle
TriangleGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2],原创 2015-09-17 08:52:51 · 673 阅读 · 0 评论 -
LeetCode----Perfect Squares
Perfect SquaresGiven 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原创 2015-09-23 19:22:45 · 1204 阅读 · 0 评论 -
LeetCode----Longest Valid Parentheses
Longest Valid ParenthesesGiven a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest va原创 2015-09-24 20:08:02 · 592 阅读 · 0 评论