剑指(五题)

// class Solution {
//     public int[] exchange(int[] nums) {
//         int[] res = new int[nums.length];
//         int t = 0;
//         for (int i = 0; i < nums.length; i++) {
//             if (!method(nums[i])) {
//                 res[t++] = nums[i];
//             }
//         } 
//         for (int i = 0; i < nums.length; i++) {
//             if (method(nums[i])) {
//                 res[t++] = nums[i];
//             }
//         } 
//         return res;
//     }
//     //判断是否是偶数
//     public static boolean method(int n) {
//         if (n % 2 == 0) {
//             return true;
//         } else {
//             return false;
//         }
//     }
// }


//双指针,原地修改
class Solution {
    public int[] exchange(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while (i < j) {
            while (i < j && (nums[i] % 2 != 0)) i++;
            while (i < j && (nums[j] % 2 == 0)) j--;
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
        return nums;
    }
}

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(-1);
        ListNode cur = pre;
        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;
        }
        if (l1 != null) {
            cur.next = l1;
        }
        if (l2 != null) {
            cur.next = l2;
        }
        return pre.next;
    }
}

 

/**
 * 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;
        return method(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);    
    }
    //判断以A为根的和以B为根的是否是一样得结构
    public boolean method(TreeNode A, TreeNode B) {
        
        if (B == null) return true;
        //得先判断null值啊,条件是一步一步往下累加的
        if (A == null && B != null) return false;
        if (A.val != B.val) return false;
        

        return method(A.left, B.left) && method(A.right, B.right);
    }
}
// class Solution {
//     public boolean isSubStructure(TreeNode A, TreeNode B) {
//         //这个函数对这棵树进行前序遍历:即处理根节点,再递归左子节点,再递归处理右子节点
//         //特殊情况是:当A或B是空树的时候 返回false
//         //用||关系可以达到 不同顺序遍历的作用
//         if(A==null||B==null){
//             return false;
//         }
//         return recur(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B);
//     }
//     //此函数的作用是从上个函数得到的根节点开始递归比较 是否是子树
//     boolean recur(TreeNode A, TreeNode B){
//         //结束条件
//         //当最后一层B已经为空的,证明则B中节点全是A中节点
//         if(B==null){
//             return true;
//         }
//         //这里因为有上一个条件,则说明 A已经为空了,B却不为空,则一定不是子数
//         if(A==null){
//             return false;
//         }
//         //处理本次递归,即处理当前节点
//         if(A.val!=B.val){
//             return false;
//         }

//         //递归,同时递归左右两个子节点
//         return recur(A.left,B.left)&&recur(A.right,B.right);
//     }
// }

 

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length == 0) return new int[0];
        int[] res = new int[matrix.length * matrix[0].length];
        int t = 0; 
        int b = matrix.length - 1;
        int l = 0;
        int r = matrix[0].length - 1;

        int tt = 0;
        while (tt < matrix.length * matrix[0].length) {
            for(int i = l; tt < matrix.length * matrix[0].length && i <= r; i++) res[tt++] = matrix[t][i];
            t++;
            for(int i = t; tt < matrix.length * matrix[0].length && i <= b; i++) res[tt++] = matrix[i][r];
            r--;
            for(int i = r; tt < matrix.length * matrix[0].length && i >= l; i--) res[tt++] = matrix[b][i];
            b--;
            for(int i = b; tt < matrix.length * matrix[0].length && i >= t; i--) res[tt++] = matrix[i][l];
            l++; 
        }
        return res;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值