博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - 445. Add Two Numbers II
阅读量:7110 次
发布时间:2019-06-28

本文共 2698 字,大约阅读时间需要 8 分钟。

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.

Follow up:

What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 8 -> 0 -> 7 大数加法+链表操作,用长整型会溢出的。
/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */class Solution {    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {        String s1 = toStr(l1);        String s2 = toStr(l2);        return toNode(addTowStr(s1, s2));    }        private String toStr(ListNode node) {        StringBuilder sr = new StringBuilder();        if (node == null)            return sr.toString();        while (node != null) {            sr.append(node.val);            node = node.next;        }        return sr.toString();    }        private ListNode toNode(String str) {        if (str == null)            return null;                ListNode cur = new ListNode(str.charAt(str.length()-1)-'0');        ListNode head = cur;        for (int i=str.length()-2; i>=0; i--) {            cur.next = new ListNode(str.charAt(i)-'0');            cur = cur.next;        }        return head;    }        private String addTowStr(String s1, String s2) {        if (s1 == null || s1.length() <= 0)            return s2;        if (s2 == null || s2.length() <= 0)            return s1;                if (s1.length() < s2.length()) {            String t = s1;            s1 = s2;            s2 = t;        }        int minLen = s2.length();        int maxLen = s1.length();        int over = 0;        StringBuilder ret = new StringBuilder();        for (int i=minLen-1,j=maxLen-1; i>=0 && j>=maxLen-minLen; i--,j--) {            int sum = (s1.charAt(j)-'0') + (s2.charAt(i)-'0') + over;            if (sum < 10) {                ret.append(sum+"");                over = 0;            }            else {                ret.append((sum % 10)+"");                over = sum / 10;            }        }                for (int i=maxLen-minLen-1; i>=0; i--) {            int sum = (s1.charAt(i)-'0') + over;            if (sum < 10) {                ret.append(sum+"");                over = 0;            }            else {                over = sum / 10;                ret.append((sum % 10)+"");            }        }        if (over > 0)            ret.append(over);        return ret.toString();    }        }

 

转载地址:http://lllhl.baihongyu.com/

你可能感兴趣的文章
拼钱拼福利!云计算、大数据人才遭哄抢
查看>>
DBA五大致命失误:你共享密码没?
查看>>
Win 10 NEON新界面命名Fluent Design System
查看>>
如何在Linux中删除超大的(100-200GB)文件
查看>>
《 产品设计思维:电商产品设计全攻略》一一第3章 电商梦的开始:首页设计...
查看>>
《C++语言入门经典》一2.7 语句
查看>>
欧盟将制定新物联网安全规定
查看>>
Win10 RS3高DPI截图对比:200%依旧清晰
查看>>
《MongoDB管理与开发精要》——2.4节停止数据库
查看>>
建立市民满意的智慧城市评估模型
查看>>
教授谈投资型FO 的CIO究竟需要哪些能力?
查看>>
如何优化数据表格设计
查看>>
CCAI | 今日头条实验室李磊:我们离会思考的机器人还有多远?
查看>>
Opera:被中国财团收购后不会堕落
查看>>
兵家必争大数据,争来争去是大数据时代的话语权
查看>>
中国医疗大数据“痛点” :孤岛怎么破
查看>>
亚信安全联合国家计算机病毒应急处理中心 发起病毒疫情调查
查看>>
当Gartner说OpenStack内外交困时,我的内心是不要不要的
查看>>
快易省智:数据中心UPS四大演进方向
查看>>
框架设计之菜鸟漫漫江湖路系列 二:自学求索
查看>>