classSolution { public: vector<int> twoSum(vector<int>& nums, int target){ vector<int> res; unordered_map<int,int> m; for (int i = 0; i < nums.size(); i ++ ) { int another = target - nums[i]; if (m.count(another)) { res = vector<int>({m[another], i}); break; } m[nums[i]] = i; } return res; } };
1 2 3 4 5 6 7 8 9 10 11
functwoSum(nums []int, target int) []int { m := make(map[int]int) for i := 0; i < len(nums); i++ { res := target - nums[i] if _, ok := m[res]; ok { return []int{m[res], i} } m[nums[i]] = i } returnnil }
funcaddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { res := &ListNode{Val: -1}; cur := res; n1, n2, carry := 0, 0, 0; for l1 != nil || l2 != nil { if l1 == nil { n1 = 0 } else { n1 = l1.Val } if l2 == nil { n2 = 0 } else { n2 = l2.Val } sum := n1 + n2 + carry carry = sum / 10; cur.Next = &ListNode{Val: sum % 10} cur = cur.Next if l1 != nil { l1 = l1.Next } if l2 != nil { l2 = l2.Next; } } if carry != 0 { cur.Next = &ListNode{Val: 1} } return res.Next }
3.无重复字符的最长子串
给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intlengthOfLongestSubstring(string s){ if(s.size() == 0) return0; unordered_set<char> cnt; int res = 0; for(int i = 0, j = 0; j < s.size(); j++) { while(cnt.find(s[j]) != cnt.end()) { cnt.erase(s[i]); i++; } res = max(res, j - i + 1); cnt.insert(s[j]); } return res; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funclengthOfLongestSubstring(s string)int { m := make(map[byte]int) res := 0 for i, j := 0, 0; j < len(s); j++ { if idx, ok := m[s[j]]; ok && idx >= i { i = idx + 1 } m[s[j]] = j res = max(res, j - i + 1) } return res }
funcmax(a int, b int)int { if a > b { return a } return b }
4.寻找两个正序数组的中位数
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
classSolution { public: doublefindMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){ int tot = nums1.size() + nums2.size(); if (tot % 2 == 0) { int left = find(nums1, 0, nums2, 0, tot / 2); int right = find(nums1, 0, nums2, 0, tot / 2 + 1); return (left + right) / 2.0; } else { returnfind(nums1, 0, nums2, 0, tot / 2 + 1); } }
intfind(vector<int> &a, int i, vector<int> &b, int j, int k) { if(a.size() - i > b.size() - j) returnfind(b, j, a, i, k); if (k == 1) { if (a.size() == i) return b[j]; elsereturnmin(a[i], b[j]); } if(a.size() == i) return b[j + k - 1]; int si = min((int)a.size(), i + k / 2); int sj = j + k - k / 2; if (a[si - 1] > b[sj - 1]) returnfind(a, i, b, sj, k - (sj - j)); else returnfind(a, si, b, j, k - (si - i)); } };
funclongestPalindrome(s string)string { if s == "" { return"" } start, end := 0, 0 for i := 0; i < len(s); i++ { left1, right1 := expandAroundCenter(s, i, i) left2, right2 := expandAroundCenter(s, i, i + 1) if right1 - left1 > end - start { start, end = left1, right1 } if right2 - left2 > end - start { start, end = left2, right2 } } return s[start:end+1] }
funcexpandAroundCenter(s string, left, right int) (int, int) { for ; left >= 0 && right < len(s) && s[left] == s[right]; left, right = left-1 , right+1 { } return left + 1, right - 1 }