您的位置:首页 > 其它

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: