数据结构/算法/Leetcode
数据结构和算法相关;Leetcode题解
延禾xy
个人学习记录及思考。专注当下,停止焦虑。
展开
-
leetcode 257. Binary Tree Paths
/*leetcode 257. Binary Tree PathsGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1/ \2 3\ 5All root-to-leaf paths are:["1->2->5", "1->3"]题原创 2017-02-24 10:53:18 · 319 阅读 · 0 评论 -
leetcode 101. Symmetric Tree
/*leetcode 101. Symmetric TreeGiven 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:1/ \2 2/ \ /原创 2017-02-23 10:52:55 · 244 阅读 · 0 评论 -
leetcode 112. Path Sum
TreeInclude.h:测试用#ifndef _TREE_INCLUDE_H_ #define _TREE_INCLUDE_H_#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;// 节点数据结构struct TreeNode{ int va原创 2017-02-23 10:22:54 · 203 阅读 · 0 评论 -
leetcode 111. Minimum Depth of Binary Tree
头文件在http://blog.csdn.net/charles1e/article/details/56479700/*leetcode 111. Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the原创 2017-02-22 19:51:43 · 252 阅读 · 0 评论 -
leetcode 110. Balanced Binary Tree
头文件在http://blog.csdn.net/charles1e/article/details/56479700/*leetcode 110. Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree原创 2017-02-22 18:54:07 · 276 阅读 · 0 评论 -
leetcode 404. Sum of Left Leaves
/*leetcode 404. Sum of Left LeavesFind the sum of all left leaves in a given binary tree.题目大意:求二叉树左叶子节点之和解题思路:1. 递归,遇到左叶子节点就求和2. BFS。*/#include "TreeInclude.h"#include <queue>class Solution1 {p原创 2017-02-22 16:42:14 · 215 阅读 · 0 评论 -
leetcode 107. Binary Tree Level Order Traversal II
头文件在http://blog.csdn.net/charles1e/article/details/56479700/*leetcode 107. Binary Tree Level Order Traversal IIGiven a binary tree, return the bottom-up level order traversal of its nodes' values. (ie原创 2017-02-22 15:55:51 · 266 阅读 · 0 评论 -
leetcode 501. Find Mode in Binary Search Tree
头文件说明:该头文件为一些简单是二叉树操作,提交leetcode时不需要,只是为了本地测试而已~#ifndef _TREE_INCLUDE_H_ #define _TREE_INCLUDE_H_#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;// 节点数据原创 2017-02-22 09:53:03 · 624 阅读 · 0 评论 -
leetcode 327. Count of Range Sum
/*leetcode 327. Count of Range SumGiven an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums bet原创 2017-02-21 21:09:43 · 478 阅读 · 0 评论 -
leetcode 315. Count of Smaller Numbers After Self
/*leetcode 315. Count of Smaller Numbers After SelfYou are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of sm原创 2017-02-14 11:28:02 · 285 阅读 · 0 评论 -
leetcode 240. Search a 2D Matrix II
/*leetcode 240. Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending fr原创 2016-11-30 15:12:31 · 247 阅读 · 0 评论 -
leetcode 380. Insert Delete GetRandom O(1)
/*leetcode 380. Insert Delete GetRandom O(1)Design a data structure that supports all following operations in average O(1) time.insert(val): Inserts an item val to the set if not already present.remo原创 2016-11-21 22:42:04 · 314 阅读 · 0 评论 -
leetcode 347. Top K Frequent Elements
/*leetcode 347. Top K Frequent ElementsGiven a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note:You may assume k is alway原创 2016-11-21 21:28:30 · 276 阅读 · 0 评论 -
leetcode 166. Fraction to Recurring Decimal
/* leetcode 166. Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating,原创 2016-11-13 18:25:51 · 321 阅读 · 0 评论 -
leetcode : 3. Longest Substring Without Repeating Characters
/*leetcode : 3. Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", wh原创 2016-11-09 11:37:36 · 387 阅读 · 0 评论 -
leetcode 409. Longest Palindrome
/*leetcode 409. Longest PalindromeGiven 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 sensiti原创 2016-10-19 17:11:56 · 569 阅读 · 0 评论 -
leetcode 202. Happy Number
/*leetcode 202. Happy NumberWrite 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原创 2016-10-19 16:49:44 · 411 阅读 · 0 评论 -
leetcode 204. Count Primes
/***leetcode 204. Count Primes**Description:Count the number of prime numbers less than a non-negative number, n.题目大意:求小于n个素数的个数解题思路:打表,素数筛选法求素数,然后统计。*/#include <iostream>#include <string>#includ原创 2016-10-19 16:22:47 · 405 阅读 · 0 评论 -
leetcode 36. Valid Sudoku
/*leetcode 36. Valid SudokuDetermine 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 '.'.题目大原创 2016-10-19 15:48:42 · 310 阅读 · 0 评论 -
leetcode 389. Find the Difference
/*leetcode 389. Find the DifferenceGiven 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 positio原创 2016-10-19 15:48:23 · 304 阅读 · 0 评论 -
leetcode 37. Sudoku Solver
/*leetcode 37. Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique soluti原创 2016-10-19 15:48:05 · 301 阅读 · 0 评论 -
leetcode 290. Word Pattern
/*leetcode 290. Word PatternGiven 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 no原创 2016-10-19 15:45:27 · 303 阅读 · 0 评论 -
leetcode 349. Intersection of Two Arrays
/*leetcode 349. Intersection of Two ArraysGiven 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原创 2016-10-18 10:19:40 · 332 阅读 · 0 评论 -
leetcode 205. Isomorphic Strings
/*leetcode 205. Isomorphic StringsGiven 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 characte原创 2016-10-18 10:19:13 · 353 阅读 · 0 评论 -
350. Intersection of Two Arrays II
/*350. Intersection of Two Arrays IIGiven two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:Each element in the resu原创 2016-10-17 20:43:39 · 352 阅读 · 0 评论 -
leetcode 219. Contains Duplicate II
/*leetcode 219. Contains Duplicate IIGiven an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference be原创 2016-10-17 20:42:57 · 292 阅读 · 0 评论 -
leetcode 242. Valid Anagram
/*242. Valid AnagramGiven 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:Y原创 2016-10-06 19:39:20 · 318 阅读 · 0 评论 -
leetcode 136. Single Number
/*leetcode 136. Single NumberGiven 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 imple原创 2016-10-06 19:38:29 · 392 阅读 · 0 评论 -
leetcode 241. Different Ways to Add Parentheses
/*leetcode 241. Different Ways to Add ParenthesesGiven a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The原创 2016-09-10 18:43:22 · 401 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array
/*leetcode 215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example原创 2016-09-08 15:07:41 · 362 阅读 · 0 评论 -
leetcode 169. Majority Element
/*leetcode 169. Majority ElementGiven an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-emp原创 2016-09-07 16:43:52 · 379 阅读 · 0 评论 -
leetcode 264. Ugly Number II
/*leetcode 264. Ugly Number IIWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is th原创 2016-09-07 15:29:46 · 332 阅读 · 0 评论 -
leetcode 334 ----字符串翻转
/*leetcode 334 ----字符串翻转 解题思路:把字符串第i个和第len-1-i个位置的字符交换即可。*/#include <iostream>#include <string>using namespace std;class Solution {public: string reverseString(string s) { int len =原创 2016-09-06 13:31:08 · 657 阅读 · 0 评论 -
leetcode 064. Minimum Path Sum
/* * leetcode 064. Minimum Path Sum * dp问题 dp(i,j) = min(dp(i,j-1), dp(i-1,j)) *解决思路:用一个二维数组记录从起点到每一个点的最短路径。*/#include <iostream>#include <vector>#include <algorithm>using namespace std;class Sol原创 2016-09-06 13:30:29 · 433 阅读 · 0 评论 -
leetcode 20. Valid Parentheses
/*leetcode 20. Valid ParenthesesGiven a string containing just the characters '(', ')', '{', '}', '[' and ']',determine if the input string is valid.The brackets must close in the correct order,"(原创 2016-09-06 13:29:58 · 294 阅读 · 0 评论 -
leetcode 17. Letter Combinations of a Phone Number
/*leetcode 17. Letter Combinations of a Phone NumberGiven a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the tele原创 2016-09-06 13:29:30 · 374 阅读 · 0 评论 -
leetcode 304. Range Sum Query 2D - Immutable
/*leetcode 304. Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2,原创 2016-09-06 13:01:49 · 276 阅读 · 0 评论 -
leetcode 151. Reverse Words in a String
/*leetcode 151. Reverse Words in a StringGiven an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Clarification:What constitutes a wo原创 2016-09-05 08:49:56 · 384 阅读 · 0 评论 -
leetcode 198. House Robber
/*leetcode 198. House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amountof money stashed, the only constraint stopping you from robbing each o原创 2016-09-05 08:49:30 · 263 阅读 · 0 评论 -
leetcode 28. Implement strStr()
/*28. Implement strStr()Returns the index of the first occurrence of needle in haystack,or -1 if needle is not part of haystack.*/#include <iostream>#include <string>using namespace std;class Solu原创 2016-09-05 08:48:50 · 270 阅读 · 0 评论