
leetcode
ppplinday
这个作者很懒,什么都没留下…
展开
-
12. Integer to Roman 和 13. Roman to Integer
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.把一个数字变成罗马数字,直接递归暴力,代码有点丑23333class Solution {public: string intToRoman(int num) {原创 2016-07-22 16:15:53 · 230 阅读 · 0 评论 -
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.求最长公共前缀,直接暴力class Solution {public: string longestCommonPrefix(vector<string>& strs) { string s = "";原创 2016-07-22 16:51:31 · 228 阅读 · 0 评论 -
15. 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: The solution set must not contain duplic原创 2016-07-23 11:49:54 · 197 阅读 · 0 评论 -
16. 3Sum 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 exactly原创 2016-07-23 15:25:52 · 178 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string “23”原创 2016-07-26 16:14:08 · 226 阅读 · 0 评论 -
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution set mu原创 2016-07-26 16:35:56 · 213 阅读 · 0 评论 -
1. Two Sum QuestionEditorial Solution My Submissions
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl原创 2016-07-15 18:10:03 · 425 阅读 · 0 评论 -
2. 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 linke原创 2016-07-15 20:58:19 · 236 阅读 · 0 评论 -
19. 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 list be原创 2016-07-27 10:33:44 · 256 阅读 · 0 评论 -
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 valid but “原创 2016-07-27 10:49:55 · 199 阅读 · 0 评论 -
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 ListNode原创 2016-07-27 10:53:54 · 181 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le原创 2016-07-16 11:07:03 · 352 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1: nums1 = [1, 3] num原创 2016-07-16 21:30:47 · 284 阅读 · 0 评论 -
5. Longest Palindromic Substring 和 poj3974
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 这两题都是给你一个字符串,求出最长回文子串。在以前原创 2016-07-18 20:47:59 · 279 阅读 · 0 评论 -
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 N A P L S I I G原创 2016-07-19 21:29:08 · 295 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321水题没啥好说的,就一个小陷阱,如果转换后的数大于int的范围就return0。class Solution {public: int reverse(int x) { if(x == 0)原创 2016-07-20 10:20:44 · 228 阅读 · 0 评论 -
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 cases.N原创 2016-07-20 10:43:59 · 212 阅读 · 0 评论 -
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.没啥好说,判断一个数字是否为回文数字。class Solution {public: bool isPalindrome(int x) { int newx = 0; int xx = x; wh原创 2016-07-20 10:49:29 · 228 阅读 · 0 评论 -
11. Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2016-07-22 11:12:09 · 303 阅读 · 0 评论 -
86. Partition List
这题1刷只想到用新的点,2刷试试不用新的点,直接在原链表中运行。 Ps 链表的申请 复习/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * };原创 2017-02-07 01:15:19 · 242 阅读 · 0 评论 -
87. Scramble String
一开始以为很难, 其实就是递归判断,只要第一次做错的原因是我以为分割点是中间,其实是每一个点都可以是分割点,想想2叉树就想懂的。 2刷应该就用dp吧, 这个也在手动刷一次!理解多一次class Solution {public: bool isScramble(string s1, string s2) { if(s1 == s2) return true;原创 2017-02-07 01:16:05 · 161 阅读 · 0 评论 -
88. Merge Sorted Array
案例很奇怪的一题,其实超级简单,但是奇怪,2刷看代码就ok,不用刷了class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i = m - 1; int j = n - 1; int k = m + n - 1原创 2017-02-07 01:16:58 · 166 阅读 · 0 评论 -
89. Gray Code
规律题, 如果模拟的话或者递归的话应该超级烦 2刷可以看看其他方法,个人觉得不用刷class Solution {public: vector<int> grayCode(int n) { vector<int>ve; ve.push_back(0); if(n == 0) return ve; ve.push_back(1)原创 2017-02-07 01:17:40 · 144 阅读 · 0 评论 -
90. Subsets II
这题我是改了之前的那个就ac了,2刷一定要刷,跟第一道一起刷,因为这个dfs有问题的,会出现重复,需要改!!!class Solution {public: vector<vector<int>>fve; set<vector<int>>ve; set<vector<int>>ve1; vector<int>vee; void dfs(vector<int>原创 2017-02-07 01:18:17 · 143 阅读 · 0 评论 -
92. Reverse Linked List II
题意易懂,就是烦,要判断m是否是1,2刷想个更好的方法,最好是什么情况都通用的方法。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */原创 2017-02-07 01:19:29 · 143 阅读 · 0 评论 -
93. Restore IP Addresses
无敌代码暴力。。。。。,2刷还是乖乖dfs吧class Solution {public: vector<string> restoreIpAddresses(string s) { vector<string> ret; string ans; for (int a=1; a<=3; a++) for (int b=1; b<原创 2017-02-08 00:19:08 · 160 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
简单题, 中序遍历/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-02-08 00:22:08 · 167 阅读 · 0 评论 -
95. Unique Binary Search Trees II
思路跟96一样,就是烦了一点,在右边的时候需要加上n,注意一点!!错了很多的 就是要new 新的点!!!别重复使用!!!每次生成都要new,2刷可以刷,应该有其他做法!/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNod原创 2017-02-08 00:23:31 · 176 阅读 · 0 评论 -
96. Unique Binary Search Trees
找规律,对于每一次的n,都可以看成有不同的数目作为根节点,然后再算这个根节点的左右有多少的不同的节点,就有多少种可能,最后左右相乘就okclass Solution {public: int numTrees(int n) { vector<int>ve; ve.push_back(1); ve.push_back(1); v原创 2017-02-08 00:24:29 · 166 阅读 · 0 评论 -
97. Interleaving String
dfs: 题意就是s1和s2是否可以交叉变成s3,一开始我使用dfs,但是超时了 然后剪枝,加入一个标记数组,如果dfs过的就不进行dfs,然后就ac了class Solution {public: int mapp[500][500]; int mark = 0; void pipei(string s1, string s2, string s3, int a, i原创 2017-02-08 00:26:24 · 174 阅读 · 0 评论 -
98. Validate Binary Search Tree
这题要多想,首相二叉平衡树的判断一定要从全局来看,分别大于小于某个数!!!,第二点,我被最大最小值卡了,所以就用了long long int然后改最大最小值,2刷刷好看一点的代码,思路要更加清晰/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *原创 2017-02-08 00:27:28 · 164 阅读 · 0 评论 -
99. Recover Binary Search Tree
做这题的时候发现其实上一题的做法是错误的,BST的中序排序是一个上升序列,所以判断的时候只需要按中序排序, 然后判断是不是上升的就ok!所以98也是这样做的!!! 至于这题,为什么n1 和 n2 记录的不一样呢? N1 是记录pre的而n2是记录root的 因为你在纸上写一个上升序列,然后换砖两个数,你就会发现原因!!!/** * Definition for a binary tree no原创 2017-02-08 00:28:31 · 196 阅读 · 0 评论 -
100. Same Tree
没什么好说的,判断两个数是不是一样,简单题/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NUL原创 2017-02-08 00:29:13 · 797 阅读 · 0 评论 -
101. Symmetric Tree
必须看看学习一下代码,!!!!!! 可以的这一题!!/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), rig原创 2017-02-08 00:29:56 · 260 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
简单题 bfs搞定/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-02-08 00:30:36 · 159 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
跟上一题一样,就最后换了vector的顺序。。。下次就应该在queue中换顺序吧/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), le原创 2017-02-08 00:34:15 · 176 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
求树的深度,简单题/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-02-08 00:35:56 · 144 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
必须要学习的代码,很骚!/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *原创 2017-02-08 00:37:02 · 163 阅读 · 0 评论 -
127. Word Ladder
单向bfs 方法也是想到和写了出来,就是没有用find这个方法导致超时!!!用了iterator 其实就是bfs不断判断是否是那就ok了,数据结构的使用方法还是有点弱!class Solution {public: void addnext(string s, queue<string>& qu, unordered_set<string>& wordList){ wo原创 2017-02-17 11:49:04 · 272 阅读 · 0 评论 -
128. Longest Consecutive Sequence
有意思的一题,首先,时间需要时on,这个时间想到的只有hashmap,treemap不行,因为tree的操作的ologn,hasdmap是o1,然后遍历一次。如果这个数的左右两边没数,则·dp=1 有数的话,就更新一段联系区间的最左右的端点的dp值,dp值表示包括这个数在内的连续最大长度。所以时间上应该是on,其实我觉得是o3n,不过好像题解就是这个方法。。。。 2刷学习一下别人6巷代码的写法原创 2017-02-17 11:50:03 · 284 阅读 · 0 评论