![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
FreeeLinux
A3GM5H3647LPD4QVYMYAER2S47P4WZ2UKBXBCGZES2V62WEY2HULQA
展开
-
448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you原创 2017-03-17 02:06:50 · 328 阅读 · 0 评论 -
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原创 2017-03-21 14:25:43 · 337 阅读 · 0 评论 -
532. K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in原创 2017-03-11 16:40:41 · 1088 阅读 · 0 评论 -
459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Englis原创 2017-03-06 15:02:19 · 648 阅读 · 0 评论 -
148. Sort List(快排、归并)
Sort a linked list in O(n log n) time using constant space complexity.首先给出快排解法:class Solution {public: ListNode *sortList(ListNode *head) { if(head == NULL) return NULL;原创 2017-02-20 20:34:33 · 453 阅读 · 0 评论 -
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 with cons原创 2017-03-05 21:26:18 · 252 阅读 · 0 评论 -
28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.class Solution {public: int strStr(string haystack, string needle) {原创 2017-03-05 19:54:14 · 296 阅读 · 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 "原创 2017-03-05 12:59:04 · 296 阅读 · 0 评论 -
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Example:Input:原创 2017-03-05 02:23:04 · 306 阅读 · 0 评论 -
214. Shortest Palindrome(KMP)
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.For exam原创 2016-11-14 18:25:00 · 548 阅读 · 0 评论 -
55. Jump Game(贪心)
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are原创 2017-02-22 15:41:31 · 343 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.解法一:BFSclass Solution {public: int min原创 2017-02-22 12:24:41 · 295 阅读 · 0 评论 -
208. Implement Trie (Prefix Tree)--Trie树
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.我的解法如下:const int MAX_NODE = 26;struct TrieNode { long count;原创 2017-02-22 11:08:30 · 387 阅读 · 0 评论 -
51. N-Queens/52. N-Queens II
八皇后问题参考这张图片: 51The 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 p原创 2017-02-22 00:15:36 · 372 阅读 · 0 评论 -
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not原创 2017-02-21 17:40:33 · 486 阅读 · 0 评论 -
383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; oth原创 2017-03-11 18:40:02 · 357 阅读 · 0 评论 -
67. Add Binary
``c++ Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.```c++class Solution {public: string addBinary(string a, string b) {原创 2017-03-11 20:32:23 · 376 阅读 · 0 评论 -
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-03-17 01:14:03 · 472 阅读 · 0 评论 -
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.这道题让我印象深刻啊,库函数用好就是好。这道题意思是求一个字符串数组,我们求出数组中所有元素的最长公共前缀。所以我们初始以strs[0]为最长公共前缀,然后在后面其他字符串中find,如果匹配,返回首位置必须为0,否则我们需要缩小原创 2017-03-25 10:02:22 · 394 阅读 · 0 评论 -
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.Subscribe to see which companies asked this question.这道题没什么难的,唯一难的就是鬼知道罗马数字都有什么。实际上对于罗马数字举例:4->原创 2017-03-24 00:37:57 · 351 阅读 · 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 l原创 2017-03-22 16:28:25 · 424 阅读 · 0 评论 -
409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not consider原创 2017-03-13 02:27:16 · 346 阅读 · 0 评论 -
389. Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added in原创 2017-03-13 01:55:06 · 347 阅读 · 0 评论 -
349. Intersection of Two Arrays/350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be unique.The result can be in an原创 2017-03-13 01:34:41 · 452 阅读 · 0 评论 -
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.Examples:p原创 2017-03-13 01:09:13 · 384 阅读 · 0 评论 -
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may assume the stri原创 2017-03-12 23:23:13 · 349 阅读 · 0 评论 -
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara原创 2017-03-12 23:03:51 · 333 阅读 · 0 评论 -
204. Count Primes(埃拉托色尼)
Description:Count the number of prime numbers less than a non-negative number, n.Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.class Solution {public: in原创 2017-03-12 20:33:43 · 328 阅读 · 0 评论 -
202. Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of i原创 2017-03-12 19:41:54 · 265 阅读 · 0 评论 -
63. Unique Paths II[dp]
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For ex原创 2017-03-21 18:20:37 · 390 阅读 · 0 评论 -
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 it原创 2017-03-21 16:39:53 · 405 阅读 · 0 评论 -
147. Insertion Sort List(插入排序)
Sort a linked list using insertion sort.单链表的插入排序:class Solution {public: ListNode* insertionSortList(ListNode* head) { ListNode* new_head = NULL; while(head != NULL){ L原创 2017-02-21 16:52:51 · 380 阅读 · 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 l原创 2017-02-21 16:06:21 · 314 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters [DP]
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 "原创 2016-12-07 16:43:58 · 482 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321代码如下:class Solution {public: int reverse(int x) { long long res = 0; while原创 2016-12-07 03:31:01 · 333 阅读 · 0 评论 -
413. Arithmetic Slices
PS:说好的一天一道leetcode,前天停电,今天打算补3道,昨天其实还写了一道,太简单,就没有往博客上放。A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive eleme原创 2016-11-14 19:38:10 · 352 阅读 · 0 评论 -
453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1,2原创 2016-11-10 18:31:49 · 340 阅读 · 0 评论 -
287. Find the Duplicate Number(链表判环)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,原创 2016-11-07 01:17:55 · 339 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
PS:这是我真正意义上一次AC的第一道题,以前出现过的虽然程序没问题,但是可能有手误打错出现编译错误,然后轻松修改提交。这次连手抖都没有了:)Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).Fo原创 2016-11-15 20:27:20 · 334 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2016-11-14 22:10:05 · 296 阅读 · 0 评论