Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6731ac2

Browse files
168 581 633 643
1 parent 163b3ce commit 6731ac2

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__author__ = 'vaan'
2+
3+
4+
class Solution(object):
5+
def convertToTitle(self, n):
6+
"""
7+
:type n: int
8+
:rtype: str
9+
"""
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
__author__ = 'vaan'
2+
3+
class Solution(object):
4+
def findUnsortedSubarray(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
# start, end = -1, len(nums)
10+
# for i in range(len(nums)):
11+
# if i+1 < len(nums) and nums[i+1] < nums[i]:
12+
# if i-1 > 0 and nums[i] != nums[i-1]:
13+
# start = i
14+
# break
15+
# else:
16+
# for k in range(i, -1, -1):
17+
# if nums[k] == nums[i]:
18+
# start = k
19+
# break
20+
# for j in range(len(nums)-1, -1, -1):
21+
# if j-1 >= 0 and nums[j] < nums[j-1]:
22+
# if j+1 < len(nums) and nums[j] != nums[j+1]:
23+
# end = j
24+
# break
25+
# else:
26+
# for k in range(j, len(nums)):
27+
# if nums[k] == nums[j]:
28+
# end = k
29+
# break
30+
# if start == -1 and end == len(nums):
31+
# return 0
32+
# return end-start+1
33+
start, end = -1, len(nums)
34+
sorted_nums = sorted(nums)
35+
for i in range(len(nums)):
36+
if nums[i] != sorted_nums[i]:
37+
start = i
38+
break
39+
for j in range(len(nums)-1, -1, -1):
40+
if nums[j] != sorted_nums[j]:
41+
end = j
42+
break
43+
if start == -1 and end == len(nums):
44+
return 0
45+
return end-start+1
46+
47+
s = Solution()
48+
print s.findUnsortedSubarray([2, 6, 4, 8, 10, 9, 15])
49+
print s.findUnsortedSubarray([1, 2, 3, 4])
50+
print s.findUnsortedSubarray([2, 1])
51+
print s.findUnsortedSubarray([1, 3, 2, 2, 2])
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__author__ = 'vaan'
2+
3+
class Solution(object):
4+
def judgeSquareSum(self, c):
5+
"""
6+
:type c: int
7+
:rtype: bool
8+
"""
9+
def is_square(n):
10+
import math
11+
root = int(math.sqrt(n))
12+
return root*root == n
13+
14+
for i in range(int(c/2)+1):
15+
if is_square(i) and is_square(c-i):
16+
return True
17+
return False
18+
19+
20+
s = Solution()
21+
print s.judgeSquareSum(100000000)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
__author__ = 'vaan'
2+
3+
class Solution(object):
4+
def findMaxAverage(self, nums, k):
5+
"""
6+
:type nums: List[int]
7+
:type k: int
8+
:rtype: float
9+
"""
10+
# Memory Limit Exceeded
11+
# return max(map(sum, [nums[i: i+k] for i in range(len(nums)-k+1)])) / float(k)
12+
13+
s = max_sum = sum(nums[:k])
14+
for i in range(k, len(nums)):
15+
s += nums[i] - nums[i-k]
16+
if s > max_sum:
17+
max_sum = s
18+
return max_sum / float(k)
19+
20+
s = Solution()
21+
print s.findMaxAverage([1, 12, -5, -6, 50, 3], 4)
22+
print s.findMaxAverage([5], 1)
23+
print s.findMaxAverage([0, 1, 1, 3, 3], 4)
24+
print s.findMaxAverage([0, 4, 0, 3, 2], 1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /