leetcode
pengweixuan008
这个作者很懒,什么都没留下…
展开
-
58.Length of Last Word (最后一个单词长度)
描述:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word原创 2015-07-28 14:11:15 · 419 阅读 · 0 评论 -
160.Intersection of Two Linked Lists(两个单链表的相交结点)
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-07-30 14:22:22 · 226 阅读 · 0 评论 -
116.Populating Next Right Pointers in Each Node(二叉完全树的next结点)
Note:You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).For example,Given the原创 2015-07-30 15:17:48 · 180 阅读 · 0 评论 -
141.Linked List Cycle (判断一个单链表是否有环)
public class Solution { public boolean hasCycle(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next;原创 2015-07-30 15:06:19 · 202 阅读 · 0 评论 -
144.Binary Tree Preorder Traversal(二叉树先序遍历)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Sol原创 2015-07-30 15:00:30 · 208 阅读 · 0 评论 -
136.Single Number (数组中只出现一次的数)
Given an array of integers, every element appears twice except for one. Find that single one.代码:public class Solution { public int singleNumber(int[] nums) { HashMap map = new Hash原创 2015-07-30 15:11:01 · 186 阅读 · 0 评论 -
155.Min Stack(求一个栈中的最小元素)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2015-07-30 14:32:46 · 253 阅读 · 0 评论 -
1.Two Sum (数组中两个数之和为n)
Given an array of integers, find two numbers such that they add up to a specific target number.Input: numbers={2, 7, 11, 15}, target=9Output: index1=1, index2=2代码:public class Solution原创 2015-08-03 14:27:57 · 407 阅读 · 0 评论 -
19.Remove Nth Node From End of List(移除单链表中倒数第N个结点)
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end,原创 2015-08-03 14:37:39 · 300 阅读 · 0 评论 -
20.Valid Parentheses (括号匹配)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2015-08-03 14:38:58 · 346 阅读 · 0 评论 -
108.Convert Sorted Array to Binary Search Tree (将有序数组转化成二叉排序树)
public class Solution { public TreeNode sortedArrayToBST(int[] nums) { return build(nums,0,nums.length-1); } public TreeNode build(int[]nums,int left,int right){原创 2015-08-03 15:01:38 · 323 阅读 · 0 评论 -
104.Maximum Depth of Binary Tree(二叉树深度)
public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; if(root.left==null&&root.right==null) return 1; int x = Math.max(maxDepth(root.l原创 2015-08-03 15:05:22 · 234 阅读 · 0 评论 -
100.Same Tree (判断两颗树是否相等)
public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null&&q==null){ return true; }else if(p==null||q==null){ return false原创 2015-08-03 15:17:09 · 373 阅读 · 0 评论 -
94.Binary Tree Inorder Traversal(二叉树先序遍历)
For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].public class Solution { public List inorderTraversal(TreeNode root) { List res = new原创 2015-08-03 15:25:56 · 198 阅读 · 0 评论 -
145.Binary Tree Postorder Traversal (二叉树后序遍历)
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].代码:/** * Defini原创 2015-07-30 14:43:37 · 229 阅读 · 0 评论 -
162.Find Peak Element (寻找数组中的峰值点)
题目:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple p原创 2015-07-30 14:14:27 · 509 阅读 · 0 评论 -
217.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 eleme原创 2015-07-28 15:57:55 · 300 阅读 · 0 评论 -
203.Remove Linked List Elements(删除链表中值为X的结点)
题目:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5思路:新建一个val=-1的头节点,从he原创 2015-07-28 16:22:20 · 263 阅读 · 0 评论 -
226.Invert Binary Tree(交换二叉树左右结点)
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1方法:先创建一个空结点,将左右结点交换,再用递归代码:/** * Definition for a binary原创 2015-07-28 14:36:43 · 272 阅读 · 0 评论 -
232.Implement Queue using Stacks(用栈实现队列)
题目:Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.emp原创 2015-07-28 14:15:55 · 172 阅读 · 0 评论 -
206.Reverse Linked List(单链表逆置)
Reverse a singly linked list./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } **/ 方法原创 2015-07-28 16:12:40 · 332 阅读 · 0 评论 -
225.Implement Stack using Queues (用队列实现栈)
题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Retu原创 2015-07-28 15:14:46 · 269 阅读 · 0 评论 -
231.Power of Two(2的N次方)
Given an integer, write a function to determine if it is a power of two.方法:如果这个数是2的倍数(n%2==0),将这个数除以二(n /=2 ),循环上述操作,最后n==1返回true代码:public class Solution { public boolean isPowerOfTwo原创 2015-07-28 14:30:15 · 272 阅读 · 0 评论 -
112.Path Sum (数的路径为N)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2015-08-03 14:54:13 · 327 阅读 · 0 评论 -
171.Excel Sheet Column Number (AB=28)
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 class原创 2015-07-29 12:01:43 · 180 阅读 · 0 评论 -
202.Happy Number
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equ原创 2015-07-29 11:06:49 · 239 阅读 · 0 评论 -
169.Majority Element (数组中出现次数超过一半的数)
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.思路:方法一:将数组排序,最中间的那个数就是您要找的数。方法二:用了两个辅助变量。 k用于临时存储数组中的数原创 2015-07-29 14:06:59 · 240 阅读 · 0 评论 -
168.Excel Sheet Column Title(28 -> AB)
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 代码:publ原创 2015-07-29 14:02:22 · 198 阅读 · 0 评论 -
198.House Robber (求非相邻正整数的和最大值)
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-07-29 11:14:09 · 262 阅读 · 0 评论 -
83.Remove Duplicates from Sorted List (删除单链表中重复结点)
For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null) return n原创 2015-08-03 15:28:11 · 271 阅读 · 0 评论