alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【436】Solution for LeetCode Problems

Coding everyday. ^_^

1. Two Sum

  • 重点知识:指针可以存储数值,通过 malloc 新建数组
  • int* returnSize:Size of the return array. Store the value in a pointer, say 2.
    *returnSize = 2
  • My solution:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* twoSum(int* nums, int numsSize, int target, int* returnSize){
        *returnSize = 2;
        int* returnArray = malloc(sizeof(int)*(*returnSize));
        for (int i = 0; i < numsSize-1; i++) {
            for (int j = i+1; j < numsSize; j++) {
                if (nums[i] + nums[j] == target) {
                    returnArray[0] = i;
                    returnArray[1] = j;
                    return returnArray;
                }
            }
        }
        returnArray[0] = -1;
        returnArray[1] = -1;
        return returnArray;
    }

     

2. Add Two Numbers

  • 重点知识:不能通过数字来计算,考虑进位,考虑链表遍历和插入节点,通过 malloc 新建节点
  • Don't use integer to calculate in this problem since the numbers are very long.
  • Need to consider carry bit.
  • This linked list's head is the first node.
  • My solution:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
     
     
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
        struct ListNode* p1;
        struct ListNode* p2;
        struct ListNode* l3 = NULL;
        struct ListNode* p3 = NULL;
        p1 = l1;
        p2 = l2;
         
        int carry_bit = 0;
        while (p1 != NULL || p2 != NULL || carry_bit == 1) {
            int num1;
            int num2;
            int num3;
            if (p1 == NULL && p2 == NULL && carry_bit == 1) {
                num3 = 1;
                carry_bit = 0;
            }
            else {
                if (p1 == NULL) {
                    num1 = 0;
                }
                else {
                    num1 = p1->val;
                    p1 = p1->next;
                }
                if (p2 == NULL) {
                    num2 = 0;
                }
                else {
                    num2 = p2->val;
                    p2 = p2->next;
                }
                 
                num3 = num1 + num2 + carry_bit;
                if (num3 >= 10) {
                    carry_bit = 1;
                    num3 = num3 - 10;
                }
                else {
                    carry_bit = 0;
                }
            }
     
            struct ListNode* tmp;
            tmp = malloc(sizeof(struct ListNode));
            if (tmp == NULL) {
                fprintf(stderr, "Out of memory.\n");
                exit(1);
            }
            tmp->val = num3;
            tmp->next = NULL;
             
            if (p3 == NULL) {
                p3 = tmp;
                l3 = p3;
            }
            else {
                p3->next = tmp;
                p3 = p3->next;
            }
        }
         
        return l3;
    }

 

3. Longest Substring Without Repeating Characters

  • 重点知识:多层遍历,时间复杂度过高
  • My solution:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    int lengthOfLongestSubstring(char * s){
        int length = strlen(s);
        if (length == 1){
            return 1;
        }
        int max = 0;
        for (int i = 0; i < length; i++) {
            int flag = 1;
            for (int j = i + 1; j < length & flag; j++) {
                for (int k = j - 1; k >= i; k--) {
                    if (s[j] == s[k]) {
                        int tmp = j - i;
                        if (max < tmp) {
                            max = tmp;
                        }
                        i = k;
                        flag = 0;
                        break;
                    }
                    if (k == i) {
                        int tmp = j - i + 1;
                        if (max < tmp) {
                            max = tmp;
                        }
                    }
                }
            }
        }
        return max;
    }

     

 

posted on   McDelfino  阅读(281)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示