赞
踩
思路:hash表用于存数据,新的数据在hash表中进行查询
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// hashtable用于查询,时间复杂度O(1),空间复杂度O(n)
unordered_map<int, int> hashtable;
for (int i = 0; i < nums.size(); i++) {
int temp = target - nums[i];
if (hashtable.find(temp) != hashtable.end()) {
return {hashtable.find(temp)->second, i};
}
hashtable[nums[i]] = i;
}
return {};
}
};
思路:按位相加,需要考虑满10进位问题
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode * l1_temp = new ListNode(); ListNode * newNode = l1_temp; bool mthanTen = false; while (l1!=nullptr || l2!= nullptr) { int sum = 0; if (l1) { sum = sum + l1->val; l1 = l1->next; } if (l2) { sum = sum + l2->val; l2 = l2->next; } if (mthanTen) { sum += 1; mthanTen = false; } if (sum > 9) { mthanTen = true; sum = sum % 10; } l1_temp->next = new ListNode(sum); l1_temp = l1_temp->next; } if (mthanTen) { l1_temp->next = new ListNode(1); } return newNode->next; } };
思路:双指针,指针数字一样时,获取一个可能的最大值
class Solution { public: int lengthOfLongestSubstring(string s) { // 双指针 int start = 0; int maxLen = 0; if (s.size() == 0 || s.size() == 1) { return s.size(); } for (int i = 1; i < s.size(); i++) { for (int j = start; j < i; j++) { if (s[j] == s[i]) { start = j + 1; if (i - j > maxLen) { maxLen = i - j; break; } } } if (maxLen < i - start + 1) { maxLen = i - start + 1; } } return maxLen; } };
思路:从中间a处或者aa处向外查找,找到最长的回文
class Solution { public: int longLenFromI(const string &s, int begin, int end) { if (s[begin] != s[end]) { return 1; } int lenS = s.size(); while (begin >= 0 && end < s.size()) { if (s[begin] != s[end]) { break; } begin--; end++; } return end - begin - 1; } string longestPalindrome(string s) { int start = 0; int end = 0; int maxLen = 0; if (s.size() <= 1) { return s; } for (int i = 0; i < s.size() - 1; i++) { int len1 = longLenFromI(s, i, i); int len2 = longLenFromI(s, i, i + 1); maxLen = max(maxLen, max(len1, len2)); if (maxLen > end - start + 1) { start = i - (maxLen - 1) / 2; end = i + maxLen / 2; } } return s.substr(start, maxLen); } };
思路:动态规划,出现对应字符相等或者匹配串出现.
为一个条件,下一个匹配是*
为另一个条件,根据两个条件梳理出逻辑
class Solution { public: int n; int m; vector<vector<int>> f; bool isMatch(string s, string p) { n = s.size(); m = p.size(); f = vector<vector<int>> (n + 1, vector<int> (m + 1, -1)); return dp(0, 0, s, p); } bool dp(const int &x, const int &y, const string &s, const string &p) { if (y == m) { return x == n; } bool ans = false; int first_conn = (x < n) && (s[x] == p[y] || p[y] == '.'); if (y + 1 < m && p[y + 1] == '*') { ans = dp(x, y + 2, s, p) || (first_conn && dp(x + 1, y, s, p)); } else { ans = first_conn && dp(x + 1, y + 1, s, p); } return ans; } };
思路:从左右两边至中间,要使得最终获取到最多的水
当y轴长 左边 < 右边:左边右移,重新计算面积;否则相反
class Solution { public: int maxArea(vector<int>& height) { // 从左右两边至中间,要使得最终获取到最多的水 // 当y轴长 左边 < 右边:左边右移,重新计算面积,否则相反 int i = 0; int j = height.size() - 1; int max_area = (j - i) * min(height[i], height[j]); while(i < j) { max_area = max(max_area, (j - i) * min(height[i], height[j])); if (height[i] < height[j]) { i++; } else { j--; } } return max_area; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。