今日题解

一周中的第几天

w = (day+2month+3(month+1)/5 + year +year/4 - year/100 + year/400) % 7

``````class Solution {
public:
string dayOfTheWeek(int day, int month, int year) {
string res[] = { "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" } ;
if ( month == 1 || month == 2 )
{
month = month + 12 ;
year -- ;
}
int index = 0 ;
//基姆拉尔森计算公式
index = ( day + 2 * month + 3 * ( month + 1 ) / 5 + year + year / 4 - year / 100 + year / 400 ) % 7 ;
return res[index] ;
}
};
``````

一年中的第几天

``````class Solution {
public:
int dayOfYear(string date) {
int cnt = 0;
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(5, 2));
int day = stoi(date.substr(8, 2));
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//判断是否是闰年
if((year%400) == 0 || (year%4) == 0 && (year/100) != 0) {
months[1] = 29;
}
for(int i=1; i<month; ++i) {
cnt += months[i-1];
}
cnt += day;

return cnt;
}
};
``````

判断子序列

``````class Solution {
public:
bool isSubsequence(string s, string t) {
int i=0, j=0;
while( i< s.size() && j <t.size()) {
if(s[i] == t[j]) {
++i;
}
++j;
}
return i == s.size();
}
};
``````

差的绝对值为k的数对数目

``````class Solution {
public:
int countKDifference(vector<int>& nums, int k) {
int res = 0;
for(int i=0; i<nums.size()-1; i++)
{
for(int j=i+1; j<nums.size(); j++)
{
if(abs(nums[i] -nums[j]) == k) res++;//abs：求绝对值
}
}
return res;
}
};
``````

找不同

``````class Solution {
public:
char findTheDifference(string s, string t) {
int res = 0;
for(auto &i : t) {
res += i;
}
for(auto &i : s) {
res -= i;
}
return res;
}
};
``````

拥有糖果最多的孩子

``````class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {

//  *max_element：输出集合最大元素
int maxCandies = *max_element(candies.begin(), candies.end());
vector<bool> res;
for(int i=0; i<candies.size(); ++i) {
res.push_back(candies[i] + extraCandies >= maxCandies);
}
return res;
}
};
``````

所有奇数长度子数组的和

``````class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int res = 0;
for(int i=0; i<arr.size(); i++)
{
int sum = 0, len = 0;
for(int j=i; j<arr.size(); j++)
{
sum += arr[j];
len++;
if(len%2 == 1) res += sum;
}
}
return res;
}
};
``````

统计好三元组

``````class Solution {
public:
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
const int size = arr.size();
int res = 0;
for(int i=0; i<size; ++i) {
for(int j=i+1; j<size; ++j) {
for(int k=j+1; k<size; ++k) {
if(abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) ++res;
}
}
}
return res;
}
};
``````

``````class Solution {
public:
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
const int size = arr.size();
int res = 0;
for(int i=0; i<size; ++i) {
for(int j=i+1; j<size; ++j) {
if(abs(arr[i] - arr[j]) > a) {
continue;
}
for(int k=j+1; k<size; ++k) {
if(abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) ++res;
}
}
}
return res;
}
};
``````

按既定顺序创建目标数组

``````class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int> res;
for(int i=0; i<index.size(); ++i) {
res.insert(res.begin() + index[i], nums[i]);
}
return res;
}
};
``````

统计平方和三元组的数目

``````class Solution {
public:
int countTriples(int n) {
int res = 0;
// 枚举 a 与 b
for (int a = 1; a <= n; a++){
for (int b = 1; b <= n; b++){
// 判断是否符合要求
int c = int(sqrt(a * a + b * b));
if (c <= n && c * c == a * a + b * b){
res++;
}
}
}
return res;
}
};
``````

搜索二维矩阵Ⅱ

``````class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.size() == 0) return false;
int i=0;
int j = matrix[0].size()-1;
while(i<matrix.size() && j >= 0) {
if(matrix[i][j] == target) {
return true;
}
if(matrix[i][j] < target) {
++i;
}else --j;
}
return false;
}
};
``````

THE END