LeetCode (Two Sum & Add Two nums)

还是觉得有必要坚持刷leetcode   在这里打卡。 加油。    这是第一遍  很惭愧。做这一行这么久 才开始刷力扣。但是开始了总是好的。尽管代码丑,但是至少都是能通过的吧。 

坚持下去!

定个目标  如果不加班就要刷 一道题。 每个周至少两道!   坚持坚持坚持

1、 Total Accepted: 159385 Total Submissions: 822358 Difficulty: Medium

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

My Answer:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
        int temp,temp_j;
        map<int,int> num_map;
        vector<int> ret;
        for(int i=0;i<nums.size();++i){
            
            temp = nums.at(i);
            temp_j = target-temp;
            if( num_map.count(temp_j)){
            
                if(i > num_map[temp_j]){
                    ret.push_back(num_map[temp_j]+1);
                    ret.push_back(i+1);
                    return ret;
                }
            }
            num_map.insert( pair<int,int>(temp,i) );
        }
    }
};

 

2、You are given two linked lists representing two non-negative numbers. 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.

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

My Answer:  

/**
 * 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 *t_l1 = l1;
        ListNode *t_l2 = l2;
        ListNode *p=NULL;
        ListNode *l = t_l1;
        
        bool inc_ = false;
        while( t_l1!=NULL || t_l2!=NULL ){
            if(t_l1){
                p = t_l1;
            }else{            
                if(p){
                    p->next = t_l2;
                }
                p = t_l2;
            }
                
            if(t_l1&&t_l2){
                t_l1->val = t_l1->val+t_l2->val;                
                if( t_l1->val>=10 ){
                    inc_ = true;
                    t_l1->val = t_l1->val%10;          
                }
                t_l1 = t_l1->next;
                t_l2 = t_l2->next;
            }else if(t_l1){                
                if( t_l1->val>=10 ){
                    inc_ = true;
                    t_l1->val = t_l1->val%10;
                }
                t_l1 = t_l1->next;
            }else if(t_l2){                
                t_l1 = t_l2;                
                if( t_l1->val>=10 ){
                    inc_ = true;
                    t_l1->val = t_l1->val%10;
                }
                t_l1 = t_l1->next;
                t_l2 = NULL;
            }
            
            if( inc_ ){
                if(!t_l1){
                    t_l1 = new ListNode(0);
                    p->next = t_l1;
                }
                
                t_l1->val = t_l1->val+1;
                inc_ = false;
            }
            
        }
       return l;
    }
};

 

转载于:https://www.cnblogs.com/lesten/p/10714020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值