
leetcode
文章平均质量分 75
i_dont_give_a_fxxk
专注。。。坚持。。。
展开
-
Text Justification
这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。[1]原创 2014-08-04 13:53:06 · 431 阅读 · 0 评论 -
LRU Cache & Max Points on a Line & Valid Number
(1) LRU CacheLRU简洁明了的算法描述[1],就是用一个双向链表+map,不用map查找的话就要遍历了。。。时间复杂度就上升了。双向链表的好处就是。。。用map定位到那个节点,然后很方便的移动或者删除啊什么的,单向就做不到啦,因为你要删除还要找prev双向链表就不写了,用stl的list代替原创 2014-08-03 10:51:07 · 553 阅读 · 0 评论 -
Surrounded Regions & Wildcard Matching
(1) Surrounded Regions 首先,外围一圈上的O肯定会保留下来。然后,从外围的O能达到的O也要保留。剩下其他的O就是内部的O。所以方法就是从外围的一圈进行DFS算法。特殊用例:只有外围轮廓没有内部。比如长或者宽小于等于2,此时不存在被包围的'X'。[1]参考:[1]原创 2014-08-02 15:07:05 · 418 阅读 · 0 评论 -
Decode Ways & String to Integer & Reverse Words in a String
(1) Decode Ways 第一: s[i-1]不能是0,如果s[i-1]是0的话,number[i]就只能等于number[i-2]第二,s[i-2,i-1]中的第一个字符不能是0,而且Integer.parseInt(s.substring(i-2,i))获得的整数必须在0到26之间。[1]class Solution {private: bool isValid原创 2014-08-02 09:15:32 · 451 阅读 · 0 评论 -
Substring with Concatenation of All Words & Median of Two Sorted Arrays & Divide Two Integers
(1) Substring with Concatenation of All Words原创 2014-08-01 10:18:21 · 458 阅读 · 0 评论 -
Word Ladder I && II
(1) Word LadderBFS(宽度优先搜索),但是我们可以假想构建一个图,其中图中的每个顶点都是我们的元素,点和点是如何联系起来的呢?如果一个单词通过改变一次字母,能够变成另外一个单词,我们称之为1 edit distance 距离(是不是想起了leetcode中edit distance那道题目了?)所以,图中的所有相邻元素都是edit distance 距离为1的元素。那么,我们只原创 2014-07-31 20:53:56 · 726 阅读 · 0 评论 -
Interleaving String & Candy & Minimum Window Substring
(1) Interleaving String动态规划,原创 2014-07-31 11:25:13 · 456 阅读 · 0 评论 -
Simplify Path & Word Search & Longest Valid Parentheses
(1) Simplify Path用栈来做,先把输入字符串以'/'为分隔符分来,如果遇到'.'或者空输入什么都不做。如果遇到'..'就弹栈。然后每从栈里退出一个元素就用'/'连接起来,注意顺序。原创 2014-07-29 11:00:54 · 523 阅读 · 0 评论 -
Insert Interval & *Regular Expression Matching & Binary Tree Maximum Path Sum
(1) Insert Interval 利用merge intervals的成果:bool comp(Interval a,Interval b) { return a.start < b.start; } class Solution {private: vector merge(vector &intervals) {原创 2014-07-28 15:10:18 · 430 阅读 · 0 评论 -
Restore IP Addresses & Sort List & Reorder List
(1) Restore IP Addressesdfs, 几点注意的地方[1]:1. 在验证字符串是否是数字的时候,要注意0的情况,001,010,03都是非法的。所以,如果第一位取出来是0,那么我们就判断字符串是否是"0",不是的情况都是非法的。2. 取字符串的时候,注意位数不够的问题,不仅原创 2014-07-27 13:57:08 · 404 阅读 · 0 评论 -
Merge Intervals & *Longest Palindromic Substring & Multiply Strings
(1) Merge Intervals先对区间按照左边界排序,然后顺序扫描,合并重叠的区间即可。 在原区间数组上操作,不使用额外的空间,但是需要删除多余的区间,这样会移动数组元素.原创 2014-07-26 16:40:49 · 454 阅读 · 0 评论 -
Sudoku Solver & Word Break I && *II
(1) Sudoku Solver注意细节[1]:原创 2014-07-25 16:43:51 · 581 阅读 · 0 评论 -
Implement strStr() & Largest Rectangle in Histogram & Maximal Rectangle
(1) Implement strStr() 暴力法原创 2014-07-23 20:24:36 · 364 阅读 · 0 评论 -
Permutation Sequence
可以用递归遍历所有可能的排列 然后找出第k个。这样时间复杂度会很高.仔细想一下可以找到一下规律:n个数的的第k个排列为:a1, a2, a3,...an;接下来我们一个一个数的选取,如何确定第一个数应该是哪一个呢?选取第一个数后剩下全排列的个数为(n-1)! 所以选取的第一个数应该为第 K1 = k;a1 = K1/(n-1)!位数字同理当选完a1后只剩下n-1个数字,原创 2014-07-22 20:56:24 · 361 阅读 · 0 评论 -
Longest Substring Without Repeating Characters & Rotate List
(1) Longest Substring Without Repeating Characters 此题用一个hash table保存每个字符上一次出现过的位置。从前往后扫描,假如发现字符上次出现过,就把当前子串的起始位置start移动到上次出现过的位置之后——这是为了保证从start到i的当前子串中没有任何重复字符。同时,由于start移动,当前子串的内容改变,start移动过程中经历的字原创 2014-07-21 19:49:12 · 310 阅读 · 0 评论 -
First Missing Positive & Clone Graph & Sqrt(x)
(1) First Missing Positive根据[1]: 贪心的策略,O(n)的循环把数字放到其对应的位置,即满足A[i]=i+1,就能保证每次交换就是有意义的。如果当前位置已经存在正确的值,就不交换,否则会死循环。最后在扫描一遍,如果当前位置上数字不对就输出,如果都正确,就没有漏的数字,输出n+1。原创 2014-07-19 16:10:33 · 369 阅读 · 0 评论 -
Scramble String
根据[1]:使用了一个三维数组boolean result[len][len][len],其中第一维为子串的长度,第二维为s1的起始索引,第三维为s2的起始索引。result[k][i][j]表示s1[i...i+k]是否可以由s2[j...j+k]变化得来。class Solution {public: bool isScramble(string s1, string s2) {原创 2014-07-18 16:20:10 · 508 阅读 · 0 评论 -
Copy List with Random Pointer & Add Two Numbers & Valid Palindrome
(1) Copy List with Random Pointer原创 2014-07-17 16:00:38 · 371 阅读 · 0 评论 -
Insertion Sort List & Gas Station & Distinct Subsequences
(1) Insertion Sort Listclass Solution {public: ListNode *insertionSortList(ListNode *head) { if(!head || !head->next) return head; ListNode begin(0);原创 2014-06-19 14:23:53 · 420 阅读 · 0 评论 -
Recover Binary Search Tree & Edit Distance & Reverse Nodes in k-Group
(1) Recover Binary Search Tree这题的要点就是想到使用树的递归中序遍历,因为二叉查找树合法的情况,中序遍历的值是从小到大排列的。当出现当前值比前一个值小的时候,就是存在不合法的节点。用pre存中序遍历时当前节点的前一个节点,方便值的大小对比,用s1,s2记录这两个不合法序列的位置,s1存较大的值,s2存较小的值。最后把两个不合法的值交原创 2014-06-18 14:54:20 · 613 阅读 · 0 评论 -
Reverse Linked List II && Add Binary
(1) Reverse Linked List IIclass Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode *begin,*end, *pre, *cur; ListNode init(0); i原创 2014-06-05 15:22:32 · 451 阅读 · 0 评论 -
Palindrome Partitioning I && II
(1) Palindrome Partitioning动态规划,原创 2014-06-04 15:39:54 · 396 阅读 · 0 评论 -
Combination Sum I && II && Validate Binary Search Tree
(1) Combination Sum利用深度优先遍历BFS[1]:原创 2014-06-03 15:37:35 · 498 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal & Partition List & Letter Combinations of a Phone Number
(1) Binary Tree Zigzag Level Order Traversal class Solution {public: vector > zigzagLevelOrder(TreeNode *root) { vector> ret; int flag=0; queue que; if(原创 2014-05-23 17:59:55 · 472 阅读 · 0 评论 -
Triangle & Count and Say
(1) Triangle 动态规划,从下往上gengx原创 2014-05-22 16:05:31 · 628 阅读 · 0 评论 -
Longest Consecutive Sequence & Longest Common Prefix & Search for a Range
(1) Longest Consecutive Sequence根据[1]:原创 2014-05-21 18:28:35 · 356 阅读 · 0 评论 -
Valid Sudoku & Subsets I&&II & Flatten Binary Tree to Linked List & Jump Game I&&II
(1) Valid Sudoku就是每一行,每一列,每一个方块的check[1]:原创 2014-05-14 20:24:56 · 480 阅读 · 0 评论 -
Minimum Depth of Binary Tree & Length of Last Word & Trapping Rain Water
(1) Minimum Depth of Binary Treeclass Solution {private: void solve(TreeNode *root, int curDep, int &minDep){ if(!root) return; if(!root->left && !root->right)原创 2014-05-10 16:52:00 · 363 阅读 · 0 评论 -
Palindrome Number & Remove Nth Node From End of List & Sum Root to Leaf Numbers
(1)Palindrome Number[1],注意溢出情况:原创 2014-05-08 10:41:35 · 397 阅读 · 0 评论 -
Spiral Matrix I & II && Search in Rotated Sorted Array I & II
(1) Spiral Matrix一圈圈循环,主要难点在于原创 2014-04-29 15:55:31 · 368 阅读 · 0 评论 -
Container With Most Water & Path Sum I && II
(1) Container With Most Water取最左边的为left,最右边的为right,所有可能比这种大的情况只能是这两个高度较小的那一端向中间移动。一直循环这个动作,最大值肯定在其中。原创 2014-04-27 17:39:15 · 349 阅读 · 0 评论 -
N-Queens && II and Permutations && II && Next Permutation
(1) N-Queens典型的N皇后问题[1]:class Solution {private: void print(int *q, vector> &ret,int n) { vector tmp; for(int i=1;i<=n;i++) { string s(n,'.');原创 2014-04-26 17:42:04 · 678 阅读 · 0 评论 -
Search a 2D Matrix & Set Matrix Zeroes & Combinations
(1) Search a 2D Matrixclass Solution {public: bool searchMatrix(vector > &matrix, int target) { int i=0,j=matrix[0].size()-1; while(i=0) {原创 2014-04-25 18:02:26 · 543 阅读 · 0 评论 -
Rotate Image & Binary Tree Postorder Traversal & Minimum Path Sum
(1) Rotate Image根据[1]:原创 2014-04-25 18:00:19 · 310 阅读 · 0 评论 -
Sort Colors & Plus One
(1) Sort Colors参考了[1]:class Solution {public: void sortColors(int A[], int n) { int r=0,b=n-1,cur=0; while(cur<=b) { if(A[cur]==0)原创 2014-04-24 17:26:56 · 383 阅读 · 0 评论 -
Binary Tree Level Order Traversal I & II
(1) Binary Tree Level Order Traversal原创 2014-04-24 16:31:05 · 335 阅读 · 0 评论 -
Swap Nodes in Pairs & Symmetric Tree & Gray Code
(1) Swap Nodes in Pairs原创 2014-04-17 20:41:09 · 450 阅读 · 0 评论 -
Balanced Binary Tree & Pascal's Triangle I & II
(1) Balanced Binary Tree原创 2014-04-17 20:36:26 · 342 阅读 · 0 评论 -
Convert Sorted Array & List to Binary Search Tree
(1) Convert Sorted Array to Binary Search Tree根据[1]参考原创 2014-04-17 17:04:38 · 352 阅读 · 0 评论 -
Integer to Roman & Roman to Integer
这两道题是原创 2014-04-15 20:49:41 · 371 阅读 · 0 评论