cy69855522 / shortest-leetcode-python-solutions Goto Github PK
View Code? Open in Web Editor NEWLeet Code 刷题笔记 - - 不求最快最省,但求最短最优雅,Shorter is better here.
Leet Code 刷题笔记 - - 不求最快最省,但求最短最优雅,Shorter is better here.
class Solution: def thirdMax(self, nums): nums = sorted(list(set(nums))) return (len(nums) < 3) * nums[-1] or nums[-3]
如果 对角线元素个数 是偶数则应该把 temp 反转
有误,比如 3*8 的矩阵,后面的对角线元素个数都是3,那就一直不会翻转,这是不对的。
代码里面是根据 l
的奇偶性来判断是否反转,是正确的。
def addTwoNumbers(l1, l2):
temp1 = int(''.join(map(str,l1[::-1])))
temp2 = int(''.join(map(str,l2[::-1])))
return (map(int,list(str(temp1+temp2)[::-1])))
class Solution:
def isPalindrome(self, x: int) -> bool:
return (k:=str(x)) == k[::-1]
字符数好像还多了
class Solution:
def isPowerOfFour(self, num: int) -> bool:
return num > 0 and ((math.log10(num)/math.log10(4)) % 1) == 0
class Solution:
def missingNumber(self, nums: List[int]) -> int:
return sum(list(range(len(nums) + 1))) - sum(nums)
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
nums[:] = nums[len(nums) - k:] + nums[:len(nums) - k]
class Solution:
def search(self, nums: List[int], target: int) -> int:
if target in nums:
return nums.index(target)
else:
return -1
66题,写了一个另外的简单写法,没有用map函数
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
return [int(i) for i in str(int(str(digits)[1:-1].replace(',', '').replace(' ', '')) + 1)]
operator 库里有 xor 函数,不需要从 int 类里调
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(xor, nums)
虽然很简单,但是还是想说下
class Solution:
def lengthOfLastWord(self, s: str) -> int:
return len(s.strip(' ').split(' ')[-1])
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
perfect = [6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128]
if num in perfect:
return True
return False
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.