intthreeSumClosest(vector<int>& nums, int target){ sort(nums.begin(),nums.end()); int size = nums.size(); int res = nums[0] + nums[1] + nums[2]; for (int i = 0; i < size - 2; i++) { int j = i + 1; int k = size - 1; while (j < k) { int sum = nums[i] + nums[j] + nums[k]; if (abs(sum-target) < abs(res-target)) res = sum; if (sum < target) ++j; elseif (sum > target) --k; else return target; } } return res; }
for i in range(96): # 插入数据 tmpId = r.choice(userId) # 在列表中随机取一个 print(tmpId, str(dateTime[i]), distance[i], lux[i], angle[i]) cur.execute("insert into data_series(user_id,date,distance,lux,angle) values(%s,%s,%s,%s,%s)", (tmpId, str(dateTime[i]), int(distance[i]), int(lux[i]), int(angle[i]))) cur.execute('select * from data_series') # 查询数据 for s in cur.fetchall(): print(s) conn.commit()
int size = nums.size(); vector<vector<int>> res; int usedi = 0; int usedj = 0; bool flag = true;//有三个以上的0时,只加入一次 for (int i = 0; i < size - 2; ++i) { int j = i + 1; int k = size - 1;
while (j < k) { int t = nums[i] + nums[j] + nums[k]; if (t == 0) { vector<int> y; y.push_back(nums[i]); y.push_back(nums[j]); y.push_back(nums[k]); res.push_back(y); while (j < size - 2 && nums[j] == nums[j + 1]) ++j; while (k > 0 && nums[k] == nums[k - 1]) --k; ++j; --k; continue; } elseif (t > 0) --k; else ++j; } usedi = nums[i]; while (i < size - 3 && usedi == nums[i + 1]) ++i;//与前面重复的数字就不要再向后搜索 } return res; }
stringlongestCommonPrefix(vector<string>& strs){ string s = ""; int size = strs.size(); if (size == 0) return s; int* lens = newint[size]; int min= strs[0].length(); for (int i = 1; i < size; ++i) { lens[i] = strs[i].length(); if (lens[i] < min) min = lens[i]; } char ch; bool flag = true; for (int i = 0; i < min && flag; ++i)//字符串长度 { ch = strs[0][i]; for (int j = 0; j < size; ++j)//遍历每个字符串 { if (strs[j][i] != ch) { flag = false; break; } } if (flag) s = s + ch; } return s; }