leetcode题解日练--2016.8.30
来源:互联网 发布:大学算法教程 编辑:程序博客网 时间:2024/05/04 04:16
###不给自己任何借口
今日题目:
1、字符串乘法;
2、两个链表加法
今日摘录:
年轻吗 不要紧
听过几首歌 爱过几个人 就老了
——独木舟《深海里的星星Ⅱ》
43. Multiply Strings | Difficulty: Medium
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
The numbers can be arbitrarily large and are non-negative.
Converting the input string to integer is NOT allowed.
You should NOT use internal library such as BigInteger.
tag:数学|字符串
题意:字符串之间的乘法,不允许转换为整型。
思路:
1、题目明确说了不能转整型,可以考虑https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation/2的解法
class Solution {public: string multiply(string num1, string num2) { int m = num1.size(),n=num2.size(); int *pos = new int[m+n](); for(int i=m-1;i>=0;i--) { for(int j=n-1;j>=0;j--) { int posHigh = i+j,posLow = i+j+1; int mul = (num1[i]-'0')*(num2[j]-'0')+pos[posLow]; pos[posHigh] += mul/10; pos[posLow] = (mul)%10; } } string res=""; for(int i=0;i<m+n;i++) res =res+char(pos[i]-0+'0'); while(res[0]=='0') { res = res.substr(1,res.size()-1); } return res.size()==0?"0":res; }};
结果:8ms
2. Add Two Numbers | Difficulty: Medium
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
tag:链表|数学
题意:两个链表都是逆序存储数位,求它们的加法
思路:
1、两个链表的长度可能相等也可能不相等,分两种情况讨论:
链表长度相等的时候,直接依次访问链表中元素,一定会同时访问完两个链表,最后判断下有没有进位来决定是否还需要加一个1.
链表长度不相等的时候,一定最后还有一个链表没有访问完毕,继续对该链表的元素进行访问,最后判断下有没有进位来决定是否还需要加一个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) { if(l1==NULL || l2==NULL) return l1==NULL?l2:l1; ListNode *res = new ListNode(INT_MIN); ListNode*cur = res; int c=0; while(l1 && l2) { cur->next = l2; int digit = l1->val+l2->val+c; l2->val = digit%10; c = digit/10; cur = cur->next; l1 = l1->next; l2 = l2->next; } if(l1||l2) { if(l1) cur->next = l1; cur = cur->next; while(c&&cur) { int digit = (cur->val+c); cur->val = digit%10; c = digit/10; if(cur->next) cur = cur->next; else break; } } if(c) {cur->next=new ListNode(1);} return res->next; }};
结果:48ms
- leetcode题解日练--2016.8.30
- leetcode题解日练--2016.8.1
- leetcode题解日练--2016.8.5
- leetcode题解日练--2016.8.6
- leetcode题解日练--2016.8.8
- leetcode题解日练--2016.8.9
- leetcode题解日练--2016.8.10
- leetcode题解日练--2016.8.26
- leetcode题解日练--2016.8.27
- leetcode题解日练--2016.8.29
- leetcode题解日练--2016.6.30
- leetcode题解日练--2016.6.16
- leetcode题解日练--2016.6.17
- leetcode题解日练--2016.6.19
- leetcode题解日练--2016.6.18
- leetcode题解日练--2016.6.20
- leetcode题解日练--2016.6.21
- leetcode题解日练--2016.6.22
- 动态规划和贪心算法的区别
- 解决docker WARNING: Usage of loopback devices is strongly discouraged for production use的正确姿势
- 详解android四种布局之TableLayout
- 猜生日
- java编程自学记录(day14)
- leetcode题解日练--2016.8.30
- 查看linux cpu和内存利用率
- 边界问题
- [LeetCode] 119. Pascal's Triangle II
- JAVA设计模式——各种设计模式简述
- gdb 多线程调试
- 接口的语言和应用场景
- 2016.8.30
- 接口的具体语法