leetcode全解

Posted futurehau

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode全解相关的知识,希望对你有一定的参考价值。

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution 
public:
    vector<int> twoSum(vector<int>& nums, int target) 
        unordered_map<int, int> num_index_dict;
        vector<int> res;
        for (int i = 0; i < nums.size(); i++) 
            if (num_index_dict.find(nums[i]) != num_index_dict.end()) 
                int index = num_index_dict[nums[i]];
                res.push_back(index);
                res.push_back(i);
                return res;
             else 
                num_index_dict[target - nums[i]] = i;
            
        
        return res;
    
;

 

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. 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:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
/**
 * Definition for singly-linked list.
 * struct ListNode 
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) 
 * ;
 */
class Solution 
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
        int carry = 0;
        ListNode* dummy_head = new ListNode(0);
        ListNode* head = dummy_head;
        while (l1 || l2 || carry) 
            int temp = carry;
            if (l1) 
                temp += l1 -> val;
                l1 = l1 -> next;
            
            if (l2) 
                temp += l2 -> val;
                l2 = l2 -> next;
            
            carry = temp / 10;
            int cur_val = temp % 10;
            head -> next = new ListNode(cur_val);
            head = head -> next;
        
        return dummy_head -> next;
    
;

 

3. Longest Substring Without Repeating Characters

 

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

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 
Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
class Solution 
public:
    int lengthOfLongestSubstring(string s) 
        if (s.empty()) 
            return 0;
        
        int left = 0;
        int right = 0;
        int pos_dict[128];
        for (int i = 0; i < 128; i++) 
            pos_dict[i] = -1;
        
        int res = 0;
        while (right < s.size()) 
            char cur_char = s[right];
            if (pos_dict[(int)cur_char] == -1) 
                pos_dict[(int)cur_char] = right;
                right += 1;
             else 
                res = max(res, right - left);
                int last_index = pos_dict[(int)cur_char];
                // 注意此处
                for (int i = left; i<= last_index; i++) 
                    pos_dict[(int)s[i]] = -1;
                
                left = last_index + 1;
                pos_dict[(int)cur_char] = right;
                right += 1;
            
        
        res = max(res, right - left);
        return res;
    
;

  

 

以上是关于leetcode全解的主要内容,如果未能解决你的问题,请参考以下文章

动态规划LeetCode[简单]题全解

最大子数组问题全解

强烈推荐收藏坚持3个月爆肝华为机试108题C++全解(适合新手入门,就业必刷套题)

强烈推荐收藏坚持3个月爆肝华为机试108题C++全解(适合新手入门,就业必刷套题)

算法岗必刷套题-入门级坚持3个月爆肝华为机试108题C++全解(强烈推荐收藏,适合学生党,校招必备)

一个demo学会js