★算法入门
文章平均质量分 58
zhengjihao
这个作者很懒,什么都没留下…
展开
-
辗转相除法求最大公约数证明
辗转相除法求最大公约数证明假设整数aaa,bbb,且a>ba > ba>b,rrr为aaa除以bbb的余数,则有(a,b)=(b,c)。证明:假设d=(a,b)d=(a,b)d=(a,b),则d∣a,d∣b⇒d∣a−k∗b(k≥0)⇒d∣rd|a,d|b \Rightarrow d|a-k*b(k \geq 0) \Rightarrow d|rd∣a,d∣b⇒d∣...原创 2019-11-24 10:33:12 · 363 阅读 · 0 评论 -
排座位(百度2017秋招真题)
排座位(百度2017秋招真题)原创 2017-06-22 19:55:52 · 4831 阅读 · 3 评论 -
LeetCode 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 has原创 2017-09-25 21:52:11 · 201 阅读 · 0 评论 -
[编程题] 地下迷宫 滴滴编程题
小青蛙有一天不小心落入了一个地下迷宫,小青蛙希望用自己仅剩的体力值P跳出这个地下迷宫。为了让问题简单,假设这是一个n*m的格子迷宫,迷宫每个位置为0或者1,0代表这个位置有障碍物,小青蛙达到不了这个位置;1代表小青蛙可以达到的位置。小青蛙初始在(0,0)位置,地下迷宫的出口在(0,m-1)(保证这两个位置都是1,并且保证一定有起点到终点可达的路径),小青蛙在迷宫中水平移动一个单位距离需要消耗1点体力原创 2017-09-10 10:09:04 · 859 阅读 · 0 评论 -
[编程题] 末尾0的个数 滴滴笔试
输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2 输入描述: 输入为一行,n(1 ≤ n ≤ 1000)输出描述: 输出一个整数,即题目所求输入例子1: 10输出例子1: 2思路:通过举例: 2*5=10 4*5=20 6*5=30 8*5=40 产生0的都是5的倍数。所以,有多少个5,就有多少个0.(因为偶数肯原创 2017-09-10 10:22:12 · 389 阅读 · 0 评论 -
数字和为sum的方法数 滴滴笔试题
给定一个有n个正整数的数组A和一个整数sum,求选择数组A中部分数字和为sum的方案数。 当两种选取方案有一个数字的下标不一样,我们就认为是不同的组成方案。 输入描述: 输入为两行: 第一行为两个正整数n(1 ≤ n ≤ 1000),sum(1 ≤ sum ≤ 1000) 第二行为n个正整数Ai,以空格隔开。输出描述: 输出所求的方案数输入例子1: 5 15 5 5 10 2 3原创 2017-09-10 13:09:46 · 1678 阅读 · 2 评论 -
LeetCode 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 since原创 2017-09-26 18:41:52 · 284 阅读 · 0 评论 -
Leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, ret原创 2017-09-12 09:54:11 · 220 阅读 · 0 评论 -
LeetCode 338. 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 = 5 you sh原创 2017-09-26 20:52:25 · 271 阅读 · 0 评论 -
POJ 1157 LITTLE SHOP OF FLOWERS
Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The原创 2017-08-27 13:26:44 · 313 阅读 · 0 评论 -
LeetCode 322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money c原创 2017-09-26 22:24:58 · 376 阅读 · 0 评论 -
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) In原创 2017-09-26 22:55:43 · 368 阅读 · 0 评论 -
石子合并问题汇总
1:任意版 有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动任意的2堆石子合并,合并花费为将的一堆石子的数量。设计一个算法,将这N堆石子合并成一堆的总花费最小(或最大)。 此类问题比较简单,就是哈夫曼编码的变形,用贪心算法即可求得最优解。即每次选两堆最少的,合并成新的一堆,直到只剩一堆为止。证明过程可以参考哈夫曼的证明过程。2:链式合并 设有N堆沙子排成一排,其编号为1,2原创 2017-09-02 10:56:04 · 1947 阅读 · 0 评论 -
换零钱问题
题型1:求总数 1角 2角 5角硬币有无限个,给定一个money值,求由可以兑换的硬币的组合数 思路: 完全背包的变体。 dp[i][j]表示前i个硬币且要兑换的钱数为j时的总的组合数, 假设money = 6,具体情况如下: 表头 0 1 2 3 4 5 6 0 1 0 0 0 0 0 0 1 1 1 1 1原创 2017-10-18 22:35:35 · 1100 阅读 · 0 评论 -
链家笔试算法题
1 给定一个数组,将奇数数排在偶数前面 思路:类似于快排的思想#include<iostream>#include<stdio.h>#include<memory.h>using namespace std;void exchange(int a[], int n){ int low = 0, high = n-1; while(low < high) {原创 2017-10-18 22:51:27 · 754 阅读 · 0 评论 -
错位排列
错位排列是指:n个对象排队,每个人都不在他们原来的位置上,这样的排列有多少种? 我们假设有ABCDE 5个对象,则让A先排,可以有BCDE 4个位置。假设A占了B的位置,下面可以分两种情况: (1) B也占了A的位置。这时剩下的3个刚好是大小为3的错位排列问题。 (2) B 不占A的位置。此时的问题形式为 A B C D E A 因为B的位置被占了原创 2017-09-07 20:25:45 · 4082 阅读 · 0 评论 -
最大公约数和最小公倍数
求最大公约数的方法是辗转相除法,最小公倍数是两个数的积除以最大公约数。 递归的最大公约数:int gcd(int a,int b){ if(a < b) { a = a^b; b = a^b; a = a^b; } if(b == 0) return a; return gcd(b , a % b);}非原创 2017-09-06 22:28:45 · 383 阅读 · 0 评论 -
POJ 1164 放苹果 经典的组合问题
Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。Input 第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。Output 对输入的每组数据M和N,用一行输出相应的K。Sample Input1 7 3Sample原创 2017-09-05 17:05:56 · 495 阅读 · 0 评论 -
Exponentiation
DescriptionProblems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many comp原创 2017-04-19 11:07:41 · 274 阅读 · 0 评论 -
网易笔试 分饼干
[编程题] 分饼干时间限制:1秒空间限制:32768K易老师购买了一盒饼干,盒子中一共有k块饼干,但是数字k有些数位变得模糊了,看不清楚数字具体是多少了。易老师需要你帮忙把这k块饼干平分给n个小朋友,易老师保证这盒饼干能平分给n个小朋友。现在你需要计算出k有多少种可能的数值输入描述:输入包括两行:第一行为盒子上的数值k,模糊的数位用X表示,长度小于18(可能有多原创 2017-06-29 20:38:21 · 1058 阅读 · 0 评论 -
#1489 : Legendary Items
原题链接时间限制:10000ms单点时限:1000ms内存限制:256MB描述Little Hi is playing a video game. Each time he accomplishes a quest in the game, Little Hi has a chance to get a legendary item.At the b转载 2017-04-09 18:42:16 · 482 阅读 · 0 评论 -
最大子阵和(百度2017秋招真题)
题目描述 给出一个n行m列的二维矩阵A[m,n],其每个元素的取值范围是[-1000,1000],其中1<=n<=100,1<=m<=100。求出p,q,r,s,满足条件1<=p<=q<=n,1<=r<=s<=m且p<=i<=q,r<=j<=s的(i,j)对应的A[i,j]之和最大。若(p,q,r,s)有多个解,输出最大子阵和即可。原创 2017-06-26 20:31:13 · 1035 阅读 · 0 评论 -
POJ 1050 To the Max
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 49003 Accepted: 25928DescriptionGiven a two-dimensional array of positive and negative integer原创 2017-07-02 18:08:43 · 224 阅读 · 0 评论 -
POJ 1018 Communication System
Communication SystemTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 28974 Accepted: 10303DescriptionWe have received an order from Pizoor Communications原创 2017-07-02 17:19:48 · 191 阅读 · 0 评论 -
HDOJ 1024 Max Sum Plus Plus 最大M字段和
Problem DescriptionNow I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more diff原创 2017-07-05 19:03:54 · 286 阅读 · 0 评论 -
POJ 1083 Moving Tables
Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.The floor has 200 rooms each on the north side and south side along原创 2017-07-09 14:20:50 · 198 阅读 · 0 评论 -
POJ 1125 Stockbroker Grapevine
Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in原创 2017-07-09 14:25:57 · 277 阅读 · 0 评论 -
POJ 1088 滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 1原创 2017-07-09 14:10:55 · 257 阅读 · 0 评论 -
Leetcode 464. Can I Win
In the “100 game,” two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.What if we change the game so tha原创 2017-07-09 17:23:50 · 458 阅读 · 0 评论 -
POJ 1143Number Game
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First,原创 2017-07-09 17:45:19 · 316 阅读 · 0 评论 -
POJ 1179 Polygon
Description Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with eith原创 2017-09-04 23:03:23 · 440 阅读 · 0 评论 -
POJ 1189 钉子和小球
Description 有一个三角形木板,竖直立放,上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1)。每颗钉子和周围的钉子的距离都等于d,每个格子的宽度也都等于d,且除了最左端和最右端的格子外每个格子都正对着最下面一排钉子的间隙。 让一个直径略小于d的小球中心正对着最上面的钉子在板上自由滚落,小球每碰到一个钉子都可能落向左边或右边(概率各1/2),且球的中心还会正对着下一原创 2017-09-05 14:45:30 · 255 阅读 · 0 评论 -
DNA Sorting
DescriptionOne measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this mea原创 2017-04-19 09:20:25 · 298 阅读 · 0 评论