2019-05-24(剑指offer13~18题)

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
我的做法是空间换时间用了两个辅助数组,一个存奇数一个存偶数
public class Solution {
    public void reOrderArray(int [] array) {
        int[] odd = new int[array.length];
        int[] even = new int[array.length];
        int ok = 0, ek = 0;
        for(int x : array){
            if(x % 2 == 0) even[ek ++] = x;
            else odd[ok ++] = x;
        }
        int k = 0;
        for(int i = 0; i < ok; i ++) array[k ++] = odd[i];
        for(int i = 0; i < ek; i ++) array[k ++] = even[i];
    }
}
输入一个链表,输出该链表中倒数第k个结点。https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
先算出链表长度,再确定位置
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            len ++;
            cur = cur.next;
        }
        if(k < 1 || k > len) return null;
        cur = head;
        for(int i = 0; i < len - k; i ++) cur = cur.next;
        return cur;
    }
}
输入一个链表,反转链表后,输出新链表的表头。https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
用两个节点存pre,next,然后就指前面,指后面就行了
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode pre = null;
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
比较两个链表头谁小放谁,放了后往右移一位
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val) {
                cur.next = new ListNode(list1.val);
                list1 = list1.next;
            }else{
                cur.next = new ListNode(list2.val);
                list2 = list2.next;
            }
            cur = cur.next;
        }
        while(list1 != null){
            cur.next = new ListNode(list1.val);
            list1 = list1.next;
            cur = cur.next;
        }
        while(list2 != null){
            cur.next = new ListNode(list2.val);
            list2 = list2.next;
            cur = cur.next;
        }
        return dummy.next;
    }
}
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88?tpId=13&tqId=11170&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
遍历A数发现元素相同就进去比较
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1 == null || root2 == null) return false;
        if(dfs(root1, root2) == true) return true;
        else return HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
    }
    public boolean dfs(TreeNode root1, TreeNode root2){
        if(root2 == null) return true;
        else if(root1 == null || root1.val != root2.val) return false;
        else return dfs(root1.left, root2.left) && dfs(root1.right, root2.right);
    }
}
操作给定的二叉树,将其变换为源二叉树的镜像。https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
交换左右子树,递归操作
public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null) return;
        TreeNode node = root.left;
        root.left = root.right;
        root.right = node;
        Mirror(root.left);
        Mirror(root.right);
    }
}

你可能感兴趣的:(2019-05-24(剑指offer13~18题))