Leetcode
文章平均质量分 67
zhixin_wen95
https://github.com/zhixinwen
展开
-
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 · 712 阅读 · 0 评论 -
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 · 263 阅读 · 0 评论 -
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 · 310 阅读 · 0 评论 -
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 · 301 阅读 · 0 评论 -
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 · 237 阅读 · 0 评论 -
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 · 303 阅读 · 0 评论 -
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 · 259 阅读 · 0 评论 -
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 · 256 阅读 · 0 评论 -
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 · 277 阅读 · 0 评论 -
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 · 216 阅读 · 0 评论 -
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 · 209 阅读 · 0 评论 -
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 · 217 阅读 · 0 评论 -
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 · 214 阅读 · 0 评论 -
[Leetcode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2015-10-09 11:11:00 · 199 阅读 · 0 评论 -
[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 · 216 阅读 · 0 评论 -
[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 · 202 阅读 · 0 评论 -
[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 · 179 阅读 · 0 评论 -
Minimum Size Subarray
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the array [2,3原创 2015-10-13 13:55:56 · 176 阅读 · 0 评论 -
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 · 267 阅读 · 0 评论 -
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 · 280 阅读 · 0 评论 -
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 · 325 阅读 · 0 评论 -
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 · 227 阅读 · 0 评论 -
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 · 339 阅读 · 0 评论 -
[Leetcode] Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a原创 2015-10-16 08:43:02 · 188 阅读 · 0 评论 -
[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 · 212 阅读 · 0 评论 -
[Leetcode] Minimum Window Substring My Submissions Question Solution
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN原创 2015-10-15 13:14:32 · 176 阅读 · 0 评论 -
[Leetcode]Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-10-15 11:51:00 · 162 阅读 · 0 评论 -
[Leetcode] Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first原创 2015-10-14 11:18:57 · 171 阅读 · 0 评论 -
[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 · 207 阅读 · 0 评论 -
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 · 217 阅读 · 0 评论 -
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 · 219 阅读 · 0 评论 -
[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 · 208 阅读 · 0 评论 -
[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 · 195 阅读 · 0 评论 -
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 · 241 阅读 · 0 评论 -
[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 · 203 阅读 · 0 评论 -
[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 · 189 阅读 · 0 评论 -
【Leetcode】Gas Station #134
没有想出来,查阅后发现题目思路很巧妙,详见:http://www.programcreek.com/2014/03/leetcode-gas-station-java/美中不足的是fact1的证明网上很少或者说不是很流畅,但是自己也写不出好的证明class Solution {public: int canCompleteCircuit(vector& gas, vector原创 2015-08-17 03:44:11 · 260 阅读 · 0 评论 -
【Leetcode】PlusOne #66
很简单,不多说class Solution {public: vector plusOne(vector& digits) { int i = digits.size() - 1; int cur = 1; while(cur && i != -1) { int temp = digits[i] + cur;原创 2015-08-16 03:08:42 · 364 阅读 · 0 评论 -
【Leetcode】Remove Element #27
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 length.题原创 2015-08-17 07:58:34 · 262 阅读 · 0 评论 -
【Leetcode】Longest Common Prefix #14
Write a function to find the longest common prefix string amongst an array of strings.最长的common prefix肯定是随着比较的增加不断减少的,所有设max_common_length_possible, 每次比较最多不会超过这个数,比较完后再update此数值。class Solution {p原创 2015-08-16 03:33:58 · 230 阅读 · 0 评论