![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法课平时作业
xiuwunianhua
这个作者很懒,什么都没留下…
展开
-
[LeetCode338]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 =原创 2016-09-13 07:27:28 · 163 阅读 · 0 评论 -
[LeetCode416]Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array原创 2016-10-31 22:09:15 · 460 阅读 · 0 评论 -
[LeetCode152]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原创 2016-10-31 21:25:24 · 265 阅读 · 0 评论 -
[LeetCode303]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 sumRan原创 2016-10-18 08:46:54 · 223 阅读 · 0 评论 -
[LeetCode392]Is Subsequence
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2016-10-18 08:27:42 · 161 阅读 · 0 评论 -
[LeetCode109]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. 给定一元素已经排好序的链表,将其转化为二叉查找树,只要根据二叉查找树的定义,用递归方法求解即可。为了方便求解,首先将链表的元素存到数组中。算法的时间复杂度为O(logn)。 /原创 2016-11-08 07:36:37 · 156 阅读 · 0 评论 -
[LeetCode402]Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will b原创 2016-11-08 07:22:07 · 177 阅读 · 0 评论 -
[LeetCode343]Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, ret原创 2016-10-24 22:41:47 · 166 阅读 · 0 评论 -
[LeetCode121]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),原创 2016-10-24 22:23:16 · 138 阅读 · 0 评论 -
[LeetCode238]Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i]. Solve it without division and in O(原创 2016-09-27 07:42:31 · 195 阅读 · 0 评论 -
[LeetCode392]Is Subsequence
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2016-09-27 07:14:59 · 174 阅读 · 0 评论 -
[LeetCode108]Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给定一个由小到大排好序的数组,求这个数组对应的平衡二叉查找树,则数组中间位置的值为二叉查找树根节点的值,从数组中间位置(除去中间位置)将数组分为左右两部分,分别生成根节点的左右子树,因为左右子树也都是平原创 2016-10-10 08:51:42 · 132 阅读 · 0 评论 -
[LeetCode129]Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota原创 2016-10-10 08:37:01 · 179 阅读 · 0 评论 -
[LeetCode268]Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm sho原创 2016-09-07 07:45:48 · 157 阅读 · 0 评论 -
[LeetCode74]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原创 2016-09-20 07:31:34 · 178 阅读 · 0 评论 -
[LeetCode73]Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 先将矩阵扫描一遍,用一个队列来记录矩阵中元素为0的位置,然后再通过不断出队把矩阵中对应的行和列的元素改为0,直到队列为空。时间复杂度为O(mn)。 class Solution { public:原创 2016-09-20 07:20:54 · 160 阅读 · 0 评论 -
[LeetCode136]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 ext原创 2016-09-06 07:21:17 · 165 阅读 · 0 评论 -
[LeetCode104]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. 这道题我用的是递归,如果树的根节点为空,则返回0,否则返回1加上左子原创 2016-09-13 07:43:49 · 152 阅读 · 0 评论 -
《算法概论》第8章习题8.3题解
题目: 吝啬SAT问题是这样的:给定一组字句(每个字句都是其中文字的析取)和整数k,求一个最多有k个变量为true的满足赋值——如果该赋值存在。证明吝啬SAT是NP-完全问题。 证明: 1、先证明吝啬SAT是NP的:给定一组文字的赋值,能在多项式时间内验证该解,因此是NP的。 2、再证明吝啬SAT是NP-Hard的:将SAT问题归约到吝啬SAT问题。 假设存在一个SAT实例I和一个吝原创 2017-01-01 20:52:35 · 272 阅读 · 0 评论