- 博客(12)
- 收藏
- 关注
原创 红黑树的插入与删除
本文内容搬运了一篇博客以及java TreeMap实现的源码,还有一些自己的体会。本以为红黑树的插入操作就挺难实现了,结果看到了红黑树的删除操作,发现自己还是太年轻了,首先看一下红黑树的性质:每个结点要么是红的要么是黑的。 根结点是黑的。 每个叶结点(叶结点即指树尾端NIL指针或NULL结点)都是黑的。 如果一个结点是红的,那么它的两个儿子都是黑的。 对于任意结点而言,其到叶
2016-06-22 20:58:39 281
转载 《Algorithms》——删除二叉查找树中的结点
在算法这本书上看到删除BST结点的方法,思考可以发现删除一个只有左子结点或者右子结点的结点是非常容易的,只需要返回它不为空的那个结点即可,但是如果左右结点都不为空就比较麻烦了,因为该结点的父结点只有一个空结点,T.Hibbard在1962年提出了这个算法,大概原理可以归结如下:1、将指向即将被删除结点的链接保存为t;2、将x指向他的后继结点min(t.right),后继结点是指t的右子树中
2016-06-21 21:57:02 315
原创 总结一下常见的几种排序
1、插入排序public void insertsort(int[] a){ for(int i=1;ilength;i++) { for(int j=i;j>0;j--) { if(a[j]1]) swap(a,j,j-1); } }}时间复杂度O(n^
2016-06-20 21:20:32 405
转载 二叉树层次遍历——编程之美
代码如下,感觉最巧妙的地方是sublength的使用。public void TreeLevelOrder(TreeNode root){ if(root==null) return; ArrayList list=new ArrayList; int curNodes=0; int sublength=1; list.add(root);
2016-06-18 15:55:26 208
原创 leetcode number of 1 bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000
2016-06-17 18:31:14 205
原创 leetcode linked list cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?一开始没有考虑空间复杂度,借助hashmap找到第一个出现重复的listnode:Map,Integer> map=new HashMap;while(h
2016-06-17 18:27:11 180
原创 leetcode same tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.很简单的一道题,不过
2016-06-17 16:54:36 256
原创 leetcode80 Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first fi
2016-06-15 21:33:52 179
原创 leetcode rotate image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Subscribe to see which companies asked this que
2016-06-14 20:46:24 233
原创 leetcode power of two
Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.Subscribe to se
2016-06-14 18:03:32 180
转载 leetcode count pirmes
Description:Count the number of prime numbers less than a non-negative number, n.Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.传统方法会出现TLC,转载一个埃拉托斯
2016-06-14 12:50:03 158
原创 leetcode uglynumber
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.Note that 1 is typically trea
2016-06-13 19:13:10 187
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人