1. 两数之和

说明:在查找、去重可以考虑利用哈希表以降低时间复杂度

vector<int> twoSum(vector<int>& nums, int target) {
        //method1
        //         int len = nums.size();
        //         vector<int> result;
        //         for (int i = 0; i < len; i++)
        //         {
        //             for (int j = i+1; j < len; j++)
        //             {
        //                 if (nums[i] + nums[j] == target)
        //                 {
        //                     result.push_back(i);
        //                     result.push_back(j);
        //                     break;
        //                 }
        //             }
        //         }
        //         return result;

        //method 2
        //依次遍历nums 查找 target - nums[i] 没找到 则存入map
        unordered_map<int, int> hashtable;// unordered_map<key value>
        for (int i = 0; i < nums.size(); ++i)
        {
            auto it = hashtable.find(target - nums[i]);
            //unordered_map find() 如果key存在,则find返回key对应的迭代器,如果key不存在,则find返回unordered_map::end。因此可以通过
            if (it != hashtable.end())
            {
                return{ it->second, i };
            }
            hashtable[nums[i]] = i;
        }
        return{};
    }

2. 两数相加

两个链表中同一位置的数字可以直接相加,如果大于10则当前位置取余数且进1给下一个位置。

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        //method2  
        ListNode* list_result = nullptr;
        ListNode* list_temp = nullptr;
        int carry = 0;
        int sum = 0;
        while (l1 != nullptr || l2 != nullptr)
        {
            sum = 0;
            if (l1 != nullptr)
            {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2 != nullptr)
            {
                sum += l2->val;
                l2 = l2->next;
            }
            sum += carry;
            if (sum >= 10)
            {
                carry = 1;
                sum = sum % 10;
            }
            else
            {
                carry = 0;
            }

            if (list_result == nullptr)
            {
                list_result = list_temp = new ListNode(sum);
            }
            else
            {
                list_temp->next = new ListNode(sum);
                list_temp = list_temp->next;
            }
        }
        if (carry > 0)
        {
            list_temp->next = new ListNode(carry);
            list_temp = list_temp->next;
        }
        return list_result;
    }

3. 无重复字符的最长子串

实现思路:活动窗口

/*
类似题目:
76. 最小覆盖子串
159. 至多包含两个不同字符的最长子串
340. 至多包含 K 个不同字符的最长子串
30. 串联所有单词的子串
76. 最小覆盖子串
159. 至多包含两个不同字符的最长子串
209. 长度最小的子数组
239. 滑动窗口最大值
567. 字符串的排列
632. 最小区间
727. 最小窗口子序列
*/

int lengthOfLongestSubstring(string s) {
        int ans = 0;
        int n = s.length();
        int left = 0;
        unordered_set<char> book; // 哈希表,记录窗口中已经出现的字符
        for (int right = 0; right < n; ++right) {
            // 当新加入窗口右侧的字符是重复字符,让窗口左边界(左指针)右移(第一次map为空不进入循环)
            while (book.find(s[right]) != book.end()) {
                book.erase(s[left]);//删除重复字符以及左侧所有字符
                ++left;//left跟新为重复字符位置+1
            }
            // 将右指针处的字符加入窗口
            book.insert(s[right]);
            ans = max(ans, right - left + 1);//取当前最大无重复字符的最长子串
        }
        return ans;
    }

4. 寻找两个正序数组的中位数

5. 最长回文子串


Simple
10 声望4 粉丝