Skip to content

Commit e38d4ea

Browse files
committed
3/2/18
1 parent a493b70 commit e38d4ea

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

C/371. Sum of Two Integers.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
371. Sum of Two Integers
3+
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
4+
5+
Example:
6+
Given a = 1 and b = 2, return 3.
7+
*/
8+
9+
int getSum(int a, int b) {
10+
return b==0? a:getSum(a^b, (a&b)<<1);
11+
}

python/14. Longest Common Prefix.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#14. Longest Common Prefix
2+
3+
"""
4+
Write a function to find the longest common prefix string amongst an array of strings.
5+
6+
"""
7+
8+
class Solution:
9+
def longestCommonPrefix(self, strs):
10+
"""
11+
:type strs: List[str]
12+
:rtype: str
13+
"""
14+
if not strs:
15+
return ""
16+
minLen = len(strs[0])
17+
for sx in strs:
18+
minLen = min(minLen,len(sx))
19+
res = ""
20+
for i in range(minLen):
21+
c = strs[0][i]
22+
for sx in strs:
23+
if sx[i] != c:
24+
return res
25+
res += c
26+
return res

python/2. Add Two Numbers.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#2. Add Two Numbers
2+
"""
3+
You are given two non-empty linked lists representing two non-negative integers. 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.
4+
5+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
7+
Example
8+
9+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
10+
Output: 7 -> 0 -> 8
11+
Explanation: 342 + 465 = 807.
12+
13+
Pretty straight forward. Only keep in mind the carry.
14+
15+
"""
16+
# Definition for singly-linked list.
17+
# class ListNode:
18+
# def __init__(self, x):
19+
# self.val = x
20+
# self.next = None
21+
22+
class Solution:
23+
def addTwoNumbers(self, l1, l2):
24+
"""
25+
:type l1: ListNode
26+
:type l2: ListNode
27+
:rtype: ListNode
28+
"""
29+
# 212 ms 43.49%
30+
# first base cases:
31+
# if either of the
32+
# list is empty, return
33+
# the other one
34+
# if both are empty return none
35+
if not l1:
36+
return l2
37+
if not l2:
38+
return l1
39+
if not l2 and not l1:
40+
return none
41+
# else init a carry propagator
42+
carry = 0
43+
# instantiate new linked list
44+
root = n = ListNode(1)
45+
# while there is something
46+
# to be added, ll1 or ll2
47+
# or the carry itself
48+
# do:
49+
while l1 or l2 or carry:
50+
v1 = v2 = 0
51+
if l1:# while you can still traverse LL1
52+
v1 = l1.val# get current node value
53+
l1 = l1.next# get next node
54+
if l2:# while you can still traverse LL2
55+
v2 = l2.val
56+
l2 = l2.next
57+
58+
59+
val = v1 + v2 + carry # add numbers + carry if any
60+
carry = val//10 # if sum > 10, get the quotient as carry
61+
val %= 10# this is the rem or second digit of sum
62+
n.next = ListNode(val)# push that into new LL
63+
n = n.next# move to next node
64+
return root.next#return the head of new LL

python/415. Add Strings.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#415. Add Strings
2+
"""
3+
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
4+
5+
Note:
6+
7+
The length of both num1 and num2 is < 5100.
8+
Both num1 and num2 contains only digits 0-9.
9+
Both num1 and num2 does not contain any leading zero.
10+
You must not use any built-in BigInteger library or convert the inputs to integer directly.
11+
12+
"""
13+
class Solution:
14+
def addStrings(self, num1, num2):
15+
"""
16+
:type num1: str
17+
:type num2: str
18+
:rtype: str
19+
"""
20+
sums = []
21+
i = len(num1)-1
22+
j = len(num2)-1
23+
carry = 0
24+
while i >= 0 or j >= 0:
25+
total = carry
26+
if i >= 0:
27+
total += int(num1[i])
28+
i -= 1
29+
if j >= 0:
30+
total += int(num2[j])
31+
j -= 1
32+
sums.append(str(total % 10))
33+
carry = total / 10
34+
if carry:
35+
sums.append(str(carry))
36+
37+
return "".join(sums[::-1])

0 commit comments

Comments
 (0)