python学习思路

话不多说,就是开始练习,因为之前编程的经验比较少,怎么办,就像马化腾一样,先抄袭,只有抄袭完成了之后,你才会获得你自己的知识
总体思路是先抄袭,再领悟。最后一定要自己默写打一次。不然没印象
案例1:求最长的回文数
python学习思路_第1张图片

class Solution:
    def longestPalindrome(self, s: str) -> str:
        def huiwenshu(s, l, r):
            while l >= 0 and r < len(s) and s[l] == s[r]:
                l -= 1
                r += 1
            return s[l + 1:r]

        res = ''
        for i in range(len(s)):
            sub1 = huiwenshu(s, i, i)
            sub2 = huiwenshu(s, i, i + 1)
            res = sub1 if len(sub1) > len(res) else res
            res = sub2 if len(sub2) > len(res) else res
        return res

a=Solution()
f=a.longestPalindrome('abcdcda')
print(f)

案例二
python学习思路_第2张图片

class Solution:
    def isPalindrome(self, x: int) -> bool:
         return str(x) == str(x)[::-1]

案例三:

class Solution:
    def mySqrt(self, x: int) -> int:
        left,right = 0,x
        while left <= right:
            mid = (left + right)//2
            if mid * mid == x:
                return mid
            elif mid * mid > x:
                if (mid-1)*(mid-1) <= x:
                    return mid-1
                right = mid -1
            else:
                if (mid+1) * (mid+1) > x:
                    return mid
                left = mid + 1
        return left

案例四
python学习思路_第3张图片
案例五
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(int(num1)+int(num2))python学习思路_第4张图片
python学习思路_第5张图片
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
res = 0
for i in s+t:
res ^= ord(i)
return chr(res)

案例六
python学习思路_第6张图片
案例七
`# Definition for a binary tree node.

class TreeNode:

def init(self, val=0, left=None, right=None):

self.val = val

self.left = left

self.right = right

class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def jugde(left,right):
if not left and not right:
return True
if not left or not right:
return False
if left.val != right.val:
return False
return jugde(left.left,right.right) and jugde(left.right, right.left)
return jugde(root.left, root.right)`
python学习思路_第7张图片

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s1=''.join(filter(str.isalnum, s)).lower()
        return s1==s1[::-1]

案例八
python学习思路_第8张图片

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        ans = [1]
        for i in range(1, rowIndex+1):
            ans.append(ans[-1]*(rowIndex-i+1)//i)
        return ans

案例九
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(lambda x, y: x ^ y, nums)`python学习思路_第9张图片
https://www.runoob.com/python/python-func-reduce.html

自己改写

class Solution:
    def yihuo(self,a,b):
        return a^b
    def singleNumber(self, nums: List[int]) -> int:
        return reduce(self.yihuo, list(nums)) 

案例10,合并两个有序数组

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        nums1[m:] = nums2
        nums1.sort()
        return nums1

python学习思路_第10张图片

你可能感兴趣的:(python,python,学习,开发语言)