LeetCodeノート

5383 ワード

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 {};
        }
    };
    

    メモ
    vectorcount: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
    初期バージョン
    2つのチェーンテーブルの対応するアイテムを加算し、新しいチェーンテーブル(ノード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;
    }