LeetCode笔记

Leetcode笔记

    • Two Sum
      • C(初始版)
      • C++版1
      • C++版2
      • 笔记
    • Add Two Numbers
      • 初始版本
      • C

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

C(初始版)

#include 
int main(int argc, const char * argv[]) {
    int nums[4]={2,7,11,15};
    int target;
    scanf("%d",&target);
    for(int i=0;i<4;i++)
    {
        for(int j=i+1;j<4;j++)
        {
            if(nums[i]+nums[j]==target)
            {
                printf("%d %d",i,j);
                return 0;
            }
        }
    }
    return 0;
}

C++版1

链接: [link]https://www.cnblogs.com/grandyang/p/4130379.html

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map m;
        vector res;
        for (int i = 0; i < nums.size(); ++i) {
            m[nums[i]] = i;
        }
        for (int i = 0; i < nums.size(); ++i) {
            int t = target - nums[i];
            if (m.count(t) && m[t] != i) {
                res.push_back(i);
                res.push_back(m[t]);
                break;
            }
        }
        return res;
    }
};

C++版2

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map m;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.count(target - nums[i])) {
                return {i, m[target - nums[i]]};
            }
            m[nums[i]] = i;
        }
        return {};
    }
};

笔记

vector count:返回元素值为target的元素个数。

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

初始版本

将两个链表的对应项相加,再从头节点遍历新链表(若该节点val>9,则-10,且next的val+1)
代码暂未修改

 /**
     * 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) {
            ListNode now1=l1,now2=l2;
            ListNode *newlist,pnode;
            pnode=newlist;
            while(now1!=NULL && now2!=NULL)
            {
                pnode.val=now1.val+now2.val;
                now1=now1->next;
                now2=now2->next;
                pnode=pnode->next;
            }
            if(now1==NULL)
            {
                pnode->next=now2;
            }
            else
            {
                pnode->next=now1;
            }
            pnode=newlist;
            while(pnode!=NULL)
            {
                if(pnode>9)
                {
                    pnode.val-=10;
                    if(pnode->next!=NULL)
                    {
                        pnode->next.val+=1;
                    }
                    else if(pnode->next==NULL)
                    {
                        pnode->next.val=1;
                    }
                }
            }
            return newlist;
        }
    };

C

链接:https://www.cnblogs.com/JeroZeng/p/4668784.html

比较过程中利用carry实时进位,相较于初始版本中再次遍历链表,节省了时间。

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode head, *p;  
    p = &head;
    
    int carry = 0;

    while(1)
    {
        if(l1 && l2)
        {
            l1->val += (l2->val + carry);
            carry = l1->val / 10;
            l1->val = l1->val % 10;

            p->next = l1;
            p = l1;
        
            l1 = l1->next;
            l2 = l2->next;
        }
        else if(l1)
        {
            l1->val += carry;
            carry = l1->val / 10;
            l1->val = l1->val % 10;
            
            p->next = l1;
            p = l1;
            
            l1 = l1->next;
        }
        else if(l2)
        {
            l2->val += carry;
            carry = l2->val / 10;
            l2->val = l2->val % 10;
            
            p->next = l2;
            p = l2;
            
            l2 = l2->next;
        }
        else if(carry)
        {
            struct ListNode *cur = (struct ListNode*)malloc(sizeof(struct ListNode));
            cur->val = 1;
            cur->next = NULL;
            p->next = cur;
            return head.next;
        }
        else
            return head.next;
    }
    return NULL;
}

你可能感兴趣的:(笔记,LeetCode)