• 2 两数相加


    2 两数相加——Medium

    • 链接:https://leetcode-cn.com/problems/add-two-numbers/

    • 方法1:将长度较短的链表在末尾补零使得两个连表长度相等,再一个一个元素对其相加(考虑进位)

      • 思路
        • 获取两个链表所对应的长度
        • 在较短的链表末尾补零
        • 对齐相加考虑进位
      • code
      class Solution {
      public:
          ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
              int len1 = 1;//记录l1的长度
              int len2 = 1;//记录l2的长度
              ListNode* cur1 = l1;
              ListNode* cur2 = l2;
              while (cur1->next != NULL)//获取l1的长度
              {
                  len1++;
                  cur1 = cur1->next;
              }
              while (cur2->next != NULL)//获取l2的长度
              {
                  len2++;
                  cur2 = cur2->next;
              }
              if (len1 > len2)//l1较长,在l2末尾补零
              {
                  for (int i = 1; i <= len1 - len2; i++)
                  {
                      cur2->next = new ListNode(0);
                      cur2 = cur2->next;
                  }
              }
              else//l2较长,在l1末尾补零
              {
                  for (int i = 1; i <= len2 - len1; i++)
                  {
                      cur1->next = new ListNode(0);
                      cur1 = cur1->next;
                  }
              }
              cur1 = l1;
              cur2 = l2;
              bool carry = false;//记录进位
              ListNode* l3 = new ListNode(-1);//存放结果的链表
              ListNode* cur = l3;//l3的移动指针
              int i = 0;//记录相加结果
              while (cur1 != NULL && cur2 != NULL)
              {
                  i = carry + cur1->val + cur2->val;
                  cur->next = new ListNode(i % 10);
                  carry = i >= 10 ? true : false;
                  cur = cur->next;
                  cur1 = cur1->next;
                  cur2 = cur2->next;
              }
              if (carry)//若最后还有进位
              {
                  cur->next = new ListNode(1);
                  cur = cur->next;
              }
              return l3->next;
          }
      };
      
    • 方法2:不对齐补零,若链表不为空则用sum(代表每个位的和的结果)加上,考虑进位

      • 思路
      • code
      class Solution {
      public:
          ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
              ListNode* head = new ListNode(-1);//存放结果的链表
              ListNode* cur = head;//移动指针
              int sum = 0;//每个位的加和结果
              bool carry = false;//进位标志
              while (l1 != nullptr || l2 != nullptr){
                  sum = 0;
                  if (l1 != nullptr) {
                      sum += l1->val;
                      l1 = l1->next;
                  }
                  if (l2 != nullptr) {
                      sum += l2->val;
                      l2 = l2->next;
                  }
                  if (carry) {
                      sum++;
                  }
                  cur->next = new ListNode(sum % 10);
                  cur = cur->next;
                  carry = sum >= 10 ? true : false;
              }
              if (carry) {
                  cur->next = new ListNode(1);
              }
              return head->next;
          }
      };
      
  • 相关阅读:
    微信授权页面执行ajax不执行,刷新才能执行
    spring boot使用jpa的@Modify的clearAutomatically=true的作用
    Excel转HTML
    reporting reportservice(SSRS) 让某行变成自增序号
    Excel的操作插件
    https-加解密
    防篡改防重
    加密解密
    git 常用命令

  • 原文地址:https://www.cnblogs.com/bky-hbq/p/13136760.html
Copyright © 2020-2023  润新知