C++算法设计与应用
lisa___33
这个作者很懒,什么都没留下…
展开
-
Leetcode 第 120 场周赛----在二叉树中分配硬币
给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币。在一次移动中,我们可以选择两个相邻的结点,然后将一枚硬币从其中一个结点移动到另一个结点。(移动可以是从父结点到子结点,或者从子结点移动到父结点。)。返回使每个结点上只有一枚硬币所需的移动次数。原题链接:在二叉树中分配硬币 解题思路:小白为了练手,...原创 2019-01-20 16:50:06 · 335 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5原创 2017-06-22 12:11:22 · 136 阅读 · 0 评论 -
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by原创 2017-06-10 12:04:50 · 131 阅读 · 0 评论 -
Add to List 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.Subscribe to see which companies asked this原创 2017-06-10 11:45:14 · 160 阅读 · 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 = 2原创 2017-06-10 11:25:20 · 178 阅读 · 0 评论 -
Add to List 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:1 / \ 2 3 \ 5 All root-to-leaf paths are:[“1->2->5”, “1->3”] 这道题是利用DFS算法的思路。先从root出发,然后递归原创 2017-06-10 10:58:34 · 147 阅读 · 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 inte原创 2017-06-21 09:43:44 · 137 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
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原创 2017-06-21 09:36:32 · 174 阅读 · 0 评论 -
437. Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go原创 2017-06-04 11:33:37 · 192 阅读 · 0 评论 -
543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may no原创 2017-06-04 11:15:25 · 158 阅读 · 0 评论 -
Add to List 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree nod原创 2017-06-04 10:54:23 · 493 阅读 · 0 评论 -
572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’原创 2017-06-03 22:06:58 · 169 阅读 · 0 评论 -
199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example: Given the following binary tree, 1原创 2017-06-28 21:01:53 · 165 阅读 · 0 评论 -
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2017-06-19 15:57:04 · 163 阅读 · 0 评论 -
452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordina原创 2017-06-28 17:39:18 · 224 阅读 · 0 评论 -
455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cook原创 2017-06-28 16:59:27 · 194 阅读 · 0 评论 -
62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto原创 2017-06-22 12:31:19 · 134 阅读 · 0 评论 -
63. Unique Paths II
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-06-22 13:34:16 · 154 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 采用递归的算法,同时每次只要为比较大的一边+1就好了。 代码如下:/** *原创 2017-06-28 15:04:09 · 166 阅读 · 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.这道题用dfs去做。代码如下: 注意TreeNode *root=new TreeNode(nums[mid])往root中加数 代码如下: /** * Definition for a b原创 2017-06-28 14:48:44 · 173 阅读 · 0 评论 -
算法设计期末作业-08-8.22
题目 ##(题目比较长,找了一个中文版的贴在下面)在任务调度,常常会用到图。其中节点应用于任务,任务i到j的有向边表示i是j的先期条件。这样的图描述了调度问题中的任务先后关系(约束) 。显然,一个调度是可行的当且仅当该图无环;如果调度不可行,我们需要使其无环所需的最小约束数量。 给定一个有向图G =(V,E),子集E’⊆E称为一个反馈弧集,反馈弧集是指将其移除后,将使得G无环。 反馈弧集(FAS原创 2017-07-14 23:17:55 · 320 阅读 · 0 评论 -
算法设计期末作业-08-8.22
题目 ##(题目比较长,找了一个中文版的贴在下面)在任务调度,常常会用到图。其中节点应用于任务,任务i到j的有向边表示i是j的先期条件。这样的图描述了调度问题中的任务先后关系(约束) 。显然,一个调度是可行的当且仅当该图无环;如果调度不可行,我们需要使其无环所需的最小约束数量。 给定一个有向图G =(V,E),子集E’⊆E称为一个反馈弧集,反馈弧集是指将其移除后,将使得G无环。 反馈弧集(FAS原创 2017-07-14 23:17:38 · 508 阅读 · 0 评论 -
算法设计期末作业07-8.16
题目有点长久去网上截了一个一小段的题目题目菜谱和3SAT问题 Input: n, the number of ingredients to choose from: D,the n*n “discord” matrix; some number p >= 0 Output: The maximum number of ingredients we can choose with penalty原创 2017-07-13 17:59:37 · 354 阅读 · 0 评论 -
算法设计期末作业-06-8.15
题目Show that the following problem is NP-complete. MAXIMUM COMMON SUBGRAPH Input: Two graphs G1=(V1,E1) and G2=(V2,E2);a budget b. Output: Two set of nodes V1’属于V1, and V2’属于V2 whose deletion leaves原创 2017-07-13 17:12:22 · 432 阅读 · 0 评论 -
算法设计期末作业05-8.14
题目Prove that the following problem is NP-complete: given an undirected graph G = (V, E) and an integer k, return a clique of size k as weill as an independent set of size k, provided both exist.问题解答这个问原创 2017-07-13 16:44:55 · 353 阅读 · 0 评论 -
算法设计期末作业04-8.12
题目The k-SPANNING TREE problem is the following. Input: An undirected graph G = (V,E) Output: Aspanningtreeof G in which each node has degree≤ k,原创 2017-07-13 16:26:50 · 310 阅读 · 0 评论 -
486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a原创 2017-06-23 16:27:35 · 179 阅读 · 0 评论 -
516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.Example 1: Input:“bbbab” Output: 4 One possible longest palindromic sub原创 2017-06-23 15:25:51 · 156 阅读 · 0 评论 -
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:1/ \ 2 2 / \ / \ 3 4 4 3 But the fol原创 2017-06-11 09:27:36 · 151 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example: Given binary tree [3,9,20,null,null,15,7],原创 2017-06-11 09:16:31 · 166 阅读 · 0 评论 -
算法设计期末作业03-8.10
题目Proving NP-completeness by generalization. For each of the problems below, prove that it is NP-complete by showing that it is a generalization of some NP-complete problem we have seen in this chapter原创 2017-07-11 17:18:56 · 445 阅读 · 0 评论 -
算法设计期末作业02-8.8
题目:In theEXACT4SATproblem, the input is a set of clauses, each of which is a disjunction of exactly four literals, and such that each variable occurs at most once in each clause. The goal is to find原创 2017-07-11 15:57:47 · 264 阅读 · 0 评论 -
算法设计期末作业01-8.3
题目STINGY SAT is the following problem: given a set of clauses(each a disjunction of literals) ans an integer k, find a satisfying assignment in which at most k variables are true, if such an assignment原创 2017-07-11 15:56:27 · 315 阅读 · 0 评论 -
547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A原创 2017-06-28 15:52:38 · 173 阅读 · 0 评论 -
373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.Define a pair (u,v) which consists of one element from the first array and one element from the second array.原创 2017-05-22 15:28:24 · 204 阅读 · 0 评论 -
欢迎使用CSDN-markdown编辑器
Perfect Number We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.Now, given an integer n, write a function that returns true when原创 2017-05-02 10:04:28 · 120 阅读 · 0 评论 -
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-26 12:43:10 · 176 阅读 · 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: 3 Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-03-26 12:35:54 · 171 阅读 · 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-26 11:20:40 · 226 阅读 · 0 评论 -
414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).这道题我采用了set和反向迭代器的方法,理解起来比较容易。懂得了原创 2017-04-02 16:14:40 · 162 阅读 · 0 评论