从0开始刷剑指Offer(20~30]

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

思路一:暴力扫描O(n)

class Solution {
    public int[] trainingPlan(int[] actions) {
        int[] q = new int[actions.length];
        int len = actions.length;
        int k = 0;
        for(int i = 0; i < len; i ++ ){
            if(actions[i] % 2 == 1) {
                q[k ++] = actions[i];
            }
        }
        for(int i = 0; i < len; i ++ ){
            if(actions[i] % 2 == 0) {
                q[k ++] = actions[i];
            }
        }
        return q;
    }
}

思路二:双指针扫描O(n)

class Solution {
    public int[] trainingPlan(int[] actions) {
        for(int l = 0, r = actions.length - 1; l < r;) {
            if(l < r && actions[l] % 2 == 1) l ++;
            if(l < r && actions[r] % 2 == 0) r --;
            int temp = actions[l];
            actions[l] = actions[r];
            actions[r] = temp;
        }
        return actions;
    }
}

剑指 Offer 22. 链表中倒数第k个节点

思路:链表O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode trainingPlan(ListNode head, int cnt) {
        int len = 0;
        ListNode dummy = head;
        while(head != null) {
            len ++;
            head = head.next;
        }
        cnt = len - cnt;
        while(cnt -- > 0) {
            dummy = dummy.next;
        }
        return dummy;
    }
}

剑指 Offer 24. 反转链表

思路一: (双指针,迭代)O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode trainningPlan(ListNode head) {
     ListNode pre = null;
     ListNode dummy = head;
     while (dummy != null ) {
         ListNode temp = dummy.next;
         dummy.next = pre;
         pre = dummy;
         dummy = temp;
     }   
     return pre;
    }
}

思路二: 递归O(n)

class Solution {
    public ListNode trainningPlan(ListNode head) {
        return recur(head, null);    // 调用递归并返回
    }
    private ListNode recur(ListNode cur, ListNode pre) {
        if (cur == null) return pre; // 终止条件
        ListNode res = recur(cur.next, cur);  // 递归后继节点
        cur.next = pre;              // 修改节点引用指向
        return res;                  // 返回反转链表的头节点
    }
}

剑指 Offer 25. 合并两个排序的链表

思路:二路归并O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode trainningPlan(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while(l1 != null && l2 != null) {
            if(l1.val <= l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 != null ? l1 : l2;
        return dummy.next;
    }
}

剑指 Offer 26. 树的子结构

思路:(二叉树,递归)O(nm)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) return false;
        if(isSame(A, B)) return true;
        return isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }
    boolean isSame(TreeNode A, TreeNode B) {
        if(B == null) return true;
        if(A == null || A.val != B.val) return false;
        return isSame(A.left, B.left) && isSame(A.right, B.right);
    }
}

剑指 Offer 27. 二叉树的镜像

思路: (二叉树,递归)O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root == null) return null;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }
}

剑指 Offer 28. 对称的二叉树

思路: (二叉树,递归)O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean checkSymmetricTree(TreeNode root) {
        if(root == null) return true;
        return isSame(root.left, root.right);
        
    }
    boolean isSame(TreeNode A, TreeNode B){
        if(A == null && B == null) return true;
        if(A == null || B == null) return false;
        if(A.val != B.val) return false;
        //否则说明当前这个点是匹配的,然后递归判断左子树和右子树是否分别匹配即可
        return isSame(A.left, B.right) && isSame(A.right, B.left);
    }
}

剑指 Offer 29. 顺时针打印矩阵

思路: 模拟O(nm)

class Solution {
    public int[] spiralArray(int[][] array) {
        if(array.length == 0) return new int[]{};
        int l = 0, r = array[0].length - 1, t = 0, b = array.length - 1;
        int x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i ++) res[x ++] = array[t][i];
            if(++t > b) break; //上边界下移
            for(int i = t; i <= b; i ++) res[x ++] = array[i][r];
            if(l > -- r) break; //右边界左移
            for(int i = r; i >= l; i --) res[x ++] = array[b][i];
            if(t > -- b) break;
            for(int i = b; i >= t; i --) res[x ++] = array[i][l];
            if(++l > r) break;
        }
        return res;
    }
}

你可能感兴趣的:(剑指OFFER,java,leetcode,算法)