算法基础
qq_34673038
这个作者很懒,什么都没留下…
展开
-
PAT A 1011
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...原创 2019-12-28 18:40:54 · 83 阅读 · 0 评论 -
PAT A 1010
1010 Radix (25point(s))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 an...原创 2019-12-28 18:24:26 · 163 阅读 · 0 评论 -
PAT A 1009
1009 Product of Polynomials (25point(s))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 li...原创 2019-12-28 16:54:42 · 86 阅读 · 0 评论 -
PAT A 1008
1008 Elevator (20point(s))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 spec...原创 2019-12-28 16:32:53 · 81 阅读 · 0 评论 -
PAT A 1007
1007 Maximum Subsequence Sum (25point(s))Given a sequence of K integers { N1 , N2 , …, NK }. A continuous subsequence is defined to be { Ni , Ni+1 , …, Nj } where 1≤i≤j...原创 2019-12-28 16:23:01 · 75 阅读 · 0 评论 -
PAT A 1006
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...原创 2019-12-27 23:49:16 · 94 阅读 · 0 评论 -
PAT A 1005
1005 Spell It Right (20point(s))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 f...原创 2019-12-27 23:17:56 · 108 阅读 · 0 评论 -
PAT A 1004
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts w...原创 2019-12-27 22:54:09 · 181 阅读 · 0 评论 -
PAT A 1003
1003 Emergency (25point(s))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 ...原创 2019-12-27 21:45:07 · 90 阅读 · 0 评论 -
PAT A 1002
1002 A+B for Polynomials (25point(s))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...原创 2019-12-27 13:20:09 · 75 阅读 · 0 评论 -
pat A 1001
1001 A+B Format (20point(s))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).Input ...原创 2019-12-27 11:52:24 · 90 阅读 · 0 评论 -
455. 分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。注意:你可以假设胃口值为正。一个小...原创 2019-05-27 13:39:39 · 90 阅读 · 0 评论 -
405. 数字转换为十六进制数
在这里插入代码片class Solution {public: string toHex(int num) { string res = "", str = "0123456789abcdef"; int cnt = 0; while (num != 0 && cnt++ < 8) { res...原创 2019-05-28 00:25:53 · 115 阅读 · 0 评论 -
168. Excel表列名称
class Solution {public: string convertToTitle(int n) { string res; while (n) { res = char ((n - 1) % 26 + 'A') + res; n = (n - 1) / 26; //注意corner case: n=...原创 2019-05-28 00:35:08 · 89 阅读 · 0 评论 -
172. 阶乘后的零
class Solution {public: int trailingZeroes(int n) { return n < 5 ? 0 : n/5 + trailingZeroes(n/5); //递归 }};原创 2019-05-28 00:41:59 · 76 阅读 · 0 评论 -
462/minimum-moves-to-equal-array-elements-ii/
int res = 0, n = nums.size(), mid = n / 2; nth_element(nums.begin(), nums.begin() + mid, nums.end()); for (int i = 0; i < n; ++i) { res += abs(nums[i] - nums[mid]); ...原创 2019-05-28 00:51:06 · 132 阅读 · 0 评论 -
367 https://leetcode-cn.com/problems/valid-perfect-square/
class Solution {public: bool isPerfectSquare(int num) { int subNum = 1; while (num > 0) { num -= subNum; subNum += 2; } return num == 0; }};原创 2019-05-28 00:54:56 · 668 阅读 · 0 评论 -
326 https://leetcode-cn.com/problems/power-of-three/
class Solution {public: bool isPowerOfThree(int n) { return n > 0 && 1162261467 % n == 0; }};原创 2019-05-28 00:57:23 · 540 阅读 · 0 评论 -
238 https://leetcode-cn.com/problems/product-of-array-except-self/
class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); vector<int> output(n,1);//初始化一个全为1的向量 for(int i=1;i&l...原创 2019-05-28 00:58:26 · 528 阅读 · 0 评论 -
628 https://leetcode-cn.com/problems/maximum-product-of-three-numbers/
class Solution {public: int maximumProduct(vector<int>& nums) { int max1 = INT_MIN ,max2= INT_MIN, max3 = INT_MIN, min1 = INT_MAX, min2 = INT_MAX; for (int n : nums) { ...原创 2019-05-28 01:08:42 · 345 阅读 · 0 评论 -
67 https://leetcode-cn.com/problems/add-binary/
class Solution {public: string addBinary(string a, string b) { if (a.size() < b.size()) swap(a, b); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); ...原创 2019-05-28 01:11:03 · 530 阅读 · 0 评论 -
504 七进制数
class Solution {public: string convertToBase7(int num) { if(num==0) return "0"; bool tag = false; if(num<0){ num=-num; tag=true; } ...原创 2019-05-28 00:16:09 · 118 阅读 · 0 评论 -
122. 买卖股票的最佳时机 II
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/class Solution {public: int maxProfit(vector<int>& prices) { int maxprofit=0; for(int i=1;i<price...原创 2019-05-27 15:48:49 · 126 阅读 · 0 评论 -
121. 买卖股票的最佳时机
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int minprice = prices[0],maxprofit = 0; for(int i=1;i<prices.size(...原创 2019-05-27 15:47:04 · 98 阅读 · 0 评论 -
406. 根据身高重建队列
bool cmp(const vector<int>& a,const vector<int>& b) { return a[0]>b[0]||(a[0]==b[0]&&a[1]<b[1]);}class Solution {public: vector<vector<int>>...原创 2019-05-27 15:45:05 · 167 阅读 · 0 评论 -
452. 用最少数量的箭引爆气球
bool cmp(const vector<int>& a,const vector<int>& b) { return a[1]<b[1];}class Solution {public: int findMinArrowShots(vector<vector<int>>& interva...原创 2019-05-27 15:03:07 · 114 阅读 · 0 评论 -
leetcode 435不重叠区间
bool cmp(const vector<int>& a,const vector<int>& b) { return a[1]<b[1];}class Solution {public: int eraseOverlapIntervals(vector<vector<int>>& int...原创 2019-05-27 14:41:01 · 172 阅读 · 0 评论 -
75-颜色分类
给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。注意:不能使用代码库中的排序函数来解决这道题。示例:输入: [2,0,2,1,1,0]输出: [0,0,1,1,2,2]进阶:一个直观的解决方案是使用计数排序的两趟扫描算法。首先,迭代计...原创 2019-05-27 13:20:30 · 106 阅读 · 0 评论 -
451. 根据字符出现频率排序
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。示例 1:输入:“tree”输出:“eert”解释:'e’出现两次,'r’和’t’都只出现一次。因此’e’必须出现在’r’和’t’之前。此外,"eetr"也是一个有效的答案。示例 2:输入:“cccaaa”输出:“cccaaa”解释:'c’和’a’都出现三次。此外,"aaaccc"也是有效的答案。注意"cac...原创 2019-05-27 12:50:55 · 180 阅读 · 0 评论 -
347. 前K个高频元素
347. 前K个高频元素题目描述给定一个非空的整数数组,返回其中出现频率前 k 高的元素。示例 1:输入: nums = [1,1,1,2,2,3], k = 2输出: [1,2]示例 2:输入: nums = [1], k = 1输出: [1]说明:你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。你的算法的时间复杂度必须优于 O(n l...原创 2019-05-27 12:08:08 · 108 阅读 · 0 评论 -
基于leetcode215题的对于排序算法的思考
leetcode 215题目描述在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。样例示例 1:输入: [3,2,1,5,6,4] 和 k = 2输出: 5示例 2:输入: [3,2,3,1,2,4,5,5,6] 和 k = 4输出: 4说明:你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的...原创 2019-05-26 00:46:56 · 178 阅读 · 0 评论 -
605 种花问题
class Solution {public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { for(int i=0;i<flowerbed.size();i++) if(flowerbed[i]==0){ if((i==0||f...原创 2019-05-27 16:06:16 · 164 阅读 · 0 评论 -
双指针算法总结
双指针算法实用i,j两个变量,不会退的扫描一个数组常规写法for(int i=0,j=0,i<n;i++){ while(j<i&&check(i,j)) j++;}这是i,j从0开始扫,j<i的扫法for(int i=0,j=n-1;i<j;i++){ while(check()) j--;}这是i,j分别两端的写法或...原创 2019-05-25 04:15:53 · 660 阅读 · 0 评论