
算法
文章平均质量分 74
littledou2015
真正了不起的程序员对自己程序的每一个字节都了如指掌。
展开
-
使用数组来模拟邻接表存储图信息
一般使用邻接表存储图信息的数据结构如下图vector vec[N];使用数组模拟邻接表的数据结构如下图(这个存储方法很新颖,可以了解一下,以前在别人的算法中屡次遇见,今天才明白它的存储逻辑)int head[N];int p[M]; 存储的是边指向的点下标int nextP[M]; 存储的是边int edgecnt = 0;//加入边void addedge(in原创 2015-05-25 21:27:22 · 2124 阅读 · 1 评论 -
12、Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: string intToRoman(int num) { int numtt[4] = {0};原创 2015-03-03 18:52:36 · 434 阅读 · 0 评论 -
Leetcode Sum一类题目
Leetcode里有一类这样的题目,给定一个数组和一个target值,寻找数组中的几个数相加等于target值。1、two sum,寻找数组中两个数相加等于target值2、three sum,寻找数组中三个数相加等于target值3、four sum,,寻找数组中四个数相加等于target值4、寻找任意几个数相加等于target值5、寻找几个数相加最接近于target值注原创 2014-12-11 22:18:55 · 573 阅读 · 0 评论 -
172、Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.int trailingZeroes(int n) { //返回n!中0的个数,这个题目就是求质数5的个数,原创 2015-01-27 16:05:19 · 370 阅读 · 0 评论 -
1089. Insert or Merge (25)
According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input dat原创 2014-12-17 18:54:45 · 432 阅读 · 0 评论 -
1091. Acute Stroke (30)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to c原创 2014-12-17 21:53:37 · 475 阅读 · 0 评论 -
1010. Radix (25)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.Now for any pair of positive原创 2014-11-10 18:45:29 · 426 阅读 · 0 评论 -
1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers原创 2014-11-24 14:09:13 · 350 阅读 · 0 评论 -
1034. Head of a Gang (30)
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be t原创 2014-11-27 23:00:18 · 380 阅读 · 0 评论 -
10、Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st原创 2015-03-03 18:25:39 · 374 阅读 · 0 评论 -
PAT 1073. Scientific Notation (20)
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the in原创 2015-03-12 23:24:25 · 412 阅读 · 0 评论 -
PAT 1078. Hashing (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key %原创 2015-03-13 15:24:49 · 391 阅读 · 0 评论 -
PAT 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a原创 2015-03-14 09:33:34 · 420 阅读 · 0 评论 -
PAT 1079. Total Sales of Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on原创 2015-03-13 16:24:30 · 470 阅读 · 0 评论 -
PAT 1077. Kuchiguse (20)
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called原创 2015-03-13 14:42:02 · 413 阅读 · 0 评论 -
PAT 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker原创 2015-03-12 19:23:01 · 396 阅读 · 0 评论 -
PAT 1075. PAT Judge (25)
The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.Input Specification:Each input fil原创 2015-03-13 11:16:43 · 385 阅读 · 0 评论 -
PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now原创 2015-03-12 17:23:48 · 375 阅读 · 0 评论 -
PAT 1074. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K原创 2015-03-13 10:07:45 · 394 阅读 · 0 评论 -
PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the hou原创 2015-03-12 22:42:20 · 398 阅读 · 0 评论 -
1023. Have Fun with Numbers (20)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number con原创 2014-11-23 16:03:40 · 430 阅读 · 0 评论 -
1022. Digital Library (30)
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number a原创 2014-11-23 15:51:24 · 381 阅读 · 0 评论 -
1009. Product of Polynomials (25)
This time, you are supposed to find A*B where A and B are two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the inf原创 2014-11-10 15:00:56 · 395 阅读 · 0 评论 -
1008. Elevator (20)
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 sec原创 2014-11-10 14:37:00 · 366 阅读 · 0 评论 -
1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj} where 1 Maximum Subsequence is the continuous subsequence which has the largest sum原创 2014-11-10 10:33:27 · 389 阅读 · 0 评论 -
1006. Sign In and Sign Out (25)
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you原创 2014-11-09 23:17:59 · 322 阅读 · 0 评论 -
1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.InputEach input file contains one test case. Each case starts with原创 2014-11-09 22:36:52 · 346 阅读 · 0 评论 -
1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l原创 2014-11-09 21:55:33 · 366 阅读 · 0 评论 -
1005. Spell It Right (20)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.Input Specification:Each input file contains one test case.原创 2014-11-09 22:47:58 · 375 阅读 · 0 评论 -
PAT 1001. A+B Format (20)
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).InputEach input file cont原创 2014-11-08 12:11:23 · 428 阅读 · 0 评论 -
1011. World Cup Betting (20)
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Si原创 2014-11-10 19:05:56 · 348 阅读 · 0 评论 -
1012. The Best Rank (25)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - Eng原创 2014-11-11 14:06:33 · 358 阅读 · 0 评论 -
1013. Battle Over Cities (25)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we nee原创 2014-11-11 15:55:39 · 382 阅读 · 0 评论 -
1033. To Fill or Not to Fill (25)
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different ga原创 2014-11-27 19:40:09 · 369 阅读 · 0 评论 -
1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root原创 2014-11-20 18:00:19 · 312 阅读 · 0 评论 -
1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the cor原创 2014-11-20 17:50:52 · 463 阅读 · 0 评论 -
1018. Public Bike Management (30)
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the原创 2014-11-20 16:53:19 · 344 阅读 · 0 评论 -
1017. Queueing at Bank (25)
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow li原创 2014-11-20 14:32:16 · 440 阅读 · 0 评论 -
1016. Phone Bills (25)
A long-distance telephone company charges its customers by the following rules:Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. Wh原创 2014-11-20 10:41:02 · 418 阅读 · 0 评论 -
1014. Waiting in Line (30)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:The s原创 2014-11-13 22:03:53 · 361 阅读 · 0 评论