回溯

lc

lc2121

hash抽象分组后前缀和

注意 这个地方的下标个数统计要-1...

++(ll)bi * (m - i-1);++

class Solution {

typedef long long ll;

public:

vector<long long> getDistances(vector<int>& arr)

{

int n=arr.size();

vector<ll> ret(n);

unordered_map<int,vector<int>> hash;

for(int i=0;i<n;i++)

{

hasharr\[i].push_back(i);

}

for(auto& a,b:hash)

{

int m=b.size();

vector<ll> p(m+1);

for(int i=1;i<=m;i++)

pi=pi-1+bi-1;

for(int i=0;i<m;i++)

{

ll f = (ll)bi * i - pi;

ll e = (pm - pi+1) - ++(ll)bi * (m - i-1);++

retb\[i] = f + e;

}

}

return ret;

}

};

lc320

优雅的回溯

//缩写

dfs(p + 1, cnt + 1);

//结算 +cnt +p后

dfs(p + 1, 0); // p+1、重置cnt为0

数字串回溯tricks:

++if (cnt > 0)++

++path.erase(path.end() - to_string(cnt).size(), path.end());++

class Solution {

public:

vector<string> generateAbbreviations(string word)

{

int n = word.size();

string path;

vector<string> ret;

auto dfs = \&(this auto&& dfs,int p, int cnt)

{

if (p == n) {

if (cnt > 0)

path += to_string(cnt);

ret.push_back(path);

++if (cnt > 0) path.erase(path.end() - to_string(cnt).size(), path.end());//回溯++

return;

}

//缩写

dfs(p + 1, cnt + 1);

// 结算+保留

if (cnt > 0)

path += to_string(cnt);

path += wordp;

++dfs(p + 1, 0); // p+1、重置cnt为0++

// 回溯

path.pop_back();

if (cnt > 0) {

path.erase(path.end() - to_string(cnt).size(), path.end());

}

};

dfs(0, 0);

return ret;

}

};

相关推荐
维克兜率天1 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
AI情绪识别开源2 小时前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
老当益壮梁奶奶2 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法
不会就选b3 小时前
算法日常・每日刷题--<BFS拓扑排序>4
算法
不正经学生3 小时前
C语言动态内存管理(上):堆上的自由与责任
c语言·开发语言·c++·算法·面试
伟大的车尔尼3 小时前
贪心的概念
数据结构·算法·贪心
方方洛3 小时前
vllm教程-00-前言与导读
人工智能·算法·vllm
IT毕设实战小研3 小时前
电影数据可视化推荐系统
大数据·后端·爬虫·python·算法·信息可视化·课程设计
且随疾风前行.3 小时前
从驱动来分析服务的添加过程
算法
CoderYanger4 小时前
A.每日一题:输入单词需要的最少按键次数 Ⅰ+Ⅱ
java·数据结构·算法·leetcode·面试