c++
默然相爱712
这个作者很懒,什么都没留下…
展开
-
138-Copy List with Random Pointer
题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.分析复制每一个节点,复制后的节点放在原节点之后更新Random 指针原创 2017-07-19 10:29:53 · 264 阅读 · 0 评论 -
98_ValidateBinarySearchTree
题目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 rig原创 2017-06-25 18:39:19 · 183 阅读 · 0 评论 -
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. 1 3 3 2 1 \ /原创 2017-06-25 18:38:05 · 282 阅读 · 0 评论 -
95-Unique Binary Search Trees II
题目Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below. 1原创 2017-06-25 18:37:49 · 167 阅读 · 0 评论 -
94-Binary Tree Inorder Traversal
题目:Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3],1 \ 2 / 3return [1,3,2].分析: 二叉树的中序遍历 思路1: 递归 思路2: 用S原创 2017-06-25 18:36:23 · 163 阅读 · 0 评论 -
91-DecodeWays
题目A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given an encoded message containing digits, determine the total number原创 2017-06-25 18:35:43 · 129 阅读 · 0 评论 -
90-Subsets II
题目Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,2], a solution i原创 2017-06-23 15:50:34 · 165 阅读 · 0 评论 -
88-Merge Sorted Array
题目Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addition原创 2017-06-23 15:50:19 · 137 阅读 · 0 评论 -
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 ne原创 2017-06-23 15:49:59 · 144 阅读 · 0 评论 -
78-Subsets
题目Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:[ [3], [1], [2], [原创 2017-06-23 15:39:28 · 126 阅读 · 0 评论 -
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-06-23 15:39:07 · 174 阅读 · 0 评论 -
74-Search A 2D Matrix
题目Write 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 from left to right. The first integer of each row原创 2017-06-23 15:38:47 · 135 阅读 · 0 评论 -
73-Set Matrix Zeros
题目Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.分析方法一自己的思路,用两个数组分别记录行、列哪个为零方法二是参考的别人的代码,直接用数组的第一行和第一列记录,但是会有点麻烦,需要用一个额外的变量存储,有点不太好理解,所以自己写了一个方法一实现class原创 2017-06-23 15:38:29 · 227 阅读 · 0 评论 -
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 positive in原创 2017-06-23 15:38:09 · 151 阅读 · 0 评论 -
100-Same Tree
题目Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.分析两个树相等,判断条件是 1.原创 2017-06-25 18:40:04 · 181 阅读 · 0 评论 -
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], 3 / \ 9 20 /原创 2017-06-25 18:40:37 · 172 阅读 · 0 评论 -
217-Contains Duplicate
题目Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element i原创 2017-06-25 18:47:34 · 207 阅读 · 0 评论 -
141-Linked List Cycle
题目Given a linked list, determine if it has a cycle in it.分析快慢指针,当快指针追上慢指针,说明链表成环实现class Solution {public: bool hasCycle(ListNode *head) { if (head == NULL|| head->next == NULL)原创 2017-06-25 18:47:19 · 199 阅读 · 0 评论 -
132-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”, Return 1 sinc原创 2017-06-25 18:46:51 · 208 阅读 · 0 评论 -
131-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”原创 2017-06-25 18:46:35 · 176 阅读 · 0 评论 -
123-Best Time to Buy and Sell Stock III
题目Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note: You may not eng原创 2017-06-25 18:46:03 · 162 阅读 · 0 评论 -
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-06-25 18:45:47 · 311 阅读 · 0 评论 -
120-Triangle
题目Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7],原创 2017-06-25 18:45:27 · 150 阅读 · 0 评论 -
114- Flatten Binary Tree to Linked List
题目Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2原创 2017-06-25 18:44:55 · 232 阅读 · 0 评论 -
112- Path Sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and sum =原创 2017-06-25 18:44:33 · 248 阅读 · 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.分析该问题是判断根节点到叶子结点的最小深度。不同于判断最大深度,当根节点的其中一个原创 2017-06-25 18:43:54 · 191 阅读 · 0 评论 -
109-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.分析这个和Covert Sorted Array to Binary Search Tree 其实是类似的,都是通过递归构造关键就在于如何找到链表的中间结点。链表的中间结点可以用快原创 2017-06-25 18:43:03 · 168 阅读 · 0 评论 -
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.分析每次选择中间的那个数字d实现class Solution {public: //108. Convert Sorted Array to Binary Search Tree Tre原创 2017-06-25 18:41:41 · 157 阅读 · 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 tre原创 2017-06-25 18:41:11 · 178 阅读 · 0 评论 -
69-Sqrt(x)
题目Implement int sqrt(int x).Compute and return the square root of x.分析这个题里面是有许多陷阱的 - 首先确定用二分法处理这个问题 - 然而0-x之间的num的平方有可能会溢出,所以不要使用num*num,如果使用的话要用long long 存储 - 还有left 不能为0,如果mid 也为零的话,x/mid 就会出错误 -原创 2017-06-23 15:37:43 · 2826 阅读 · 0 评论 -
66-Plus One
题目Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are原创 2017-06-23 15:37:25 · 228 阅读 · 0 评论 -
64-Minimum Path Sum
题目Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at an原创 2017-06-23 15:36:42 · 139 阅读 · 0 评论 -
42-Trapping Rain Water
题目Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], r原创 2017-06-22 14:42:20 · 155 阅读 · 0 评论 -
40-Combination Sum II
题目:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination原创 2017-06-22 14:41:31 · 154 阅读 · 0 评论 -
39-Combination Sum
题目: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-22 14:38:50 · 167 阅读 · 0 评论 -
37-Sudoku Solver
题目Write 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 solution.分析数独问题。 和N-Queens Permu原创 2017-06-22 14:37:57 · 135 阅读 · 0 评论 -
36-Valid Sudoku
题目A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.分析这问题是在判断数独问题的当前解是否合法,也就是需要验证当前状态下的数字是否满足要求。 - 首先,判断每一行是否合理 - 其次,判断每一列是否合理 - 最后,判断每一原创 2017-06-22 14:37:00 · 141 阅读 · 0 评论 -
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 the原创 2017-06-22 14:36:13 · 137 阅读 · 0 评论 -
32-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 h原创 2017-06-22 14:35:22 · 130 阅读 · 0 评论 -
31-NextPermutation
题目: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 o原创 2017-06-22 14:35:06 · 183 阅读 · 0 评论