题目描述:
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() -- Return whether the queue is ...
Implement the following operations of a queue using stacks.
Given an integer, write a function to determine if it is a power of two.
给定一个整数,编写函数判断它是否是2的幂。
如果一个整数是2的幂,那么它的二进制形式最高位为1,其余各位为0
等价于:n & (n - 1) = 0,且n > 0
class Solution:
# @param {integer} n
# @return {boolean ...
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Follow up:
What if the BST ...
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.
Hint:
How many majority elements could it possibly ...
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
给定一个链表,判断其中是否有环。
进一步思考:
你可以在不使用额外空间的条件下完成本题吗?
使用“快慢指针”法即可
fast指针每次向前运动两个节点,slow指针每次向前运动一个节点
如果fast和slow在链表的某处相遇,则说明链表中有环
# Definition for singly-linked list ...