本文按照??途W(wǎng)的順序,牛客網(wǎng)劍指offer刷題網(wǎng)址:https://www.nowcoder.com/ta/coding-interviews
本文涉及的題目:
1、用兩個棧實現(xiàn)隊列
2、旋轉(zhuǎn)數(shù)組中的最小數(shù)字
3、斐波那契數(shù)列
4、跳臺階
5、變態(tài)跳臺階
6、矩形覆蓋
1、用兩個棧實現(xiàn)隊列
問題描述
用兩個棧來實現(xiàn)一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。
思路解析
定義兩個stack,分別是stack1和stack2,隊列的push和pop是在兩側(cè)的,push操作很簡單,只需要在stack1上操作,而pop操作時,先將stack1的所有元素push到stack2中,然后stack2的pop返回的元素即為目標元素,然后把stack2中的所有元素再push到stack1中。
代碼實現(xiàn)
java
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
int temp;
while(!stack1.empty()){
temp = stack1.pop();
stack2.push(temp);
}
int res = stack2.pop();
while(!stack2.empty()){
temp = stack2.pop();
stack1.push(temp);
}
return res;
}
}
python
class Solution:
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
# return xx
if not self.stack1:
return None
while self.stack1:
self.stack2.append(self.stack1.pop())
res = self.stack2.pop()
while self.stack2:
self.stack1.append(self.stack2.pop())
return res
2、旋轉(zhuǎn)數(shù)組中的最小數(shù)字
問題描述
把一個數(shù)組最開始的若干個元素搬到數(shù)組的末尾,我們稱之為數(shù)組的旋轉(zhuǎn)。 輸入一個非遞減排序的數(shù)組的一個旋轉(zhuǎn),輸出旋轉(zhuǎn)數(shù)組的最小元素。 例如數(shù)組{3,4,5,1,2}為{1,2,3,4,5}的一個旋轉(zhuǎn),該數(shù)組的最小值為1。 NOTE:給出的所有元素都大于0,若數(shù)組大小為0,請返回0。
思路解析
從頭到尾兩兩相鄰元素進行比較進行,如果前面一個元素大于后面一個元素,則返回后面一個元素。如果從頭到尾都沒有滿足條件的元素,則返回第一個元素。
代碼實現(xiàn)
java
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length==0){
return 0;
}
for(int i=0;i<array.length-1;i++){
if(array[i] > array[i+1]){
return array[i+1];
}
}
return array[0];
}
}
python
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if not rotateArray:
return 0
for i in range(len(rotateArray)-1):
if rotateArray[i] > rotateArray[i+1]:
return rotateArray[i+1]
return rotateArray[0]
3、斐波那契數(shù)列
問題描述
大家都知道斐波那契數(shù)列,現(xiàn)在要求輸入一個整數(shù)n,請你輸出斐波那契數(shù)列的第n項。n<=39
思路解析
只需要定義兩個整形變量,b表示后面的一個數(shù)字,a表示前面的數(shù)字即可。每次進行的變換是:temp = a,a=b,b=temp + b
代碼實現(xiàn)
java
public class Solution {
public int Fibonacci(int n) {
if (n<=0)
return 0;
int a=1,b = 1;
int temp;
for(int i=2;i<n;i++){
temp = a;
a = b;
b = temp + b;
}
return b;
}
}
python
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
# write code here
if n<=0:
return 0
a = b = 1
for i in range(2,n):
a,b = b,a+b
return b
4、跳臺階
問題描述
一只青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法。
思路解析
一道典型的動態(tài)規(guī)劃問題:
我們用f(n)表示跳上n級臺階的跳法。
可以看出,f(1)=1;f(2)=2。
那么,假設到了n級臺階,那么上一步就有兩種情況,跳一步跟跳兩步。
如果是跳一步跳上了n級臺階,那么他上一步在n-1級臺階,那么有多少種方法跳到n-1級呢,顯然是f(n-1),同理,如果跳兩步,那么上一步在n-1級臺階,此時方法種數(shù)是f(n-1),所以總數(shù)是f(n)=f(n-1)+f(n-2)。
反向思考,但是編寫代碼的時候要正向求解,首先根據(jù)f(1)和f(2)計算出f(3),再根據(jù)f(2)和f(3)計算出f(4)…..一次類推計算出第n項。很容易理解這種思路的時間復雜度是O(n).
代碼實現(xiàn)
java
public class Solution {
public int JumpFloor(int target) {
if(target <= 0)
return 0;
if(target <= 2)
return target;
int a=1,b=2;
int temp;
for(int i=3;i<=target;i++){
temp = a;
a = b;
b += temp;
}
return b;
}
}
python
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
if number <= 0:
return 0
if number == 1:
return 1
if number == 2:
return 2
res = [1,2]
for i in range(2,number):
res.append(res[-1] + res[-2])
return res[-1]
5、變態(tài)跳臺階
問題描述
一只青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。
思路解析
和普通跳臺階問題同樣的思路,反向思考,正向?qū)懘a。我們用f(n)表示跳上n級臺階的跳法。那么,假設到了n級臺階,我們可以一步上來,也可以從第一級跳n-1級上來,從第二級跳n-2級上來。。。從n-1級跳一步上來,所以f(n) = sum(f(1) + f(2) +...+f(n-1)) + 1
代碼實現(xiàn)
java
public class Solution {
public int JumpFloorII(int target) {
if(target<=0)
return 0;
int sumPath = 0;
int path = 0;
for(int i=0;i<target;i++){
path = sumPath + 1;
sumPath = sumPath * 2 + 1;
}
return path;
}
}
python
# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
if number <= 0:
return 0
res = []
sumPath = 0
for i in range(0,number):
res.append(sumPath + 1)
sumPath = sumPath * 2 + 1
return res[-1]
6、矩形覆蓋
問題描述
我們可以用2*1的小矩形橫著或者豎著去覆蓋更大的矩形。請問用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
思路解析
我們用f(n)表示覆蓋2*n的矩形的方法數(shù)。
可以看出,f(1)=1;f(2)=2。
那么,假設到了n,那么上一步就有兩種情況,在n-1的時候,豎放一個矩形,或著是在n-2時,橫放兩個矩形(這里不能豎放兩個矩形,因為放一個就變成了n-1,那樣情況就重復了),所以總數(shù)是f(n)=f(n-1)+f(n-2)。
反向思考,但是編寫代碼的時候要正向求解,首先根據(jù)f(1)和f(2)計算出f(3),再根據(jù)f(2)和f(3)計算出f(4)…..一次類推計算出第n項。很容易理解這種思路的時間復雜度是O(n).
代碼實現(xiàn)
java
public class Solution {
public int RectCover(int target) {
if(target <= 0)
return 0;
if(target <= 2)
return target;
int a=1,b=2;
int temp;
for(int i=3;i<=target;i++){
temp = a;
a = b;
b += temp;
}
return b;
}
}
python
# -*- coding:utf-8 -*-
class Solution:
def rectCover(self, number):
# write code here
if number == 0:
return 0
if number<=2:
return number
res = [1,2]
for i in range(2,number):
res.append(res[-1] + res[-2])
return res[-1]