- 博客(217)
- 收藏
- 关注
原创 剑指OFFER 26. 二叉搜索树与双向链表,python iteration, one-pass, O(n)
class Solution: def Convert(self, root): if not root: return stack=[] newhead=None while root or stack: if root: stack.append(root) root=root.left else:
2021-04-04 22:36:35
172
原创 Day 30 Leetcode: Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
# 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 = rightclass S...
2020-04-30 22:16:31
224
原创 Leetcode 84 largest Rectangle Area
class Solution: def largestRectangleArea(self, heights: List[int]) -> int: stack = [(0, -1)]#dummy variable, (0.-1) o represens dummy initial height, -1 represents dummy intial positio...
2020-04-10 15:53:58
220
原创 Leetcode-Dynamically Programming-Edit Distance-hard
https://www.youtube.com/watch?v=MiqoA-yF-0Mclass Solution: def minDistance(self, word1: str, word2: str) -> int: if word1==word2: return 0 dp=[] n=len(w...
2020-04-10 13:52:26
172
原创 Leetcode-DP-Coing Change 2
class Solution: def change(self, amount: int, coins: List[int]) -> int: m=len(coins)+1 n=amount+1 dp=[[0]*n for i in range(m)] dp[0][0]=1 for i in range...
2020-04-07 18:38:00
129
原创 Leetcode_DP_Unique Jumps
class Solution: def __init__(self): self.memory = {} def uniquePaths(self, m: int, n: int) -> int: if (m,n) in self.memory: return self.memory[(m,n)] ...
2020-04-06 23:59:56
433
原创 Leetcode_Dynamic Programming_Jump Game
class Solution: def canJump(self, nums: List[int]) -> bool: n = len(nums) right_most = n - 1 for i in range(n - 2, -1, -1): if i + nums[i] >= right_mos...
2020-04-06 22:36:23
113
原创 PDD_2019_01
题目:A 国的手机号码由且仅由 N 位十进制数字(0-9)组成。一个手机号码中有至少 K 位数字相同则被定义为靓号。A 国的手机号可以有前导零,比如 000123456 是一个合法的手机号。小多想花钱将自己的手机号码修改为一个靓号。修改号码中的一个数字需要花费的金额为新数字与旧数字之间的差值。比如将 1 修改为 6 或 6 修改为 1 都需要花 5 块钱。给出小多现在的手机号码,问将其修...
2020-03-20 16:59:47
327
原创 编程笔试-20200314-01
题目:n个长度为m的字符串,每个字符串由m个大写字母组成。任选第i和第j个字符串,并交换长度为k的前缀,可以在交换后的基础上进行任意次这样的操作。请问一共可以生成多少个不同的字符串,包括原串本身。样例输入:2 3ABCDEF输出:8暴力求解line1=list(map(int,input().split()))n,m=line1[0],line1[1]li...
2020-03-14 22:57:15
375
原创 34. Find First and Last Position of Element in Sorted Array.
class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: # if target>nums[-1] or target<nums[0]: # return [-1,-1] #ans=[] left...
2020-03-06 16:33:08
104
转载 Regular Expression in Python with Examples | Set 1
Function compile()# Module Regular Expression is imported using __import__(). import re # compile() creates regular expression character class [a-e], # which is equivalent to [abcde]. # class ...
2020-03-06 16:10:58
134
原创 25. Reverse Nodes in k-Group ListNode 链表各个节点顺序倒过来,再复原顺序
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reverseKGroup(self, head: ListNode,...
2020-03-06 14:52:45
186
原创 36. Valid Sudoku
1. 暴力循环import numpy as npclass Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: for i in range(9): if len(list(set(board[i][:])))<9-board[i][:].co...
2020-03-06 13:04:04
154
原创 28. Implement strStr()
class Solution: def strStr(self, haystack: str, needle: str) -> int: for i in range(len(haystack)-len(needle)+1): if haystack[i:i+len(needle)]==needle: ...
2020-03-06 01:09:08
90
原创 24. Swap Nodes in Pairs
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def swapPairs(self, head: ListNode) -&g...
2020-03-06 00:46:42
116
原创 21. Merge Two Sorted Lists
#If both lists are non-empty, make sure a starts smaller, use its head as result, and merge the remainders behind it. Otherwise, i.e., if one or both are empty, return what's there.class Solution...
2020-03-04 01:22:16
97
原创 20. Valid Parentheses
方法一:class Solution: def isValid(self, s: str) -> bool: left=['(', '{', '[' ] right=[ ')','}',']'] if len(s)==0: return True if s[0] in right:...
2020-03-04 01:07:37
136
原创 19. Remove Nth Node From End of List 两个指针
方法一:先得到listnode的长度,然后再L-n+1处断开,接上下一个node# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: ...
2020-03-03 23:54:43
132
原创 17. Letter Combinations of a Phone Number ##backtracking
class Solution: def letterCombinations(self, digits: str) -> List[str]: #先把字典写出来 d={'2':['a','b','c'], '3':['d','e','f'], '4':['g','h','i'], '...
2020-03-03 19:55:29
148
原创 16. 3Sum Closest- Medium - 排序之后用夹逼的方法 (求和,找最接近目标的值)
class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: if len(nums)<=2: return None if len(nums)==3: return sum(nums) ...
2020-02-22 20:57:10
174
原创 15. 3Sum
#暴力求解,超时class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res=[] n=len(nums) for x in range(n-2): for y in range(x+1,n-1): ...
2020-02-22 19:37:24
98
原创 14. Longest Common Prefix
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs)==0: return '' if len(strs)==1: return strs[0] s='' ...
2020-02-22 17:56:01
223
原创 11. Container With Most Water - Medium
#暴力求解'''class Solution: def maxArea(self, height: List[int]) -> int: maxvol=0 n=len(height) for i in range(n-1): for j in range(i+1,n): vo...
2020-02-22 16:44:17
122
原创 9. Palindrome Number - Easy
方法一:class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False if x<10: return True res=0 num=x ...
2020-02-22 02:05:55
103
原创 7. Reverse Integer
方法一:class Solution: def reverse(self, x: int) -> int: minus=False if x<0: x=-x minus=True li=[] while x>0: ele=x%...
2020-02-22 01:04:04
140
原创 6. ZigZag Conversion
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows==1: return s if len(s)<=numRows: return s ans='' ...
2020-02-21 21:25:14
103
原创 5. 最长回文子串 Longest Palindromic Substring
(1) 暴力求解class Solution: def longestPalindrome(self, s: str) -> str: maxlen=0 result='' for i in range(len(s)): for j in range(i,len(s)+1):##注意首尾坐标 ...
2020-02-21 19:49:41
229
原创 编程笔试2020221
题目:一个字符串, 当其中出现超过2个的连续字符时候,将其改写成该字符的数字+该字符。例如'aaaabbcd',改写成‘4abbcd’。请写一个unzip函数,判断输入的字符串是否符合该改写规则,如果符合,打印原始字符串。注意:使用left变量传递参数。判断每个字符的前一位是否为数字。s='10abb4c51d'def unzip(s): left=0 ...
2020-02-21 14:00:23
126
原创 牛客网-华为-字符串分隔
while True: try: s=str(input()) if len(s)==8: print(s) elif len(s)<8: while len(s)<8: s=s+'0' print(s) ...
2020-02-20 17:09:41
163
原创 3. Longest Substring Without Repeating Characters
#import itertools as itclass Solution: def lengthOfLongestSubstring(self, s: str) -> int: mlen=0 n=len(s) for i in range(n): zd={} #字典比list运算快许多 ...
2020-02-15 15:21:57
85
原创 2. add-two-numbers
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2...
2020-02-15 13:36:48
111
原创 Time Complexity: Primality
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the primality function below.def primality(n): if n==1: return 'Not prime' if n==2: retur...
2020-02-14 17:42:55
164
原创 Miscellaneous: Flipping bits
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the flippingBits function below.def flippingBits(n): li=[0]*33 for i in range(1,33): n=n-2**(33...
2020-02-14 17:29:54
197
原创 TreesTrees: Is This a Binary Search Tree?
""" Node is defined asclass node: def __init__(self, data): self.data = data self.left = None self.right = None"""def checknode(node,min,max): if node == None: ...
2020-02-14 17:04:51
143
原创 Linked Lists:Reverse a doubly linked list
def reverse(head): left=None right=head while right: tmp=right.next #先保存下一个 right.next=left #换顺序 left=right #进1 right=tmp #进1 return left...
2020-02-14 02:06:59
221
原创 Linked Lists: Inserting a Node Into a Sorted Doubly Linked List
#!/bin/python3import mathimport osimport randomimport reimport sysclass DoublyLinkedListNode: def __init__(self, node_data): self.data = node_data self.next = None ...
2020-02-14 01:18:20
192
原创 Linked Lists: Insert a node at a specific position in a linked list
#!/bin/python3import mathimport osimport randomimport reimport sysclass SinglyLinkedListNode: def __init__(self, node_data): self.data = node_data self.next = Noneclass ...
2020-02-14 00:52:03
167
原创 Dynamic Programming: Max Array Sum
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the maxSubsetSum function below.def maxSubsetSum(arr): n=len(arr) v = [0] * n for i in range(n): ...
2020-02-14 00:37:11
156
原创 Hash Tables: Ice Cream Parlor
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the whatFlavors function below.#naive waydef whatFlavors0(cost, money): n=len(cost) #li=[] for i in ...
2020-02-13 23:00:31
174
原创 Recursion: Davis' Staircase
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the stepPerms function below.def stepPerms0(n): if n==1: return 1 if n==2: return 2 ...
2020-02-13 01:30:33
153
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人