博客园 首页 私信博主 显示目录 隐藏目录 管理 动画
摘要: public static void heapSort(int[] nums) { int length = nums.length; heapify(nums); int i = length - 1; while (i >= 1) { swap(nums, 0, i); i--; adjustH 阅读全文
posted @ 2022-01-15 16:31 影梦 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 1 public void Mirror(TreeNode root) { 2 // 如果根节点为空,则直接返回 3 if (root == null) { 4 return; 5 } 6 // 如果根节点两边都为空,则直接返回 7 if (root.left == null && ... 阅读全文
posted @ 2019-04-27 12:01 影梦 阅读(135) 评论(0) 推荐(0) 编辑
摘要: 1 // 遍历整个树 2 public boolean HasSubtree(TreeNode root1, TreeNode root2) { 3 boolean result = false; 4 // 如果root1,root2中只要有一个是null,则返回false 5 if (root1 != null && roo... 阅读全文
posted @ 2019-04-27 11:48 影梦 阅读(163) 评论(0) 推荐(0) 编辑
摘要: 1 public ListNode Merge(ListNode list1, ListNode list2) { 2 // list1,list2中,只要有一个是空的,则返回另外一个链表 3 if (list1 == null) { 4 return list2; 5 } 6 if (list2 ... 阅读全文
posted @ 2019-04-27 10:48 影梦 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 1 public ListNode ReverseList(ListNode head) { 2 if (head == null) { 3 return null; 4 } 5 if (head.next == null) { 6 return head; 7 } 8... 阅读全文
posted @ 2019-04-27 10:34 影梦 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 1 // 思想很巧妙,一个快指针,一个慢指针,二者相隔k-1 2 public ListNode FindKthToTail(ListNode head, int k) { 3 if (head == null || k <= 0) { 4 return null; 5 } 6 ListNode p... 阅读全文
posted @ 2019-04-27 10:23 影梦 阅读(75) 评论(0) 推荐(0) 编辑
摘要: 1 public void reOrderArray(int[] array) { 2 // 如果输入的数组为空,则直接返回return 3 // 如果输入的数组内容为空,则直接返回return 4 if (array == null || array.length == 0) { 5 return; 6 ... 阅读全文
posted @ 2019-04-27 10:15 影梦 阅读(96) 评论(0) 推荐(0) 编辑
摘要: 1 public double Power(double base, int exponent) { 2 // exponent正负都一样,如果为负数,最终结果取倒数 3 // 为了方便,取其绝对值 4 int n=Math.abs(exponent); 5 // 下面两个if是递归终止条件 6 if(n==0... 阅读全文
posted @ 2019-04-27 10:05 影梦 阅读(104) 评论(0) 推荐(0) 编辑
摘要: 1 public int NumberOf1(int n) { 2 // 统计1的个数 3 int count = 0; 4 // flag表示二进制的1 5 int flag = 1; 6 // 1、flag最多只能有32位1,超过32位之后, 7 // 2、最高位的1就会移出去,就变成0 ... 阅读全文
posted @ 2019-04-27 09:57 影梦 阅读(113) 评论(0) 推荐(0) 编辑
摘要: // 本质是斐波那契数列 public int JumpFloor(int target) { // 如果台阶数小于2,则返回1 if (target < 2) { return 1; } int f1 = 1; int f2 = 1; // 台阶数从2开始,一直到t... 阅读全文
posted @ 2019-04-25 18:55 影梦 阅读(83) 评论(0) 推荐(0) 编辑