
LeetCode
文章平均质量分 58
My LeetCode solution with explanation.
chfe910
这个作者很懒,什么都没留下…
展开
-
LeetCode[Backtracking]: Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For exampl原创 2015-03-23 15:54:15 · 1020 阅读 · 0 评论 -
LeetCode[String]: Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.这个题目非常简单,只要弄清楚题意就可以非常快地解决,我的C++代码实现如下: string longestCommonPrefix(vector<string> &strs) { if (strs.empt原创 2015-03-04 17:06:28 · 989 阅读 · 0 评论 -
LeetCode[stack]: 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. - to原创 2015-03-04 16:47:40 · 1004 阅读 · 0 评论 -
LeetCode[String]: Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be原创 2015-03-03 15:47:08 · 808 阅读 · 0 评论 -
LeetCode[Math]: Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.欲了解罗马数字的拼写规则请参考:http://blog.youkuaiyun.com/chfe007/article/details/44037079C++代码实现如下: string原创 2015-03-03 14:46:32 · 779 阅读 · 0 评论 -
LeetCode[Math]: Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.这个题目首先要了解罗马数字的拼写规则(以下引自维基百科):罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的规则原创 2015-03-03 13:36:26 · 908 阅读 · 1 评论 -
LeetCode[Tree]: Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next()原创 2015-03-03 00:23:08 · 1345 阅读 · 0 评论 -
LeetCode[BFS]: Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. For example, X X X X X O O X原创 2015-03-02 18:29:07 · 736 阅读 · 0 评论 -
LeetCode[Map]: 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原创 2015-03-02 13:14:45 · 825 阅读 · 0 评论 -
LeetCode[Sort]: Largest Number
LeetCode[Sort]: 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 953433原创 2015-02-17 19:55:54 · 865 阅读 · 0 评论 -
LeetCode[Linked List]: Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.原创 2014-11-01 19:53:53 · 848 阅读 · 0 评论 -
LeetCode[Array]: 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 length.方原创 2014-10-31 00:38:11 · 892 阅读 · 0 评论 -
LeetCode[Array]: Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.一开始我误解了题目的意思,以为是新建一个原创 2014-10-31 21:08:36 · 1127 阅读 · 0 评论 -
LeetCode[Array]: Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B.原创 2014-10-31 22:19:23 · 632 阅读 · 0 评论 -
LeetCode[Array]: 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]]这个问题比较简单,大家的解法大同小异。时间复杂度都原创 2014-10-31 23:18:42 · 811 阅读 · 0 评论 -
LeetCode[Linked list]: Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.看到这个题目感到奇怪的是为什么是“右旋”,而不是“左旋原创 2014-11-02 12:21:37 · 781 阅读 · 0 评论 -
LeetCode[Linked List]: 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./** * Definition for singly-linke原创 2014-11-02 20:17:27 · 723 阅读 · 0 评论 -
LeetCode[Linked List]: 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 end, the linked原创 2014-11-03 19:25:18 · 862 阅读 · 0 评论 -
LeetCode[Linked List]: 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 the f原创 2014-11-04 19:45:06 · 650 阅读 · 0 评论 -
LeetCode[Linked List]: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1原创 2014-11-04 20:42:59 · 836 阅读 · 0 评论 -
LeetCode[stack]: 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 valid bu原创 2014-11-04 23:37:20 · 750 阅读 · 0 评论 -
LeetCode[string]: 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 defin原创 2014-11-06 13:02:45 · 790 阅读 · 0 评论 -
LeetCode[Array]: 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 the原创 2014-11-06 16:14:30 · 773 阅读 · 0 评论 -
LeetCode[Linked List]: Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of t原创 2014-11-22 17:59:03 · 711 阅读 · 0 评论 -
LeetCode[Hash Table]: Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially filled su原创 2014-11-24 01:04:27 · 1513 阅读 · 0 评论 -
LeetCode[Hash Table]: 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, where原创 2014-11-24 13:51:39 · 1360 阅读 · 3 评论 -
LeetCode[Hash Table]: Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思路:对每一个单词的所有字母按照字典顺序排序,排序结果作为key,所有具有相同key的单词组合在一起成为一个Anagram group。最后返回所有的Anag原创 2014-11-25 20:42:18 · 1033 阅读 · 0 评论 -
LeetCode[Array]: 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].Note:Could you optimize your algorithm to use only O(k) extra space?这个问题比较简单。v原创 2014-11-26 13:35:37 · 831 阅读 · 0 评论 -
LeetCode[Linked List]: 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?这个题目如果没有空间复杂度O(1)的限制,我可以想到的方法就是:遍历整个list,将每个节点的地址存入一个vector,如果发现某个节点的next的地址已经在vec原创 2014-11-26 17:04:27 · 818 阅读 · 0 评论 -
LeetCode[Linked List]: Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?这个题目跟Linked List Cycle一样,我也没有能够自己独立找到解决方法,原创 2014-11-26 17:47:34 · 885 阅读 · 0 评论 -
LeetCode[String]: Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".我的C++实现代码如下:string addBinary(string a, string b) { string sum; int c原创 2014-11-26 20:53:40 · 839 阅读 · 0 评论 -
LeetCode[Tree]: Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).Recursive Algorithmclass Solution {public: bool isSymmetric(TreeNode *root) { return原创 2014-11-27 01:01:44 · 682 阅读 · 0 评论 -
LeetCode[Tree]: 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).这个题目比较简单,借助容器queue即可完成二叉树的层序遍历。我的C++实现代码如下:vector > levelOrder(TreeNode *原创 2014-11-27 22:33:39 · 579 阅读 · 0 评论 -
LeetCode[Tree]: 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).这个简单的问题可以这样解决:利用LeetCode[Tree]: Binary Tree Level原创 2014-11-27 23:37:18 · 563 阅读 · 0 评论 -
LeetCode[Linked List]: Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.Notes:If the two linked lists have no intersection at all, return null.The linked lists must retain t原创 2014-11-28 20:31:58 · 731 阅读 · 0 评论 -
LeetCode[Array]: 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.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad ide原创 2014-11-30 15:33:45 · 656 阅读 · 0 评论 -
LeetCode[Array]: Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row原创 2014-11-30 15:55:20 · 743 阅读 · 0 评论 -
LeetCode[Tree]: Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode {TreeLinkNode left;TreeLinkNode right;TreeLinkNode next;}Populate each next pointer to point to its next right node. If there is no next right node,原创 2014-11-30 21:25:31 · 664 阅读 · 0 评论 -
LeetCode[Tree]: 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].这个题目的递归算法很简单:class Solution {public: v原创 2014-11-30 23:09:47 · 560 阅读 · 0 评论 -
LeetCode[Tree]: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given1/ \2 5/ \ \3 4 6The flattened tree should look like:1\2\3\4\5\6递归算法这种问题非常适合用原创 2014-12-02 13:42:30 · 686 阅读 · 0 评论