LeetCode
zjajgyy
这个作者很懒,什么都没留下…
展开
-
【C++】【LeetCode】152. Maximum Product Subarray
题目Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest pro原创 2017-08-24 10:34:51 · 369 阅读 · 0 评论 -
【C++】【LeetCode】98. Validate Binary Search Tree
题目Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The原创 2017-08-08 17:05:31 · 422 阅读 · 0 评论 -
【C++】【LeetCode】79. 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原创 2017-07-30 23:23:41 · 602 阅读 · 0 评论 -
【C++】【LeetCode】54. Spiral Matrix && 59. Spiral Matrix II
题目Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9原创 2017-06-30 16:21:56 · 264 阅读 · 0 评论 -
【C++】【LeetCode】56. 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].思路先给数组排序,这样保证了每个interval的start都是依次递增的,这样只要考虑end的问题。原创 2017-07-09 16:51:44 · 557 阅读 · 0 评论 -
【LeetCode】参照LeetCode上的Solution总结:同类解法
LeetCode上看到的一类提醒的一种解法总结: A general approach to backtracking questions in Java (Subsets, Permutations, Combination Sum, Palindrome Partioning) 原作者是用Java实现的,这里我因为试验,把第一个改成了C++版。 虽然这种解法不见得效率很高,但是不失为在没有原创 2017-06-25 11:31:02 · 787 阅读 · 0 评论 -
【C++】【LeetCode】77. Combinations
题目Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3],原创 2017-07-30 16:39:50 · 457 阅读 · 0 评论 -
【C++】【LeetCode】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 a原创 2017-07-09 10:27:53 · 182 阅读 · 0 评论 -
【C++】【LeetCode】70. Climbing Stairs
题目You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positiv原创 2017-07-17 22:53:04 · 216 阅读 · 0 评论 -
【C++】【LeetCode】53. Maximum Subarray
题目Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has t原创 2017-07-15 23:58:06 · 389 阅读 · 0 评论 -
【C++】【LeetCode】49. Group Anagrams
题目Given an array of strings, group anagrams together. For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return: [ [“ate”, “eat”,”tea”], [“nat”,”tan”], [“bat”] ]思路s原创 2017-06-25 16:25:13 · 615 阅读 · 0 评论 -
【C++】【LeetCode】48. Rotate Image
题目You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place?思路顺时针旋转一个二维矩阵。通过在纸上演算推出,其实就是把原来坐标为(i, j)的点中的值移到(j, size -原创 2017-06-25 15:40:52 · 604 阅读 · 0 评论 -
【C++】【LeetCode】75. Sort Colors
题目Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0,原创 2017-08-11 18:32:29 · 217 阅读 · 0 评论 -
【C++】【LeetCode】141. Linked List Cycle
题目Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?思路这道题不让用额外的空间,所以很简单,每遍历一个节点,就把它的next指向它本身,也就表明这个节点已经被遍历过了。如果遍历到某个节点,它的next是一个指向自身的节点,即它的n原创 2017-08-18 19:36:19 · 277 阅读 · 0 评论 -
【C++】【LeetCode】101. Symmetric Tree
题目Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric, But the following [1,2,2,null,3,null,3] is原创 2017-08-11 21:20:09 · 196 阅读 · 0 评论 -
【C++】【LeetCode】148. 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; * ListNo原创 2017-08-23 10:49:27 · 307 阅读 · 0 评论 -
【C++】【LeetCode】114. Flatten Binary Tree to Linked List
题目Given a binary tree, flatten it to a linked list in-place.思路有两种方法,第一种比较笨,把所有的点按照前序遍历存入队列,然后取出来构建一棵树;还有一种,把左子树的最右叶子节点找到,将右子树作为最右叶子节点的右子树,然后将左子树作为右子树。代码思路一/** * Definition for a binary tree node. *原创 2017-08-22 11:49:21 · 318 阅读 · 0 评论 -
【C++】【LeetCode】【指针】2. Add Two Numbers
Add Two NumbersYou 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原创 2017-04-01 11:40:01 · 267 阅读 · 0 评论 -
【C++】【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
题目Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.思路前序排列,根结点在最前面;中序排列,根结点前面是左子树的结点,后面是右子数的结点。以此将左右子树分开,又可以按照前序原创 2017-08-21 12:20:24 · 283 阅读 · 0 评论 -
【C++】【LeetCode】96. Unique Binary Search Trees
题目Given 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.思路给定n,求不重复二叉查找树的数量,可以分为n种情况,根节点是x(x的取值从1到n),左边原创 2017-08-06 21:12:54 · 306 阅读 · 0 评论 -
【C++】【LeetCode】139. Word Break
题目Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may ass原创 2017-08-14 10:21:13 · 507 阅读 · 0 评论 -
【C++】【LeetCode】92. Reverse Linked List II
题目Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.Note: Given m, n satisfy the fo原创 2017-08-05 15:43:41 · 252 阅读 · 0 评论 -
【C++】【LeetCode】136. Single Number
题目Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra原创 2017-08-12 12:20:14 · 280 阅读 · 0 评论 -
【C++】【LeetCode】121. Best Time to Buy and Sell Stock
题目Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), de原创 2017-08-12 11:57:18 · 266 阅读 · 0 评论 -
【C++】【LeetCode】108. Convert Sorted Array to Binary Search Tree
题目Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路目的是要得到一个平衡二叉树,也就说是每棵数的根节点应该是整棵树上结点的中位数。所以取数组的中位数作为根节点,中位数左边为左子树的数组,右边为右子数的数组。以此类推。代码/** * Definitio原创 2017-08-12 11:36:53 · 738 阅读 · 0 评论 -
【C++】【LeetCode】102. Binary Tree Level Order Traversal
题目Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], return its level order原创 2017-08-11 21:47:00 · 245 阅读 · 0 评论 -
【C++】【LeetCode】31. Next Permutation && 46. Permutations
题目Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible原创 2017-06-11 11:37:24 · 397 阅读 · 0 评论 -
【C++】【LeetCode】36. Valid Sudoku
题目Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. Note: A valid Sudoku boar原创 2017-06-12 23:07:15 · 289 阅读 · 0 评论 -
【C++】【LeetCode】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原创 2017-06-07 22:53:54 · 174 阅读 · 0 评论 -
【C++】【LeetCode】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. Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list beco原创 2017-06-08 10:26:37 · 230 阅读 · 0 评论 -
【C++】【LeetCode】22. Generate Parentheses
题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ “((()))”, “(()())”, “(())()”,原创 2017-06-08 17:50:43 · 347 阅读 · 0 评论 -
【C++】【LeetCode】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原创 2017-06-09 17:01:01 · 191 阅读 · 0 评论 -
【C++】【LeetCode】29. Divide Two Integers
题目Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.思路这题是参考了leetcode上的solution。 这题比较重要的是long long的运用。因为不能使用乘法,除法和取余,所以使用位移。首先排除极端情况,除数为0或者原创 2017-06-10 11:10:45 · 290 阅读 · 0 评论 -
【C++】【LeetCode】50. Pow(x, n)
Powx n题目思路代码Pow(x, n)题目Implement pow(x, n).思路pow(x, n),即x的n次方。通过递归,每次判断n是不是2的倍数,如果不是,减1后再除以2,如果n等于1或者-1,返回x或者1/x。代码class Solution {public: double myPow(double x, int n) { map<int, doubl原创 2017-06-30 15:35:11 · 273 阅读 · 0 评论 -
【C++】【LeetCode】43. Multiply Strings
题目Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-原创 2017-06-18 20:18:38 · 504 阅读 · 0 评论 -
【C++】【LeetCode】15. 3Sum & 16. 3Sum Closest & 18. 4Sum
3Sum题目例子思路代码3Sum Closet题目思路代码4Sum题目思路代码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 s原创 2017-06-07 12:56:26 · 267 阅读 · 0 评论 -
【C++】【LeetCode】39. Combination Sum&40. Combination Sum II
题目Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from原创 2017-06-14 10:26:13 · 419 阅读 · 0 评论 -
【C++】【LeetCode】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原创 2017-06-07 12:30:51 · 174 阅读 · 0 评论 -
【C++】【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.思路为了不多占用内存,直接利用l1和l2的空间。比较l1和l2的值,小的加入结果链表,然后继续后移链表比较。代码/*原创 2017-06-07 10:15:26 · 321 阅读 · 0 评论 -
【C++】【LeetCode】3. Longest Substring Without Repeating Characters
Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc",原创 2017-04-01 11:42:09 · 284 阅读 · 0 评论