- 博客(21)
- 收藏
- 关注
原创 算法概论第八章部分习题解答
8.3 吝啬SAT问题是这样的:给定一组子句(每个子句都是其中文字的析取)和整数k,求一个最多有k各变量为true的满足赋值 —— 如果该赋值存在。证明吝啬SAT是NP完全问题。1.证明吝啬SAT是NP问题。2.证明吝啬SAT是NP完全问题。解:1.若存在一组对应于吝啬SAT问题子句变量的值,将这组值代入该问题中,可以在多项式时间内验证问题的解是否为真。因此吝啬SA
2017-07-14 16:19:51
584
原创 Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num
2017-06-19 18:18:38
219
原创 Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]Solution
2017-06-19 18:16:55
208
原创 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()
2017-06-19 18:13:48
201
原创 Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have thesame probability of being chosen.Follow up:What if the linked list is extremely large an
2017-06-19 17:44:03
258
原创 Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals tok.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:
2017-06-19 17:38:22
259
原创 Minimum Factorization
Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a. If there is no answer or the answer is not fit in 32-bit signed integer, then r
2017-06-19 17:35:42
376
原创 Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called
2017-06-19 17:31:07
208
原创 Evaluate Division
Equations are given in the format A / B = k, where A andB are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the ans
2017-06-19 17:22:10
213
原创 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
2017-06-12 09:16:00
201
原创 Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indicesi and j (i ≤ j), inclusive.The update(i, val) function modifies nums by updating the element at indexi to val.Examp
2017-06-12 09:10:31
153
原创 Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3
2017-06-12 08:57:20
167
原创 Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary
2017-06-12 08:47:20
255
原创 Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with val
2017-06-12 08:42:07
167
原创 Reverse Linked List
Reverse a singly linked list.Solution:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {}
2017-06-12 08:39:59
147
原创 Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example: Input: 1 \ 3 / 2Output:1Explanatio
2017-06-12 08:29:21
188
原创 Intersection of Two Arrays
Given 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 result must be unique.The res
2017-06-12 08:22:32
171
原创 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appearsmore than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
2017-03-16 22:30:42
165
原创 Kth Largest Element in an Array
Find 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,Given [3,2,1,5,6,4] and k = 2, return
2017-03-13 13:04:13
170
原创 Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the
2017-03-07 00:09:24
163
原创 Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function shouldreturn 0 when the r
2017-03-06 23:13:06
152
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人