剑指offer 1

1 二维数组中的查找

public class Solution {
    public boolean Find(int target, int [][] array) {
        if (array.length == 0) {
            return false;
        }
        int len1 = array.length;//求行
        int len2 = array[0].length;//求列
        for (int j = len2 - 1; j >= 0; j--) {
            for (int i = 0; i <= len1 - 1; i++) {
                if (target < array[i][j]) {
                    break;
                }else if (target > array[i][j]) {
                    continue;
                }else {
                    return true;
                }
            }
        }
        return false;
    }
}

2 替换字符串

public class Solution {
    public String replaceSpace(StringBuffer str) {
        if (str == null) {
            return null;
        }
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == ' ') {
                str.replace(i,i+1,"%20");
            }
        }
        return str.toString();
    }
}

3 从尾到头打印链表

import java.util.Stack;//注意添加包
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if (listNode == null) {
            ArrayList<Integer> list = new ArrayList<>();
            return list;
        }
        Stack<Integer> stack = new Stack<>();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> list = new ArrayList<>();
        while (!stack.isEmpty()) {
            list.add(stack.pop());
        }
        return list;
    }
}

4 重建二叉树

/**
 * 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 == 0||in.length == 0){
            return null;
        }
        TreeNode node = new TreeNode(pre[0]);
        for(int i = 0; i < in.length; i++){
            if(pre[0] == in[i]){
                node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
                node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1,in.length));
            }
        }
        return node;
    }
    
}

5 两个栈实现队列

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() {
        if (stack2.isEmpty()) {//只有当stack2为空时才一次性将stack1中的数据全部压入
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

6 旋转数组的最小数字

//二分查找
import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if (array.length == 0) {
            return 0;
        }
        int low = 0;
        int high = array.length - 1;
        while (low < high) {
            int mid = (low + high) / 2;
            if (array[mid] > array[high]) {//注意是拿mid和high比
                low = mid + 1;
            }else if (array[mid] < array[high]) {
                high = mid;
            }else {
                high -= 1;
            }
        }
        return array[low];
    }
}

7 斐波那契数列

//1,1,2,3,5,8,13,21
public class Solution {
    public int Fibonacci(int n) {
        if (n == 0) {
            return 0;
        }
        int a = 1, b = 1, c = 0;
        if (n == 1 || n == 2) {
            return 1;
        }else {
            for (int i = 3; i <= n; i++) {
                c = a + b;
                a = b;
                b = c;
            }
        }
        return c;
    }
}

8 跳台阶

//变版斐波那契数列:1,2,3,5,8,13,21
//当前台阶的跳法总数=当前台阶后退一阶的台阶的跳法总数+当前台阶后退二阶的台阶的跳法总数
//n级=n-1级的每个方法后面添上1,n-2级的每个方法后面填上2

public class Solution {
    public int JumpFloor(int target) {
        if (target <= 0) {
            return 0;
        }
        if (target == 1) {
            return 1;
        }
        if (target == 2) {
            return 2;
        }
        int a = 1;
        int b = 2;
        int c = 0;
        for (int i = 3; i <= target; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
}

9 变态跳台阶

//n = 之前所以方法的和再加1,
//1,2,4,8,16,32
public class Solution {
    public int JumpFloorII(int target) {
        if (target <= 0) {
            return 0;
        }
        if (target == 1) {
            return 1;
        }
        if (target == 2) {
            return 2;
        }
        int sum = 3;
        int c = 0;
        for (int i = 3; i <= target; i++) {
            sum = sum + c;
            c = sum + 1;
        }
        return c;
    }
}

10 矩形覆盖

//变斐波那契:1,2,3,5,8,13,21;同跳台阶
public class Solution {
    public int RectCover(int target) {
        if (target <= 0) {
            return 0;
        }
        if (target == 1) {
            return 1;
        }
        if (target == 2) {
            return 2;
        }
        int a = 1;
        int b = 2;
        int c = 0;
        for (int i = 3; i <= target; i++) {
            c = a + b;
            a = b; 
            b = c;
        }
        return c;
    }
}

11 二进制中1的个数

//一个数减去1再和这个数做位与运算,结果是将这个数  最  右边的那位1置0.一直操作直到这个数为0,这样有几个1就操作几次
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while (n != 0) {//java中必须为boolean表达式
            count++;
            n = (n - 1) & n;
        }
        return count;
    }
}

12 数值的整数次方

import java.util.*;
public class Solution {
    public double Power(double base, int exponent) {
        if (base ==0.0 & exponent < 0) {//如果底数为0,且指数为负数,需要取倒数,但0不能做分母。
            return 0.0;
        }
        int abs = Math.abs(exponent);//取绝对值
        double result = pow(base, abs);//求指数为绝对值时的结果
        if (exponent < 0) {
            return 1 / result;
        }
        return result;
  }
    public double pow(double base, int abs) {
        if (abs == 0) {//绝对值为0,返回1.0
            return 1.0;
        }
        if (abs == 1) {//绝对值为1,返回底数
            return base;
        }
        double re = pow(base,abs >> 1);
        re *= re;
        if ((abs & 1) == 1) {//奇数要多乘个base,
            return re *= base;
        }
        return re;
    }
}

13 奇数位于偶数前

//快排,不能保证相对位置不变。
//low奇数,low向后走直到遇到偶数。
//high是偶数,high向前走直到遇到奇数。
//如果low在high前,交换。
public class Solution {
    public void reOrderArray(int [] array) {
        if (array.length == 0) {
            return;
        }        
        int low = 0;
        int high = array.length - 1;
        while (low < high) {
            while ((array[low] & 1) == 1) {//位与比求余效率高
                low += 1;
            }
            while ((array[high] & 1) == 0) {
                high -= 1;
            }
            if (low < high) {
                int temp = array[low];
                array[low] = array[high];
                array[high] = temp;
            }
        }
    }
}
//插入排序,保证相对位置不变
//第1个数先不管,i从第2个数开始向后走,先找到第1个奇数
//j=i,找到j前一个偶数,j向前走,一次换
//2,4,6,5,7:i先走到5,j也定位到5,5和6,4,2依次换
public class Solution {
    public void reOrderArray(int [] array) {
        if (array.length == 0) {
            return;
        }
        for (int i = 1; i < array.length; i++) {
            int target = array[i];
            if ((array[i] & 1) == 1) {//如果为奇数
                int j = i;
                while (j >= 1 && (array[j - 1] & 1) == 0 ) {
                    array[j] = array[j - 1];
                    j--;
                }
                array[j] = target;
            }
        }
    }
}

14 输入一个链表,输出该链表中倒数第k个结点。

/*
1.定义两个游标p1,p2,其中p2先走k步,然后p1和p2一起走,当p2走到空时,p1为倒数第k个结点
2.需要验证head为空,k<1,k大于链表长度三种情况
*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if (head == null || k < 1) 
            return null;
        ListNode p1 = head;
        ListNode p2 = head;
        for (int i = 0; i < k; i++) {
            if (p2 == null)//当k大于链表长度时会发生
                return null;
            p2 = p2.next;
        }
        while (p2 != null) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1;           
    }
}

15 反转链表

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode newHead = null;
        ListNode pNode = head;
        ListNode pPre = null;
        while (pNode != null) {
            ListNode pNext = pNode.next;//若pNode一开始为空,这句话写到外面就是错的
            if (pNext == null) {
                newHead = pNode;
            }
            pNode.next = pPre;
            pPre = pNode;
            pNode = pNext;
        }
        return newHead;
    }
}

16 合并两个排序链表

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode node = null;
        if (list1.val <= list2.val) {
            node = list1;
            node.next = Merge(list1.next, list2);
        }else {
            node = list2;
            node.next = Merge(list1, list2.next);
        }
        return node;
    }
}

17 树的子结构

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if (root1 == null || root2 == null) {
            return false;
        }
        boolean flag = false;
        if (root1.val == root2.val) {//如果根值相同才去判断是否为子结构,以这个根点开始
            flag = IsSubtree(root1, root2);//判断是否为子结构的方法
        }
        if (!flag) {//以这个点开始的,不是子结构
            flag = HasSubtree(root1.left, root2);//左子树结点和root2比
            if (!flag) {
                flag = HasSubtree(root1.right, root2);//如果左子树不是,比较右子树
            }
        }
        return flag;
    }
    public boolean IsSubtree(TreeNode root1, TreeNode root2) {
        if (root2 == null) {//右树已比完,左树还有,情况正确
            return true;
        }
        if (root1 == null && root2 != null) {//左树已完,右树还有,错误
            return false;
        }
        if (root1.val == root2.val) {
            return IsSubtree(root1.left, root2.left) && IsSubtree(root1.right, root2.right);
        }else {
            return false;
        }
    }
}

18 树的镜像

public class Solution {
    public void Mirror(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {//叶结点
            return;
        }
        TreeNode pTemp = root.left;//交换root的左右子树
        root.left = root.right;
        root.right = pTemp;
        if (root.left != null) {//还有子结点
            Mirror(root.left);
        }
        if (root.right != null) {
            Mirror(root.right);
        }
    }
}

19 顺时针打印矩阵

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> list = new ArrayList<>();
       if (matrix.length == 0) return list;
       int len1 = matrix.length;
       int len2 = matrix[0].length;
       int layers = (Math.min(len1, len2) + 1) / 2;
        
       for (int i = 0; i < layers; i++) {
           for (int m = i; m < len2 - i; m++) {//注意考虑一列
               list.add(matrix[i][m]);
           }
           for (int n = i + 1; n < len1 - i; n++) {//注意考虑一行
               list.add(matrix[n][len2 - 1 - i]);
           }
           for (int p = len2 - 2 - i; (p >= i) && (len1 - 1 - i != i);p--) {//防止只有一行
               list.add(matrix[len1 - 1 - i][p]);
           }
           for (int k = len1 - 2 - i; (k > i) && (len2 - 1 - i != i);k--) {//防止只有一列
               list.add(matrix[k][i]);
           }
       }
       return list;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容

  • 说明: 本文中出现的所有算法题皆来自牛客网-剑指Offer在线编程题,在此只是作为转载和记录,用于本人学习使用,不...
    秋意思寒阅读 1,143评论 1 1
  • 剑指offer 最近在牛客网上刷剑指offer的题目,现将题目和答案(均测试通过)总结如下: 二维数组的查找 替换...
    闫阿佳阅读 883评论 0 10
  • 刷题啦刷题啦,剑指offer好像比较有名,所以就在牛客网上刷这个吧~btw,刷了一些题发现编程之美的题好典型啊!!...
    Cracks_Yi阅读 414评论 0 1
  • 剑指 offer 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成...
    faremax阅读 2,193评论 0 7
  • 1 二维数组中的查找 2 替换字符串 3 从尾到头打印链表 4 重建二叉树 5 两个栈实现队列 6 旋转数组的...
    Attitudeljh阅读 239评论 0 0