LeetCode刷题记录贴
文章平均质量分 72
一天一道LeetCode, 加油加油
酱油瓶被人注册了
这个作者很懒,什么都没留下…
展开
-
LeetCode 51 | 52. N-Queens (回溯)
经典回溯问题~~~The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle....原创 2018-06-12 16:58:30 · 137 阅读 · 0 评论 -
LeetCode 49. Group Anagrams (哈希表)
这道题肯定是哈希表了,我一开始很傻的自己写了个哈希函数,还是一看就知道是会有重合的那种哈希函数算了 还是用自带的unordered map吧 = = 进行哈希判断重复之前先排个序,原来string还有string.begin( )这个东西,毕竟string也是可以迭代的XDDGiven an array of strings, group anagrams together.Example:In...原创 2018-05-31 10:58:45 · 173 阅读 · 0 评论 -
PAT - 1144 The Missing Number(20 分) || LeetCode 41. First Missing Positive
这道题同时在PAT和LeetCode中出现哈不过PAT并没有要求用 O(1)空间复杂,可是LeetCode要求用 O(1),时间复杂度都适用O(N);1144 The Missing Number(20 分)Given N integers, you are supposed to find the smallest positive integer that is NOT in the give...原创 2018-06-14 13:19:29 · 206 阅读 · 0 评论 -
LeetCode 36 | 37 | 39 | 40 | 46 | 47 . (回溯)
这道题是让你判断现有的数字是不是合法的,也就是说只需要去判断数字,不用去判断空的部分。(其实是数独解法的判断部分)36题的解析:9个数组来记录每行的重复情况,用数组下标来表示该数,比如 row[ 2 ] [ 3 ],表示第二行数字3的情况,如果 row[2][3] = 0,说明第三行已记录的没有3; 如果row[2][3] = 1,说明第二行已记录的已经有3,表示重复,直接return false...原创 2018-06-11 14:12:54 · 196 阅读 · 0 评论 -
LeetCode 34. Search for a Range (二分查找)
所有不会做这道题的人应该好好的去研究下STL源码的lower_bound和upper_bound,两个都是用二分查找法实现的。只要仿照着写就好了。http://www.cplusplus.com/reference/algorithm/lower_bound/http://www.cplusplus.com/reference/algorithm/upper_bound/lower_bound:t...原创 2018-06-09 21:14:39 · 174 阅读 · 0 评论 -
LeetCode 33. Search in Rotated Sorted Array (二分查找)
Suppose an array sorted in ascending order 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]).You are given a target value to search. If found in t...原创 2018-06-09 19:50:36 · 134 阅读 · 0 评论 -
Leetcode 31. Next Permutation
https://www.cnblogs.com/eudiwffe/p/6260699.html我觉得它写的挺好的,STL里面确实有permutation的,而且我觉得写的真的很好permutations后面还有一道题,求全排序的,呜完全可以用STL里的方法来做。Implement next permutation, which rearranges numbers into the lexicog...原创 2018-06-06 21:07:20 · 136 阅读 · 0 评论 -
LeetCode26/27/28 三道easy Remove Duplicates from Sorted Array / Remove Element / Implement strStr( )
26. Remove Duplicates from Sorted Array (就是STL里面的move)Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra ...原创 2018-06-01 16:22:54 · 151 阅读 · 0 评论 -
LeetCode 24. Swap Nodes in Pairs(单链表)
这道题只打败了39%的人,呜呜呜,大家都是 O(N) 线性时间,怎么别人就那么优秀呢- -单链表操作,除了有点烦没其他的。Given a linked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2-...原创 2018-05-31 12:07:34 · 176 阅读 · 0 评论 -
LeetCode 23. Merge k Sorted Lists (单链表 + 递归)
这道题我是用递归做的,注定了只打败了一半的人- - 讲真,这道题和MergeSort根本没有任何差别啊不过我的是尾递归,尾递归总是可以改成while循环的。改了的话应该会快很多吧。Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:In...原创 2018-06-01 15:26:12 · 199 阅读 · 0 评论 -
LeetCode 22. Generate Parentheses (Catalan数字 + 递归)
这题看的第一眼,这不就是Catalan Number么 - - 这不就是递归么。这是我之前学Catalan Number的教材的部分摘录,看完就知道怎么写啦。Catalan numbers:Set P of balanced parentheses strings are recursively defined as• λ ∈ P (λ is empty string)• Ifα,β ∈ P,th...原创 2018-05-29 14:17:25 · 273 阅读 · 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.Example:Input: 1->2->4, 1->3->4 Output: 1->1...原创 2018-05-29 13:13:18 · 110 阅读 · 0 评论 -
LeetCode 20. Valid Parentheses (栈)
这道题比较简单,用一个栈就好了,比什么前缀后缀表达式要简单的多啊有木有 - -Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:Open brackets m...原创 2018-05-28 12:32:59 · 195 阅读 · 0 评论 -
LeetCode 19. Remove Nth Node From End of List (我觉得别人的代码比我更好)
Given a linked list, remove the n-th node from the end of list and return its headExample:Given linked list : 1->2->3->4->5, and n = 2;After removing the second node from the end, the link...翻译 2018-06-06 20:20:31 · 225 阅读 · 0 评论 -
LeetCode18. 4 Sum (双指针)
无论是4 sum还是 3sum都是一样的问题,本质上都是双指针的问题:3 sum是固定一个数,剩下来的双指针; 4sum是固定两个两个数,剩下来的双指针;关于预处理排序我之前见过另外一道题,是3个长度为N的序列,从三个序列中分别选一个数,使其相加为0。可以保证 N^2的复杂度。也是对其中两个先排序,然后可以算出一张表格,呈从上到下,从左到右的增加。那么查找的时候至多 2 * N步。 哎,这种题目就...原创 2018-06-08 10:34:10 · 147 阅读 · 0 评论 -
LeetCode 17. Letter Combinations of a Phone Number (迭代)
迭代和递归一直都是我的弱项(说的好像我有强项一样- - ) 不过迭代和递归感觉很多用在无法确定输出长度的时候。就当作给自己记下来好了Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mappin...翻译 2018-05-28 11:48:20 · 242 阅读 · 0 评论 -
LeetCode 16. 3Sum Closest (预处理排序)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-05-27 20:28:44 · 140 阅读 · 0 评论 -
LeetCode 15. 3Sum (预处理排序)
这道题我也觉得很棒,自己还是用的暴力的方法去做,然后就超时了啦,呜呜呜。。。然后感叹一下讨论区的大佬真聪明。Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives t...原创 2018-05-24 20:00:53 · 170 阅读 · 0 评论 -
LeetCode 14. Longest Common Prefix
这道题我做的不太好,虽然AC了,但是因为要急着去吃饭,所以就不优化了- - Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Example 1:Input: ["f...原创 2018-05-24 17:08:37 · 103 阅读 · 0 评论 -
LeetCode 11. Container With Most Water
前面两道Hard类型的题目 等我彻彻底底弄明白了再仔仔细细写一篇先。这道Medium的题我觉得超级棒的说。出题的人太有水平了,比直方图面积的单调栈要简单,暴力解法会超时,但是O(N)的解法是又精巧又直观,我自己就没想出来,看了提示才知道的。Given n non-negative integers a1, a2, ..., an, where each represents a point at ...原创 2018-05-24 16:46:33 · 159 阅读 · 0 评论 -
LeetCode 9. Palindrome Number
这道题比上一题还无聊 - -最后还说不要让我用整数转字符串,那么就reverse好了,如果reverse后相等,就返回true,10分钟的题;Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Inp...原创 2018-05-24 11:53:41 · 119 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi)
Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this ...原创 2018-05-24 11:40:58 · 144 阅读 · 0 评论 -
LeetCode 7. Reverse Integer ( 简单题,注意INT_MAX 和INT_MIN就可以 )
这道题也是信心题~~Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123 Output: 321Example 2:Input: -123 Output: -321Example 3:Input: 120 Output: 21Note:Assume we are dealing with a...原创 2018-05-21 12:25:51 · 590 阅读 · 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 N A P L S I I G ...原创 2018-05-21 11:52:41 · 123 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring (straight forward DP)
为什么直接到第五题呢- - 因为第四题好难,我得消化一下Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad" Output: "bab" Note: "aba" is al...原创 2018-05-20 19:56:32 · 190 阅读 · 0 评论 -
Leetcode 3. Longest Substring Without Repeating Characters (滑动窗口解法)
话说我有一个小习惯,我会去看一下 Debugu code in playground, 我觉得它的题目的输入输出的代码都写的特别的棒。Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc",...原创 2018-05-17 16:09:04 · 300 阅读 · 0 评论 -
Leetcode 2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2018-05-17 14:03:18 · 129 阅读 · 0 评论 -
LeetCode 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same el...原创 2018-05-17 11:10:10 · 109 阅读 · 0 评论