- Design Circular Quene(设计一个循环队列)-Number622
思路:
使用三个变量,head,tail,count表示队列的首元素位置,尾元素位置的下一位置,队列的个数。
代码:
class MyCircularQueue {
private int[] nums;
private int head = 0;
private int tail =0;
private int count=0;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
nums = new int[k];
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull())
return false;
else{
nums[tail]=value;
tail = (tail+1)%nums.length;
count++;
return true;
}
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()){
return false;
}else{
head = (head+1)%nums.length;
count--;
return true;
}
}
/** Get the front item from the queue. */
public int Front() {
if(isEmpty()){
return -1;
}else{
return nums[head];
}
}
/** Get the last item from the queue. */
public int Rear() {
if(isEmpty())
return -1;
else{
int temp = tail ==0?nums.length-1:tail-1;
return nums[temp];
}
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return count==0;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return count == nums.length;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
- Design Circular Deque(设计一个循环双端队列)-Number641
思路:
该题与设计一个循环队列的思路基本一致
代码:
class MyCircularDeque {
private int[] nums;
private int count = 0;
private int head = 0;
private int tail = 0;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
nums = new int[k];
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull())
return false;
if(head == 0)
head = nums.length-1;
else
head -=1;
nums[head] = value;
count ++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull())
return false;
nums[tail] = value;
count++;
tail = (tail+1)%nums.length;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(isEmpty())
return false;
head = (head+1)%nums.length;
count--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(isEmpty())
return false;
if(tail==0)
tail = nums.length-1;
else
tail -=1;
count--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
if(isEmpty())
return -1;
return nums[head];
}
/** Get the last item from the deque. */
public int getRear() {
if(isEmpty())
return -1;
if(tail ==0)
return nums[nums.length-1];
return nums[tail-1];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return count==0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return count==nums.length;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/
- Climbing Stairs(爬楼梯)-Number70
思路:
首先会想到使用递归来进行计算,爬到第n层要么是从n-1层爬上来的,要么是从n-2层爬上来的,所以f(n)=f(n-1)+f(n-2)
代码:
class Solution {
public int climbStairs(int n) {
if(n==1)
return 1;
if(n==2)
return 2;
return climbStairs(n-1)+climbStairs(n-2);
}
}
但是,当我们提交代码的时候会发现:
运行时间超时,所以还应该借助动态规划来进行计算。
class Solution {
public int climbStairs(int n) {
if(n<=1)
return 1;
int[] dp = new int[n];
dp[0]=1;
dp[1]=2;
for(int i=2;i<n;i++){
dp[i] = dp[i-1]+dp[i-2];
}
return dp[n-1];
}
}