
Python
weixin_45405128
stay hungry, stay foolish.
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
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 · 240 阅读 · 0 评论 -
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 · 234 阅读 · 0 评论 -
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 · 183 阅读 · 0 评论 -
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 · 135 阅读 · 0 评论 -
PDD_2019_01
题目:A 国的手机号码由且仅由 N 位十进制数字(0-9)组成。一个手机号码中有至少 K 位数字相同则被定义为靓号。A 国的手机号可以有前导零,比如 000123456 是一个合法的手机号。小多想花钱将自己的手机号码修改为一个靓号。修改号码中的一个数字需要花费的金额为新数字与旧数字之间的差值。比如将 1 修改为 6 或 6 修改为 1 都需要花 5 块钱。给出小多现在的手机号码,问将其修...原创 2020-03-20 16:59:47 · 337 阅读 · 0 评论 -
编程笔试-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 · 390 阅读 · 0 评论 -
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 · 143 阅读 · 0 评论 -
牛客网-华为-字符串分隔
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 · 173 阅读 · 0 评论 -
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 · 176 阅读 · 0 评论 -
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 · 215 阅读 · 0 评论 -
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 · 150 阅读 · 0 评论 -
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 · 231 阅读 · 0 评论 -
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 · 203 阅读 · 0 评论 -
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 · 180 阅读 · 0 评论 -
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 · 175 阅读 · 0 评论 -
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 · 184 阅读 · 0 评论 -
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 · 162 阅读 · 0 评论 -
Greedy Algorithms: Max Min
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the maxMin function below.def maxMin(k, arr): s=sorted(arr) li=[] for i in range(len(s)-k+1): ...原创 2020-02-12 18:46:54 · 219 阅读 · 0 评论 -
Greedy Algorithms: Greedy Florist
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the getMinimumCost function below.#my own solutiondef getMinimumCost0(k, c): if k>=len(c): retu...原创 2020-02-12 18:28:37 · 334 阅读 · 0 评论 -
Greedy Algorithms: Luck Balance
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the luckBalance function below.def luckBalance0(k, contests): print(contests) imp=[] score=0 fo...原创 2020-02-12 17:44:01 · 192 阅读 · 0 评论 -
Greedy Algorithms: Minimum Absolute Difference in an Array
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the minimumAbsoluteDifference function below.def minimumAbsoluteDifference(arr): s=sorted(arr) diff=s[-...原创 2020-02-12 16:28:34 · 128 阅读 · 0 评论 -
Dictionaries and Hashmaps: Count Triplets
#!/bin/python3'''import mathimport osimport randomimport reimport sys# Complete the countTriplets function below.#naive waydef countTriplets0(arr, r): s=sorted(arr) count=0 n=le...转载 2020-02-09 23:39:57 · 203 阅读 · 0 评论 -
Dictionaries and Hashmaps:Frequency Queries
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the freqQuery function below.from collections import defaultdictdef freqQuery(queries): data=defaultdict(...原创 2020-02-09 22:10:56 · 168 阅读 · 0 评论 -
Binary Search Tree : Lowest Common Ancestor
class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(sel...原创 2020-02-09 01:44:33 · 136 阅读 · 0 评论 -
Tree: Height of a Binary Tree
class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(sel...原创 2020-02-09 01:22:10 · 431 阅读 · 0 评论 -
Hash Tables: Ransom Note
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the checkMagazine function below.from collections import defaultdictdef checkMagazine(magazine, note): z...原创 2020-02-09 00:40:34 · 177 阅读 · 0 评论 -
Dictionaries and Hashmaps: Sherlock and Anagrams
import mathimport osimport randomimport reimport sys# Complete the sherlockAndAnagrams function below.from collections import defaultdictdef get_all_substrings(s):#先提出所有的子集 n=len(s)...原创 2020-02-09 00:16:29 · 189 阅读 · 0 评论 -
Sorting:Fraudulent Activity Notifications _bisect.insort() python
#!/bin/python3'''import mathimport osimport randomimport reimport sys# Complete the activityNotifications function below.#runtime error for some casesdef activityNotifications(expenditure...原创 2020-02-08 16:01:45 · 342 阅读 · 0 评论 -
Bisect Algorithm Functions in Python
1. bisect(list, num, beg, end):- This function returns thepositionin thesortedlist, where the number passed in argument can be placed so as tomaintain the resultant list in sorted order. If the ...转载 2020-02-08 15:47:16 · 123 阅读 · 0 评论 -
Sorting: Comparator
Sample Input5amy 100david 100heraldo 50aakansha 75aleksa 150Sample Outputaleksa 150amy 100david 100aakansha 75heraldo 50from functools import cmp_to_keyclass Player: de...原创 2020-02-08 02:09:43 · 174 阅读 · 0 评论 -
Sorting: Mark and Toys
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the maximumToys function below.def maximumToys(prices, k): p=sorted(prices) ####直接用sorted(),如果用自己写的算法,runti...原创 2020-02-08 00:11:12 · 176 阅读 · 0 评论 -
Sorting: Bubble Sort
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the countSwaps function below.def countSwaps(a): count=0 for i in range(n): for j in range(n-1-i)...原创 2020-02-07 23:45:56 · 116 阅读 · 0 评论 -
String Manipulation:Special String Again
#!/bin/python3import mathimport osimport randomimport reimport sys'''# Complete the substrCount function below.def substrCount(n, s): #每个单独字符 result=n #连续两个,三个,,,n个字符相同 f...原创 2020-02-07 19:00:19 · 296 阅读 · 0 评论 -
String ManipulationSherlock and the Valid String
#!/bin/python3import mathimport osimport randomimport reimport sys# Complete the isValid function below.def isValid(s): zd={} for c in s: if c not in zd.keys(): ...原创 2020-02-07 01:57:05 · 164 阅读 · 0 评论 -
3 Ways to Check if all Elements in List are Same [Python Code]
Method 1: Using Python set:listChar = ['z','z','z','z'] if(len(set(listChar))==1): print "All elements in list are same"else: print "All elements in list are not same"Method 2: Using Pytho...转载 2020-02-07 00:57:20 · 135 阅读 · 0 评论 -
String Manipulation: Alternating Characters
Shashank非常喜欢字符串,特别是那些连续字符都是不一样的字符串。比如:他喜欢,但他不喜欢。给定一个字符串,该字符串只可能由字母和组成。Shashank想把这个字符串转变成他喜欢的字符串,在转变的过程中,他允许删除字符串中的某些字符。 你的任务就是找出最少需要删除几个字符,才能把给定的字符串转变成Shashank喜欢的字符串。样例输入:5AAAABBBBBABABABABBA...原创 2020-02-07 00:35:11 · 203 阅读 · 0 评论 -
ManipulationStrings: Making Anagrams
Alice is taking a cryptography class and findinganagramsto be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second str...原创 2020-02-07 00:13:14 · 155 阅读 · 0 评论 -
Hackerrank Day 9: Multiple Linear Regression
Multiple Regression in Pythonfrom sklearn import linear_modelx = [[5, 7], [6, 6], [7, 4], [8, 5], [9, 6]]y = [10, 20, 60, 40, 50]lm = linear_model.LinearRegression()lm.fit(x, y)a = lm.interc...原创 2020-02-06 14:02:45 · 211 阅读 · 0 评论 -
Python | end parameter in print()
# This Python program must be run with # Python 3 as it won't work with 2.7. # ends the output with a <space> print("Welcome to" , end = ' ') print("GeeksforGeeks", end = ' ') Welcome ...转载 2020-01-26 22:22:07 · 218 阅读 · 0 评论 -
zip() in Python
# Python code to demonstrate the working of # zip() # initializing lists name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ] roll_no = [ 4, 1, 3, 2 ] marks = [ 40, 50, 60, 70 ] # using zip()...转载 2020-01-26 21:53:22 · 282 阅读 · 0 评论