LeetCode 算法整理

本文精选了LeetCode上的四个经典算法题目,包括两数之和、求两数相加、最长不重复子串及两个有序数组的中位数。通过这些题目展示了不同的数据结构和算法实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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].

Subscribe to see which companies asked this question.

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    int i,j,sum;
    int *res = (int *) malloc(sizeof(int)*2);
    for(i=0;i<numsSize;i++){
        res[0] = i;
        for(j=i+1;j<numsSize;j++){ 
            if(nums[i] +nums[j] == target){
                res[1] = j;
                return res;
            }
            
        }
    }
    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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int a=0,b=0,i = 0;
    
    struct ListNode *ln = (struct ListNode*)malloc(sizeof(struct ListNode)); 
    ln->val = 0;
    ln->next = NULL;
    struct ListNode *p = ln;
    struct ListNode *p1 = l1;
    struct ListNode *p2 = l2;
    while(p1!=NULL||p2!=NULL){
        struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));
        l->val=0;
        l->next = NULL;
        p->next = l;
        p = l; 
        
        if(p1)a=p1->val;else a=0;
        if(p2)b=p2->val;else b=0;
        p->val = a+b+i; 
        if(p->val/10>=1){
            i = 1;
            p->val = p->val%10;
        }else{
            i = 0;
        }
        if(p1)p1=p1->next;
        if(p2)p2=p2->next;
    }
    if(i==1){
        struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));
        l->val=1;
        l->next = NULL;
        p->next = l;
        p = l; 
    }
    return ln->next;
}



3. Longest Substring Without Repeating Characters

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", 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.

Subscribe to see which companies asked this question.

int findstr(char *s,int len,char a){
    int i;
    for(i=0;i<len;i++){
        if(s[i]==a)return 0;
    }
    return 1;
}
int lengthOfLongestSubstring(char* s) {
    int i=0,m=0;
    char a;
    while(*s!='\0'){
        a = s[i];
        while(a!='\0')a=findstr(s,i,a)?s[++i]:'\0';
        if(i>m)m=i;
        s++;i=1;
    }
    return m;
}

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

Subscribe to see which companies asked this question.


double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    double r = 0;
    int a=0,b=0,i=0,j=0,x=0;
    int *n = (int*)malloc(sizeof(int)*(nums1Size+nums2Size));
    memset(n,9,nums1Size+nums2Size);
    while(x<nums1Size+nums2Size){
        if(i<nums1Size){
            a = nums1[i];
        }else{
            n[x] = nums2[j];
            j++;x++;continue;
        }
        if(j<nums2Size){
            b=nums2[j];
        }else{
            n[x] = nums1[i];
            i++;x++;continue;
        }
        a<b?(i++,n[x]=a):(j++,n[x]=b);
        x++;
    }
    if(x%2!=1)
        r = (n[x/2]+n[x/2-1])/2.0;
    else
        r = n[x/2];
    return r;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值