LeetCode
文章平均质量分 64
zhaoyuxiang126
这个作者很懒,什么都没留下…
展开
-
LeetCode---(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-26 19:59:55 · 376 阅读 · 0 评论 -
LeetCode---(6)ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I转载 2015-07-10 19:59:19 · 194 阅读 · 0 评论 -
LeetCode---(107)Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-07-11 13:46:31 · 174 阅读 · 0 评论 -
LeetCode---(8)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 input ca转载 2015-07-10 22:11:45 · 203 阅读 · 0 评论 -
LeetCode---(102)Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20转载 2015-07-11 11:10:52 · 192 阅读 · 0 评论 -
LeetCode---(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 is原创 2015-07-10 14:58:28 · 285 阅读 · 0 评论 -
LeetCode---(21)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./** * Definition for singly-linked list. * struct转载 2015-06-06 22:53:48 · 205 阅读 · 0 评论 -
LeetCode---(88)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 addit原创 2015-06-05 22:13:55 · 228 阅读 · 0 评论 -
LeetCode---(92) Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy t转载 2015-05-19 17:03:03 · 159 阅读 · 0 评论 -
LeetCode---(83) 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-19 21:23:21 · 209 阅读 · 0 评论 -
LeetCode---(16) 3 Sum 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 have exact原创 2015-05-10 21:28:05 · 278 阅读 · 0 评论 -
LeetCode---(73) Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.第一种:O(m + n) space O(m*n) timeclass Solution {public: void setZeroes(vector>& matrix) {原创 2015-05-18 14:27:56 · 301 阅读 · 0 评论 -
LeetCode---(31) Next Permutation
题意寻找比当前排列顺序大的下一个排列。因为降序序列是没法变的更大的,所以从后往前找到第一个升序对的位置。然后就存在调整大小排列顺序的可能,从后往前找到比当前位置大的元素,交换之。当前位置后面的元素还是降序排列,将他们反转得到最小顺序排列。其实就是原来当前位置元素后面是最大的排列,而交换后的新元素之后是最小的排列,他们就是相邻的顺序。当不存在升序,则当前排列是原创 2015-05-13 22:39:30 · 217 阅读 · 0 评论 -
Leetcode---(38)Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as转载 2015-06-15 19:02:07 · 169 阅读 · 0 评论 -
LeetCode---(118)Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]将该三角形的左边对齐,就能够发现,tri[i][j] =转载 2015-06-15 22:54:02 · 232 阅读 · 0 评论 -
LeetCode---(7)Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c转载 2015-06-13 16:36:28 · 211 阅读 · 0 评论 -
LeetCode---(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-21 12:49:24 · 226 阅读 · 0 评论 -
LeetCode---(154)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-07-16 20:10:36 · 243 阅读 · 0 评论 -
LeetCode---(50)Pow(x, n)
Implement pow(x, n).pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了。class Solution {public: double myPow(do转载 2015-07-16 21:26:20 · 270 阅读 · 0 评论 -
LeetCode---(69)Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.利用二分法查找:第一种可能性是直接找到能够麻烦要求的数;第二种可能性是找到相邻的两个数,可以比较两个数哪一个离target更近,不过题目当中希望找的是更小的那个数。class Solution {public: int mySqr转载 2015-07-16 22:13:13 · 228 阅读 · 0 评论 -
LeetCode---(153)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-07-16 19:08:09 · 225 阅读 · 0 评论 -
LeetCode---(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.empty(转载 2015-07-15 20:42:41 · 176 阅读 · 0 评论 -
LeetCode---(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() -- Return whet转载 2015-07-15 21:18:41 · 193 阅读 · 0 评论 -
LeetCode---(106)Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for a binary tree node. * struct Tr原创 2015-07-15 18:48:33 · 214 阅读 · 0 评论 -
LeetCode---(105)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.第一个元素即a是根节点,从对应的中序中找到a。从而进一步知道其左边的b在左树中,其右边的c在右树中,这样结合前原创 2015-07-15 17:28:52 · 183 阅读 · 0 评论 -
LeetCode---(151)Reverse Words in a String
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".class Solution {public: void reverseWords(string &s) {转载 2015-07-26 22:28:15 · 275 阅读 · 0 评论 -
LeetCode---(197)Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve转载 2015-07-26 16:16:55 · 352 阅读 · 0 评论 -
LeetCode---(13)Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.罗马数字有如下符号:基本字符IVXLCDM对应阿拉伯数字15105010转载 2015-06-15 20:03:54 · 227 阅读 · 0 评论 -
LeetCode---(15) 3 sum
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-05-09 22:22:48 · 311 阅读 · 0 评论 -
LeetCode---(26) Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place wi转载 2015-05-13 16:51:21 · 254 阅读 · 0 评论 -
LeetCode---(67) Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".两种方法使用相同的思路,只是使用的函数不一样class Solution {public: string addBinary(string a转载 2015-05-22 15:58:19 · 273 阅读 · 0 评论 -
LeetCode---(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-06-23 15:35:04 · 273 阅读 · 0 评论 -
LeetCode---(152)Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest转载 2015-06-21 20:01:37 · 260 阅读 · 0 评论 -
LeetCode---(137)Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.之前做过了数组中其他数出现两次,仅有一个出现一次的,直接用所有元素异或就行了(只要是偶数次,都可以用这个方法),本题变为其他元素出现3次,而且时间复杂度要求线性,空间为常数。解法为:转载 2015-06-19 22:15:14 · 230 阅读 · 0 评论 -
LeetCode---(53)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-06-21 10:50:55 · 283 阅读 · 0 评论 -
LeetCode---(164)Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements转载 2015-06-19 20:31:53 · 196 阅读 · 0 评论 -
Leetcode---(104)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.首先考虑递归/** * Definition for a bina转载 2015-06-17 22:03:51 · 180 阅读 · 0 评论 -
LeetCode---(100)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-06-17 21:34:54 · 223 阅读 · 0 评论 -
LeetCode---(119)Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].方法一:由于之前我们曾经遇到过建立Pascal三角形,所以我们自然联想到建立一个K+1的三角形,然后取出来第K行,就得到所求的结果。class Soluti转载 2015-06-16 22:41:23 · 190 阅读 · 0 评论 -
LeetCode---(125) Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a转载 2015-05-21 18:18:16 · 244 阅读 · 0 评论