leetcode
陈超q
当你的才华还撑不起你的野心时,就应该静下心来学习;当你的能力还驾驭不了你的目标时,就应该沉下心来历练。梦想不是浮躁,而是沉淀和积累。只有拼出来的美丽,没有等出来的辉煌。机会永远是留给最渴望的那个人,学会与内心深处的你对话,问问自己想要怎样的人生。
展开
-
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 · 330 阅读 · 0 评论 -
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 · 286 阅读 · 0 评论 -
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 · 275 阅读 · 0 评论 -
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 · 361 阅读 · 0 评论 -
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 · 324 阅读 · 0 评论 -
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 · 347 阅读 · 0 评论 -
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 · 290 阅读 · 0 评论 -
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 · 360 阅读 · 0 评论 -
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 · 322 阅读 · 0 评论 -
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 · 287 阅读 · 0 评论 -
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 · 301 阅读 · 0 评论 -
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 · 420 阅读 · 0 评论 -
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 · 457 阅读 · 0 评论 -
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 · 366 阅读 · 0 评论 -
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 · 269 阅读 · 0 评论 -
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 · 256 阅读 · 0 评论 -
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 · 388 阅读 · 0 评论 -
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 · 309 阅读 · 0 评论 -
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 · 333 阅读 · 0 评论 -
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 · 298 阅读 · 0 评论 -
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 · 397 阅读 · 0 评论 -
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 · 254 阅读 · 0 评论 -
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 · 212 阅读 · 0 评论 -
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 · 284 阅读 · 0 评论 -
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 · 280 阅读 · 0 评论 -
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 · 254 阅读 · 0 评论 -
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 · 278 阅读 · 0 评论 -
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 · 331 阅读 · 0 评论 -
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 · 278 阅读 · 0 评论 -
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 · 332 阅读 · 0 评论