本周开始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);
}
};