贴一个回溯 + 剪枝 实际时间复杂度应该很低 每一位数最多两种可能O(2^9) 欢迎大佬指正 #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; int solve(vector<int> nums, int n){ sort(nums.begin(), nums.end()); nums.erase(unique(nums.begin(), nums.end()), nums.end()); int m = nums.size(); int res = 0; string target = to_string(n); int len = target.size(); function<bool(int)> dfs = [&](int x){ if(x == len && res < n){ if(res == 0) return false; return true; } for(int i = m - 1; i >= 0; i --){ if(res + nums[i] * (int) pow(10, len - x - 1) >= n) continue; res += nums[i] * (int)pow(10, len - x - 1); // cout << res << "\n"; if(dfs(x + 1)) return true; res -= nums[i] * (int)pow(10, len - x - 1); } if(x == 0){ res = 0; if(dfs(x + 1)) return true; } return false; }; if(dfs(0)) return res; return -1; } int main() { vector<int> nums = {6, 9, 3, 5}; cout << solve(nums, 56449) << "\n"; }
点赞 评论

相关推荐

rbjjj:太杂了吧,同学,项目似乎都没深度,都是api调度耶,分层架构思想没有体现出来了,前端没有前端优化前端工程化体现,后端微服务以及分层架构没体现以及数据安全也没体现,核心再改改,注重于计算机网络,工程化,底层原理吧
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务