码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode全解

时间:2019-10-07 00:43:53      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:lap   eating   note   for   long   highlight   NPU   lse   sub   

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全解

标签:lap   eating   note   for   long   highlight   NPU   lse   sub   

原文地址:https://www.cnblogs.com/futurehau/p/11629068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!