先吐槽一下leetcode周赛时间又变了… 等进去的时候才发现都快结束了= =
1013. Partition Array Into Three Parts With Equal Sum
Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + … + A[i] == A[i+1] + A[i+2] + … + A[j-1] == A[j] + A[j-1] + … + A[A.length - 1])
Example 1:
Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:
Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false
Example 3:
Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
给定数组A,判断能否将其划分为三段,且每段和相同
先求出数组的总和sum,若该和不能被3整除,则直接返回false
然后遍历整个数组,若和为 sum / 3 的子数组个数≥3,则返回true,否则false
class Solution {
public:
bool canThreePartsEqualSum(vector& A) {
int len = A.size();
vector a(len, 0);
a[0] = A[0];
for(int i = 1; i < len; ++i)
{
a[i] = a[i - 1] + A[i];
}
if(a[len - 1] % 3) return false;
int each = a[len - 1] / 3, cur = 0, time = 0;
for(int i = 0; i < len; ++i)
{
cur += A[i];
if(cur == each)
{
time++;
cur = 0;
}
}
return time >= 3;
}
};
1014. Best Sightseeing Pair
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
给定一个数组A,找出两个下标i, j (i < j),使得A[i] + A[j] + i - j的值最大
先给一个初始值maxn = A[0] + A[1] + 0 - 1
然后分别维护两个数add, sub,前一个add[i]表示到第i位,最大的A[i] + i, sub表示到第i位的A[i]-i
每次将maxn与add+sub做比较,保存较大值
class Solution {
public:
int maxScoreSightseeingPair(vector& A) {
int len = A.size();
int add = A[0], sub = A[1] - 1;
int maxn = add + sub;
for(int i = 2; i < len; ++i)
{
sub = A[i] - i;
add = max(A[i - 1] + i - 1, add);
maxn = max(maxn, sub + add);
}
return maxn;
}
};
1015. Smallest Integer Divisible by K
Given a positive integer K, you need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1.
Return the length of N. If there is no such N, return -1.
Example 1:
Input: 1
Output: 1
Explanation: The smallest answer is N = 1, which has length 1.
Example 2:
Input: 2
Output: -1
Explanation: There is no such positive integer N divisible by 2.
Example 3:
Input: 3
Output: 3
Explanation: The smallest answer is N = 111, which has length 3.
给定一个整数K,找出一个仅由1组成的数N,使得N%K = 0,返回N的长度
先明白一点,既然个位数为1,那么所有偶数和能被5整除的数都不可能存在对应的N,直接返回-1即可
对于整数K,共有K个余数,分别从0到K-1,而如果找到一个数m%K=r,这个r在前面已经出现过,那么(m* 10+1) % K也一定循环出现了
例如
1 % 12 = 1
11 % 12 = 11
111 % 12 = 3
1111 % 12 = 7
11111 % 12 = 11
11已经在上面出现了,那么下一个余数一定是3,并依次循环
而事实111111 % 12 = 3
数学证明
第一次出现余数r:m % k = r
第二次出现余数r:(m* 10t) % k = r
令 n = (m* 10t)
则下一个数(n* 10 + 1) % k = ((n%k * 10%k)%k + 1)%k
而(m10+1)%k=((m%k10%k)%k + 1)%k
可以看到上下两式仅m和n不同,而m%k=n%k,因此上下两式的结果相同
因此最多遍历到K个1的数,若还没找到,则直接返回-1即可
class Solution {
public:
int smallestRepunitDivByK(int K) {
if(K % 2 == 0 || K % 5 == 0) return -1;
set s;
long long int cnt = 1, cur = 1;
while(cnt <= K)
{
cur %= K;
if(cur == 0) return cnt;
if(s.find(cur) != s.end()) return -1;
cur = 10 * cur + 1;
cnt++;
}
return -1;
}
};
1016. Binary String With Substrings Representing 1 To N
Given a binary string S (a string consisting only of ‘0’ and '1’s) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = “0110”, N = 3
Output: true
Example 2:
Input: S = “0110”, N = 4
Output: false
给定一个字符串和整数N,判断字符串中是否包含所有从1~N的二进制表示
直接暴力解即可= =
class Solution {
public:
string intToBin(int n)
{
string s = "";
while(n)
{
s += (n % 2) + '0';
n /= 2;
}
reverse(s.begin(), s.end());
return s;
}
bool queryString(string S, int N) {
for(int i = N; i > 0; --i)
{
if(S.find(intToBin(i)) == string::npos) return false;
}
return true;
}
};
若有错或更好的方法,尤其是最后一题,还望告知,谢谢~