本周开始leetcode增加了双周的contest,不过就目前来看似乎都锁了,那就不贴链接了

1064 Fixed Point
Given an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists.
Example 1:
Input: [-10,-5,0,3,7]
Output: 3
Explanation:
For the given array, A[0] = -10, A[1] = -5, A[2] = 0, A[3] = 3, thus the output is 3.
Example 2:
Input: [0,2,5,8,17]
Output: 0
Explanation:
A[0] = 0, thus the output is 0.
Example 3:
Input: [-10,-5,3,4,7,9]
Output: -1
Explanation:
There is no such i that A[i] = i, thus the output is -1.

很简单,判断第一个A[i] = i的数,若不存在则返回-1

class Solution {
public:int fixedPoint(vector<int>& A) {int res = -1;for(int i = 0; i < A.size(); ++i)if(i == A[i])return i;return res;}
};

1065 Index Pairs of a String
Given a text string and words (a list of strings), return all index pairs [i, j] so that the substring text[i]…text[j] is in the list of words.
Example 1:
Input: text = “thestoryofleetcodeandme”, words = [“story”,“fleet”,“leetcode”]
Output: [[3,7],[9,13],[10,17]]
Example 2:
Input: text = “ababa”, words = [“aba”,“ab”]
Output: [[0,1],[0,2],[2,3],[2,4]]
Explanation:
Notice that matches can overlap, see “aba” is found in [0,2] and [2,4].

从text中找出字符串,使其能够由words中的字符串组成,返回所有满足条件的字符串的下标集
暴力两轮for循环,先用哈希表存储所有words中的字符串,方便索引
第二轮循环的最大长度为words最大长度与原串的剩余字符串的较小值

class Solution {
public:vector<vector<int>> indexPairs(string text, vector<string>& words) {map<string, int> ms;int maxn = 0;for (string s : words){ms[s] = 1;maxn = max(maxn, s.length());}vector<vector<int>> res;for (int i = 0; i < text.size(); ++i){int len = min(text.length() - i, maxn);for (int j = 1; j <= len; ++j)if (ms[text.substr(i, j)]){vector<int> v = { i, j + i - 1 };res.emplace_back(v);}}return res;}
};

1066 Campus Bikes II
On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker and bike is a 2D coordinate on this grid.
We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized.
The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.
Return the minimum possible sum of Manhattan distances between each worker and their assigned bike.
Example 1:
Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
Output: 6
Explanation:
We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6.
Example 2:
Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: 4
Explanation:
We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. Both assignments lead to sum of the Manhattan dist

有N个worker和M辆bike,判断最优的分配策略,使所有worker都能分配到一辆bike,并使曼哈顿距离最小
其中 Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.
典型的dfs,若当前距离已大于当前最小值,则直接返回
这道题有点气,本来可以AK的,结果有个细节没注意到,导致这道题TLE了。。。

class Solution {
public:void assignBikesHelper(vector<int>& grid, vector<vector<int>>& workers, vector<vector<int>>& bikes, int& minn, int cur, int pos){if (cur >= minn) return;if (pos >= workers.size()){minn = min(minn, cur);return;}int x = workers[pos][0], y = workers[pos][1];for (int i = 0; i < bikes.size(); ++i){// vector<int> v = { bikes[i][0], bikes[i][1] };int dx =  bikes[i][0], dy =  bikes[i][1];if (grid[i] == 1) continue;grid[i] = 1;assignBikesHelper(grid, workers, bikes, minn, cur + abs(dx - x) + abs(dy - y), pos + 1);grid[i] = 0;}}int assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {vector<int> grid(bikes.size(), 0);int minn = 1e9;assignBikesHelper(grid, workers, bikes, minn, 0, 0);return minn;}
};

上面 // vector<int> v = { bikes[i][0], bikes[i][1] };这一行会导致TLE,把这一行纠正为下面dx 和 dy的表达即可AC(我也不知道当时当时为啥会这样写= =

1067 Digit Count in Range
Given an integer d between 0 and 9, and two positive integers low and high as lower and upper bounds, respectively. Return the number of times that d occurs as a digit in all integers between low and high, including the bounds low and high.
Example 1:
Input: d = 1, low = 1, high = 13
Output: 6
Explanation:
The digit d=1 occurs 6 times in 1,10,11,12,13. Note that the digit d=1 occurs twice in the number 11.
Example 2:
Input: d = 3, low = 100, high = 250
Output: 35
Explanation:
The digit d=3 occurs 35 times in 103,113,123,130,131,…,238,239,243.

给定三个数字d,low, high,判断从在[low, high]的所有数字中,数字d出现的次数
跟剑指offer中**“1~n中1出现的次数很像”**,只是这里处理0出现的次数时,会需要多考虑一些。
整体而言可以将其分为两部分[0, high] 中出现数字d的数目为x,[0, low - 1]中出现数字d的数目为y
则最后结果为y - x
算是个经典也稍微有点绕的题,详细解析可见添加链接描述0到n之间数字d出现的次数
附上AC代码:

class Solution {
public:int digitsCount(int d, int low, int high) {return count(high, d) - count(low-1, d);}unsigned check(unsigned n, unsigned d){unsigned res = 0;while (n){if (n % 10 == d) res++;n /= 10;}return res;}unsigned count(unsigned n, unsigned d){if (n < 10) return (d > 0 && n >= d);if (n % 10 != 9) return check(n, d) + count(n - 1, d);return 10 * count(n / 10, d) + n / 10 + (d > 0);}
};

Leetcode进阶之路——Biweekly Contest 1相关推荐

  1. LeetCode笔记:Biweekly Contest 56(补发)

    LeetCode笔记:Biweekly Contest 56 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  2. LeetCode笔记:Biweekly Contest 38 比赛记录

    LeetCode笔记:Biweekly Contest 38 0. 赛后总结 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 ...

  3. LeetCode笔记:Biweekly Contest 83

    LeetCode笔记:Biweekly Contest 83 0. 小结 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. ...

  4. LeetCode笔记:Biweekly Contest 55(补发)

    LeetCode笔记:Biweekly Contest 55 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  5. LeetCode笔记:Biweekly Contest 68

    LeetCode笔记:Biweekly Contest 68 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  6. LeetCode笔记:Biweekly Contest 65

    LeetCode笔记:Biweekly Contest 65 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  7. LeetCode笔记:Biweekly Contest 33 比赛记录

    LeetCode笔记:Biweekly Contest 33 0. 赛后总结 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 ...

  8. LeetCode笔记:Biweekly Contest 94

    LeetCode笔记:Biweekly Contest 94 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  9. LeetCode笔记:Biweekly Contest 88

    LeetCode笔记:Biweekly Contest 88 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

最新文章

  1. exe一机一码加密工具_Python代码加密混淆
  2. Android开发--SharedPreferences初步介绍
  3. php phppowerpoint 生成表格_php之EXCEL导出代码生成器的实现思路
  4. [NOIP2001] 提高组 洛谷P1024 一元三次方程求解
  5. js如何查看元素类型
  6. 2 s锁是什么_《演员请就位》:一场戏拿了2张S卡,任敏凭什么打败老戏骨?
  7. PySide: 信号、槽
  8. php 获取 body json,从PHP中的JSON POST读取HTTP请求正文的问题
  9. Django之WSGI浅谈
  10. 湖南麒麟下加载RapidIO驱动
  11. 职称计算机 将计算机broad_1下的e盘映射为k盘网络驱动器,职称计算机考试网络基础答案(1)...
  12. android 描点抠图源码,一款功能强大的AI驱动一键安卓抠图软件,人物商品图章签名logo...
  13. linux 代码编辑器软件下载,Visual Studio Code下载
  14. 科大讯飞 asr 实时语音转写 rtasr 基于Netty编写的websocket client SDK
  15. C# RSA2048 公钥加密,私钥解密
  16. 世界五大学习方法之费曼技巧
  17. os.system和os.popen和commands
  18. Android入门教程 (一) Android简介和android studio安装
  19. k线图基础知识图解——单根K线的含义
  20. qpsk的映射过程_QPSK实验报告

热门文章

  1. codeforces 632A Grandma Laura and Apples 模拟
  2. 案例实战-信用卡欺诈检测
  3. EDA(Quartus II)——正弦信号发生器的设计
  4. linux tcp setsockopt,linux网络编程系列(五)--setsockopt的经常使用选项
  5. ChatGPT炒股:自动批量下载choice每日热门研报
  6. Android开启手电筒功能(完美适配Android4x,5x,6x)
  7. 如何查看container工作在哪种网络模式
  8. 【Android】四大组件之 ContentProvider
  9. Linux下几种RTP协议实现的比较和JRTPLIB编程讲解
  10. python线程协程进程的区别_进程和线程、协程的区别