![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode 总结
文章平均质量分 53
mashuangwe
这个作者很懒,什么都没留下…
展开
-
求数组中任意连续位置元素和的最大值
求数组中任意连续位置元素和的最大值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 · 1057 阅读 · 0 评论 -
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 · 107 阅读 · 0 评论 -
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 · 165 阅读 · 0 评论 -
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 · 136 阅读 · 0 评论 -
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 · 146 阅读 · 0 评论 -
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 · 122 阅读 · 0 评论 -
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 · 254 阅读 · 0 评论 -
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 · 147 阅读 · 0 评论 -
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 · 149 阅读 · 0 评论 -
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 · 399 阅读 · 0 评论 -
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 · 235 阅读 · 0 评论 -
Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely原创 2017-02-12 20:58:13 · 161 阅读 · 0 评论 -
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 · 116 阅读 · 0 评论 -
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 · 157 阅读 · 0 评论 -
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 · 132 阅读 · 0 评论 -
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 · 157 阅读 · 0 评论 -
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 · 209 阅读 · 0 评论 -
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 · 141 阅读 · 0 评论 -
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 · 192 阅读 · 0 评论 -
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 · 222 阅读 · 0 评论 -
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 · 242 阅读 · 0 评论 -
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 · 148 阅读 · 0 评论 -
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 · 218 阅读 · 0 评论 -
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 · 179 阅读 · 0 评论 -
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 · 189 阅读 · 0 评论 -
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 · 223 阅读 · 0 评论 -
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 · 118 阅读 · 0 评论 -
Power of Two
Given an integer, write a function to determine if it is a power of two.class Solution(object): def isPowerOfTwo(self, n): """ :type n: int :rtype: bool """原创 2017-02-13 10:59:49 · 116 阅读 · 0 评论 -
Search Insert Position
Given 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 assume no duplicates in the array.原创 2017-02-13 10:58:24 · 97 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3# Definition for singly-lin原创 2017-02-13 10:54:33 · 133 阅读 · 0 评论 -
Climbing Stairs
You 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?Note: Given n will be a positi原创 2017-02-13 10:43:01 · 146 阅读 · 0 评论 -
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).For example, the 32-bit integer ’11' has binary representation 000000000原创 2017-02-13 10:25:12 · 144 阅读 · 0 评论 -
Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example,6, 8 are ugly while 14 is not ugly since i原创 2017-02-13 10:24:05 · 122 阅读 · 0 评论 -
Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without lo原创 2017-02-13 10:20:19 · 129 阅读 · 0 评论 -
Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh". class Solution(object): def reverseString(self, s): """原创 2017-02-12 20:56:56 · 115 阅读 · 0 评论 -
Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”.原创 2017-02-12 20:27:47 · 166 阅读 · 0 评论 -
Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note: 0 ≤ x, y < 231.原创 2017-02-12 18:14:20 · 122 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has原创 2017-02-12 18:04:34 · 118 阅读 · 0 评论 -
Happy Number
Write 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 the sum of the squares o原创 2017-02-13 11:02:30 · 133 阅读 · 0 评论 -
Power of Three
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion? class Solution(object): def isPowerOfThree(self, n原创 2017-02-13 11:06:06 · 221 阅读 · 0 评论