-
cpp
class Solution { public: int monotoneIncreasingDigits(int n) { string s = to_string(n); int flag = s.size(); for(int i = s.size() - 1; i > 0; i -- ) { if(s[i] < s[i - 1]) { flag = i; s[i - 1] --; } } for(int i = flag; i < s.size(); i++ ) { s[i] = '9'; } return stoi(s); } };前一个数字 > 当前数字,说明单调性被破坏,需要把前一个数字 减 1,并把它后面的所有数字变为 '9'
-
cpp
class Solution { public: vector<int> partitionLabels(string s) { int hash[26] = {0}; vector<int>result; int left = 0; int right = 0; for(int i = 0 ; i < s.size(); i ++ ) { hash[s[i] - 'a'] = i; } for(int i = 0; i < s.size() ; i++ ) { right = max(right, hash[s[i] - 'a']); if(i == right) { result.push_back(right - left + 1); left = i + 1; } } return result; } };用个hash表保存
-
cpp
class Solution { public: static bool cmp(vector<int>& a, vector<int>& b) { return a[0] < b[0]; } vector<vector<int>> merge(vector<vector<int>>& intervals) { sort(intervals.begin(),intervals.end(),cmp); vector<vector<int>>result; result.push_back(intervals[0]); for(int i = 1 ; i < intervals.size(); i ++ ) { if(result.back()[1] >= intervals[i][0]) { result.back()[1] = max(result.back()[1],intervals[i][1]); }else { result.push_back(intervals[i]); } } return result; } };先把第一个存入,再依次比较。
-
线程安全的单例
cppclass singleton { public: static singleton &getinstance() { static singleton instance; return instance; } singleton (const singleton &) = delete; singleton& operator= (const singleton&) = delete; private: singleton() { } ~singleton() { } }; int main() { singleton& pinstance = singleton::getinstance(); }
贪心+线程安全单例
TheLegendMe2025-11-28 12:28
相关推荐
地平线开发者7 小时前
J6B vio scenario sampleBothSavage19 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复小林ixn19 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生烬羽21 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌先吃饱再说2 天前
判断回文字符串,从一行代码到双指针优化黄敬峰2 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序得物技术2 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理AI小老六2 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent