Skip to content

Commit 9331488

Browse files
author
zq29
committed
restarting ...
1 parent 6dd9e87 commit 9331488

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

Franklin/addTwoNumbers.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
lc problem 2
3+
You are given two non-empty linked lists representing two non-negative integers.
4+
The digits are stored in reverse order and each of their nodes contain a single digit.
5+
Add the two numbers and return it as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
10+
Output: 7 -> 0 -> 8
11+
*/
12+
13+
/**
14+
* Definition for singly-linked list.
15+
* public class ListNode {
16+
* int val;
17+
* ListNode next;
18+
* ListNode(int x) { val = x; }
19+
* }
20+
*/
21+
public class Solution {
22+
// My solution
23+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
24+
int carry=0;
25+
int p = l1.val;
26+
int q = l2.val;
27+
int sum = p+q+carry;
28+
carry = sum/10; sum = sum%10;
29+
ListNode ln = new ListNode(sum);
30+
ListNode result = ln;
31+
l1 = l1.next; l2 = l2.next;
32+
while (l1!=null || l2!=null) {
33+
p = l1!=null?l1.val:0; l1 = l1!=null?l1.next:null;
34+
q = l2!=null?l2.val:0; l2 = l2!=null?l2.next:null;
35+
sum = p+q+carry;
36+
carry = sum/10; sum = sum%10;
37+
ln.next = new ListNode(sum); ln = ln.next;
38+
}
39+
if (carry>0) {
40+
ln.next = new ListNode(1); ln = ln.next;
41+
}
42+
return result;
43+
}
44+
45+
// Editorial solution
46+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
47+
ListNode dummyHead = new ListNode(0);
48+
ListNode p = l1, q = l2, curr = dummyHead;
49+
int carry = 0;
50+
while (p != null || q != null) {
51+
int x = (p != null) ? p.val : 0;
52+
int y = (q != null) ? q.val : 0;
53+
int sum = carry + x + y;
54+
carry = sum / 10;
55+
curr.next = new ListNode(sum % 10);
56+
curr = curr.next;
57+
if (p != null) p = p.next;
58+
if (q != null) q = q.next;
59+
}
60+
if (carry > 0) {
61+
curr.next = new ListNode(carry);
62+
}
63+
return dummyHead.next;
64+
}
65+
66+
// instead of doing an extra operation before the start of while loop,
67+
// the editorial soln just used a dummyHead var and return dummyHead.next at the end.
68+
// Pretty elegant!
69+
}

Franklin/isPalindrome.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
lc problem 9
3+
Determine whether an integer is a palindrome.
4+
*/
5+
public class Solution {
6+
public boolean isPalindrome(int x) {
7+
if (x<0) return false;
8+
int rev = 0, y = x;
9+
while (y!=0) {
10+
rev = rev*10 + y%10;
11+
y /= 10;
12+
}
13+
return (x==rev);
14+
}
15+
}

Franklin/isPalindrome.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def isPalindrome(self, x):
77
:type x: int
88
:rtype: bool
99
"""
10-
if (x<0 or ()):
10+
if (x<0):
1111
return False
12-
rev = 0
13-
while (x!=0):
14-
rev = rev*10 + x%10
15-
x = x/10
16-
return (x==rev or x==rev/10)
17-
# what's wrong with the if condition?
12+
13+
rev = 0; y = x
14+
while (y!=0):
15+
rev = rev*10 + y%10
16+
y = y/10
17+
return (x==rev)

0 commit comments

Comments
 (0)