证明题8.3,8.12 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 assignme
8道算法机考模拟题 1000、函数求值定义超级和函数F如下: F(0, n) = n,对于所有的正整数n.. F(k, n) = F(k – 1, 1) + F(k – 1, 2) + … + F(k – 1, n),对于所有的正整数k和n.例1:F(1, 3) = 6 例2:F(2, 3) = 10 例3:F(10, 10) = 167960请实现下面Solution类中计算F(k, n)的函数(1 <= k
【LeetCode】215. Kth Largest Element in an Array找第K大的元素 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 5.最基本的解法
【LeetCode】152. 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 p
【LeetCode】122. Best Time to Buy and Sell Stock II 问题描述股票交易。数组第i个元素表示第i天股票的价格,整个过程能交易多次,但是卖出之前必须保证有买入。设计一个算法寻找最大收益。知道了价格走势,只要后一天比前一天价格高,我就交易。纯赚的做法。class Solution {public: int maxProfit(vector<int>& prices) { int result=0; for(int i
【LeetCode】121. Best Time to Buy and Sell Stock 问题描述股票交易。数组第i个元素表示第i天股票的价格,整个过程只能买一次和卖一次,设计一个算法寻找最大收益。例1: Input: [7, 1, 5, 3, 6, 4] Output: 5 只能先买进才能卖出,所以最大收益为6-1=5,而不是7-1=6。例2: Input: [7, 6, 4, 3, 1] Output: 0贪心。其实就是找一个数组里面的最大差值j-i,保证i//但是这个方法
【LeetCode】312. Burst Balloons爆破气球得到最大金币数 问题描述给定n个气球,每个气球对应一个编号,用数组nums[0…n-1]存放,打爆一个气球能够获得金币数:nums[left] * nums[i] * nums[right],然后这个数被移除,left和right变为相邻。求能够获得的最多的金币数量。 注:①假设nums[-1] = nums[n] = 1 ② 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100例:Given [3,
【LeetCode】72. Edit Distance单词变换的最少操作 问题描述Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a
【LeetCode】455. Assign Cookies贪心寻找最大解 问题描述每个孩子最多一块饼干。 gi:the greed factor of ith children sj:the size of jth cookie if sj >= gi,则能把cookie j分配给children i。 问:最多能分配到饼干的孩子数量? 例1: Input: [1,2,3], [1,1] Output: 1 只能满足小孩g[0] 例2: Input
【LeetCode】210. Course Schedule II 问题描述在207.Course Schedule问题的基础上,求出能够完成所有课程的其中一种可能的顺序方案,如果不存在,则返回空数组。问题就变成:如何按照post值递减来保存节点。class Solution {public: //1、设置计步器,全局变量 int count = 0; vector<int> findOrder(int numCourses, vector<
【LeetCode】207. Course Schedule判断有向图是否有环 1、问题描述输入:n, [[1,0], [2,1], …., [n, n-1], [0,1], …..],假设没有重复的边 n表示总共0~n-1门课程,即n个节点 [1,0]表示先上课程0,才能上课程1,即0→1有一条有向边 输出:能否完成所有课程,即无环true/有环false2、思路DFS。 除了树边,假设存在一条边v→u,则有如下性质: 1. pre(u) < pre(v),pos
【LeetCode】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. Input: “bbbab” Output: 4 一种可能的最长子序列为”bbbb”.
【LeetCode】2.Add Two Numbers两个单链表相加 You are given two non-empty linked lists(非空链表) representing two non-negative integers(非负整数). The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers a
虚函数和纯虚函数的区别 首先:强调一个概念定义一个函数为虚函数,不代表函数为不被实现的函数。定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。定义一个函数为纯虚函数,才代表函数没有被实现。定义纯虚函数是为了实现一个接口,起到一个规范的作用,规范继承这个类的程序员必须实现这个函数。1、简介假设我们有下面的类层次:[cpp] view plain copy print?class A { public
TCP 长连接/短连接 三次握手/四次握手 建立TCP需要三次握手才能建立,而断开连接则需要四次握手。整个过程如下图所示:先来看看如何建立连接的。首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源。Client端接收到ACK报文后也向Server段发生ACK报文,并分配资源,这样TCP连接就建立了。那如何断开连接呢?简单的过程如下:在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN,
【笔试】百度编程题 百度2017春招笔试真题编程题集合1、找第三便宜的帽子。#include <iostream>#include <vector>#include <algorithm>using namespace std;int main() { int n; cin >> n; vector<int> result; int price; for (int i =
【LeetCode】300.Longest Increasing Subsequence最长递增子序列LIS 给定数组 [10, 9, 2, 5, 3, 7, 101, 18],其中一个最长递增子序列(不需要连续)为[2, 3, 7, 101] ,length为4。因为最长子序列不一定唯一,所以我们只需要返回长度值。【解题思路】老师上课讲的经典的一道动态规划。而且有两种方法:O(n2)和O(nlogn)。(1)方法1:常规思路,O(n2)f[i]:表示以元素a[i]为结尾的最长递增子序列的
【LeetCode】53.Maximum Subarray最大连续子序列和 给定数组 [-2,1,-3,4,-1,2,1,-5,4],子序列[4,-1,2,1] 有最大和6。【解题思路】老师上课讲的经典的一道动态规划。f[i]:表示以元素ai为结尾的子序列的最大和。当i=0,f[i]=ai当i>0,如果f[i-1]如果f[i-1]>=0,f[i]=f[i-1]+ai答案:max{f[0],f[1],....,f[n]}。T(n)=O
Map map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字key,每个关键字只能在map中出现一次,第二个可能称为该关键字的值value)的数据处理能力,由于这个特性,它完成有可能在处理一对一数据时,在编程上提供快速通道。map内部数据的组织——map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后面会见识到
【LeetCode】No.55 Jump Game 【原题】Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents yourmaximum jump length at that position. Determine