xmchxup / kata Goto Github PK
View Code? Open in Web Editor NEWCoding Practice
Coding Practice
模拟遍历右下左上,当遍历完所有元素结束。
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
m, n = len(matrix), len(matrix[0])
visited = [[False for _ in range(n)] for _ in range(m)]
dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]]
x, y = 0, 0
dir_idx = 0
def is_valid_coordinate(x, y):
return not (x < 0 or x >= n or y < 0 or y >= m or visited[y][x])
while len(res) < m * n:
res.append(matrix[y][x])
visited[y][x] = True
delta_x, delta_y = dirs[dir_idx]
if not is_valid_coordinate(x + delta_x, y + delta_y):
dir_idx = (dir_idx + 1) % 4
delta_x, delta_y = dirs[dir_idx]
x, y = x + delta_x, y + delta_y
return res
Quick Select**
from typing import List
import random
class Solution:
def findKthLargestV2(self, nums: List[int], k: int) -> int:
pivot = random.choice(nums)
left = [x for x in nums if x < pivot]
mid = [x for x in nums if x == pivot]
right = [x for x in nums if x > pivot]
if k <= len(right):
return self.findKthLargestV2(right, k)
elif k > len(right) + len(mid):
return self.findKthLargestV2(left, k - (len(right) + len(mid)))
return mid[0]
def findKthLargest(self, nums: List[int], k: int) -> int:
def quickSelect(lo: int, hi: int, k: int):
if lo >= hi:
return nums[lo]
x = nums[lo]
i, j = lo - 1, hi + 1
while i < j:
i += 1
while i <= j:
if nums[i] >= x:
break
i += 1
j -= 1
while j >= i:
if nums[j] <= x:
break
j -= 1
if i < j:
nums[i], nums[j] = nums[j], nums[i]
largeCnt = hi - j
if k <= largeCnt:
return quickSelect(j + 1, hi, k)
else:
return quickSelect(lo, j, k - largeCnt)
return quickSelect(0, len(nums) - 1, k)
dp[i][j] = 到达i,j位置的唯一路径方案
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1 for _ in range(n)] for _ in range(m)]
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[m - 1][n - 1]
将两个数组排序,从大到小遍历尽可能的满足Big小孩的需要。
from typing import List
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
res = 0
i, j = len(g) - 1, len(s) - 1
while i >= 0 and j >= 0:
if s[j] >= g[i]:
res += 1
j -= 1
i -= 1
else:
i -= 1
return res
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.