1.在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
public class Solution {
public boolean Find(int target, int [][] array) {
int h = array.length;
int l = array[0].length;
if(array != null && h > 0 && l > 0){
int hang = 0;
int lie = l - 1;
while(hang < h && lie >= 0){
if(array[hang][lie] == target){
return true;
}else if(array[hang][lie] < target){
hang ++;
}else{
lie --;
}
}
}
return false;
}
}
2.請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
String string = "";
for (char c:str.toString().toCharArray()
) {
if(c == ' '){
string += "%20";
}else{
string += c;
}
}
return string;
}
}
3.輸入一個鏈表,按鏈表從尾到頭的順序返回一個ArrayList。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<>();
while(listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> list = new ArrayList<Integer>();
while(!stack.isEmpty()){
list.add(stack.pop());
}
return list;
}
}
4.輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length < 1){
return null;
}
int rootVal = pre[0];
int rootIndex = 0;
for(int i = 0;i < in.length; i++){
if(in[i] == rootVal){
rootIndex = i;
break;
}
}
TreeNode root = new TreeNode(rootVal);
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,rootIndex+1),Arrays.copyOfRange(in,0,rootIndex));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,rootIndex+1,pre.length),Arrays.copyOfRange(in,rootIndex+1,in.length));
return root;
}
}
5.用兩個棧來實現一個隊列,完成隊列的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);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
int first = stack2.pop();
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return first;
}
}
6.把一個數組最開始的若干個元素搬到數組的末尾,我們稱之爲數組的旋轉。
輸入一個非遞減排序的數組的一個旋轉,輸出旋轉數組的最小元素。
例如數組{3,4,5,1,2}爲{1,2,3,4,5}的一個旋轉,該數組的最小值爲1。
NOTE:給出的所有元素都大於0,若數組大小爲0,請返回0.
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length == 0){
return 0;
}
if(array.length == 1){
return array[0];
}
for(int i = 0; i < array.length - 1;i++){
if(array[i] > array[i+1]){
return array[i+1];
}else{
if(i == array.length - 2){
return array[0];
}
}
}
return 0;
}
}
7.大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項爲0)。 n<=39
public class Solution {
public int Fibonacci(int n) {
if(n<=0){
return 0;
}
if(n == 1 || n == 2){
return 1;
}
int a = 1;
int b = 1;
int c = 0;
for(int i = 3;i <= n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}
}
8.一隻青蛙一次可以跳上1級臺階,也可以跳上2級。求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)。
public class Solution {
public int JumpFloor(int target) {
if (target <= 0){
return 0;
}
if(target == 1){
return 1;
}
if(target == 2){
return 2;
}
return JumpFloor(target -1) + JumpFloor(target -2);
}
}
9.一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。
解法一
public class Solution {
public int JumpFloorII(int target) {
if(target <= 0){
return 0;
}
if(target == 1){
return 1;
}
return 2 * JumpFloorII(target - 1);
}
}
f(1) = 1
f(2) = f(2-1) + f(2-2) //f(2-2) 表示2階一次跳2階的次數。
f(3) = f(3-1) + f(3-2) + f(3-3)
…
f(n) = f(n-1) + f(n-2) + f(n-3) + … + f(n-(n-1)) + f(n-n)
f(n) = f(n-1)+f(n-2)+…+f(n-(n-1)) + f(n-n) =>f(0) + f(1) + f(2) + f(3) + … + f(n-1)
f(n-1) = f(0) + f(1)+f(2)+f(3) + … + f((n-1)-1) = f(0) + f(1) + f(2) + f(3) + … + f(n-2)
f(n) = f(0) + f(1) + f(2) + f(3) + … + f(n-2) +
f(n-1) = f(n-1) + f(n-1)
可以得出:
f(n) = 2*f(n-1)
解法二
經過計算,發現跳1,2,3,4…級臺階有1,2,4,8…種方法,方法個數爲2的臺階個數減一次方.
public class Solution {
public int JumpFloorII(int target) {
if(target <= 0){
return 0;
}
if(target == 1){
return 1;
}
return (int)Math.pow(2.,target - 1);
}
}
10.我們可以用21的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個21的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
public class Solution {
public int RectCover(int target) {
if (target <= 0){
return 0;
}
if (target == 1){
return 1;
}
if(target == 2){
return 2;
}
return RectCover(target -1) + RectCover(target - 2);
}
}