LeetCode
西湖有大葱
这个作者很懒,什么都没留下…
展开
-
[LeetCode][Java] Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题意:给定一个链表,判断链表中是否存在循环圈。算法分析: * 利用快慢指针slow,fast,slow指针每次走一步,fas原创 2015-05-20 19:30:16 · 465 阅读 · 0 评论 -
[LeetCode][Java] Populating Next Right Pointers in Each Node
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next rig原创 2015-05-20 19:44:10 · 513 阅读 · 0 评论 -
[LeetCode][Java] Number of 1 Bits
题目:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation原创 2015-05-20 18:46:13 · 684 阅读 · 0 评论 -
[LeetCode][Java] Binary Tree Inorder Traversal
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive原创 2015-05-20 20:08:08 · 507 阅读 · 0 评论 -
[LeetCode][Java] Unique Binary Search Trees
题目:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2原创 2015-05-20 21:09:31 · 559 阅读 · 0 评论 -
[LeetCode][Java] Binary Tree Preorder Traversal
题目:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].题意:二叉树先序遍历原创 2015-05-20 21:17:01 · 552 阅读 · 0 评论 -
[LeetCode][Jave] 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 题意:原创 2015-05-20 19:19:13 · 509 阅读 · 0 评论 -
[LeetCode][Java] Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in t原创 2015-05-21 14:02:05 · 525 阅读 · 0 评论 -
[LeetCode][Java] Maximum Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−原创 2015-05-21 19:15:27 · 495 阅读 · 0 评论 -
[LeetCode][Java] 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.You may assume that the array is non-empty and the majo原创 2015-05-21 14:40:15 · 555 阅读 · 0 评论 -
[leetCode][Java] Remove Duplicates from Sorted List
题目:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.题意:给定一个有序单原创 2015-05-21 19:47:49 · 507 阅读 · 0 评论 -
[LeetCode][Java] Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?题意:假设爬梯子,你需要n步跑到顶。原创 2015-05-21 20:02:02 · 520 阅读 · 0 评论 -
[LeetCode][Java] Convert Sorted Array to Binary Search Tree
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST。题意:给定一个数组,其中的数组元素降序排列,将其转化为平衡二叉查找树。算法分析:首先明白概念二叉查找树和平衡二叉查找树:二叉排序树或者是一棵空树,或者是具有下列性质的二叉树原创 2015-05-21 20:20:23 · 594 阅读 · 0 评论 -
[LeetCode][Java] Reverse Linked List
题目:Reverse a singly linked list.题意:倒转单链表.算法分析:方法一:将单链表转化为数组,利用数组倒序重建新的单链表。代码如下:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ne原创 2015-05-21 21:36:47 · 685 阅读 · 0 评论 -
[LeetCode][Java] Single Number II
题目:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it wi原创 2015-05-21 14:23:22 · 633 阅读 · 0 评论 -
[LeetCode][Java] Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of th原创 2015-05-22 21:28:59 · 655 阅读 · 0 评论 -
[LeetCode][Java] Unique Paths
题目:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to r原创 2015-05-22 21:19:43 · 554 阅读 · 0 评论 -
[LeetCode][Java] Minimum Path Sum
题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or原创 2015-05-22 15:59:00 · 843 阅读 · 0 评论 -
[LeetCode][Java] Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题意:将两个单链表合并,合并一个新的单链表。算法分析:比较容易,注意此题并未要求删除原创 2015-05-22 20:19:39 · 482 阅读 · 0 评论 -
[LeetCode][Java] 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-05-15 21:36:17 · 526 阅读 · 0 评论 -
[LeetCode][Java] 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-05-15 21:52:22 · 659 阅读 · 0 评论 -
[LeetCode][Java] Single Number
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without us原创 2015-05-15 21:27:57 · 644 阅读 · 0 评论 -
[LeetCode][Java] Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(()原创 2015-05-23 20:59:54 · 594 阅读 · 0 评论 -
[LeetCode][Java] Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant原创 2015-05-23 20:32:44 · 593 阅读 · 0 评论 -
[LeetCode][Java] Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new len原创 2015-05-23 21:16:19 · 583 阅读 · 0 评论 -
[LeetCode][Java] Balanced Binary Tree
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne原创 2015-05-23 21:25:22 · 713 阅读 · 0 评论 -
[LeetCode][Java] Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i,原创 2015-07-09 10:01:38 · 815 阅读 · 0 评论 -
[LeetCode][Java] Regular Expression Matching
题目:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire原创 2015-07-08 20:54:40 · 617 阅读 · 0 评论 -
[LeetCode][Java] Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to原创 2015-07-07 17:12:35 · 936 阅读 · 0 评论 -
[LeetCode][Java] Roman to Integer
题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.题意:给定一个罗马数字,将其转化为整数。给定的输入保证在1-3999之间算法分析: * 罗马数字规则: * 1, 罗马原创 2015-07-09 10:20:11 · 773 阅读 · 0 评论 -
[LeetCode][Java] Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.题意:写出一个函数,找到一组数组中的最长公共子串。算法分析:需要构建两重循环。第一层是最短子串的长度,另一层是遍历字符串数组中的每个成员。brute force的想法,以第一个字符串为标准,对于原创 2015-07-09 11:17:18 · 754 阅读 · 0 评论 -
[LeetCode][Java] String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible原创 2015-07-06 12:10:08 · 696 阅读 · 0 评论 -
[LeetCode][Java] 3Sum Closest
题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would h原创 2015-07-09 12:07:06 · 840 阅读 · 0 评论 -
[LeetCode][Java] Integer to Roman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题意:给定一个整数,将其转化为罗马数字。题目给定的输入保证在1-3999之间。算法分析: 参考了博客http://www.cnbl原创 2015-07-09 10:12:28 · 689 阅读 · 0 评论 -
[LeetCode][Java] 3Sum
题目:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a tripl原创 2015-07-09 11:34:57 · 708 阅读 · 0 评论 -
[LeetCode][Java] Letter Combinations of a Phone Number
题目:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input原创 2015-07-09 19:03:39 · 813 阅读 · 0 评论 -
[LeetCode][Java] 4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2015-07-09 20:25:23 · 791 阅读 · 0 评论 -
[LeetCode][Java] Remove Nth Node From End of List
题目: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原创 2015-07-09 20:52:08 · 616 阅读 · 0 评论 -
[LeetCode][Java] 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原创 2015-07-10 11:16:59 · 754 阅读 · 0 评论 -
[LeetCode][Java] Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题意:合并 K 个有序的链表,把他们合并成为一个有序链表。分析并描述它的复杂度。算法分析:先将k个链表转化为数组,合并,之后利用Collections.sort()排序。原创 2015-07-10 11:46:50 · 684 阅读 · 0 评论