LeetCode C++ 刷题记录(摆烂中,偶尔会来施个工)

1、Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order.

Example 1:Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1]

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4         unordered_map<int, int> ret;
 5         int n = nums.size();
 6         for(int i = 0; i < n; ++i){
 7             ret[nums[i]] = i; 
 8         }
 9         for(int i = 0; i < n; ++i){
10             auto it = ret.find(target - nums[i]);
11             if(it != ret.end() && i != it->second) 
12                 return vector<int>{i, it->second}; 
13         }
14         return {-1, -1};
15     }
16 };

 

2、Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8]

Explanation: 342 + 465 = 807.

Example 2:Input: l1 = [0], l2 = [0] Output: [0]

Example 3:Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]

/**
 * 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 *h = new ListNode();
        ListNode *p = h;
        int tmp = 0;
        while(l1 != nullptr || l2 != nullptr){
            if(l1 == nullptr) l1 = new ListNode();
            if(l2 == nullptr) l2 = new ListNode();
            p->next = new ListNode((tmp + l1->val + l2->val) % 10);
            tmp = (tmp + l1->val + l2->val) / 10;
            p = p->next;
            l1 = l1->next;
            l2 = l2->next;
        }
        if(tmp){
            p->next = new ListNode(tmp);
        }
        return h->next;
    }
};

 

3、Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

Example 1:Input: s = "abcabcbb" Output: 3

Explanation: The answer is "abc", with the length of 3.

Example 2:Input: s = "bbbbb" Output: 1

Explanation: The answer is "b", with the length of 1.

Example 3:Input: s = "pwwkew" Output: 3

Explanation: The answer is "wke", with the length of 3.

Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

s consists of English letters, digits, symbols and spaces.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int begin = 0, end = 0, len = s.length();
        int maxLen = 0;
        int book[256] = {0};
        while(end < len){
            if(book[s[end]]){
                maxLen = [&maxLen, end, begin]{return max(maxLen, end-begin);}();
                while(s[begin] != s[end]){
                    book[s[begin]] = 0;
                    begin++;
                }
                begin++;
                end++;
            } else {
                book[s[end]] = 1;
                end++;
            }
        }
        maxLen = [&maxLen, end, begin]{return max(maxLen, end-begin);}();
        return maxLen;
    }
};

 

posted @ 2023-06-27 17:33  karinto  阅读(21)  评论(0)    收藏  举报