python
Leobing001
这个作者很懒,什么都没留下…
展开
-
python变量作用域:list直接就是全局变量
a = 1def fun(a): a += 1 print(a)fun(a)print(a)a = [1]def fun(): a.append(2) print(a)fun()print(a)输出21[1, 2][1, 2]list直接就是全局变量。原创 2018-08-08 22:34:47 · 5344 阅读 · 0 评论 -
264. Ugly Number II
class Solution: def nthUglyNumber(self, n): """ :type n: int :rtype: int """ ugly = [1] i2, i3, i5 = 0, 0, 0 for _ in range(2, n + 1): ...原创 2018-09-01 18:08:53 · 170 阅读 · 0 评论 -
279. Perfect Squares
class Solution: def numSquares(self, n): """ :type n: int :rtype: int """# # BFS# # 每一层都减去所有可以减去的平方数,下一层判断所有(x-平方数)# if n < 2:# ...原创 2018-09-01 20:39:11 · 308 阅读 · 0 评论 -
300. Longest Increasing Subsequence
class Solution: def lengthOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ # dp[i] if not nums: return 0 n...原创 2018-09-02 13:09:02 · 150 阅读 · 0 评论 -
304. Range Sum Query 2D - Immutable
class NumMatrix: def __init__(self, matrix): """ :type matrix: List[List[int]] """ if not matrix: return None r = len(matrix) c = len(...原创 2018-09-02 13:50:58 · 311 阅读 · 0 评论 -
309. Best Time to Buy and Sell Stock with Cooldown
class Solution: def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if not prices or len(prices) == 1: return 0 ...原创 2018-09-02 14:51:13 · 113 阅读 · 0 评论 -
322. Coin Change
class Solution: def coinChange(self, coins, amount): """ :type coins: List[int] :type amount: int :rtype: int """ MAX = float('inf') # 初始条件...原创 2018-09-06 10:55:24 · 155 阅读 · 0 评论 -
338. Counting Bits
class Solution: def countBits(self, num): """ :type num: int :rtype: List[int] """ # 右移一位(之前计算的记录下来)+ 末尾是否是1 num += 1 # 初始条件dp[0] = 0 ...原创 2018-09-06 13:57:28 · 142 阅读 · 0 评论 -
343. Integer Break
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ dp = [0 for i in range(n+1)] dp[1] = 1 for i in range(2, n+1):...原创 2018-09-06 18:05:20 · 136 阅读 · 0 评论 -
357. Count Numbers with Unique Digits
class Solution: def countNumbersWithUniqueDigits(self, n): """ :type n: int :rtype: int """ # 解法来源https://leetcode.com/problems/count-numbers-with-unique-d...原创 2018-09-21 10:08:57 · 160 阅读 · 0 评论 -
368. Largest Divisible Subset
class Solution: def largestDivisibleSubset(self, nums): """ :type nums: List[int] :rtype: List[int] """ if not nums: return [] if len(n...原创 2018-09-21 11:15:51 · 186 阅读 · 0 评论 -
python给多个数赋值,交换变量等
如果a = 1b = 2那么,交换a和b的值用python怎么做?如下:a, b = b, a原理是什么?是把 b, a当成了(b, a)元组。但之后本人在学习过程中经常遇到容易混淆的情形,比如:a, b = b, a + b结果是 还是 ? 记住一句话即可:先算右面! 正确。...原创 2018-08-29 21:28:49 · 1269 阅读 · 0 评论 -
213. House Robber II
class Solution: def rob(self, nums): """ :type nums: List[int] :rtype: int """ # 将第一家和最后一家分别去掉,用动态规划求出两个最大值,然后较大的那个就是 n = len(nums) if n ==...原创 2018-08-29 21:20:27 · 315 阅读 · 0 评论 -
5. Longest Palindromic Substring
class Solution: def longestPalindrome(self, s): """ :type s: str :rtype: str """ # dp ans = '' max_len = 0 n = len(s) DP = ...原创 2018-08-27 16:24:13 · 138 阅读 · 0 评论 -
62. Unique Paths
class Solution: def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ # 让初始第一行和第一列为1 dp = [[1] * n for _ in range(m)]...原创 2018-08-27 16:35:33 · 110 阅读 · 0 评论 -
63. Unique Paths II
class Solution: def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ # base cases:第一行第一列为1,但遇到obstacl...原创 2018-08-27 17:06:33 · 124 阅读 · 0 评论 -
64. Minimum Path Sum
class Solution: def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ R = len(grid) C = len(grid[0]) dp = [[0] * C fo...原创 2018-08-28 11:58:23 · 156 阅读 · 0 评论 -
91. Decode Ways
class Solution: def numDecodings(self, s): """ :type s: str :rtype: int """ # 动态规划dp[i]表示s第i个数时decode方式 if s == "": return 0 d...原创 2018-08-28 13:06:33 · 152 阅读 · 0 评论 -
120. Triangle
class Solution: def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """ # dp if not triangle: return 0 ...原创 2018-08-28 14:14:55 · 218 阅读 · 0 评论 -
python一行读取多个数字
最简单快捷的方式:l = list(map(int, input().split()))print(l)原创 2018-08-21 12:44:34 · 9932 阅读 · 0 评论 -
221. Maximal Square
class Solution: def maximalSquare(self, A): """ :type A: List[List[str]] :rtype: int """ for i in range(len(A)): for j in range(len(A[i])): ...原创 2018-08-31 23:46:27 · 165 阅读 · 0 评论 -
139. Word Break
class Solution: def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str] :rtype: bool """ n = len(s) dp = [False] * (n ...原创 2018-08-29 14:35:45 · 134 阅读 · 0 评论 -
152. Maximum Product Subarray
class Solution: def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) maxi = nums[0] dp_min = [0] * n ...原创 2018-08-29 20:11:52 · 156 阅读 · 0 评论 -
numpy.split, array_split, hsplit, vsplit, dsplit大家庭参数含义与用法
见该说明文档:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.split.html尤其值得记忆的是当indices_or_sections是1-D array(比如[2, 3])时代表的含义。另外,split默认是axis=0,代表沿行分离。...原创 2018-10-31 14:49:59 · 1278 阅读 · 0 评论