
leetcode
pynash123
这个作者很懒,什么都没留下…
展开
-
leetcode:链表中环的入口结点
题目://给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/思路:快慢指针,快指针每次前进两个长度,慢指针每次前进一个长度,若存在环,则一定会在环内相遇。假设起点到环入...原创 2019-08-16 14:28:29 · 725 阅读 · 0 评论 -
leetcode binary-tree-maximum-path-sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree,1/ 2 3return6#define max(x,y,z) (x>y?x:y)>z?(x...原创 2019-04-07 16:28:11 · 90 阅读 · 0 评论 -
leetcode candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one cand...原创 2019-04-07 16:28:32 · 192 阅读 · 0 评论 -
leetcode 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.//A剩余的顺...原创 2019-04-07 16:28:48 · 69 阅读 · 0 评论 -
leetcode median-of-two-sorted-arrays
There are two sorted arrays A and B 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)).//二分法 定义findkth函数class Solution {...原创 2019-04-07 16:32:04 · 125 阅读 · 0 评论 -
palindrome-partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s =“aab”,Return[[“aa”,“b”],[“a”,“a”,“...原创 2019-04-07 16:30:23 · 82 阅读 · 0 评论 -
leetcode palindrome-partitioning_ii
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s =“aab”,Return1since t...原创 2019-04-07 16:33:19 · 79 阅读 · 0 评论 -
leetcode insert-interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example...原创 2019-04-07 16:30:45 · 113 阅读 · 0 评论 -
leetcode simplify-path
Given an absolute path for a file (Unix-style), simplify it.For example,path ="/home/", =>"/home"path ="/a/./b/…/…/c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the c..原创 2019-04-07 16:35:44 · 87 阅读 · 0 评论 -
leetcode merge-intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]./** * Definition for an interval. * struct Interval { * ...原创 2019-04-07 16:36:02 · 108 阅读 · 0 评论 -
leetcode interleaving-string
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 =“aabcc”,s2 =“dbbca”,When s3 =“aadbbcbcac”, return true.When s3 =“aadbbbaccc”, return false.//p...原创 2019-04-07 16:37:14 · 97 阅读 · 0 评论 -
leetcode 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, and there exists one unique longest palindromic substring.//自己的解法 O(n^2)class S...原创 2019-04-07 16:27:48 · 82 阅读 · 0 评论 -
leetcode implement-strstr
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.class Solution {public: char *strStr(char *haystack, char *needle) ...原创 2019-04-07 16:23:56 · 111 阅读 · 0 评论 -
leetcode multiply-strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.class Solution {public: string multipl...原创 2019-04-07 16:23:38 · 103 阅读 · 0 评论 -
剑指offer:数组中的逆序对
剑指offer:题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007思路:复用归并排序,在合并排序时稍加修改代码:class Solution {public: int InversePairs(vector<int...原创 2019-07-17 11:41:54 · 126 阅读 · 0 评论 -
leetcode n皇后问题
题目1: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.Ea...原创 2019-07-12 13:47:58 · 126 阅读 · 0 评论 -
leetcode:是否是平衡二叉树
题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树。思路1:首先算出每个子树的深度并保存,然后判断代码1:class Solution {public: bool IsBalanced_Solution(TreeNode* pRoot) { if(NULL == pRoot) { return true; ...原创 2019-07-15 09:58:27 · 122 阅读 · 0 评论 -
leetcode longest-valid-parentheses
Given a string containing just the characters’(‘and’)’, find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has le...原创 2019-04-07 16:35:27 · 128 阅读 · 0 评论 -
leetcode simplify-Path
Given an absolute path for a file (Unix-style), simplify it.For example,path ="/home/", =&gt;"/home"path ="/a/./b/…/…/c/", =&gt;"/c"click to show corner cases.原创 2019-04-07 16:02:08 · 127 阅读 · 0 评论 -
leetcode word-lader
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word mus...原创 2019-04-07 16:02:24 · 98 阅读 · 0 评论 -
leetcode merge-k-sorted-lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.方法1 借助堆,每次new(这里待优化)class Solution {public: ListNode *mergeKLists(vector&lt;ListNode *&gt; &am...原创 2019-04-07 16:02:36 · 118 阅读 · 0 评论 -
leetcode 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 tree...原创 2019-04-07 16:22:18 · 90 阅读 · 0 评论 -
leetcode word_search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically ne...原创 2019-04-07 16:22:43 · 125 阅读 · 0 评论 -
leetcode reverse-linked-list-ii
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL, m = 2 and n = 4,return1->4->3->2->5->NULL.Note:Given...原创 2019-04-07 16:23:02 · 113 阅读 · 0 评论 -
leetcode subsetsII
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate su...原创 2019-04-07 16:38:24 · 96 阅读 · 0 评论 -
leetcode 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->NULLand k =2,return4->5->1->2->3->NULL./** * Definit...原创 2019-04-07 16:39:10 · 167 阅读 · 0 评论 -
leetcode roman-to-integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.I = 1;V = 5X = 10L = 50C = 100D = 500M = 100class Solution {public: int romanToI...原创 2019-04-07 16:52:35 · 106 阅读 · 0 评论 -
leetcode 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 co...原创 2019-04-07 16:52:56 · 120 阅读 · 0 评论 -
leetcode remove-duplicates-from-sorted-array-ii
Follow up for “Remove Duplicates”:What if duplicates are allowed at most twice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].//不是恒...原创 2019-04-07 16:53:18 · 148 阅读 · 0 评论 -
leetcode 4 sum
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:Elements in a qua...原创 2019-04-07 16:53:50 · 133 阅读 · 0 评论 -
leetcode minimum-window-substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S =“ADOBECODEBANC”T =“ABC”Minimum window is&quot;BANC&quot;.Note:If ...原创 2019-04-07 16:01:27 · 91 阅读 · 0 评论 -
leetcode first-missing-positive
Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run in O(n) time and uses constant space.//虽然通过...原创 2019-04-07 16:18:52 · 96 阅读 · 0 评论 -
leetcode substring-with-concatenation-of-all-words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an...原创 2019-04-07 16:20:00 · 90 阅读 · 0 评论 -
leetcode convert-sorted-list-to-binary-search-tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct ListNode { * int val; * List...原创 2019-04-07 16:20:18 · 88 阅读 · 0 评论 -
leetcode unique binary search tree
unique binary search treeGiven n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example,Given n = 3, there are a total of 5 unique BST’s.1 3 3 ...原创 2019-04-07 16:20:47 · 103 阅读 · 0 评论 -
leetcode largest-rectangle-in-histogram
largest-rectangle-in-histogramGiven n non-negative integers representing the histogram’s’ bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a ...原创 2019-04-07 16:21:20 · 119 阅读 · 0 评论 -
leetcode integer-to-roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.I = 1;V = 5X = 10L = 50C = 100D = 500M = 1000class Solution {public: string intT...原创 2019-04-07 16:49:50 · 106 阅读 · 0 评论 -
归并排序
归并排序class Solution {public: void mergesort(vector<int> &data) { } void merge1(vector<int> &data, int start, int end) { int ret = end ...原创 2019-04-07 16:49:23 · 101 阅读 · 0 评论 -
leetcode Sort list
Sort a linked list in O(n log n) time using constant space complexity./**Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}...原创 2019-04-07 16:49:02 · 94 阅读 · 0 评论 -
leetcode 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 XX O O XX X O XX O X XA...原创 2019-04-07 16:40:21 · 520 阅读 · 0 评论