赞
踩
/*
LeeCode 真题 测试gpt 编程(使用Cursor V0.1.10)
共5道题,难度均为困难,没有提交,只是测试了题目里给的用例
结果: 错了两道通过率20%多的难题,
*/
这只是一个尝试性的测试,其它详细测试可以参考
小编实测GPT-4科研能力,略感失望_凤凰网 (ifeng.com)
下图据说是GPT-4测试leeCode的结果
出处:OpenAI宣布发布GPT-4比前版本-3.540-JoyIndie独游网
查证过GPT-4 (openai.com),这个图片的结果是对的。
----------------------------------------------------------------------------------------
以下为Cursor自动生成的代码
//题1
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
set<long long> s;
for (int i = 0; i < nums.size(); i++) {
if (i > k) {
s.erase(nums[i - k - 1]);
}
auto it = s.lower_bound((long long)nums[i] - t);
if (it != s.end() && *it - nums[i] <= t) {
return true;
}
s.insert(nums[i]);
}
return false;
}
};
//题2
class Solution {
public:
vector<string> addOperators(string num, int target) {
vector<string> res;
if (num.empty()) {
return res;
}
dfs(num, target, 0, 0, "", res);
return res;
}
void dfs(string& num, int target, long long prev, long long cur, string expr, vector<string>& res) {
if (num.empty() && cur == target) {
res.push_back(expr);
return;
}
for (int i = 1; i <= num.size(); i++) {
string str = num.substr(0, i);
if (str.size() > 1 && str[0] == '0') {
return;
}
long long val = stoll(str);
string next = num.substr(i);
if (expr.empty()) {
dfs(next, target, val, val, str, res);
} else {
dfs(next, target, val, cur + val, expr + "+" + str, res);
dfs(next, target, -val, cur - val, expr + "-" + str, res);
dfs(next, target, prev * val, cur - prev + prev * val, expr + "*" + str, res);
}
}
}
};
//题3
class Solution {
public:
int maxLevelSum(TreeNode* root) {
if (!root) {
return 0;
}
queue<TreeNode*> q{{root}};
int level = 0, maxSum = INT_MIN;
while (!q.empty()) {
int size = q.size(), sum = 0;
++level;
for (int i = 0; i < size; ++i) {
auto t = q.front(); q.pop();
sum += t->val;
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
if (sum > maxSum) {
maxSum = sum;
}
}
return level - 1;
}
};
//题4
class Solution {
public:
int minSubArrayLen(int k, vector<int>& nums) {
int n = nums.size();
int left = 0, right = 0;
int sum = 0;
int ans = INT_MAX;
while (right < n) {
sum += nums[right];
while (sum >= k) {
ans = min(ans, right - left + 1);
sum -= nums[left];
left++;
}
right++;
}
return ans == INT_MAX ? -1 : ans;
}
};
//题5
class Solution {
public:
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int splitArray(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, INT_MAX);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0) {
dp[i] = 1;
} else if (gcd(nums[j - 1], nums[i]) > 1) {
dp[i] = min(dp[i], dp[j - 1] + 1);
}
}
}
return dp[n - 1];
}
};
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。