用两个栈实现队列的功能

用两个栈实现队列的功能思路如下:(1)将设有两个空栈A,B;A负责入队,B负责出队。(2)入队时,将元素压入栈A(3)出队时,若B栈为空,将A中的元素全部出栈,并压入B中,然后B的栈顶元素出栈。若B栈不为空,直接将B的栈顶元素出栈。

[LeetCode] Contains Duplicate II

Contains Duplicate II Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference be...

心愿



   

北京东路的日子



   

[LeetCode] Reorder List

Reorder ListGiven 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 {...

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

Binary Tree Preorder TraversalGiven a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},   1    ...

[LeetCode] Insertion Sort List



Insertion Sort ListSort a linked list using insertion sort.解题思路:类似于插入排序法。与数组的插入排序法不同的是,链表的插入排序扫描顺序是从左往右扫描,找到第一个大于指定元素的节点N,然后将之插在N节点前面。今天状态非常不好,这个题目都NG了好几遍。。。/**  * Definition for ...

[LeetCode] Maximum Subarray

Maximum SubarrayFind 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 subarra...

[LeetCode] Jump Game

Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine ...

[LeetCode] Pow(x, n)

Pow(x, n) Implement pow(x, n).解题思路:这里n是整数,注意可能是负数,因此需要先将其转化成正数。若直接用循环的方法做,会产生超时错误。我们可以这么做。比如求2^6,我们需要求2^(2*3)=4^3=4*4^2,这样时间复杂度为O(logn)。注意几个特殊的情况,当n==1时,返回x本身,当n==0时,返回1,当n为偶数时,返回myPow(x*x, ...