LeetCode
道非凡
努力升级中的凡人
展开
-
LeetCode - 617. Merge Two Binary Trees
Q: 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.You need to merge them into a new binary tre原创 2017-09-04 22:03:11 · 195 阅读 · 0 评论 -
LeetCode - 35. Search Insert Position
Q: 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.H原创 2017-09-09 09:32:22 · 182 阅读 · 0 评论 -
LeetCode - 83. Remove Duplicates from Sorted List
Q: Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.A:# Definition for singly-li原创 2017-09-09 09:57:04 · 140 阅读 · 0 评论 -
LeetCode - 387. First Unique Character in a String
Q: 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.Note: You may assu原创 2017-08-31 20:07:35 · 139 阅读 · 0 评论 -
LeetCode - 237. Delete Node in a Linked List
Q: 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 node with value原创 2017-08-31 20:25:44 · 160 阅读 · 0 评论 -
LeetCode - 206. Reverse Linked List
Q: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both?A:# Definition for singly-linked list.class ListNode(object): de原创 2017-08-31 23:41:20 · 134 阅读 · 0 评论 -
LeetCode - 671. Second Minimum Node In a Binary Tree
Q: Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s原创 2017-09-10 10:44:19 · 263 阅读 · 0 评论 -
LeetCode - 217. Contains Duplicate
Q: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2017-09-01 22:46:38 · 139 阅读 · 0 评论 -
LeetCode - 268. Missing Number
Q: 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 r原创 2017-09-01 23:39:51 · 123 阅读 · 0 评论 -
LeetCode - 415. Add Strings
Q: Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: 1. The length of both num1 and num2 is < 5100. 2. Both num1 and num2 contai原创 2017-09-01 23:48:18 · 140 阅读 · 0 评论 -
LeetCode - 1. Two Sum
Q: Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same原创 2017-09-02 10:06:09 · 174 阅读 · 0 评论 -
LeetCode - 507. Perfect Number
Q: We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.Now, given an integer n, write a function that returns true when it is a perfe原创 2017-09-03 14:53:44 · 150 阅读 · 0 评论 -
LeetCode - 414. Third Maximum Number
Q: Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). Example 1:Input: [3, 2, 1]原创 2017-09-03 15:56:19 · 168 阅读 · 0 评论 -
LeetCode - 7. Reverse Integer
Q: Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the revers原创 2017-09-03 18:41:42 · 133 阅读 · 0 评论 -
LeetCode - 69. Sqrt(x)
Q: Implement int sqrt(int x).Compute and return the square root of x.A:class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ return原创 2017-09-12 20:54:55 · 178 阅读 · 0 评论 -
LeetCode - 204. Count Primes
Q: Description:Count the number of prime numbers less than a non-negative number, n.A:class Solution(object): def countPrimes(self, n): """ :type n: int :rtype: int原创 2017-09-12 21:35:06 · 141 阅读 · 0 评论 -
LeetCode - 168. Excel Sheet Column Title
Q: Given 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 -> AA 28 -> AB A:class Soluti原创 2017-09-12 22:02:27 · 186 阅读 · 0 评论 -
LeetCode - 125. Valid Palindrome
Q: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a p原创 2017-09-12 23:19:24 · 145 阅读 · 0 评论 -
LeetCode - 541. Reverse String II
Q: 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 t原创 2017-09-09 09:11:45 · 189 阅读 · 0 评论 -
LeetCode - 653. Two Sum IV - Input is a BST
Q: Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1:Input: 5 / \ 3 6 / \ \2原创 2017-09-08 23:34:09 · 148 阅读 · 0 评论 -
LeetCode - 171. Excel Sheet Column Number
Q: Given a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 A:class Solution(obj原创 2017-09-08 19:52:13 · 161 阅读 · 0 评论 -
LeetCode - 561. Array Partition I
Q: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as p原创 2017-09-04 22:57:43 · 142 阅读 · 0 评论 -
LeetCode - 575. Distribute Candies
Q: Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute th原创 2017-09-05 19:39:47 · 168 阅读 · 0 评论 -
LeetCode - 566. Reshape the Matrix
Q: In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dime原创 2017-09-05 20:06:57 · 145 阅读 · 0 评论 -
LeetCode - 669. Trim a Binary Search Tree
Q: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re原创 2017-09-05 22:09:53 · 177 阅读 · 0 评论 -
LeetCode - 136. Single Number
Q: 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 extr原创 2017-09-05 22:53:42 · 118 阅读 · 0 评论 -
LeetCode - 278. First Bad Version
Q: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on原创 2017-09-13 20:38:53 · 278 阅读 · 0 评论 -
LeetCode - 189. Rotate Array
Q: Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].A:class Solution(object): def rotate(self, nums,原创 2017-09-13 21:26:31 · 167 阅读 · 0 评论 -
LeetCode - 367. Valid Perfect Square
Q: Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input: 16Returns: True原创 2017-09-13 21:52:52 · 166 阅读 · 0 评论 -
LeetCode - 26. Remove Duplicates from Sorted Array
Q: Given 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 another array, you must do this in place with原创 2017-09-13 22:37:43 · 181 阅读 · 0 评论 -
LeetCode - 485. Max Consecutive Ones
Q: Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-09-06 21:38:08 · 125 阅读 · 0 评论 -
LeetCode - 104. Maximum Depth of Binary Tree
Q: 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.A:# Definition for a binary tree node.原创 2017-09-06 22:16:12 · 130 阅读 · 0 评论 -
LeetCode - 448. Find All Numbers Disappeared in an Array
Q: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could原创 2017-09-06 22:57:45 · 125 阅读 · 0 评论 -
LeetCode - 532. K-diff Pairs in an Array
Q: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on原创 2017-09-14 22:18:43 · 350 阅读 · 0 评论 -
LeetCode - 389. Find the Difference
Q: 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 added原创 2017-09-07 21:09:21 · 143 阅读 · 0 评论 -
LeetCode - 283. Move Zeroes
Q: 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 f原创 2017-09-07 21:46:45 · 255 阅读 · 0 评论 -
LeetCode - 349. Intersection of Two Arrays
Q: Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: 1. Each element in the result must be unique. 2. The result原创 2017-09-07 21:57:35 · 144 阅读 · 0 评论 -
LeetCode - 169. Majority Element
Q: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alw原创 2017-08-30 23:48:11 · 149 阅读 · 0 评论 -
LeetCode 001 - 003
LeetCode 001 TwoSumLeetCode 002 Add Two NumbersLeetCode 003 无重复字符的最长子串原创 2018-06-24 09:48:02 · 289 阅读 · 0 评论