lc812.鞋带公式
s=三个梯形
梯形计算:
1/2*(上底+下底)*高=1/2*(y和)*(x差)
成功转化为了point坐标计算~
忽想起7月有场周赛题,求的是梯形面积

class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
const int N = points.size();
double res = 0;
for (int i = 0; i < N - 2; i ++) {
for (int j = i + 1; j < N - 1; j ++) {
for (int k = j + 1; k < N; k ++) {
auto& point1 = points[i];
auto& point2 = points[j];
auto& point3 = points[k];
int x1 = point1[0], y1 = point1[1];
int x2 = point2[0], y2 = point2[1];
int x3 = point3[0], y3 = point3[1];
res = max(res, 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)));
}
}
}
return res;
}
};
lc1208
前缀和+滑窗
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.size();
vector<int> pre(n + 1, 0);
// pre[0] = 0,方便计算区间和
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + abs(s[i] - t[i]);
}
int ret = 0;
for (int l = 0, r = 0; r < n; r++) {
// 找到最小的 left 使得 pre[right + 1] - pre[left] > maxCost
++while (pre[r + 1] - pre[l] > maxCost)
{
l++;
}++
ret = max(ret, r - l +++ 1);++
}
return ret;
}
};
lc299
class Solution {
public:
string getHint(string secret, string guess)
{
int n=secret.size();
vector<bool> prec(n,false);
int a=0,b=0;
unordered_map<char,int> hash;
for(int i=0;i<n;i++)
{
if(secret[i]==guess[i])
{
prec[i]=true;
a++;
}
else
{
hash[guess[i]]++;
}
}
for(int i=0;i<n;i++)
{
char s=secret[i];
if(hash.count(s) && !prec[i])
{
b++;
hash[s]--;
if(hash[s]==0)
hash.erase(s);
}
}
string ret;
string aa=to_string(a);
ret+=aa;
ret+="A";
string bb=to_string(b);
ret+=bb;
ret+="B";
return ret;
}
};
lc01.08
开数组存一下
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix)
{
int m=matrix.size(),n=matrix[0].size();
vector<vector<int>> t=matrix;
function<void(int,int)> dfs=[&](int a,int b)
{
for(int i=0;i<m;i++)
t[i][b]=0;
for(int j=0;j<n;j++)
t[a][j]=0;
};
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(matrix[i][j]==0)
{
dfs(i,j);
}
}
}
matrix=t;
}
};