- 博客(34)
- 资源 (2)
- 收藏
- 关注

原创 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
442
转载 连续子数组的最大和
public Integer findGreatestSum(int[] arr){ if(arr.length ==0) return null; int greatest = 0x80000000; int curSum = 0; for(int i
2015-08-09 17:01:22
277
转载 二叉搜索树与双向链表转化
1:由于要求链表是有序的,可以借助二叉树中序遍历,因为中序遍历算法的特点就是从小到大访问结点。当遍历访问到根结点时,假设根结点的左侧已经处理好,只需将根结点与上次访问的最近结点(左子树中最大值结点)的指针连接好即可。进而更新当前链表的最后一个结点指针。2:由于中序遍历过程正好是转换成链表的过程,即可采用递归处理struct BinaryTreeNode {
2015-08-09 16:46:30
368
原创 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
310
原创 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
232
原创 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
403
原创 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
267
原创 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
348
原创 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
347
原创 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
372
原创 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
325
原创 13.Roman to Integer (罗马数字转成整数)
public class Solution { public int romanToInt(String s) { char[] symbol = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' }; int[] val = { 1, 5, 10, 50, 100, 500, 1000 }; Map map =
2015-08-03 14:32:24
278
原创 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
209
原创 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
217
原创 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
218
原创 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
238
原创 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
258
原创 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
285
原创 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
254
原创 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
549
原创 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
267
原创 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
232
原创 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
203
原创 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
291
原创 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
261
原创 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
292
原创 204.Count Primes(1-N中,素数的个数)
题目:Count the number of prime numbers less than a non-negative number, n.代码:public class Solution { public int countPrimes(int n) { boolean [] a = new boolean[n]; int cou
2015-07-28 16:20:25
256
原创 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
361
原创 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
333
原创 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
288
原创 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
303
原创 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
304
原创 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
195
原创 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
448
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人