一、給定乙個陣列和滑動視窗的大小,找出所有滑動視窗裡數值的最大值。
例如,如果輸入陣列及滑動視窗的大小3
那麼一共存在6個滑動視窗,他們的最大值分別為;
針對陣列的滑動視窗有以下6個:
, ,
, ,
, 。
public
class
solution
//pos用於存放最大值的位置
int max = 0, pos = 0;
for(int i = 0; i < size; i++)
}res.add(max);
for(int i = size; i < num.length; i++)
res.add(max);
}else
}res.add(max);}}
return res;
}}
二、請設計乙個函式,用來判斷在乙個矩陣中是否存在一條包含某字串所有字元的路徑。
路徑可以從矩陣中的任意乙個格仔開始,每一步可以在矩陣中向左,向右,向上,向下移動乙個格仔。
如果一條路徑經過了矩陣中的某乙個格仔,則該路徑不能再進入該格仔。
例如 a b c e
s f c s
a d e e
矩陣中包含一條字串」bcced」的路徑,但是矩陣中不包含」abcb」路徑
public
class
solution
boolean vis = new
boolean[rows * cols];
//每個點都有可能是起點
for(int i = 0; i < rows; i++) }}
return
false;
}public
boolean
haspath(char matrix, int rows, int cols, char str, int x, int y, boolean vis, int pos)
//訪問過
if(vis[index])
//和字串不匹配
if(matrix[index] != str[pos])
//結束條件
if(pos == str.length - 1)
vis[index] = true;
//------------開始深入
if(haspath(matrix, rows, cols, str, x - 1, y, vis, pos+ 1) ||
haspath(matrix, rows, cols, str, x + 1, y, vis, pos+ 1) ||
haspath(matrix, rows, cols, str, x, y - 1, vis, pos+ 1) ||
haspath(matrix, rows, cols, str, x, y + 1, vis, pos+ 1))
//------------回溯
vis[index] = false;
return
false;
}}
三、地上有乙個m行和n列的方格。
乙個機械人從座標0,0的格仔開始移動,每一次只能向左,右,上,下四個方向移動一格,
但是不能進入行座標和列座標的數字之和大於k的格仔。
例如,當k為18時,機械人能夠進入方格(35,37),因為3+5+3+7 = 18。
但是,它不能進入方格(35,38),因為3+5+3+8 = 19。
請問該機械人能夠達到多少個格仔?
public
class
solution
public
intdfs(boolean vis, int threshold, int rows, int cols, int x, int y)
//訪問過不再算
if(vis[x][y])
//不能進入的不算
if(illegal(threshold, x, y))
//自身這個點可以算
int res = 1;
//訪問過的點無需再回溯了,已經統計進來了
vis[x][y] = true;
res += dfs(vis, threshold, rows, cols, x - 1, y);
res += dfs(vis, threshold, rows, cols, x + 1, y);
res += dfs(vis, threshold, rows, cols, x, y - 1);
res += dfs(vis, threshold, rows, cols, x, y + 1);
return res;
}public
boolean
illegal(int threshold, int x, int y)
while(y != 0)
if(sum > threshold) else
}}
面試1 3劍指offer
面試題1 賦值運算子函式 注意點 1 返回值為引用,最後return this,只有返回乙個引用才能連續賦值。2 傳入引數為常量引用,減少一次複製建構函式,提高 效率。const引用防止修改。3 釋放例項自身已有記憶體,防止記憶體洩露。4 傳入引數和當前例項是否為同一例項。同個則不賦值直接返回。同個...
劍指offer程式設計
二維陣列中的查詢 題目描述 在乙個二維陣列中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成乙個函式,輸入這樣的乙個二維陣列和乙個整數,判斷陣列中是否含有該整數。在乙個二維陣列中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成乙個函式,...
劍指程式設計(6)
一 輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 class randomlistnode public class solution ...