2. Add Two Numbers && 371. Sum of Two Integers && 43. Multiply Strings
2016-05-01 13:05
316 查看
2. Add Two Numbers
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
Hide Tags
Linked List Math
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 == null && l2 == null) return null; if(l2 == null) return l1; if(l1 == null) return l2; int previous = 0; int sum = l1.val+l2.val; previous = sum/10; ListNode r = new ListNode(sum%10); ListNode rc = r; l1 = l1.next; l2 = l2.next; while(l1 != null && l2 != null) { sum = l1.val+l2.val + previous; previous = sum/10; r.next = new ListNode(sum%10); r = r.next; l1 = l1.next; l2 = l2.next; } while(l1 != null) { sum = l1.val + previous; previous = sum/10; r.next = new ListNode(sum%10); r = r.next; l1 = l1.next; } while(l2 != null) { sum = l2.val + previous; previous = sum/10; r.next = new ListNode(sum%10); r = r.next; l2 = l2.next; } if(previous != 0) r.next = new ListNode(1); return rc; } }
371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator+and
-.
Bit Manipulation
Solution:
For example, we have a = 1, b = 3. In bit representation, a = 0001, b = 0011. First, we can use "and"("&") operation between a and b to find a carry.
carry = a & b, then carry = 0001
Second, we can use "xor" ("^") operation between a and b to find the different bit, and assign it to a. Then, we shift carry one position left and assign it to b, b = 0010.
Iterate until there is no carry (or b == 0)
public class Solution { public int getSum(int a, int b) { if (a == 0) return b; if (b == 0) return a; while (b != 0) { int carry = a & b; a = a ^ b; b = carry << 1; } return a; } }
43. Multiply Strings
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.
Hide Tags
Math String
public class Solution { public String multiply(String num1, String num2) { int l1 = num1.length(); int l2 = num2.length(); int[] product = new int[l1 + l2]; for (int i = l1 - 1; i >= 0; --i) { for (int j = l2 - 1; j >= 0; --j) { int posTens = i + j; int posOnes = posTens + 1; int sum = product[posOnes] + (num1.charAt(i) - '0') * (num2.charAt(j) - '0'); product[posTens] += sum / 10; product[posOnes] = (sum) % 10; } } StringBuilder sb = new StringBuilder(); for (int num : product) if (sb.length() > 0 || num > 0) sb.append(num); return sb.length() == 0 ? "0" : sb.toString(); } }
相关文章推荐
- 20145305 《Java程序设计》第9周学习总结
- leetcode-102. Binary Tree Level Order Traversal
- 微信公众号自定义菜单创建方法
- SICP 1.1-1.5
- Leetcode:97. Interleaving String
- 桶排序(Bucket Sort)的数组实现
- Tomcat和JDK的内存配置
- 第十一周项目2.1 存储班长信息类
- RemoteViews入门
- 操作系统后门
- Sun java认证考试真题答案及部分解析(三)
- 关于collectionView.performBatchUpdates()方法
- 递归删除目录
- Mysql导入大文件的处理
- 20145319 第九周学习总结
- Android studio如何使用SVN进行版本控制?
- Sun java认证考试真题答案及部分解析(二)
- 判断完全二叉树
- web页面跳转
- 链表相关笔试题(一)