![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode 题解
文章平均质量分 71
ActiveCoder
An active programmer
展开
-
Facebook Phone Interview -- Move Zeros to Right (Easy)
March 22th, 2016.Move Zeros to the right end but keep the order of non-zero numbers.#include #inlcude #include using namespace std;// move Zeros to the right end of the array.// classi原创 2016-03-23 07:13:27 · 287 阅读 · 0 评论 -
LeetCode 229. Majority Element II
Given an integer array of size n, find all elements that appear more than⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Extended version of http://blog.csdn.net/github_34原创 2016-04-11 22:51:09 · 175 阅读 · 0 评论 -
LeetCode 41. First Missing Positive
Given 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 in O(n) time and uses constant spa原创 2016-04-12 00:13:00 · 200 阅读 · 0 评论 -
LeetCode 221. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0R原创 2016-04-12 07:24:02 · 192 阅读 · 0 评论 -
LeetCode 2. Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2016-04-12 07:43:00 · 189 阅读 · 0 评论 -
LeetCode 43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.Separate the steps of multiple. It is原创 2016-04-12 08:19:26 · 270 阅读 · 0 评论 -
LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi原创 2016-04-12 08:33:43 · 175 阅读 · 0 评论 -
LeetCode 17. Letter Combinations of a Phone Number
DFS, Repeat of http://blog.csdn.net/github_34333284/article/details/50960151原创 2016-04-12 08:37:25 · 182 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?Let's analyse it a bit. the length of the linked list has two case: odd and原创 2016-04-12 08:54:29 · 214 阅读 · 0 评论 -
LeetCode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.N原创 2016-04-25 11:43:05 · 193 阅读 · 0 评论 -
LeetCode 128. Longest Consecutive Sequence
/** Given an unsorted array of integer, find the length of the longest consecutive elements sequence. For example: Given [100, 4, 200, 1, 3, 2] The longest consecutive element is [1, 2, 3, 4原创 2016-04-08 11:08:49 · 208 阅读 · 0 评论 -
LeetCode 137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using原创 2016-04-09 00:51:52 · 204 阅读 · 0 评论 -
LeetCode 73. 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.// Only provides in-place method. The trick is to remember the 0s in the first row and first column. vo原创 2016-04-09 06:15:39 · 185 阅读 · 0 评论 -
LeetCode 74. Search a 2D Matrix
Write 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 from left to right.The first integer of each原创 2016-04-09 06:55:35 · 189 阅读 · 0 评论 -
LeetCode 189. Rotate Array
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]. Note:Try to come up as many solutions as you ca原创 2016-04-09 07:15:54 · 193 阅读 · 0 评论 -
LeetCode 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The原创 2016-04-09 07:31:35 · 173 阅读 · 0 评论 -
LeetCode 283. 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原创 2016-04-09 07:53:37 · 179 阅读 · 0 评论 -
LeetCode 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right whichminimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2016-04-09 08:15:17 · 216 阅读 · 0 评论 -
LeetCode 238. Product of Array Except Self
#include #include using namespace std;// Given an array of n integers where n > 1, nums, return an array ouput such that output[i] is equal to the product of all the elements of nums except nums[原创 2016-04-06 23:58:32 · 254 阅读 · 0 评论 -
LeetCode 48. Rotate Image
Same as the CRCK one: http://blog.csdn.net/github_34333284/article/details/51002597原创 2016-04-09 08:23:46 · 185 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-04-09 08:43:16 · 186 阅读 · 0 评论 -
LeetCode 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.The logic is very cl原创 2016-04-09 09:28:13 · 344 阅读 · 0 评论 -
LeetCode 80. Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five e原创 2016-04-12 23:04:28 · 160 阅读 · 0 评论 -
LeetCode 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.Need to pay attention to the K va原创 2016-04-12 23:21:16 · 195 阅读 · 0 评论 -
LeetCode 21. 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.I personally prefer to add a dummy node. But need to原创 2016-04-12 23:54:03 · 178 阅读 · 0 评论 -
LeetCode 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists: A: a1 → a2 ↘原创 2016-04-13 00:08:33 · 171 阅读 · 0 评论 -
LeetCode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less thanx come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2016-04-13 00:21:12 · 195 阅读 · 0 评论 -
LeetCode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1This is to apply recursion. TreeNode* invertTree(TreeNode* root)原创 2016-04-13 06:15:59 · 168 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 /原创 2016-04-13 06:38:49 · 197 阅读 · 0 评论 -
LeetCode 143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4原创 2016-04-13 08:18:21 · 264 阅读 · 0 评论 -
LeetCode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b"],原创 2016-04-26 00:07:28 · 206 阅读 · 0 评论 -
LeetCode 148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.// Merge sort linked list. ListNode* mergeLists(ListNode* l1, ListNode* l2) { if(!l2) return l1; if(!l1原创 2016-04-26 00:32:03 · 190 阅读 · 0 评论 -
LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"原创 2016-04-26 01:29:27 · 227 阅读 · 0 评论 -
LeetCode 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 ton2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2016-04-26 11:13:34 · 172 阅读 · 0 评论 -
LeetCode 278. First Bad Version
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 the原创 2016-04-26 11:41:53 · 197 阅读 · 0 评论 -
LeetCode 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the array [2,3,1原创 2016-04-09 23:52:56 · 170 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal tom + n) to hold addit原创 2016-04-10 00:28:43 · 207 阅读 · 0 评论 -
LeetCode 49. Group Anagrams
Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note:For t原创 2016-04-10 07:42:41 · 185 阅读 · 0 评论 -
LeetCode 227. Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. The integer division should原创 2016-04-10 09:52:39 · 197 阅读 · 0 评论 -
LeetCode 125. Valid Palindrome
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 pa原创 2016-04-10 10:11:22 · 187 阅读 · 0 评论