自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(19)
  • 问答 (3)
  • 收藏
  • 关注

原创 leetcode第 284 场周赛(未完成)

6031. 找出数组中的所有 K 近邻下标究极简单题,用时7分37秒。我的方法:先把满足条件2的单独存一个数组,再一边遍历新数组,一边对着原数组逐一检查是否符合条件1。总结:多此一举,既然都是两次循环,直接两次循环找出最终答案得了,浪费了一个新数组的空间和第二次遍历新数组的时间,代码还更长。5203. 统计可以提取的工件简单中等题,用时15分钟多。我的方法:先遍历dig数组,将二元组[a,b]存进二维数组w[a][b]=1中,然后遍历工件数组,逐一检查每个工件占用的每个位置。总结

2022-03-13 20:38:13 5770

原创 70. 爬楼梯

法一:递归思路除了1,2层之外到达任何一层n都只有两种办法:到达n-1层再走一步1阶; 到达n-2层再走一步2阶;所以climbStairs(n)=climbStairs(n-1)+climbStairs(n-2);注意递归出口是1层返回1和2层返回2;而非1层返回1和0层返回0,因为到2有两种方法,但是按后者算只能算出一种。简单递归会超时,将计算过的用一个数组或者map存起来就行了(就本题而言,用数组int m[46]效率更高)。代码class Solution {

2022-03-12 11:24:03 1284

原创 4.寻找两个正序数组的中位数

4. 寻找两个正序数组的中位数思路有点难的题,用到划分的思想,重点是找到一种划分使得:num1中的分割线左<num2中的分割线右; num2中的分割线左<num1中的分割线右。根据题解,只考虑移动一个数组中的分割线, 用二分查找移动分割线,另一个数组中的分割线位置根据前者的位置计算得出。注意1 数组相关的极端情况这里看到两个数组,考虑:某一个数组为空时怎么办? 一个数组整体都位于分割线的一端怎么办?由于这里要考虑分割线1在nums1的首位左边的情况,分割线1在

2022-03-10 16:36:41 1300

原创 leetcode做题过程中遇到的易错细节

1.min()对比的函数类型应该相同。例.用下列代码解决541. 反转字符串 II会报错,因为i+k是常数类型,s.size()是unsigned long类型,所以会报很长一串错。string reverseStr(string s, int k) { for(int i=0;i<s.size();i+=2*k){ reverse(s.begin()+i,s.begin()+min(i+k,s.size())); } return s;}把.

2022-03-10 16:22:47 302

原创 leetcode做题方法总结

1.滑动窗口法例题:209. 长度最小的子数组(找出满足>=target的最小子数组)思路:用两个指针(类似于快慢指针)。不满足就右移r指针,直到满足为止,满足了就左移l指针,直到不满足为止。法一:单循环,每次只移动一个指针,满足移左,不满足移右。这个方法时间用得比法二少,但是代码量更多,因为需要单独处理第一个元素(否则如果第一个元素已经满足了要求,第一次循环就会把左指针往右移一位,导致左指针到了右指针的右边)。class Solution {public: int m

2022-03-10 16:22:05 1617

原创 6010. 完成旅途的最少时间,二分查找的好(ěxīn)题

6010. 完成旅途的最少时间282场周赛中的第三题,比赛中没做出来,提交基本都是超时,最后半小时想出来要用二分查找但是很多莫名bug,下午看了别人的题解自己做还是很多bug,提交第16次终于通过了,因此记录一下遇到的坑。需注意的点1.二分查找开始时确定最大值和最小值我的想法是:假设每辆车都用最少的单趟时间,得到最小值:最小时间=旅途总数*最小的单趟时间/车的数量;同理,最大时间=旅途总数*最大的单趟时间/车的数量。这样做有一个很严重的问题:int型的除法只保留整数,会造成巨大的误差

2022-02-27 19:06:31 110

原创 编程实现getMostJumps()函数功能接口:

题目编程实现下述函数功能接口:int getMostJumps(int *inputArray,int n,int *&outputIndexArray,int m);输入: n个不小于0的整数,(n为动态可变),输出:相邻两个数字的变化的绝对值,按照从大到小的降序排列, 输出前m个变化最大的位标,(m 为动态参数),如果有相同的变化大小,则从位标从小到大来排列,如果 相邻数字变化的个数小于m个,则输出实际的个数。函数返回值为 输出的数列的个数。:int inputArray[20]=..

2022-02-24 17:47:09 246

原创 leetcode做题过程中遇到的神bug

1.std::string基础数据结构不合理例:剑指 Offer 05. 替换空格请实现一个函数,把字符串 s 中的每个空格替换成"%20"。我的代码class Solution {public: string replaceSpace(string s) { char n; while((n=s.find(' '))!= string::npos){ s.replace(n,1,"%20"); }

2022-02-15 16:20:43 7060

转载 c++二分查找的方式

例209. 长度最小的子数组o(nlogn)的解法如下:class Solution {public: int minSubArrayLen(int s, vector<int>& nums) { int n = nums.size(); if (n == 0) { return 0; } int ans = INT_MAX; vector<int> .

2022-02-11 11:38:44 230

原创 27. 移除元素

题目给你一个数组 nums和一个值 val,你需要 原地 移除所有数值等于val的元素,并返回移除后数组的新长度。不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地修改输入数组。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。输入:nums = [0,1,2,2,3,0,4,2], val = 2输出:5, nums = [0,1,4,0,3]解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意...

2022-02-10 09:59:26 45

原创 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 is calledthe deepest root.Input Specification:E.

2021-09-01 10:08:28 69

原创 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 need to repair any other highways to keep the rest of t

2021-08-30 22:05:44 119

原创 1107 Social Clusters (30 分)

原题When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. Asocial clusteris a set of people who have some of their hobbies in common. You are supposed to find all th..

2021-08-28 18:17:13 82

原创 1070 Mooncake

原题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 given the inventory amounts and the prices of all ki

2021-08-26 15:41:08 57

原创 1038 Recover the Smallest Number (30 分)

原题Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different

2021-08-25 22:22:17 127

原创 1037 Magic Coupon (25 分)

需要查看代码请看通过代码栏目。原题The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also of

2021-08-25 13:29:19 62

原创 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 gas station may give different price. You are asked to

2021-08-24 19:04:16 91

原创 C/C++实用语法(针对pat考试的复习)

虽然学过C和C++但是好久没写语法都生疏了,才会像上一篇文章一样避开很多简便方法,为了之后写代码能稍微高效一些,必须整理一下实用语法(以《算法笔记》为学习材料,针对本人本次pat考试进行整理,代码采用C++中包含C的形式)。基本类型int整型绝对值在10^9(包括10^9)范围内的整数都可以定义成int类型。long long长整型范围(大于2147483647)不好记,就把超过10^9的都定义成long long吧。double浮点型单精度float的精度不够,碰到浮点型数据都

2021-08-22 03:04:35 476

原创 PAT 1001 A+B Format

(查看最终代码请直接看最后一个代码段)还有两周考pat,好久没写c++代码了,极限刷题一波,看看有没有机会抱佛大腿。题目1001 A+B Format (20 分)Calculatea+band 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)...

2021-08-19 12:01:54 60

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除