| 難度 | 題目 | 知識點 |
|---|---|---|
| 07. 斐波那契數列 | 遞推遞回 - 兩變數寫法- | |
| 08. 跳臺階 | 同上 | |
| 09. 變態跳臺階 | dp | |
| 10. 矩形覆寫 | 同上 | |
| 05. 用兩個堆疊實作佇列 | 模擬 | |
| ☆ | 20. 包含min函式的堆疊 | 堆疊 |
| 21. 堆疊的壓入彈出序列 | 模擬出堆疊序列 | |
| 65. 矩陣中的路徑 | 回溯 | |
| 66. 機器人的運動范圍 | dfs 求連通塊大小 |
07 - 10 斐波那契數列 - 遞推遞回 - 兩變數寫法
07. 斐波那契數列
T7:大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項為0), n<=39,
class Solution {
public:
int Fibonacci(int n) {
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
return Fibonacci(n-1)+Fibonacci(n-2);
}
};
08. 跳臺階
T8:一只青蛙一次可以跳上1級臺階,也可以跳上2級,求該青蛙跳上一個n級的臺階總共有多少種跳法(先后次序不同算不同的結果),
// 兩變數
class Solution {
public:
int jumpFloor(int number) {
if(number==0)return 1;
if(number==1)return 1;
if(number==2)return 2;
int f=1,g=2;
number-=2;
while(number--){
g=f+g;
f=g-f;
}
return g;
}
};
09. 變態跳臺階
T9:一只青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級,求該青蛙跳上一個n級的臺階總共有多少種跳法,
// dp
class Solution {
public:
int jumpFloorII(const int number) {
int** dp=new int*[number+10];
for(int i=0;i<number+10;i++){
dp[i]=new int[number+10];
}
memset(dp,0,sizeof dp);
for(int i=1;i<=number;i++){
dp[1][i]=1;
}
// dp[i][j] 用i步跳上臺階j
for(int i=2;i<=number;i++){
for(int j=i;j<=number;j++){
for(int k=i-1;k<j;k++){
dp[i][j]+=dp[i-1][k];
}
}
}
int ans=0;
for(int i=1;i<=number;i++){
ans+=dp[i][number];
}
return ans;// 回傳的變數打錯,不可原諒,,
}
};
10. 矩形覆寫
T10:我們可以用2*1的小矩形橫著或者豎著去覆寫更大的矩形,請問用n個2*1的小矩形無重疊地覆寫一個2*n的大矩形,總共有多少種方法?
類似于前面幾題,
class Solution {
public:
int rectCover(int number) {
if(number==0) return 0;
if(number==1) return 1;
if(number==2) return 2;
int f=1,g=2;
number-=2;
while(number--){
g=f+g;
f=g-f;
}
return g;
}
};
05. 用兩個堆疊實作佇列
用兩個堆疊來實作一個佇列,完成佇列的Push和Pop操作, 佇列中的元素為int型別,
堆疊1接收入佇列元素,堆疊2存盤出佇列元素,當堆疊2空時,把堆疊1元素倒到堆疊2中,
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.size()==0){
while(!stack1.empty()){
int x=stack1.top();
stack1.pop();
stack2.push(x);
}
}
int x=stack2.top();
stack2.pop();
return x;
}
private:
stack<int> stack1;
stack<int> stack2;
};
20. 包含min函式的堆疊
堆疊
題目描述
定義堆疊的資料結構,請在該型別中實作一個能夠得到堆疊中所含最小元素的min函式(時間復雜度應為O(1)),
每次對壓入一對元素(當前元素和目前的最小值),
Java Code
import java.util.Stack;
public class Solution {
private Stack st=new Stack<>();
public void push(int node) {
int min=node;
if(!st.empty()) min=Math.min(min,st.peek());
st.push(node);
st.push(min);
}
public void pop() {
st.pop();
st.pop();
}
public int top() {
int x=st.peek();
st.pop();
int y=st.peek();
st.push(x);
return y;
}
public int min() {
return st.peek();
}
}
更省空間的做法如下:
應用一個輔助堆疊,壓的時候,如果A堆疊的壓入比B堆疊壓入大,B堆疊不壓,,,,小于等于,AB堆疊同時壓入,出堆疊,如果,AB堆疊頂元素不等,A出,B不出,
21. 堆疊的壓入彈出序列
模擬
題目描述
輸入兩個整數序列,第一個序串列示堆疊的壓入順序,請判斷第二個序列是否可能為該堆疊的彈出順序,假設壓入堆疊的所有數字均不相等,例如序列1,2,3,4,5是某堆疊的壓入順序,序列4,5,3,2,1是該壓堆疊序列對應的一個彈出序列,但4,3,5,1,2就不可能是該壓堆疊序列的彈出序列,(注意:這兩個序列的長度是相等的)
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0)
return false;
Stack<Integer> s = new Stack<Integer>();
//用于標識彈出序列的位置
int popIndex = 0;
for(int i = 0; i< pushA.length;i++){
s.push(pushA[i]);
//如果堆疊不為空,且堆疊頂元素等于彈出序列
while(!s.empty() &&s.peek() == popA[popIndex]){
//出堆疊
s.pop();
//彈出序列向后一位
popIndex++;
}
}
return s.empty();
}
}
65. 矩陣中的路徑
回溯
題目描述
請設計一個函式,用來判斷在一個矩陣中是否存在一條包含某字串所有字符的路徑,路徑可以從矩陣中的任意一個格子開始,每一步可以在矩陣中向左,向右,向上,向下移動一個格子,如果一條路徑經過了矩陣中的某一個格子,則該路徑不能再進入該格子, 例如 a b c e s f c s d e e 矩陣中包含一條字串"bcced"的路徑,但是矩陣中不包含"abcb"路徑,因為字串的第一個字符b占據了矩陣中的第一行第二個格子之后,路徑不能再次進入該格子,
dfs回溯,
Java Code
public class Solution {
private int rows, cols;
char[][] G;
char[] str;
boolean[][] vis;
int[] dx = new int[]{1, 0, -1, 0};
int[] dy = new int[]{0, 1, 0, -1};
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (matrix == null || matrix.length < str.length)
return false;
this.str = str;
this.rows = rows;
this.cols = cols;
G = new char[rows][cols];
vis = new boolean[rows][cols];
for (int i = 0; i < matrix.length; i++) {
G[i / cols][i % cols] = matrix[i];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (G[i][j] == str[0]) {
if (str.length == 1) return true;
vis[i][j] = true;
for (int k = 0; k < 4; k++) {
if (check(i + dx[k], j + dy[k])
&& dfs(i + dx[k], j + dy[k], 1))
return true;
}
vis[i][j] = false;
}
}
}
return false;
}
private boolean dfs(int x, int y, int idx) {
if (G[x][y] != str[idx]) return false;
if (idx == str.length - 1) return true;
vis[x][y] = true;
for (int k = 0; k < 4; k++) {
if (check(x + dx[k], y + dy[k])
&& dfs(x + dx[k], y + dy[k], idx + 1))
return true;
}
vis[x][y] = false;
return false;
}
private boolean check(int x, int y) {
return x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y];
}
}
66. 機器人的運動范圍
dfs求連通塊大小
題目描述
地上有一個m行和n列的方格,一個機器人從坐標0,0的格子開始移動,每一次只能向左,右,上,下四個方向移動一格,但是不能進入行坐標和列坐標的數位之和大于k的格子, 例如,當k為18時,機器人能夠進入方格(35,37),因為3+5+3+7 = 18,但是,它不能進入方格(35,38),因為3+5+3+8 = 19,請問該機器人能夠達到多少個格子?
dfs求連通塊大小,注意要用vis陣列避免重復計數,
Java Code
public class Solution {
int cnt=0;
boolean [][]vis;
public int movingCount(int threshold, int rows, int cols)
{
vis=new boolean[rows][cols];
dfs(0,0,rows,cols,threshold);
return cnt;
}
private void dfs(int x,int y,int rows,int cols,int thrsh){
if(!check(x,y,rows,cols,thrsh)) return;
cnt++;
vis[x][y]=true;
dfs(x,y+1,rows,cols,thrsh);
dfs(x+1,y,rows,cols,thrsh);
dfs(x,y-1,rows,cols,thrsh);
dfs(x-1,y,rows,cols,thrsh);
}
private boolean check(int x,int y,int rows,int cols,int thrsh){
if(x<0||x>=rows||y<0||y>=cols||vis[x][y]) return false;
int sum=0;
while(x>0){ sum+=x%10;x/=10; }
while(y>0){ sum+=y%10;y/=10; }
return sum<=thrsh;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/137176.html
標籤:其他
上一篇:圖論篇5——關鍵路徑
下一篇:桶排序
