
leetcode
sipherhern
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode第96题 Unique Binary Search Trees
思路二叉搜索树有个性质,就是左边的数都比根小,右边的数都比根大。另外,题目说明二叉树的节点是从1到n,所以我们能确定如果根为k,则根左边的数是1到k-1,根右边的数是k+1到n。还有一点技巧是,对于通过一个根来说,唯一二叉树的数量是其左子树的数量乘以右子树的数量,这是简单的乘法原理。并且,左右子树的形态数量是跟具体的数无关的,只跟这个树里有多少节点有关。而根可以选择从1到n的任意的数,唯一转载 2016-03-01 15:27:47 · 367 阅读 · 0 评论 -
最长问题记录
1、最长回文字符串 例子:如”isabba” 、”abcbaXXab” 这样子的字符串,部分是回文的,求出最长的回文部分。解法一思路: 穷尽字符串。将字符串拆分成子字符串,然后对子字符串进行回文判断,并记录其长度。 public static void main(String[] args){ String str = "isabbaXXab"; int len原创 2016-09-12 14:49:47 · 688 阅读 · 0 评论 -
Ioc容器
IOC容器是什么? (转http://www.cnblogs.com/lihuiyy/archive/2012/05/22/2512712.html)Spring的IoC容器就是一个实现了BeanFactory接口的可实例化类。事实上,Spring提供了两种不同的容器:一种是最基本的BeanFactory,另一种是扩展的ApplicationContext。BeanFactory 仅提供了转载 2016-07-19 20:49:40 · 422 阅读 · 0 评论 -
安装完Hadoop之后,命令行输入hadoop却找不到命令的解决方法
大多数原因是没有配置环境变量解决方法 1. cd /etc/profile 2. 把这三条加到proflie文件的最后export JAVA_HOME=XXXX(在安装了jdk的前提下,echo $JAVA_HOME可以查看得到)export HADOOP_HOME=XXX(hadoop的安装路径)export PATH=.:$HADOOP_HOME/bin:$JAVA_HOME/bin:$原创 2016-06-19 17:07:31 · 26272 阅读 · 0 评论 -
349. Intersection of Two Arrays
两个数组,找出重复的元素,思路:先让两个数组排序,然后用两个指针分别来移动数组。 当数组有相同的元素时,就一直向前移动,直到和后面的元素不一样。当两个数组都停下来时,进行比较。 如果Apublic class Solution { public int[] intersection(int[] nums1, int[] nums2) { int m = nums1.lengt原创 2016-07-25 15:11:44 · 293 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
递归实现:(思路非常清晰) public ListNode swapPairs(ListNode head) { if(head == null || head.next == null){ return head; } ListNode second = head.next; ListNode third = second.next;原创 2016-05-07 20:45:34 · 261 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversal
思路:这道题目感觉有点难度。题目的意思是将每一层的节点都放到一个list中,最后返回一个list public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> answerList = new LinkedList<List<Integer>>(); // nodeList用来记原创 2016-04-20 15:35:08 · 284 阅读 · 0 评论 -
如何取出一个整数的每位数字
while(x > 0){ res = x % 10; x /= 10; }例子: 对一个整数进行逆序操作(如-321 ==》 -123,123==》321) public int reverse(int x) { int flag=x>0?1:-1,res=0; // 取符号 x=x>0?x:-x;原创 2016-04-13 19:53:32 · 3445 阅读 · 0 评论 -
LeetCode235. Lowest Common Ancestor of a Binary Search Tree
思路:应该充分利用BST的特性: 左边节点 < root <右边节点 这个特性。 因此p和q的最开始分布就只存在三种可能。要么在root的两边,要么要root的左边或右边。当p和q在root的两边时,不用多算,LCA就是root。当p和q在root的左边时,就看root.left与p和q的大小关系。右边亦是如此。还有一种特殊情况,就是 p 是 q 的parent的时候,p是q的LCA。此时三种i原创 2016-04-13 15:23:05 · 387 阅读 · 1 评论 -
LeetCode第264题目 Ugly Number ll
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first转载 2016-02-26 17:28:07 · 370 阅读 · 0 评论 -
LeetCode第258题,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 only o转载 2016-02-25 23:39:52 · 280 阅读 · 0 评论 -
LeetCode第202题. Happy Number
题目:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of th原创 2016-02-26 15:38:18 · 347 阅读 · 0 评论 -
LeetCode 257Binary Tree Paths
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Sol原创 2016-03-08 23:15:04 · 242 阅读 · 0 评论 -
LeetCode 328. Odd Even Linked List
思路:1 小于3个元素的,直接返回原有head2 大于等于3个元素的while循环{2.1 建立odd 链表2.2 建立even链表}odd链表尾部指针指向even链表的头部3 返回head代码如下:/** * Definition for singly-linked list. * public class转载 2016-03-06 16:40:25 · 290 阅读 · 0 评论 -
LeetCode 283. Move Zeroes
解题思路:用两个指针i和j分别指向零和非零,当j指向非零的时候,交换两个指针所指向的数字,并且使i向前移动;而当j指向0的时候,i则不动;j每次循环都要向前移动;如图: StepOne 10 1 0 2 (i j) // 指针 j 指向非0 指针 i j 均移动 StepTwo 10 1 0 2原创 2016-03-02 11:05:23 · 233 阅读 · 0 评论 -
LeetCode第70题 Climbing Stairs
转载于: http://www.bubuko.com/infodetail-620174.html第一反应,递归求解,貌似很简单。但是不幸,超时public int climbStairs1(int n) { if (n == 1 || n == 2) { return n; } return climbStairs1(n-1) +转载 2016-03-01 16:00:41 · 281 阅读 · 0 评论 -
LeetCode89. Gray Code
做法,先给result赋初始值为0,并给出一个k,k在每次外循环都会左移一位,然后在内循环中 和result中的每个元素(逆序,为什么要逆序?因为我们通过观察格雷码可以发现,n位格雷码,除了最左位,一头一尾是对称的,可以参考本文最下方的链接看别人的分析)作一个异或运算,并将结果放到result中。 public static List<Integer> grayCode(int n) {转载 2016-03-27 15:47:04 · 372 阅读 · 0 评论