LeetCode题解——C/C++版
文章平均质量分 61
LeetCode上题目的一些解题报告,不一定是最优解。大部分题目都会用纯C编写,但有时也会用到STL。
nudt_oys
An AC a day,keeps WA away~
展开
-
LeetCode 399 Evaluate Division(BFS)
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-11-07 17:43:31 · 707 阅读 · 0 评论 -
LeetCode 146 LRU Cache(list+unordered_map实现LRU缓存算法)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key i原创 2017-10-28 12:17:53 · 982 阅读 · 0 评论 -
LeetCode 414 Third Maximum Number(set + priority_queue)
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,原创 2017-08-29 16:58:32 · 569 阅读 · 0 评论 -
LeetCode 103 Binary Tree Zigzag Level Order Traversal(二叉树层序遍历)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tr原创 2017-08-11 21:09:14 · 882 阅读 · 0 评论 -
LeetCode 155 Min Stack(实现具有特殊操作的栈)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2017-08-10 13:04:42 · 857 阅读 · 0 评论 -
LeetCode 654 Maximum Binary Tree(构建二叉搜索树)
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array.The left subtree is the maximum tree constructed f原创 2017-08-06 19:11:58 · 1078 阅读 · 0 评论 -
LeetCode 304 Range Sum Query 2D - Immutable(维护二维数组的前缀和)
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).The above rectangle (with the red bo原创 2017-08-04 18:37:10 · 885 阅读 · 0 评论 -
LeetCode 303 Range Sum Query - Immutable(维护前缀和)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2017-08-03 19:17:45 · 820 阅读 · 0 评论 -
LeetCode 307 Range Sum Query - Mutable(树状数组 || 线段树)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val.Example:Given原创 2017-07-30 21:18:55 · 876 阅读 · 0 评论 -
LeetCode 108 Convert Sorted Array to Binary Search Tree(二分 + 递归建树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题目大意:给出一个已经排好序的数组,构建一棵二叉搜索树。解题思路:采用二分法,递归地构建BST。注意二分结束条件。代码如下:/** * Definition for a binary原创 2017-07-24 21:26:23 · 574 阅读 · 0 评论 -
LeetCode 646 Maximum Length of Pair Chain(贪心)
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b . Chain原创 2017-07-23 13:58:20 · 938 阅读 · 0 评论 -
LeetCode 637 Average of Levels in Binary Tree(二叉树层序遍历)
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanat原创 2017-07-21 19:05:21 · 1276 阅读 · 0 评论 -
LeetCode 513 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-07-19 19:49:55 · 400 阅读 · 0 评论 -
LeetCode 515 Find Largest Value in Each Tree Row(二叉树层序遍历)
You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]题目大意:给出一棵二叉树,求出每一层的最原创 2017-07-18 19:44:42 · 343 阅读 · 0 评论 -
LeetCode 232 Implement Queue using Stacks(利用栈实现队列)
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2017-07-18 18:56:06 · 548 阅读 · 0 评论 -
LeetCode 225 Implement Stack using Queues(利用队列实现栈)
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2017-07-18 13:21:46 · 1279 阅读 · 0 评论 -
LeetCode 319 Bulb Switcher(数学Tricks)
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off原创 2017-07-17 13:53:37 · 650 阅读 · 0 评论 -
LeetCode 198 House Robber(基础DP)
You 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 that adjacent house原创 2017-07-15 21:29:17 · 751 阅读 · 0 评论 -
LeetCode 199 Binary Tree Right Side View(二叉树层序遍历)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2017-07-14 22:41:47 · 744 阅读 · 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,null,null,15,7], 3 / \ 9 20原创 2017-07-13 19:51:18 · 940 阅读 · 0 评论 -
LeetCode 129 Sum Root to Leaf Numbers(递归求和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total原创 2017-07-12 20:04:54 · 947 阅读 · 0 评论 -
LeetCode 563 Binary Tree Tilt(递归 + 中间结果保存)
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree原创 2017-06-30 19:59:05 · 455 阅读 · 0 评论 -
LeetCode 38 Count and Say(字符串生成)
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read o原创 2017-07-08 08:47:08 · 3001 阅读 · 1 评论 -
LeetCode 98 Validate Binary Search Tree(判断二叉搜索树)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The r原创 2017-07-05 20:14:38 · 919 阅读 · 0 评论 -
LeetCode 20 Valid Parentheses(用栈判断括号匹配)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all vali原创 2017-07-04 19:42:48 · 1340 阅读 · 0 评论 -
LeetCode 14 Longest Common Prefix(最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings.题目大意:写一个函数,找出一组字符串的最长公共字串。解题思路:以第一个字符串为参照,逐个比较第一个字符串的第i个字符和其他字符串的第i个字符,直到遇到不相等的字符退出循环。代码如下:char* longestCo原创 2017-07-03 19:58:54 · 688 阅读 · 0 评论 -
LeetCode 538 Convert BST to Greater Tree(二叉树的右中左遍历)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.Exampl原创 2017-06-29 19:19:44 · 531 阅读 · 0 评论 -
LeetCode 1 Two Sum
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-05-31 18:30:28 · 516 阅读 · 0 评论 -
LeetCode 606 Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair "()". And you原创 2017-06-29 09:11:29 · 380 阅读 · 0 评论 -
LeetCode 617 Merge Two Binary Trees(递归合并二叉树)
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 tree原创 2017-06-27 23:51:40 · 2467 阅读 · 0 评论 -
LeetCode 257 Bianry 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"]题目大意:给出一个二叉树,返回其所原创 2017-06-25 23:28:08 · 589 阅读 · 0 评论 -
LeetCode 507 Perfect Number(完美数字)
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 perfec原创 2017-05-29 14:57:34 · 2472 阅读 · 0 评论 -
LeetCode 94 Binary Tree Inorder Traversal(二叉树中序遍历)
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solution is原创 2017-05-28 09:29:13 · 846 阅读 · 0 评论 -
LeetCode 144 Binary Tree Preorder Traversal(二叉树前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is tr原创 2017-05-28 09:48:56 · 665 阅读 · 0 评论 -
LeetCode 145 Binary Tree Postorder Traversal(二叉树后序遍历)
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is t原创 2017-05-28 09:56:05 · 614 阅读 · 0 评论 -
LeetCode 450 Delete Node in a BST(删除BST节点)
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can be divided int原创 2017-05-24 16:12:37 · 594 阅读 · 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.题目大意:合并两个有序链表,要求新链表的节点由合并之前的两个链表拼接而成。解题思路:类似于归并排序,利用二级指原创 2017-04-19 09:23:49 · 535 阅读 · 0 评论 -
LeetCode 23 Merge k Sorted Lists(归并)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题目大意:合并k个有序链表并分析时间复杂度。解题思路:每两个链表进行一次合并,再对合并好的链表重复上述过程。k个链表需要合并O(k)次,每次合并需要O(n)的时间,所以总的时间复杂度为O(kn)。原创 2017-05-19 22:00:41 · 951 阅读 · 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 ↘原创 2017-05-19 17:09:29 · 665 阅读 · 0 评论 -
LeetCode 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.题目大意:给出一个二叉树,求从根节点到叶节点的最小深度。解题思路:直接D原创 2017-05-19 11:10:55 · 642 阅读 · 0 评论