[LeetCode]112. 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]h...
[LeetCode]110. Find Minimum in Rotated Sorted Array旋转数组最小值 Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the...
[LeetCode]109. Construct Binary Tree from Inorder and Postorder Traversal由中序序列和后序序列重建二叉树... Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解法:这题和由前序序列和中序序列重建二叉树一样,解法也相同,不过是先根据后序序列的最后一个元素为树根,再...
[LeetCode]105. Word Search单词查找 Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertica...
[LeetCode]103. Find the Duplicate Number重复值查找 Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate numbe...
[LeetCode]102. Product of Array Except Self数组乘积 Given an array ofnintegers wheren> 1,nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Solve itwithout divisionand in...
[LeetCode]101. Trapping Rain Water收集雨水 Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1...
[LeetCode]114. 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?解法1:不断除去n的因子3,最后判断是否为1class Solution {public:...
[LeetCode]113. 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.解法1:很简单的DFS递归。/** * Defi...
[LeetCode]97. 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 ...
[LeetCode]96. Min Stack带Min函数的栈 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(...
[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值II Follow upfor "Find Minimum in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pi...
[LeetCode]92. Linked List Cycle判断链表是否有环 Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Subscribeto see which companies asked this question解法1:使用两个指针p1, p...
[LeetCode]108. Construct Binary Tree from Preorder and Inorder Traversal由前序序列和中序序列重建二叉树... Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解法:前序遍历的第一个元素为树根,因为树中无重复元素,因此遍历一遍可以在中序序列中找出树根所在位置,于是把...
[LeetCode]107. Best Time to Buy and Sell Stock III股票买卖III Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may n...
[LeetCode]91. Rotate List旋转链表 Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.S...
[LeetCode]89. Partition List链表划分 Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each...
[LeetCode]104. Basic Calculator II基本计算器 Implement a basic calculator to evaluate a simple expression string.The expression string contains onlynon-negativeintegers,+,-,*,/operators and empty spaces. The integer division shoul...
[LeetCode]106. Best Time to Buy and Sell Stock II股票买卖II Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy...
[LeetCode]81. 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?Subscribeto see which companies asked this question解...