1. 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""
。
示例 1:
输入:strs = ["flower","flow","flight"]
输出:"fl"
示例 2:
输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。
提示:
0 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成
出处:
https://edu.csdn.net/practice/27544135
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: string longestCommonPrefix(vector<string> &strs) { string lcp; if (strs.size() == 0) return lcp; int min_len = INT_MAX; int min_idx = 0; for (int i = 0; i < strs.size(); ++i) { auto &s = strs[i]; if (s.size() < min_len) { min_len = s.size(); min_idx = i; } } auto &smin = strs[min_idx]; for (int i = 0; i < min_len; ++i) { char c = smin[i]; int j; for (j = 0; j < strs.size(); ++j) { auto &cs = strs[j]; if (c != cs[i]) break; } if (j == strs.size()) lcp += c; else break; } return lcp; } }; int main() { Solution s; vector<string> strs = {"flower","flow","flight"}; cout << s.longestCommonPrefix(strs) << endl; strs = {"dog","racecar","car"}; cout << s.longestCommonPrefix(strs) << endl; return 0; }
输出:
fl
//空
2. 打家劫舍
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 400
出处:
https://edu.csdn.net/practice/27544136
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: int rob(vector<int> &nums) { int n = nums.size(); if (n == 0) { return 0; } vector<long> f(n + 1); f[1] = nums[0]; for (int i = 2; i <= n; ++i) { f[i] = max(f[i - 1], f[i - 2] + nums[i - 1]); } return f[n]; } }; int main() { Solution s; vector<int> nums = {1,2,3,1}; cout << s.rob(nums) << endl; nums = {2,7,9,3,1}; cout << s.rob(nums) << endl; return 0; }
输出:
4
12
3. 最接近的三数之和
给定一个包括 n 个整数的数组 nums
和 一个目标值 target
。找出 nums
中的三个整数,使得它们的和与 target
最接近。返回这三个数的和。假定每组输入只存在唯一答案。
示例:
输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
提示:
3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4
以下程序实现了这一功能,请你填补空白处内容:
```c++
#include <cstdlib> class Solution { public: int threeSumClosest(vector<int> &nums, int target) { sort(nums.begin(), nums.end()); int cur, left, right; cur = 0; int closest = nums[0] + nums[1] + nums[2]; while (cur < nums.size() - 2) { left = cur + 1; right = nums.size() - 1; int n; while (left < right) { n = nums[cur] + nums[left] + nums[right]; if (abs(target - n) < abs(target - closest)) { closest = n; } if (n == target) { break; } ____________________________; else { int t = left + 1; while (t < right && nums[t] == nums[left]) t++; left = t; } } int t = cur + 1; while (t < nums.size() && nums[t] == nums[cur]) t++; cur = t; } return closest; } }; ```
出处:
https://edu.csdn.net/practice/27544137
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: int threeSumClosest(vector<int> &nums, int target) { sort(nums.begin(), nums.end()); int cur, left, right; cur = 0; int closest = nums[0] + nums[1] + nums[2]; while (cur < nums.size() - 2) { left = cur + 1; right = nums.size() - 1; int n; while (left < right) { n = nums[cur] + nums[left] + nums[right]; if (abs(target - n) < abs(target - closest)) { closest = n; } if (n == target) { break; } else if (n > target) { int t = right - 1; while (t > left && nums[t] == nums[right]) t--; right = t; } else { int t = left + 1; while (t < right && nums[t] == nums[left]) t++; left = t; } } int t = cur + 1; while (t < nums.size() && nums[t] == nums[cur]) t++; cur = t; } return closest; } }; int main() { Solution s; vector<int> nums = {-1,2,1,-4}; cout << s.threeSumClosest(nums, 1) << endl; return 0; }
输出:
2