- 博客(56)
- 资源 (2)
- 收藏
- 关注
原创 找长字符串中的最长回文
Manacher算法复杂度O(N)Python代码:# -*- coding: utf-8 -*-# manacher算法,求出字符串中的最长回文半径,复杂度为O(N)s = list('aaaaaassaaaaa')print len(s)s.insert(0, '$')for i in range(1, 2 * len(s), 2): s.insert
2017-08-12 22:50:53
226
原创 从长度为M的无序数组中找出N个最大的数
1、将数组前N个数调整成最小堆2、堆顶元素值依次与第N个元素以后的每个元素进行比较3、若堆顶元素值小,则将堆顶元素重新赋值为新元素的值,并且将前N个数重新调整为最小堆;否则判断下一个元素4、直到遍历完原数组的最后一个元素后,则最小堆中的元素即为待求的数组中的N个最大的数复杂度为O(M*log(N))python代码:# -*- coding: utf-8 -
2017-08-08 21:26:12
5004
原创 求数组中任意连续位置元素和的最大值
求数组中任意连续位置元素和的最大值a = [-2,3,-4,1,6,-7]def consecutive_maxsum(elem_list): if len(elem_list) == 0: return max_sum = p = elem_list[0] for i in range(1, len(elem_list)): if
2017-08-02 22:06:17
1071
原创 Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answ
2017-04-11 12:55:42
230
原创 Best Time to Buy and Sell Stock with Cooldown
Share my DP solution (By State Machine Thinking)Hi,I just come across this problem, and it's very frustating since I'm bad at DP.So I just draw all the actions that can be done.Here is t
2017-04-10 17:18:26
251
原创 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the tota
2017-04-07 21:01:45
157
原创 Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3
2017-04-07 10:43:08
226
原创 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5# Definition for singly-li
2017-04-05 10:28:41
144
原创 Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 001110010
2017-04-04 22:21:03
169
原创 Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".class Solution {public: string addBinary(string a, string b) { string
2017-04-04 21:39:19
220
原创 Nth Digit
How many digits of size size can we have?1 * 9 (size 1, 1... 9)2 * 90 (size 2, 10... 99)3 * 900 (size 3, 100... 999)So we can "fast-skip" those numbers until we find the size of the number
2017-04-04 20:06:32
150
原创 Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of th
2017-04-03 17:53:25
203
原创 malloc/free and new/delete
相同点:都可用于申请动态内存和释放内存不同点:(1)操作对象有所不同。malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符。对于非内部数据类的对象而言,光用maloc/free 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数, 对象消亡之前要自动执行析构函数。由于malloc/free 是库函数而不是运算符,不在编译器控制权限之内,不
2017-03-01 10:42:11
159
原创 Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are: ["1->2->5", "1->3"]/** * Definiti
2017-02-27 22:22:31
177
原创 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15,
2017-02-23 22:15:46
189
原创 Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. Example 1:Input: ["Hello", "Alaska", "D
2017-02-21 17:23:40
406
原创 Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in t
2017-02-21 15:44:08
243
原创 Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot
2017-02-15 17:34:26
205
原创 Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2]. Note:Each element in the result must be unique.The result
2017-02-15 17:32:00
170
原创 Single Number
Given an array of integers, every element appears twice except for one. Find that single one.class Solution(object): def singleNumber(self, nums): """ :type nums: List[int]
2017-02-15 17:28:00
130
原创 Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3. Python solution with no "+-*/%", completely bit mani
2017-02-15 17:26:18
118
原创 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.My code of C++, Depth-first-search
2017-02-15 17:24:59
176
原创 Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was ad
2017-02-15 17:22:42
149
原创 Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one
2017-02-15 17:20:04
159
原创 Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1def invertTree(self, root): if root: root.left, root.rig
2017-02-15 17:17:34
137
原创 Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your fu
2017-02-15 17:14:39
270
原创 Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a coo
2017-02-15 17:06:46
157
原创 Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementingn - 1 elements by 1.Example: Input:[1,2,3]Ou
2017-02-15 16:57:13
160
原创 Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the
2017-02-13 12:23:18
230
原创 Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra
2017-02-13 12:19:24
126
原创 Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
2017-02-13 12:17:28
140
原创 First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples: s = "leetcode"return 0.s = "loveleetcode",return 2.class Solu
2017-02-13 12:15:10
133
原创 Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third nodewith valu
2017-02-13 12:11:22
168
原创 Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z
2017-02-13 12:05:32
132
原创 Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are consi# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):
2017-02-13 12:04:05
124
原创 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appearsmore than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alw
2017-02-13 12:02:55
137
原创 Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not consi
2017-02-13 12:01:39
288
原创 Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent theminutes (0-59).Each LED represents a zero or one, with the least significant bit on
2017-02-13 11:59:14
150
原创 Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run
2017-02-13 11:57:05
192
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人