from copy import deepcopy as copy
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
x_len = len(M)
y_len = len(M[0]) if x_len else 0
res = copy(M)
for x in range(x_len):
for y in range(y_len):
neighbors = [
M[_x][_y]
for _x in (x1, x, x+1)
for _y in (y1, y, y+1)
if 0 <= _x < x_len and 0 <= _y < y_len
]
res[x][y] = sum(neighbors) // len(neighbors)
return res
N
niksite
@niksite
9
Reputation
8
Posts
148
Profile views
0
Followers
0
Following
Posts made by niksite

Straightforward python solution

RE: 12 lines Python
@StefanPochmann Would it be really O(n) considering dictionary lookup (with could be O(n) in worst case itself)?

Python, two pointers, simple.
i = 1 for j in range(len(nums)): if nums[i1] != nums[j]: nums[i], nums[j] = nums[j], nums[i] i += 1 return len(nums[:i])

Simple and clear python solution.
For /subsets problem solution looks like the following:
nums.sort() res = [[]] for num in nums: res += [r + [num] for r in res] return res
Now we could have dups so it makes sense to just replace lists with sets:
nums.sort() res = {()} for num in nums: res = {r + (num,) for r in res} return list(res)
That's all.

RE: Python binary search solution  O(logn)  48ms
@eran there is no integer overflow in python

1line Python solution
class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ return sorted(set(itertools.permutations(nums)))

RE: Number of 1 Bits
In Python it could be pretty straightforward (still not so efficient) oneliner: bin(n).count('1')