聲明:本博客中的代碼都是自己實現,若有問題,請大家不吝賜教!
面試需要的基礎知識--------數組
【面試題3】 數組中重複的數字
在一個長度爲n的數組裏的所有數字都在0到n-1的範圍內。 數組中某些數字是重複的,但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出數組中任意一個重複的數字。 例如,如果輸入長度爲7的數組{2,3,1,0,2,5,3},那麼對應的輸出是第一個重複的數字2。
分析:因爲數組中數字的範圍是0到n-1,而長度是n,不妨將數組中的元素放到對應的座標下,如若有重複的數字,那麼該值代表的座標下的數字必定已經是該值。那麼就返回該值就可以了。
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 這裏要特別注意~返回任意重複的一個,賦值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(length < 2 || numbers.length != length){
return false;
}
for(int i = 0;i < length; i++){
if(numbers[i]<0 || numbers[i]>length-1){
return false;
}
while(numbers[i] != i){
if(numbers[numbers[i]] == numbers[i]){
duplication[0] = numbers[i];
return true;
}
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
return false;
}
}
【面試題4】 二維數組中查找
在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
分析:只能從右上角或左下角開始查找,否則會有兩個區域無法選擇。
public class Solution {
public boolean Find(int target, int [][] array) {
if(array == null){
return false;
}
int i = 0;
int j = array[0].length-1;
while(i<array.length && j >=0){
if(array[i][j] == target){
return true;
}else if(target > array[i][j]){
i++;
}else{
j--;
}
}
return false;
}
}
面試需要的基礎知識--------字符串
【面試題5】 替換空格
請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
int spaceNum = 0;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == ' '){
spaceNum++;
}
}
int j = str.length() + 2*spaceNum - 1;
char[] result = new char[j+1];
for(int i = str.length()-1; i>=0; i--){
if(str.charAt(i) == ' '){
result[j--] = '0';
result[j--] = '2';
result[j--] = '%';
}else{
result[j--] = str.charAt(i);
}
}
return new String(result);
}
}
【面試題6】 從尾到頭打印鏈表
輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。
分析:遞歸思想,要添加鏈表當前節點的值到數組列表中,先添加鏈表下一節點的值。
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> result = new ArrayList<Integer>();
if(listNode != null){
printCore(listNode,result);
}
return result;
}
public void printCore(ListNode listNode, ArrayList<Integer> result){
if(listNode == null){
return;
}
printCore(listNode.next,result);
result.add(listNode.val);
}
}
面試需要的基礎知識--------樹
【面試題7】 重建二叉樹
輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre==null||in==null){
return null;
}
java.util.HashMap<Integer,Integer> map= new java.util.HashMap<Integer, Integer>();
for(int i=0;i<in.length;i++){
map.put(in[i],i);
}
return preIn(pre,0,pre.length-1,in,0,in.length-1,map);
}
public TreeNode preIn(int[] p,int pi,int pj,int[] n,int ni,int nj,java.util.HashMap<Integer,Integer> map){
if(pi>pj){
return null;
}
TreeNode head=new TreeNode(p[pi]);
int index=map.get(p[pi]);
head.left=preIn(p,pi+1,pi+index-ni,n,ni,index-1,map);
head.right=preIn(p,pi+index-ni+1,pj,n,index+1,nj,map);
return head;
}
}
【面試題8】 二叉樹的下一個節點
給定一個二叉樹和其中的一個結點,請找出中序遍歷順序的下一個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指針。
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode == null){
return null;
}
else if(pNode.right != null){
TreeLinkNode node = pNode.right;
while(node.left != null){
node = node.left;
}
return node;
}else if(pNode.next != null){
int val = pNode.val;
TreeLinkNode parent = pNode.next;
while(parent.left != null && parent.left.val != val){
if(parent.next == null){
break;
}
val = parent.val;
parent = parent.next;
}
if(parent.left != null && parent.left.val == val){
return parent;
}
}
return null;
}
}
面試需要的基礎知識--------棧和隊列
【面試題9】 用兩個棧實現隊列
用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素爲int類型。
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);
// System.out.println(node);
}
public int pop() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
面試需要的基礎知識--------遞歸和循環
【面試題10】 斐波那契數列
大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項爲0)。n<=39
public class Solution {
public int Fibonacci(int n) {
int a = 0;
int b = 1;
if(n == 0){
return a;
}
if(n == 1){
return b;
}
for(int i = 2; i <= n;i++){
int sum = a+b;
a = b;
b = sum;
}
return b;
}
}
引申:青蛙跳臺問題
一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)。
public class Solution {
public int JumpFloor(int target) {
if(target < 1){
return 0;
}
if(target == 1){
return 1;
}
if(target == 2){
return 2;
}
int a = 1;
int b = 2;
for(int i = 3; i <= target; i++){
int sum = a+b;
a = b;
b = sum;
}
return b;
}
}
引申:矩形覆蓋
我們可以用2*1的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
public class Solution {
public int RectCover(int target) {
if(target < 1){
return 0;
}
if(target == 1){
return 1;
}
if(target == 2){
return 2;
}
int a = 1;
int b = 2;
for(int i = 3; i <= target; i++){
int sum = a+b;
a = b;
b = sum;
}
return b;
}
}
面試需要的基礎知識--------查找和排序
【面試題11】 旋轉數組中的最小數字
把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。 輸入一個非減排序的數組的一個旋轉,輸出旋轉數組的最小元素。 例如數組{3,4,5,1,2}爲{1,2,3,4,5}的一個旋轉,該數組的最小值爲1。 NOTE:給出的所有元素都大於0,若數組大小爲0,請返回0。
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length == 0){
return 0;
}
int len = array.length;
if(array[0] < array[len-1]){
return array[0];
}
for(int i = 0; i<len-1; i++){
if(array[i]>array[i+1]){
return array[i+1];
}
}
return array[len-1];
}
}
面試需要的基礎知識--------回溯法
【面試題12】矩陣中的路徑
請設計一個函數,用來判斷在一個矩陣中是否存在一條包含某字符串所有字符的路徑。路徑可以從矩陣中的任意一個格子開始,每一步可以在矩陣中向左,向右,向上,向下移動一個格子。如果一條路徑經過了矩陣中的某一個格子,則之後不能再次進入這個格子。 例如 a b c e s f c s a d e e 這樣的3 X 4 矩陣中包含一條字符串"bcced"的路徑,但是矩陣中不包含"abcb"路徑,因爲字符串的第一個字符b佔據了矩陣中的第一行第二個格子之後,路徑不能再次進入該格子。
需要注意的是:矩陣並不是由二維數組表示,而是由從左到右,從上到下,依次將數組保存到一維數組中。
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
int []flag=new int[matrix.length];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(helper(matrix,rows,cols,i,j,0,str,flag))
return true;
}
}
return false;
}
public boolean helper(char[]matrix,int rows,int cols,int i,int j,int k,char[]str,int[]flag)
{
int index=i*cols+j;
if(i<0||i>=rows||j<0||j>=cols||flag[index]==1||matrix[index]!=str[k])return false;
if(k==str.length-1)return true;
flag[index]=1;
if(helper(matrix,rows,cols,i+1,j,k+1,str,flag)||helper(matrix,rows,cols,i-1,j,k+1,str,flag)||helper(matrix,rows,cols,i,j+1,k+1,str,flag)||helper(matrix,rows,cols,i,j-1,k+1,str,flag))
return true;
flag[index]=0;
return false;
}
}
【面試題13】 機器人的運動範圍
地上有一個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 int movingCount(int threshold, int rows, int cols)
{
if(threshold<0 || rows<=0 || cols<=0){
return 0;
}
boolean[] visited = new boolean[rows*cols];
for(int i = 0; i<rows*cols; i++){
visited[i] = false;
}
return movingCountCore(threshold,rows,cols,0,0,visited);
}
public int movingCountCore(int threshold,int rows,int cols,int row,int col,boolean[] visited){
int count = 0;
if(check(threshold,rows,cols,row,col,visited)){
visited[row*cols+col] = true;
count = 1 + movingCountCore(threshold,rows,cols,row-1,col,visited)
+ movingCountCore(threshold,rows,cols,row+1,col,visited)
+ movingCountCore(threshold,rows,cols,row,col-1,visited)
+ movingCountCore(threshold,rows,cols,row,col+1,visited);
}
return count;
}
public boolean check(int threshold,int rows,int cols,int row,int col,boolean[] visited){
if(row>=0 && col>=0 && row<rows && col<cols && !visited[row*cols+col] && checkSum(row,col,threshold))
return true;
return false;
}
public boolean checkSum(int row,int col,int threshold){
int sum = 0;
while(row > 0){
sum += row%10;
row = row/10;
}
while(col > 0){
sum += col%10;
col = col/10;
}
if(sum > threshold)
return false;
return true;
}
}
面試需要的基礎知識--------位運算
【面試題15】 二進制中1的個數
輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。
分析:n & (n-1) 可以消除掉n的二進制表示中最右端的1.
public class Solution {
public int NumberOf1(int n) {
int result = 0;
while(n != 0){
n = n & (n-1);
result++;
}
return result;
}
}