- 博客(80)
- 收藏
- 关注
原创 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",
2015-11-19 15:58:39
287
原创 Word Pattern II
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-emptysubstring in str.
2015-11-19 13:54:33
716
原创 Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
2015-11-19 13:21:22
345
原创 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.用了一个dumb head,。对于这种header不确定的list,可以用dumb header,代码会整洁不
2015-11-19 12:50:37
296
原创 Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold add
2015-11-19 12:26:15
337
原创 Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw
2015-11-19 12:10:18
235
转载 Shortest Word Dsitance III
word1 and word2 can be equal//C++: 20msclass Solution {public: int shortestWordDistance(vector& words, string word1, string word2) { int n = words.size(); int p1 = -1, p2 =
2015-11-13 05:44:35
248
转载 Shortest Word Distance II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you
2015-11-13 05:43:00
273
原创 Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.For example,Assume that words = ["practice", "makes", "perfect", "coding", "
2015-11-13 04:53:55
321
原创 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe
2015-11-13 04:03:53
228
原创 Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unk
2015-11-13 03:53:28
270
原创 Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in
2015-11-13 03:29:51
226
原创 Maximum Product Subarray
public class Solution { public int maxProduct(int[] nums) { int maxValue = nums[0]; int minValue = nums[0]; int max = nums[0]; for(int i = 1; i < nums.length; i++){
2015-11-12 16:33:57
228
原创 [LeetCode] 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,−1,2,1] ha
2015-11-12 16:13:12
187
原创 Graph Valid Tree
Problem Description:Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.Fo
2015-11-12 14:44:24
313
原创 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot
2015-11-12 13:31:47
248
原创 Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri
2015-11-12 10:48:00
311
原创 Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin
2015-11-12 07:37:28
270
原创 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther
2015-11-12 06:29:15
269
原创 Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.
2015-11-11 14:41:29
224
原创 Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be
2015-11-11 13:38:57
290
原创 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Subscribe to see which companies asked this questio
2015-11-11 12:03:36
213
原创 Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found
2015-11-10 12:00:32
225
原创 【LeetCode】Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary
2015-11-08 04:26:20
202
原创 Flowing Water
输入是一个 N*N的矩阵,代表地势高度。如果下雨水流只能流去比他矮或者一样高的地势。矩阵左边和上边是太平洋,右边和下边是大西洋。求出所有的能同时流到两个大洋的点。For example:12345678910Pacific: ~Atlantic: *~ ~ ~ ~ ~ ~ ~~ 1 2 2 3 (5) *~ 3
2015-11-07 14:29:05
347
原创 [LeetCode] Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]./** * Definition for an interval. * public
2015-11-07 13:20:00
213
原创 【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.简单解法:O(dividend/divisor)public class Solution { public int divide(int divide
2015-11-03 15:50:34
195
原创 [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link
2015-10-29 13:29:31
217
原创 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 space. Y
2015-10-29 13:10:05
200
原创 [LeetCode] Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the follow
2015-10-29 11:48:44
213
原创 [LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E
2015-10-28 12:25:48
154
原创 [Leetcode] Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co
2015-10-28 05:33:57
266
原创 [Leetcode]Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the
2015-10-28 05:32:21
201
原创 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should
2015-10-25 13:22:24
217
原创 [LeetCode] Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?先翻转后半段,再比较/** * Definition for singly-linked list. * public class ListNo
2015-10-25 09:12:47
206
原创 [LeetCode] Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em
2015-10-25 07:01:28
209
原创 [LeetCode] Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.public class S
2015-10-25 03:11:59
204
原创 [LeetCode] Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.最简单的方法是参照clone graph,利用一个HashM
2015-10-25 02:15:08
204
原创 [LeetCode] Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.public class Solution { pub
2015-10-24 11:40:28
228
原创 [Leetcode] 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 triplet (a,b,c)
2015-10-18 11:34:27
137
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人