- 博客(98)
- 资源 (7)
- 收藏
- 关注
原创 threadLocal理解
Thread类中包含 threadLocals属性ThreadLocal.ThreadLocalMap threadLocals = null; threadLocals 为ThreadLocalMap 类型,而ThreadLocalMap 为ThreadLocal的静态内部类,因此通过threadLocals 可以得到this所对应的ThreadLocal在第
2016-11-16 09:20:13
357
原创 leetcode-Pascal's Triangle II
和Pascal's Triangle I差不多的思想。public class Solution { public List getRow(int rowIndex) { ArrayList results = new ArrayList(); results.add(1); for (int i = 1; i <= rowIndex; ++i
2015-11-06 11:51:33
493
原创 leetcode-Factorial Trailing Zeroes
因为0都来自10,也就是2*5,因此计算5的个数即可,例如15的话,它的5可以来自15,10,5,因此它的5的个数为3。public class Solution { public int trailingZeroes(int n) { int c=0; if(n==0) return 0; while(n/5!=0) { n=n/5; c=c
2015-11-06 11:05:30
457
原创 leetcode-Pascal's Triangle
每一行的首末为1,中间的元素为上一行的2个元素相加。public class Solution { public List> generate(int numRows) { List re=new ArrayList(); for(int i=1;i<=numRows;i++) { List arr=new ArrayList(); int
2015-11-06 10:55:06
395
原创 leetcode-House Robber
动态规划思想,第i天能获得的最大金钱数为dp[i] =Math.max(nums[i] + dp[i - 2], dp[i - 1]),即要么是第i-1天的金钱数,要么是第i-2天的金钱数加上第i天的金钱数。public class Solution { public int rob(int[] nums) { if(nums.length==0) return
2015-11-05 12:57:10
386
原创 leetcode-Plus One
判断最后一位是否为9,不为9,则加1直接返回即可。如果为9,则要进位,接下来,依次判断每个位是否都有进位。public class Solution { public int[] plusOne(int[] digits) { int len=digits.length; int []re=new int[len+1]; if(digits[len-1]<9)
2015-11-05 12:52:59
370
原创 leetcode-Remove Duplicates from Sorted Array
public class Solution { public int removeDuplicates(int[] nums) { if((nums==null)||(nums.length==0)) return 0; int temp=nums[0],n=nums.length; for(int i=1;i<n;i++) { if(nums[i]==temp
2015-11-05 12:47:28
302
原创 leetcode-Binary Tree Level Order Traversal II
一层层遍历。public class Solution { public List> levelOrderBottom(TreeNode root) { List> rearr=new ArrayList>(); List> rea=new ArrayList>(); List tre=new ArrayList(); if(root==null) r
2015-11-05 12:44:06
357
原创 leetcode-Remove Element
public class Solution { public int removeElement(int[] nums, int val) { int n=0,t=nums.length; for(int i=0;i<nums.length;) { if(t==0) break; if(val==nums[i]) { n++;
2015-11-05 12:42:12
286
原创 leetcode-Symmetric Tree
判断一个节点的左右子树是否满足boolean isl=symm(left.left,right.right);boolean isr=symm(left.right,right.left);即可public class Solution { public boolean isSymmetric(TreeNode root) { if(root==null)
2015-11-05 11:09:17
352
原创 leetcode-Power of Two
只要这个数的最高为1,其他位都为0,则就是2的指数幂。public class Solution { public boolean isPowerOfTwo(int n) { return (n > 0) && ((n & (n - 1))==0); }}
2015-11-05 11:02:52
326
原创 leetcode-Balanced Binary Tree
通过递归,不断地求一个节点的左右子节点的的最大深度,最后看根节点的左右子树的高度差是否大于1。public class Solution { public boolean isBalanced(TreeNode root) { if(root==null) return true; if(getDepth(root)==-1) return
2015-11-05 10:58:36
296
原创 leetcode-Merge Two Sorted Lists
跟合并两个有序数组一样的道理。public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1; ListNode p2 = l2; ListNode Head = new ListNode(0);
2015-11-05 10:52:55
275
原创 leetcode-Happy Number
public class Solution { public boolean isHappy(int n) { int a=0; boolean flag=false; int result=0; while(!flag) { a=n%10; result=result+a*a; System.out.print("r"+r
2015-11-05 10:50:38
291
原创 leetcode-Ugly Number
一直除以2,3,5,即可。public class Solution { public boolean isUgly(int num) { if(num <= 0) return false; while(num != 1){ if(num %2 == 0) num = num /2;
2015-11-04 22:27:18
297
原创 leetcode-Implement Queue using Stacks
懒地打字。class MyQueue{ Stack s; Stack t; public MyQueue() { s=new Stack(); t=new Stack(); } // Push element x to the back of queue. public void push(int x) { s.
2015-11-04 22:24:28
303
原创 leetcode-Climbing Stairs
菲波那切数列。public class Solution { public int climbStairs(int n) { int cur=0; if(n==0||n==1) return 1; int pre1=1; int pre2=1; for(int i=2;i<=n;i++) { cur=pre1+pre2;
2015-11-04 22:19:00
306
原创 leetcode-Remove Duplicates from Sorted List
若前后2个节点的值相同,就像删除节点一样,不同,则将后一个节点的值赋给一个变量,该变量用来比较前后2个节点的值是否相同。public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null) return null; ListNode t
2015-11-04 22:15:24
270
原创 leetcode-Reverse Linked List
思想:将节点一个个插入到前一个节点前面。public class Solution { public ListNode reverseList(ListNode head) { if(head==null) return head; ListNode t=null,q; while(head.next!=null) { q=head.ne
2015-11-04 22:11:44
309
原创 leetcode-Roman to Integer
搞懂罗马数字的规则即可。public class Solution { public int romanToInt(String s) { if(s==null || s.length()==0) return 0; int res = 0; for(int i=0;i<s.length();i++) {
2015-11-04 22:07:32
339
原创 leetcode-Majority Element
用哈希表保存出现的次数即可。public class Solution { public int majorityElement(int[] nums) { int max=0,result=0,curLen=0; HashMap ha=new HashMap(); for(int i=0;i<nums.length;i++)
2015-11-04 22:05:47
382
原创 leetcode-Number of 1 Bits
循环右移,与1进行与运算,如果为1,则表明该位为1。public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int num=0; for(int i=0;i<32;i++) { if((n>>>i&1)==
2015-11-04 22:02:11
243
原创 leetcode-Valid Anagram
用哈希表保存每个字母出现的次数,如果相同字母出现的次数相同,则返回true,否则返回false。public class Solution { public boolean isAnagram(String s, String t) { HashMap arr=new HashMap(); HashMap brr=new HashMap(); for(i
2015-11-04 21:59:07
309
原创 leetcode-Lowest Common Ancestor of a Binary Search Tree
因为二叉搜索树是有序的,左子节点小于父节点,右子节点大于父节点。如果2个节点的最大值小于root节点的值,说明它们的公共父节点在root节点的左子树中;若2个节点的最小值大于root节点的值,说明它们的公共父节点在root节点的右子树中;若一个大于,一个小于,则返回该节点。public class Solution { public TreeNode lowestCommonAnces
2015-11-03 19:53:31
432
原创 leetcode-Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 public c
2015-11-03 19:45:36
422
原创 leetcode-Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element
2015-11-03 19:43:52
341
原创 leetcode-Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1将一个节点的左子节点与右子节点互换位置。public class Solution { public TreeNode invert
2015-11-03 19:30:59
314
原创 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.就是比较两棵树相同位
2015-11-03 19:27:00
279
原创 leetcode-Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with val
2015-11-03 19:23:02
347
原创 leetcode-Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.求树的最大深度,也就是求左子树或右子树的最大深度,通过递归,得到最大
2015-11-03 19:06:31
348
原创 leetcode-Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on
2015-11-03 19:00:01
350
原创 java静态内部类
在一个类中创建另外一个类,叫做成员内部类。这个成员内部类可以静态的(利用static关键字修饰),也可以是非静态的。 一、静态内部类的使用目的。java中一个普通类是不可以定义为static的,只有内部类可以为静态类。在定义内部类的时候,在其前面加上一个权限修饰符static。这个内部类就变为了静态内部类。如在进行代码程序测试的时候,如果在每一个Java源文件中 都设置一个主方法(主方
2015-10-12 19:00:00
687
原创 简单工厂模式、工厂方法模式和抽象工厂模式小结
工厂模式是最重要的模式,因为大多数模式都需要用到工厂模式。 多数设计模式的内容讲解的都是如何设计接口。接口如何产生呢?如果在客户代码(类库的使用者称之为客户)中直接使用具体类,那么就失去了接口的意义。因为接口的使用目的,就是要降低客户对具体类的依赖程度。如果在客户代码中直接使用接口,那么就造成了客户对具体类名称的依赖。(客户最终需要以某种方式指明所需要的具体类,如配置文件或代码,但是
2015-09-23 22:20:13
388
原创 Trie树:统计词频、排序、查找
Trie树利用字符串的公共前缀降低了查询时间的开销,提高了查询的效率。字典树的插入,删除和查找都非常简单,用一个一重循环即可。1. 从根节点开始一次搜索2. 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索3. 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索4. 迭代过程...5. 在某个节点处,关键词的所有字母
2015-09-08 22:12:53
2459
原创 java线程间通信
线程间进行输入/输出通信最常用的方式是”管道“方式。一个线程从管道一端写入数据,另一个线程从管道另一端读出数据。public class pipedIO { public static void main(String[] args) throws IOException { pipeSender sender=new pipeSender(); pipeReceiver r
2015-09-02 19:36:13
382
转载 Stirng,Stringbuffer,Stringbuild的区别浅淡
String 1,Stirng是对象不是基本数据类型 2,String是final类,不能被继承。是不可变对象,一旦创建,就不能修改它的值。 3,对于已经存在的Stirng对象,修改它的值,就是重新创建一个对象,然后将新值赋予这个对象 StringBuffer 1,一个类似于 String 的字符串缓冲区,对它的修改的不会像String那样重创建对象。 2,使用append
2015-07-03 14:09:32
421
原创 java静态语句块、构造语句块以及构造函数的执行顺序
class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println("I'm A class"); } static { System.out.println("static A"); } } publi
2015-06-25 22:18:04
517
原创 java Thread线程run()和start()方法的区别
public static void main(String args[]) { Thread t = new Thread() { public void run() { pong(); } }; t.run(); System.out.
2015-06-25 21:47:21
886
原创 位操作
按位与运算符(&)参加运算的两个数据,按二进制位进行“与”运算。运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;即:两位同时为“1”,结果才为“1”,否则为0例如:3&5 即 0000 0011 & 0000 0101 = 0000 0001 因此,3&5的值得1。 负数按补码形式参加按位与运算。“与运算”的特殊用途:(1)清
2015-05-21 16:27:47
595
原创 查找序列中第二大的元素
这个问题,感觉很简单,一看就可能首先想到几种方法,例如: 首先对这个序列进行排序,然后取第二个位置的元素;或者循环遍历元素序列,找到最大的元素,然后将其移除。再重复此过程,得到第二大的元素。但是都没有考虑其效率。下面提出一种方法:package algrithm;public class secondbignum { public static void main(String[]
2015-05-14 20:49:22
1498
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人