Likou quiz (simple part): sum of two numbers, addition of two numbers, longest substring without repeated characters

Persistence is victory


1. The sum of two numbers

Question link: https://leetcode.cn/problems/two-sum/

Given an integer array nums and an integer target value target, please find the two integers in the array whose sum is the target value target and return their array subscripts.

You can assume that each input will correspond to only one answer. However, the same element in the array cannot appear repeatedly in the answer.

You can return answers in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, [0, 1] is returned.
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

hint:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer

Advanced: Can you come up with an algorithm with time complexity less than O(n2)?

Code ©

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    
    
    for(int i = 0;i < numsSize;i++)
    {
    
    
        for(int j = i+1;j < numsSize;j++)
        {
    
    
            if(nums[i]+nums[j] == target)
            {
    
    
                int* ret = malloc(sizeof(int) * 2);
                ret[0] = i, ret[1] = j;
                *returnSize = 2;
                return ret;
            }
        }
    }

   *returnSize = 0;
    return NULL;
}

2. Adding two numbers

Question link: https://leetcode.cn/problems/add-two-numbers/

You are given two non-empty linked lists, representing two non-negative integers. Each of their digits is stored in reverse order, and each node can only store one digit.

Please add two numbers and return a linked list representing the sum in the same form.

You can assume that except for the number 0, neither number will start with a 0.

Example 1:
Insert image description here

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]

hint:

The number of nodes in each linked list is in the range [1, 100]
0 <= Node.val <= 9.
The question data guarantees that the number represented by the list does not contain leading numbers.

Code ©

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    
    
    struct ListNode *head = NULL, *tail = NULL;
    int carry = 0;
    while (l1 || l2) {
    
    
        int n1 = l1 ? l1->val : 0;
        int n2 = l2 ? l2->val : 0;
        int sum = n1 + n2 + carry;
        if (!head) {
    
    
            head = tail = malloc(sizeof(struct ListNode));
            tail->val = sum % 10;
            tail->next = NULL;
        } else {
    
    
            tail->next = malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;
            tail = tail->next;
            tail->next = NULL;
        }
        carry = sum / 10;
        if (l1) {
    
    
            l1 = l1->next;
        }
        if (l2) {
    
    
            l2 = l2->next;
        }
    }
    if (carry > 0) {
    
    
        tail->next = malloc(sizeof(struct ListNode));
        tail->next->val = carry;
        tail->next->next = NULL;
    }
    return head;
}

3. The longest substring without repeated characters

Question link: https://leetcode.cn/problems/longest-substring-without-repeating-characters/

Given a string s, please find the length of the longest substring that does not contain repeated characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: Because the longest substring without repeated characters is "abc", its length is 3.
Example 2:

Input: s = “bbbbb”
Output: 1
Explanation: Because the longest substring without repeated characters is “b”, its length is 1.
Example 3:

Input: s = “pwwkew”
Output: 3
Explanation: Because the longest substring without repeated characters is “wke”, its length is 3.
Please note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.

hint:

0 <= s.length <= 5 * 104
s consists of English letters, numbers, symbols and spaces

Code ©

int lengthOfLongestSubstring(char * s)
{
    
    
    //类似于hash的思想
    //滑动窗口维护
    int left = 0;
    int right = 0;
    int max = 0;
    int i,j;
    int len = strlen(s);
    int haveSameChar = 0;
    for(i =0; i < len ; i++  )
    {
    
    
        if(left <= right)
        {
    
       
            //检测是否出现重复
             //循环遍历整个数组 left -> right
            haveSameChar = 0;
            for(j = left; j < right ; j++)
            {
    
    
                if(s[j] == s[right])
                {
    
    
                    haveSameChar = 1;
                    break;
                }
            }
            if(haveSameChar)
            {
    
    
                //指向下一个
                left = j +1;
            }
        }
        //统计最大的间距
        max = max < (right - left + 1) ? (right - left + 1): max;
        right++;
    }
    return max;
}



Guess you like

Origin blog.csdn.net/weixin_62529383/article/details/132968464