07/01/18 6:46 pm · sk-g/Leetcode@c88b540 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit c88b540

Browse files
committed
07/01/18 6:46 pm
1 parent 1f7841e commit c88b540

File tree

4 files changed

+123
-77
lines changed

4 files changed

+123
-77
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Leetcode
2-
My solutions for problems on Leetcode
3-
Currently most solutions will be in Python
4-
and I will try to write out solutions in C/C++ as well.
5-
6-
I will probably do a weekly update with more than one approach to problems.
7-
If help was taken from some other source, I will post links in the comments of the code, be sure to check them out.
8-
9-
10-
If you would like to contribute create an issue/pull request.
1+
# Leetcode
2+
My solutions for problems on Leetcode
3+
Currently most solutions will be in Python
4+
and I will try to write out solutions in C/C++ as well.
5+
6+
I will probably do a weekly update with more than one approach to problems.
7+
If help was taken from some other source, I will post links in the comments of the code, be sure to check them out.
8+
9+
10+
If you would like to contribute create an issue/pull request.

python/Palindrome Number.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#9 Palindrome Number
2+
# simple and straight forward
3+
# only trick in this question
4+
# is to check if the number is divisible
5+
# by 10 so that 100 becomes 1 instead of
6+
# 001 which is not a palindrome
7+
# this also might reduce submission time
8+
9+
10+
11+
class Solution(object):
12+
def isPalindrome(self, x):
13+
"""
14+
:type x: int
15+
:rtype: bool
16+
"""
17+
g=x # storing number for later use
18+
if x<0: # handling some end cases as usual
19+
return False
20+
elif x<=9: # single digit case
21+
return True
22+
elif x%10==0: # i
23+
return False
24+
elif x==11: # most numbers divisible by 11 are palindrome
25+
# but not all, so we can try to handle some cases
26+
return True
27+
elif x==111:
28+
return True
29+
else:
30+
a=0 # temporary variable for multiplication
31+
t=0 # temporary variable to build reverse
32+
while x:
33+
t=a*10+x%10
34+
if t/10!=a:
35+
return 0
36+
a=t
37+
x=x/10
38+
39+
if a==g:
40+
41+
return True
42+
else:
43+
44+
return False
45+
46+

python/Reverse Integer.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
#7 Reverse Integer
2-
# https://leetcode.com/problems/reverse-integer/
3-
4-
class Solution:
5-
def reverse(self, x):
6-
"""
7-
:type x: int
8-
:rtype: int
9-
"""
10-
11-
INT_MAX = 2**31 -1 # for integer overflow
12-
#INT_MIN = -(2**31 -1)
13-
if abs(x) < 10: return x # if the integer is single digit,
14-
# we can just return the same number.
15-
if x < 0: # checking if the number is negative
16-
r = -int(str(-x)[::-1])# python strings are not mutable,
17-
# so create a new string with slicing trick
18-
if x > 0:
19-
r = int(str(x)[::-1])
20-
if abs(r) > INT_MAX: #or r < INT_MIN: # check for overflow, using abs() we can
21-
# we can skip the extra check for MIN overflow, runs a bit faster
22-
return 0 # if overflow occurs after reversing, return 0
23-
else:
24-
return r
25-
"""
26-
if x>=0:
27-
n = int(str(abs(x))[::-1])
28-
else:
29-
n = -int(str(abs(-x))[::-1])
30-
return n if n.bit_length() < 32 else 0 # same trick but,
31-
# using inbuilt function to check for overflow
1+
#7 Reverse Integer
2+
# https://leetcode.com/problems/reverse-integer/
3+
4+
class Solution:
5+
def reverse(self, x):
6+
"""
7+
:type x: int
8+
:rtype: int
9+
"""
10+
11+
INT_MAX = 2**31 -1 # for integer overflow
12+
#INT_MIN = -(2**31 -1)
13+
if abs(x) < 10: return x # if the integer is single digit,
14+
# we can just return the same number.
15+
if x < 0: # checking if the number is negative
16+
r = -int(str(-x)[::-1])# python strings are not mutable,
17+
# so create a new string with slicing trick
18+
if x > 0:
19+
r = int(str(x)[::-1])
20+
if abs(r) > INT_MAX: #or r < INT_MIN: # check for overflow, using abs() we can
21+
# we can skip the extra check for MIN overflow, runs a bit faster
22+
return 0 # if overflow occurs after reversing, return 0
23+
else:
24+
return r
25+
"""
26+
if x>=0:
27+
n = int(str(abs(x))[::-1])
28+
else:
29+
n = -int(str(abs(-x))[::-1])
30+
return n if n.bit_length() < 32 else 0 # same trick but,
31+
# using inbuilt function to check for overflow
3232
"""

python/two sum.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
class Solution:
2-
def twoSum(self, nums, target):
3-
"""
4-
:type nums: List[int]
5-
:type target: int
6-
:rtype: List[int]
7-
"""
8-
# probably the most common question and the easiest to start with
9-
# I know of two solutions:
10-
# First using dictionary (one pass and two pass)
11-
# The other using binary search (log(n))
12-
13-
"""
14-
One pass (O(n)) solution using dictionary
15-
16-
if len(nums) <2: # handling some end cases quickly
17-
return
18-
if nums[0]+nums[1] == target: return[0,1] # just checking if
19-
# we get lucky with first two numbers
20-
else:
21-
a = {} #empty dictionary
22-
for i in range(len(nums)):
23-
if nums[i] in a: #if the current number found in dictionary
24-
return([i,a[nums[i]]]) #return the poisition from list and dict
25-
else: # or
26-
a[target - nums[i]] = i # dictionary key with target - current value
27-
# and value as the poisition
28-
29-
"""
30-
# same as above but without the fancy checks
31-
a = {}
32-
for i in range(len(nums)):
33-
if nums[i] in a:
34-
return([i,a[nums[i]]])
35-
else:
36-
a[target - nums[i]] = i
1+
class Solution:
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
# probably the most common question and the easiest to start with
9+
# I know of two solutions:
10+
# First using dictionary (one pass and two pass)
11+
# The other using binary search (log(n))
12+
13+
"""
14+
One pass (O(n)) solution using dictionary
15+
16+
if len(nums) <2: # handling some end cases quickly
17+
return
18+
if nums[0]+nums[1] == target: return[0,1] # just checking if
19+
# we get lucky with first two numbers
20+
else:
21+
a = {} #empty dictionary
22+
for i in range(len(nums)):
23+
if nums[i] in a: #if the current number found in dictionary
24+
return([i,a[nums[i]]]) #return the poisition from list and dict
25+
else: # or
26+
a[target - nums[i]] = i # dictionary key with target - current value
27+
# and value as the poisition
28+
29+
"""
30+
# same as above but without the fancy checks
31+
a = {}
32+
for i in range(len(nums)):
33+
if nums[i] in a:
34+
return([i,a[nums[i]]])
35+
else:
36+
a[target - nums[i]] = i
3737

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.