- 调整数组顺序使奇数位于偶数前面
public static void reorder(int[] arr){
int[] aclo = arr.clone();
int cnt=0;
for(int val:arr){
if(val%2==0){
cnt++;
}
}
int i = 0, j = cnt;
for(int val:arr){
if (val % 2 == 1)
arr[i++] = val;
else
arr[j++] = val;
}
}
- 合并两个排序的链表
public ListNode Merge(ListNode list1, ListNode list2) {
if (list1 == null)
return list2;
if (list2 == null)
return list1;
if (list1.val <= list2.val) {
list1.next = Merge(list1.next, list2);
return list1;
} else {
list2.next = Merge(list1, list2.next);
return list2;
}
}
- 树的子结构
public static boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null || root2==null) return false;
//下面其实包含三部分,root1==root2那么要同时满足root1.left,root2.left与root1.right,root2.right。root1!=root2那么从root1的左树开始找,或从root1的右树开始找
return (hassubtreecore(root1.left,root2.left)&&hassubtreecore(root1.right,root2.right)) || hassubtreecore(root1.left,root2) || hassubtreecore(root1.right,root2);
}
public static boolean hassubtreecore(TreeNode root1,TreeNode root2) {
if(root2==null) return true;
if(root1 ==null) return false;
if(root1.val!= root2.val) return false;
return hassubtreecore(root1.left,root2.left)||hassubtreecore(root1.right,root2.left);
}