blob_id
string
repo_name
string
path
string
length_bytes
int64
score
float64
int_score
int64
text
string
3d4216a801e37b5bdce48411fcd10a78d88ec473
PontusSven/python-course
/OOP/homework.py
1,554
4.03125
4
import math class Line(): def __init__(self, coor1, coor2): self.coor1 = coor1 self.coor2 = coor2 def distance(self): dist = math.sqrt((self.coor1[1] - self.coor1[0])**2 + (self.coor2[1] - self.coor2[0])**2) print(dist) def slope(self): m = (self.coor2[1] - ...
0a60461ce5c88b6f5fdcd157fca0d40d4e29ed25
mirdesai81/python_fundamentals
/functions.py
1,931
3.578125
4
def describe_pet(type='dog',name='rocky'): ''' Method with default value if not provided :param type: type f pet :param name: name of pet :return: void ''' print('I have a animal of type '+type) print('My animal name is '+name) describe_pet('cat','puffy') describe_pet(type='dog',nam...
0762b36411ba77f35b54a247b3a082e287d8ccd6
OmkarSurve07/Contact-Book
/car_count.py
253
3.859375
4
cars = [audi, audi, audi, BMW, jaguar, jaguar] final_car_count = {} for car in cars: if car not in final_car_count: final_car_count.update({car:1}) else: final_car_count.update({car:final_car_count[car]+1}) print(final_car_count)
b077e4ed2ed0221d53f8e15932c29b493017e22f
joonyeobjeon/Separating_hyperplane
/opt_hyperplane/solver/optimizer.py
6,899
3.578125
4
import cplex from cplex.exceptions import CplexError import numpy as np class SetSimplex(object): """Set the parameters for Simplex method. Solve the system Args: object : Object """ def __init__(self): """Initialize the SetSimplex object """ self.object_coeff = [...
fe98dd58e271a45ee7c3f660bf22ff878d54d8ff
Rettdog/PythonScripts
/d20test.py
1,686
3.59375
4
bonus = int(input("Enter Bonus: ")) DC = int(input("Enter DC: ")) advantageSum = 0; advArr = [] disadvantageSum = 0; disArr = [] normalSum = 0; normArr = [] for i in range(1+bonus,21+bonus): normalSum+=i normArr.append(i) for j in range(1+bonus,21+bonus): if i<=j: disadvantageSum+=i ...
baa11fc70c9593de8c57b6d6b8bf453fce022c5b
godwinreigh/web-dev-things
/programming projects/python projects/tutorials2/10.lists.py
927
4.5
4
# list, you can store any data types in here #l = [1,2,3] #l2 = [1, "string", True, 4.3, [1, 2, 3]] #you can put another list inside of the list #print(l2[0]) #print(l2[1]) #print(l2[2]) #print(l2[3]) #print(l2[4]) #append is use to add strings at the of the list names = ["Joe", "John", "James"] names2 = names.append...
008064ccf0ee3453f51cc4b21041079f726adff3
godwinreigh/web-dev-things
/programming projects/python projects/tutorials(fundamentals)/3.workingwithnumbers.py
326
3.9375
4
print(2) print(9.123414) print(3 * (4 + 5)) print(10 % 3) my_num = 5 print(my_num) my_num=5 print(str(my_num) + " my favorite number") my_num= -5 print(abs(my_num)) print(my_num(pow(3, 2)) print(max(4, 6)) print(min(4, 6)) print(round(3.7)) from math import * print(floor(3.7)) print(ceil(3.7)) print(sqrt(...
30bdb877b3d4f0103d95d0b43c24eb120ca36157
godwinreigh/web-dev-things
/programming projects/python projects/tkinter experiment/practice 2.py
775
4.1875
4
from tkinter import * root = Tk() e=Entry(root, width =15, borderwidth=5) e.grid(row=3, column=1) e.insert(0, 'NAME') e2 = Entry(root, width= 15, borderwidth=5) e2.grid(row=3, column=2) e2.insert(0, 'NAME') def clickbutton1(): thetext = 'THEN YOU ARE THE ' + e.get().upper() + ' PERSON.' label = Label(root, t...
1daf929feb4b23d74026c05e4cb43441f6ef3561
godwinreigh/web-dev-things
/programming projects/python projects/experiment/sandbox 6.py
1,386
4.15625
4
#data storer #collect data from the user #survey_entry() #survey collected_name = [] collected_age = [] def survey_questions2(): print("Hello") print(collected_name) print(collected_age) def survey_questions(): try: required_character_age = 2 secondQ = input("Please enter your age: ") ...
05e59d15f48d6d69863fd1f962adbca9fc5c20c3
godwinreigh/web-dev-things
/programming projects/python projects/tkinter practice/1. Intro to tkinter.py
933
4.25
4
from tkinter import * #meaning of this is from tkinter module import everything #in kinter everything is a widjets #button widjets, text widgjets, frame widjgets, everything #first thing that you will do is root widjet it is like a window or any graphical use of interface program on your computer has like a window roo...
1461b1579946d181ca80e03c70471ca5da738782
godwinreigh/web-dev-things
/programming projects/python projects/tkinter practice/14. Sliders.py
584
3.8125
4
from tkinter import * root = Tk() #how big to window will be root.geometry("400x400") #creating slider widjet #where to start where to end def slide(): my_label = Label(root, text=horizontal.get()).pack() root.geometry(str(horizontal.get()) + "x" + str(vertical.get())) vertical = Scale(root, from_=0, to=400) v...
a43723c84eb69b2aac039302ef7640525835e337
godwinreigh/web-dev-things
/programming projects/python projects/tutorials2/13. Tuples in Python.py
643
4.3125
4
# tuple is list with strings t = (1,2,3) #list have brackets while tuple has parenthesis print(t[0]) # tuples is you can't change it, you can't add and remove elements to it credit_card1 = (1234123412341234, 'Joe Roggan', 11/20, 123) credit_card2 = (1234123412341234, 'Joe Roggan', 11/20, 123) print(credit_card1 + cre...
9934f8196e7ccf7032dbdbeb24a1c6e1d92191b5
godwinreigh/web-dev-things
/programming projects/python projects/experiment/sandbox5.py
384
4.09375
4
def integer_picker(): integers = ["1", "2", "3", "4", "5"] thechosenumber = [ ] numbers = input("Please take any numbers (1,2,3,4,5) ") if numbers <="5": thechosenumber.append(numbers) integers.remove(numbers) print(integers) print(thechosenumber) else: print(...
dc963e6e7539f1d3481dbccb5bbf75f0ddd5b2d7
godwinreigh/web-dev-things
/programming projects/python projects/experiment/practicing functions.py
574
3.671875
4
# a collections of instructions # a collections of code #example def function1(): print("ahhhh") print("ahhhhh2") print("this is outside the function") function1() # a mapping #input or an argument def function2(x): return 2*x a = function2(3) #return value or output print(a) #multiple arguements def funct...
cc864ec9c34bd795e8662b5dbabca5c23c2e9a76
TechCCT/Machine-Learning-Basics-with-Python
/Label_Encoding.py
820
3.59375
4
import numpy as np from sklearn import preprocessing #Sample labels input_labels = ['red', 'black', 'green', 'yellow', 'white'] #Creating label encoder encoder = preprocessing.LabelEncoder() encoder.fit(input_labels) #Mapping between words and numbers print("\nLabel mapping: ") for i, item in enu...
38853ffa0309630aedebeb5e36c106fd36d51575
JBustos22/Physics-and-Mathematical-Programs
/recursion/catalan_rec.py
466
4.1875
4
#! /usr/bin/env python from __future__ import division, print_function # Made by: Jorge Bustos # This program calculates the nth Catalan number by using a recursive function. # he function is defined such that it calls itself. def catalan(n): if n==0: return 1 #case n=0 elif n>0: return catalan(n-1)*(4*n - 2)/(...
c94d049c159a101bb765c13ba5294d64d7f00041
JBustos22/Physics-and-Mathematical-Programs
/differential equations/lotka.py
1,385
3.765625
4
#! /usr/bin/env python """ This program uses the Runge-Kutta method to solve the lota-volterra ODEs. The equations describe the population of rabbits and foxes as time goes on. It uses the method for two dimensions, one for each equation. The two populations are plotted on top of each other to see the clear relation ...
5d07f3ca63cc71a678aed7a803fe6bff852c5f65
JBustos22/Physics-and-Mathematical-Programs
/computational integrals/mymodules/numint.py
3,084
3.71875
4
#! /usr/bin/env python """ This is the numerical integration module that is used in other programs. It includes: trapezoidal and simpson's method, their adaptive versions, and a generalized n-dimensional monte carlo integration function. Jorge Bustos Mar 4, 2019 """ from __future__ import division, print_function imp...
e5f3c1ebc5b44a3145bdf6a2ebd03a4f071663fb
JBustos22/Physics-and-Mathematical-Programs
/misc projects/bin_energy.py
1,008
4.09375
4
#! /usr/bin/env python from __future__ import division, print_function import math as m # This program asks the user for the atomic number Z and mass number A of a desired atom. With this information, it # returns the binding energy and binding energy per nucleon in MeV by using the semi-empirical mass formula # Made...
09898ee2e3e5c26efb9fa0f9969e3958671d7628
Ashleym1000/Maze
/maze.py
1,678
3.859375
4
# 10/17/20 # maze program, make it through a maze # maze size N = 4 # fn PRINTS solution matrix sol def printSolution(sol): for i in sol: for j in i: print(str(j) + " ", end="") print("") # fn CHECKS if x, y is valid for matrix N*N def isSafe( maze, x, y): if (x>=...
965b760f14efc991b1c0ca261e1e04b07bca84e8
ialeksandrov/Python
/HackBulgaria/fib.py
139
3.515625
4
def fib(n): a, b = 0, 1 result = [] for index in range(n): result.append(a) a, b = b, b + a return result
c37b67d45c6ecaa2d50bf278475b7acf475efeee
ialeksandrov/Python
/Real-Python/translate.py
294
3.609375
4
user = raw_input("Enter some text: ") user = user.replace("a", "4") user = user.replace("b", "8") user = user.replace("e", "3") user = user.replace("l", "1") user = user.replace("o", "0") user = user.replace("s", "5") user = user.replace("t", "7") print "The translated text in leet: ", user
f9174b64a574e6b74ca345d43612afca4f21cb9a
ialeksandrov/Python
/Real-Python/ex1.py
269
3.828125
4
weight = 0.2 animal = "newt" print weight, "kg is the weight of", animal print "{} kg is the weight of {}".format(weight, animal) print "{0} kg is the weight of {1}".format(weight, animal) print "{weight} kg is the weight of {animal}".format(weight=0.2, animal="newt")
98b64abbfb8455aedd6c2eda609e153c244dcd23
ialeksandrov/Python
/Python-Intermediate/except2.py
379
4.09375
4
try: print('I am sure no exception is going to occur!') except Exception: print('exception') else: # any code that should only run if no exception occurs in the try, # but for which exceptions should NOT be caught print('This would only run if no exceptions occurs. And an error here' 'would NOT be c...
e65665d36f8cf28164a5b5a5a48dab2941e26edc
ialeksandrov/Python
/insertion_sort.py
386
3.90625
4
def insertion_sort(list): for index in range(1, len(list)): value = list[index] i = index - 1 while i >= 0 and (value < list[i]): list[i + 1] = list[i] # shift number in slot i right to slot i + 1 list[i] = value # shift value lef into slot i i...
ac44462c9fe49a22e2a169603c0797d539e22d87
harinidarapu/Python-practice
/classses.py
588
4
4
# classes and inherited classes # defining base class class base_clas(): def method1(self): print("this is base class") def method2(self, somestring): print("base class" + somestring) class child_class(): def method1(self): base_clas.method1(self) print("this is c...
88e9236923f91ab02683ee95bd5125cf270a216a
ASamiAhmed/PythonPrograms
/vowels_consonants.py
324
4
4
text = input("Enter text: ") consonantc= 0 vowelc = 0 for i in text: if (i=="a")or(i=="e")or(i=="i")or(i=="o")or (i=="u"): vowelc+=1 elif (i==" "): print(end="") else: consonantc+=1 print("occurence of vowels are",vowelc) print("occurence of consonants are",consonant...
b8b843b89f1b0cb5189d8c8cd34e671ed0a04b93
ASamiAhmed/PythonPrograms
/celsius_into_fahrenheit.py
129
3.78125
4
celsius = int(input("enter temperature: ")) # formula: (C × 9/5) + 32 = F fahrenheit = (celsius * 9/5) + 32 print(fahrenheit)
53015513b288471f46231f757d890e3a6995d170
ASamiAhmed/PythonPrograms
/even_odd.py
298
4.4375
4
#Write a Python program to find whether a given number (accept from the # user) is even or odd, print out an appropriate message to the user number = int(input("enter number: ")) if number%2==0: print("The number you entered is even") else: print("The number you entered is odd")
78a44a8ad64c666cf222cc3781aeec254cd127c4
ASamiAhmed/PythonPrograms
/second_program.py
146
3.890625
4
# age = float("9") # print(type(age)) name = "sami" age = 18 print("my name is "+name) print("my age is "+str(age)) print("sami \nahmed")
353fb9922664bdcc2767799da495d96f3fd29436
ASamiAhmed/PythonPrograms
/CSV handling/data_inpuuter.py
400
3.71875
4
import csv name = input("enter your name: ") father_name = input("enter your father name: ") age =int(input("enter your age: ")) grade =input("enter your grade: ") height =float(input("enter your height: ")) with open("bio.csv","w") as f: intro = csv.writer(f,delimiter=",") intro.writerow(["name","father_name",...
a4da673a6eb10fbc439433ae9c71c5ae854f36ba
ASamiAhmed/PythonPrograms
/days-in-dates.py
252
4.34375
4
#Write a Python program to calculate number of days between two dates first_date =int(input("enter first date: ")) second_date = int(input("enter second date: ")) days = first_date - second_date print("number of days in between two dates is ",days)
ffe5c4a77a842622d5da2296c7ab27c33bb46bf4
bassemaed/HackerRank_TestDome_Practise
/NestedList_py/main.py
693
3.671875
4
if __name__ == '__main__': mylist = [[]] name_list = [] score_list = [] Result_list= [] min2_value = 0 for _ in range(int(input())): name = input() score = float(input()) name_list.append(name) score_list.append(score) for i in range(len(name_list)): m...
de69f62961f1fd8367a58a54f393dd332c11df24
zwakenberg/basictrack_2021_2a
/sessions/week_2/variables.py
361
3.90625
4
answer_to_life_the_universe_and_everything = 42 print(answer_to_life_the_universe_and_everything) print(type(answer_to_life_the_universe_and_everything)) hours = 75 half_days = hours // 12 remaining_hours = hours % 12 # so this is just for you dear reader print("In", hours, "hours there are", half_days, "half days a...
e52df03e3f3b899efa5542ede0c7d581e3c0cf1e
tgandrews/Project-Euler
/Problem 1/list results.py
230
4.09375
4
# All multiples of 3 or 5 less than 1000 a = 0; total = 0; while a < 1000: if (a % 3) == 0: total += a; print a; elif (a % 5) == 0: total += a; print a; a += 1; print "Total: ", total
c9c0a07bd2048b37593beefd2a381d5ffd793b51
rabbilyasar/bongo-test
/source/task_1.py
252
4.1875
4
def print_depth(data: dict, depth: int=1 ): """ print out depth of the key using recursive function """ for key in data: print(f"{key} {depth}") if isinstance(data[key], dict): print_depth(data[key], depth+1)
83250f58e0f368be3833af845f2ef5a634d99953
csehong/stanford-tensorflow-tutorials
/examples/01_basic.py
85
3.65625
4
def gen(): for i in range(10): yield i ** 3 for x in gen(): print(x)
46c6bf2ae41105e1df1a7046513853a045570d7a
stubhbw/py
/8.字典.py
1,814
3.640625
4
# encoding:utf-8 # 字典 # ----------------------------------------字典 print("----------------------------------------字典") # 字典是Python中唯一的映射类型 无序性 # ----字典 通过花括号,中间用逗号分隔元素定义 # ----{} 创建字典 dic = {0: 0, 1: 1, 2: 2} # 根据key取值 print(dic[0]) print(dic[1]) print(dic[2]) dic1 = {'name': 'hebw', 'age': 13, 'gender': 'male'} print(...
51a2181c94127e9249a8cef87e06d1d4ba04a369
stubhbw/py
/19.函数冗余参数.py
868
3.90625
4
# encoding:utf-8 # 函数冗余参数 # 接受的参数类似于一个元组 def f(x, y): print x, y # 这里有一个元组,如何把元组直接传入方法内.分别当做x,y t = (1, 2) # 这里使用这个方法来直接传入一个元组 利用一个*号 # 传递字典则使用**号 f(*t) # 占位符的作用 def p(x, y): print("%s : %s" % (x, y)) p(1, 2) p(*t) def fa(name="hebw", age=0): print('name:%s' % name + ' age:%s' % age) fa(**{'a...
a5c09d65fce5a0c663be9ac684b6fd10f82ef6c5
cacraig/Structs-and-Algorithms
/linkedList/singlyLinkedList.py
1,432
4.0625
4
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None def addNode(self, data): node = Node(data) # list is empty, set head to new node. if self.head == None: self.head = node # if li...
f3cbc5f066ef73ab052df54cc39c16cdaceab545
yiming1012/MyLeetCode
/LeetCode/回溯法/377. 组合总和 Ⅳ.py
2,145
4.0625
4
""" 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数。 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) 请注意,顺序不同的序列被视作不同的组合。 因此输出为 7。 进阶: 如果给定的数组中含有负数会怎么样? 问题会产生什么变化? 我们需要在题目中添加什么限制来允许负数的出现? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/combination-sum-iv 著作权归领...
86b668b54a98b3e401a4b1ba806805e1645b731a
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/背包问题/494. 目标和.py
2,241
3.734375
4
""" 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。 返回可以使最终数组和为目标数 S 的所有添加符号的方法数。   示例: 输入:nums: [1, 1, 1, 1, 1], S: 3 输出:5 解释: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 一共有5种方法让最终目标和为3。   提示: 数组非空,且长度不会超过 20 。 初始的数组的和不会超过 1000 。 保证返回的最终...
1a1900b511a71300ca52e9c7ec1ed1f4fb58d5da
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/55. Jump Game.py
2,772
4.0625
4
''' Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index...
11d75abef6172c9716b6df39be70579ee904923f
yiming1012/MyLeetCode
/LeetCode/哈希表(hash table)/460. LFU Cache.py
3,503
4.09375
4
''' Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not alread...
72a312b42e74e50cedb79a45e844c296a06a5895
yiming1012/MyLeetCode
/LeetCode/图/二分图/785. 判断二分图.py
2,233
3.984375
4
""" 给定一个无向图graph,当这个图为二分图时返回true。 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。 graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值。 示例 1: 输入: [[1,3], [0,2], [1,3], [0,2]] 输出: true 解释: 无向图如下: 0----1 | | | | 3----2 我们可以将...
db57c012d810f2b6a9880e6e2e594f5491c374e9
yiming1012/MyLeetCode
/LeetCode/前缀和/5663. 找出第 K 大的异或坐标值.py
1,653
3.75
4
""" 5663. 找出第 K 大的异或坐标值 给你一个二维矩阵 matrix 和一个整数 k ,矩阵大小为 m x n 由非负整数组成。 矩阵中坐标 (a, b) 的 值 可由对所有满足 0 <= i <= a < m 且 0 <= j <= b < n 的元素 matrix[i][j](下标从 0 开始计数)执行异或运算得到。 请你找出 matrix 的所有坐标中第 k 大的值(k 的值从 1 开始计数)。 示例 1: 输入:matrix = [[5,2],[1,6]], k = 1 输出:7 解释:坐标 (0,1) 的值是 5 XOR 2 = 7 ,为最大的值。 示例 2: 输入:matrix = [[5,2],...
ecca424511b40e9c0b83a148443fc20fac7b88b1
yiming1012/MyLeetCode
/LeetCode/链表(Linked list)/25. Reverse Nodes in k-Group.py
1,840
3.953125
4
""" Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. Example: Given this linked lis...
a6ec95e28eb193c0c4c1094a4a6d05414c14cd67
yiming1012/MyLeetCode
/LeetCode/前缀和/1343. 大小为 K 且平均值大于等于阈值的子数组数目.py
1,642
3.703125
4
""" 1343. 大小为 K 且平均值大于等于阈值的子数组数目 给你一个整数数组 arr 和两个整数 k 和 threshold 。 请你返回长度为 k 且平均值大于等于 threshold 的子数组数目。 示例 1: 输入:arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 输出:3 解释:子数组 [2,5,5],[5,5,5] 和 [5,5,8] 的平均值分别为 4,5 和 6 。其他长度为 3 的子数组的平均值都小于 4 (threshold 的值)。 示例 2: 输入:arr = [1,1,1,1,1], k = 1, threshold = 0 输出:5 示例 3: ...
2b500549b659076200275c95308e3acce613f7a7
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/区间DP/1216. 验证回文字符串 III.py
1,600
3.828125
4
""" 1216. 验证回文字符串 III 给出一个字符串 s 和一个整数 k,请你帮忙判断这个字符串是不是一个「K 回文」。 所谓「K 回文」:如果可以通过从字符串中删去最多 k 个字符将其转换为回文,那么这个字符串就是一个「K 回文」。   示例: 输入:s = "abcdeca", k = 2 输出:true 解释:删除字符 “b” 和 “e”。   提示: 1 <= s.length <= 1000 s 中只含有小写英文字母 1 <= k <= s.length 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/valid-palindrome-iii 著作...
da98bf8cb73b895c3c47a0d4a93c510fd9ca63f9
yiming1012/MyLeetCode
/LeetCode/字符串/336. 回文对.py
2,792
3.9375
4
""" 给定一组 互不相同 的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。   示例 1: 输入:["abcd","dcba","lls","s","sssll"] 输出:[[0,1],[1,0],[3,2],[2,4]] 解释:可拼接成的回文串为 ["dcbaabcd","abcddcba","slls","llssssll"] 示例 2: 输入:["bat","tab","cat"] 输出:[[0,1],[1,0]] 解释:可拼接成的回文串为 ["battab","tabbat"] 来源:力扣(LeetCode) 链接:https://l...
4574e00ac057eb98b37e11e54871e868256fbb44
yiming1012/MyLeetCode
/LeetCode/945. Minimum Increment to Make Array Unique.py
2,936
4.15625
4
''' Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return the least number of moves to make every value in A unique. Example 1: Input: [1,2,2] Output: 1 Explanation: After 1 move, the array could be [1, 2, 3]. Example 2: Input: [3,2,1,2,1,7] Output: 6 Explanation: ...
873545a96d7d3d1e55009ad7374c71046dd354cb
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/区间DP/1771. 由子序列构造的最长回文串的长度.py
2,187
3.9375
4
""" 1771. 由子序列构造的最长回文串的长度 给你两个字符串 word1 和 word2 ,请你按下述方法构造一个字符串: 从 word1 中选出某个 非空 子序列 subsequence1 。 从 word2 中选出某个 非空 子序列 subsequence2 。 连接两个子序列 subsequence1 + subsequence2 ,得到字符串。 返回可按上述方法构造的最长 回文串 的 长度 。如果无法构造回文串,返回 0 。 字符串 s 的一个 子序列 是通过从 s 中删除一些(也可能不删除)字符而不更改其余字符的顺序生成的字符串。 回文串 是正着读和反着读结果一致的字符串。 示例 1: 输入:word1...
6dffaff53e06c5841bab603bad27ad61c944a91a
yiming1012/MyLeetCode
/LeetCode/哈希表(hash table)/1363. 形成三的最大倍数.py
2,665
3.921875
4
""" 1363. 形成三的最大倍数 给你一个整数数组 digits,你可以通过按任意顺序连接其中某些数字来形成 3 的倍数,请你返回所能得到的最大的 3 的倍数。 由于答案可能不在整数数据类型范围内,请以字符串形式返回答案。 如果无法得到答案,请返回一个空字符串。 示例 1: 输入:digits = [8,1,9] 输出:"981" 示例 2: 输入:digits = [8,6,7,1,0] 输出:"8760" 示例 3: 输入:digits = [1] 输出:"" 示例 4: 输入:digits = [0,0,0,0,0,0] 输出:"0" 提示: 1 <= digits.length <= 10^4 0...
fe1ad99236c3559ed61ae0f8824b94d7e2e11e81
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/区间DP/131. 分割回文串.py
1,357
3.546875
4
""" 131. 分割回文串 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。 回文串 是正着读和反着读都一样的字符串。   示例 1: 输入:s = "aab" 输出:[["a","a","b"],["aa","b"]] 示例 2: 输入:s = "a" 输出:[["a"]]   提示: 1 <= s.length <= 16 s 仅由小写英文字母组成 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/palindrome-partitioning 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请...
aff9b6d4d89d9c4c233c4804b450250508f3b1b5
yiming1012/MyLeetCode
/LeetCode/数学/5727. 找出游戏的获胜者.py
2,198
3.703125
4
""" 5727. 找出游戏的获胜者 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1) 名小伙伴的位置,其中 1 <= i < n ,从第 n 名小伙伴顺时针移动一位会回到第 1 名小伙伴的位置。 游戏遵循如下规则: 从第 1 名小伙伴所在位置 开始 。 沿着顺时针方向数 k 名小伙伴,计数时需要 包含 起始时的那位小伙伴。逐个绕圈进行计数,一些小伙伴可能会被数过不止一次。 你数到的最后一名小伙伴需要离开圈子,并视作输掉游戏。 如果圈子中仍然有不止一名小伙伴,从刚刚输掉的小伙伴的 顺时针下一位 小伙伴 开始,回到步骤 2 继续执行...
fc78729277f8dc3710f0510c43fa25cf95333148
yiming1012/MyLeetCode
/LeetCode/二分查找(Binary Search)/1520. 旋转数组的最小数字.py
1,100
4.375
4
''' 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。   示例 1: 输入:[3,4,5,1,2] 输出:1 示例 2: 输入:[2,2,2,0,1] 输出:0 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 ''' from typing im...
39fe09af6f969c237d52d1f6754b6126bce85df5
yiming1012/MyLeetCode
/LeetCode/回溯法/294. 翻转游戏 II.py
1,406
3.78125
4
""" 294. 翻转游戏 II 你和朋友玩一个叫做「翻转游戏」的游戏。游戏规则如下: 给你一个字符串 currentState ,其中只含 '+' 和 '-' 。你和朋友轮流将 连续 的两个 "++" 反转成 "--" 。当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。 请你写出一个函数来判定起始玩家 是否存在必胜的方案 :如果存在,返回 true ;否则,返回 false 。   示例 1: 输入:currentState = "++++" 输出:true 解释:起始玩家可将中间的 "++" 翻转变为 "+--+" 从而得胜。 示例 2: 输入:currentState = "+" 输出:false   提...
e8b04fc3968b9562389d7d0341e7e0343850e0ec
yiming1012/MyLeetCode
/LeetCode/字符串/921. 使括号有效的最少添加.py
1,597
3.96875
4
""" 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效。 从形式上讲,只有满足下面几点之一,括号字符串才是有效的: 它是一个空字符串,或者 它可以被写成 AB (A 与 B 连接), 其中 A 和 B 都是有效字符串,或者 它可以被写作 (A),其中 A 是有效字符串。 给定一个括号字符串,返回为使结果字符串有效而必须添加的最少括号数。   示例 1: 输入:"())" 输出:1 示例 2: 输入:"(((" 输出:3 示例 3: 输入:"()" 输出:0 示例 4: 输入:"()))((" 输出:4   提示: S...
a41d57246de65ab382163d95a7cf3842b19835a4
yiming1012/MyLeetCode
/LeetCode/广度优先搜索(BFS)/1197. 进击的骑士.py
1,789
3.5625
4
""" 1197. 进击的骑士 一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里。 骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右)走 1 格,再向上(或下)走 2 格;或先向左(或右)走 2 格,再向上(或下)走 1 格。 每次移动,他都可以按图示八个方向之一前进。 现在,骑士需要前去征服坐标为 [x, y] 的部落,请你为他规划路线。 最后返回所需的最小移动次数即可。本题确保答案是一定存在的。 示例 1: 输入:x = 2, y = 1 输出:1 解释:[0, 0] → [2, 1] 示例 2: 输入:x ...
425e4b0b16cc4792fd4559dd55a4084a4fb09929
yiming1012/MyLeetCode
/LeetCode/链表(Linked list)/445. Add Two Numbers II.py
3,479
4
4
''' You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Fo...
38f0bc344dbbc8d4b1c2ed092e6debfa48bf6cfd
yiming1012/MyLeetCode
/LeetCode/二分查找(Binary Search)/1283. 使结果不超过阈值的最小除数.py
1,775
3.84375
4
""" 1283. 使结果不超过阈值的最小除数 给你一个整数数组 nums 和一个正整数 threshold ,你需要选择一个正整数作为除数,然后将数组里每个数都除以它,并对除法结果求和。 请你找出能够使上述结果小于等于阈值 threshold 的除数中 最小 的那个。 每个数除以除数后都向上取整,比方说 7/3 = 3 , 10/2 = 5 。 题目保证一定有解。 示例 1: 输入:nums = [1,2,5,9], threshold = 6 输出:5 解释:如果除数为 1 ,我们可以得到和为 17 (1+2+5+9)。 如果除数为 4 ,我们可以得到和为 7 (1+1+2+3) 。如果除数为 5 ,和为 5 (...
fc826f3942a48e40b3d6d601724478cb1cdc057e
yiming1012/MyLeetCode
/LeetCode/双指针(two points)/287. Find the Duplicate Number.py
3,490
4.09375
4
''' Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You ...
f33fbc0b1d1fd011237bce2d5dd12f04185f5c2d
yiming1012/MyLeetCode
/LeetCode/1143. Longest Common Subsequence.py
6,298
3.890625
4
''' Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" wh...
59ab5996ac66852d12bb8d7cfb23f93a42c83c95
yiming1012/MyLeetCode
/LeetCode/数学/1342. 将数字变成 0 的操作次数.py
1,169
3.875
4
""" 1342. 将数字变成 0 的操作次数 给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。 示例 1: 输入:num = 14 输出:6 解释: 步骤 1) 14 是偶数,除以 2 得到 7 。 步骤 2) 7 是奇数,减 1 得到 6 。 步骤 3) 6 是偶数,除以 2 得到 3 。 步骤 4) 3 是奇数,减 1 得到 2 。 步骤 5) 2 是偶数,除以 2 得到 1 。 步骤 6) 1 是奇数,减 1 得到 0 。 示例 2: 输入:num = 8 输出:4 解释: 步骤 1) 8 是偶数,除以 2 得到 4 。 步骤 2) 4 ...
5f2f78b0e044b48c244a1a20399094ce27ea0c89
yiming1012/MyLeetCode
/LeetCode/数学/1362. 最接近的因数.py
1,204
3.6875
4
""" 1362. 最接近的因数 给你一个整数 num,请你找出同时满足下面全部要求的两个整数: 两数乘积等于 num + 1 或 num + 2 以绝对差进行度量,两数大小最接近 你可以按任意顺序返回这两个整数。 示例 1: 输入:num = 8 输出:[3,3] 解释:对于 num + 1 = 9,最接近的两个因数是 3 & 3;对于 num + 2 = 10, 最接近的两个因数是 2 & 5,因此返回 3 & 3 。 示例 2: 输入:num = 123 输出:[5,25] 示例 3: 输入:num = 999 输出:[40,25] 提示: 1 <= num <= 10^9 """ from math i...
fe4cf7bf7763087581fab85af857fc64fd4272ab
yiming1012/MyLeetCode
/LeetCode/贪心算法/5641. 卡车上的最大单元数.py
2,124
3.953125
4
""" 5641. 卡车上的最大单元数 请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi] : numberOfBoxesi 是类型 i 的箱子的数量。 numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。 整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。 返回卡车可以装载 单元 的 最大 总数。   示例 1: 输入:boxTypes = [[1,3],[2,2],[3...
486374efe873f3f185dc8c7b22da2476ae10c7e1
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/状态压缩DP/5639. 完成所有工作的最短时间.py
2,236
3.671875
4
""" 5639. 完成所有工作的最短时间 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间。 请你将这些工作分配给 k 位工人。所有工作都应该分配给工人,且每项工作只能分配给一位工人。工人的 工作时间 是完成分配给他们的所有工作花费时间的总和。请你设计一套最佳的工作分配方案,使工人的 最大工作时间 得以 最小化 。 返回分配方案中尽可能 最小 的 最大工作时间 。   示例 1: 输入:jobs = [3,2,3], k = 3 输出:3 解释:给每位工人分配一项工作,最大工作时间是 3 。 示例 2: 输入:jobs = [1,2,4,7,8], k = 2 输出:11 解释:按下...
77be81f485de3cc749b82d5d23332a6b7ee748df
yiming1012/MyLeetCode
/LeetCode/1543. 和为s的连续正数序列.py
4,844
3.578125
4
''' 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。 序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。   示例 1: 输入:target = 9 输出:[[2,3,4],[4,5]] 示例 2: 输入:target = 15 输出:[[1,2,3,4,5],[4,5,6],[7,8]]   限制: 1 <= target <= 10^5 ''' from typing import List class Solution: def findContinuousSequence(self, target: int) -> List[List[...
b03e1d2307aea17ad0e233e14713c6e283ed2919
yiming1012/MyLeetCode
/LeetCode/数组/2. 早餐组合.py
2,642
3.546875
4
""" 小扣在秋日市集选择了一家早餐摊位,一维整型数组 staple 中记录了每种主食的价格,一维整型数组 drinks 中记录了每种饮料的价格。小扣的计划选择一份主食和一款饮料,且花费不超过 x 元。请返回小扣共有多少种购买方案。 注意:答案需要以 1e9 + 7 (1000000007) 为底取模,如:计算初始结果为:1000000008,请返回 1 示例 1: 输入:staple = [10,20,5], drinks = [5,5,2], x = 15 输出:6 解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是: 第 1 种方案:staple[0] + drinks[0] = 10 + 5 ...
7d78aea2401763f0477033aaf32cbc50269a4d95
yiming1012/MyLeetCode
/LeetCode/二分查找(Binary Search)/最小值最大值/5764. 准时到达的列车最小时速.py
2,821
3.546875
4
""" 5764. 准时到达的列车最小时速 给你一个浮点数 hour ,表示你到达办公室可用的总通勤时间。要到达办公室,你必须按给定次序乘坐 n 趟列车。另给你一个长度为 n 的整数数组 dist ,其中 dist[i] 表示第 i 趟列车的行驶距离(单位是千米)。 每趟列车均只能在整点发车,所以你可能需要在两趟列车之间等待一段时间。 例如,第 1 趟列车需要 1.5 小时,那你必须再等待 0.5 小时,搭乘在第 2 小时发车的第 2 趟列车。 返回能满足你准时到达办公室所要求全部列车的 最小正整数 时速(单位:千米每小时),如果无法准时到达,则返回 -1 。 生成的测试用例保证答案不超过 107 ,且 hour 的 小数点后...
a6cc73bb75f629d2f502512d4159361c6cb1d926
yiming1012/MyLeetCode
/LeetCode/字符串/1404. Number of Steps to Reduce a Number in Binary Representation to One.py
3,253
3.734375
4
''' Given a number s in their binary representation. Return the number of steps to reduce it to 1 under the following rules: If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it. It's guaranteed that you can always reach to one for all testcases.   Exampl...
9cecb0d1586f40ba519cd03f1fbac312056b87bd
yiming1012/MyLeetCode
/LeetCode/回溯法/60. 第k个排列.py
1,249
3.65625
4
""" 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132" "213" "231" "312" "321" 给定 n 和 k,返回第 k 个排列。 说明: 给定 n 的范围是 [1, 9]。 给定 k 的范围是[1,  n!]。 示例 1: 输入: n = 3, k = 3 输出: "213" 示例 2: 输入: n = 4, k = 9 输出: "2314" 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/permutation-sequence ...
0da74966eb6f0978a5e9d2a529d43f64c388c38c
yiming1012/MyLeetCode
/LeetCode/深度优先搜索(dfs)/岛屿问题/695. Max Area of Island.py
2,497
4.15625
4
''' Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area i...
b66a8000d8f9fc2bc3001d5c73a706ee3bae1956
yiming1012/MyLeetCode
/LeetCode/链表(Linked list)/138. 复制带随机指针的链表.py
2,722
3.703125
4
""" 138. 复制带随机指针的链表 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的 深拷贝。  我们用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示: val:一个表示 Node.val 的整数。 random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。   示例 1: 输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]] 输出:[[7,null],[13,0],[11,4],[10,...
5316d01c6100900566f9437b6c212e81d4fab0b5
yiming1012/MyLeetCode
/LeetCode/二分查找(Binary Search)/4. Median of Two Sorted Arrays.py
3,547
4.21875
4
""" There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] num...
0b79a77403df58800759be9673c4be1d7a4e4b55
yiming1012/MyLeetCode
/LeetCode/最短路径/5548. 最小体力消耗路径.py
2,533
4.0625
4
""" 你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度。一开始你在最左上角的格子 (0, 0) ,且你希望去最右下角的格子 (rows-1, columns-1) (注意下标从 0 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。 一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。 请你返回从左上角走到右下角的最小 体力消耗值 。   示例 1: 输入:heights = [[1,2,2],[3,8,2],[5,3...
18c7324540255df221bea1030a256069f6f0efe9
yiming1012/MyLeetCode
/LeetCode/树(Binary Tree)/1273. 删除树节点.py
2,057
3.578125
4
""" 1273. 删除树节点 给你一棵以节点 0 为根节点的树,定义如下: 节点的总数为 nodes 个; 第 i 个节点的值为 value[i] ; 第 i 个节点的父节点是 parent[i] 。 请你删除节点值之和为 0 的每一棵子树。 在完成所有删除之后,返回树中剩余节点的数目。 示例 1: 输入:nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1] 输出:2 示例 2: 输入:nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-2] 输出:6 示例 3:...
7b40800f926ab1a71b391759f7b669b060b38d53
yiming1012/MyLeetCode
/LeetCode/树(Binary Tree)/5418. 二叉树中的伪回文路径.py
3,167
4
4
""" 给你一棵二叉树,每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「伪回文」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列。 请你返回从根到叶子节点的所有路径中 伪回文 路径的数目。   示例 1: 输入:root = [2,3,1,3,1,null,1] 输出:2 解释:上图为给定的二叉树。总共有 3 条从根到叶子的路径:红色路径 [2,3,3] ,绿色路径 [2,1,1] 和路径 [2,3,1] 。 在这些路径中,只有红色和绿色的路径是伪回文路径,因为红色路径 [2,3,3] 存在回文排列 [3,2,3] ,绿色路径 [2,1,1] 存在回文排列 [1,2,1] 。 示例 2:...
39f2a05c3c968ba548b364c486605d5a4d727d61
yiming1012/MyLeetCode
/LeetCode/十叉树/440. 字典序的第K小数字.py
1,375
3.625
4
""" 440. 字典序的第K小数字 给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字。 注意:1 ≤ k ≤ n ≤ 109。 示例 : 输入: n: 13 k: 2 输出: 10 解释: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/k-th-smallest-in-lexicographical-order 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ class Solu...
752a978f5f3ede2ae04b6de25e2f576d75000ae9
yiming1012/MyLeetCode
/LeetCode/树(Binary Tree)/110. 平衡二叉树.py
2,213
4.15625
4
""" 给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true 。 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / \ 2 2 / \ 3 3 / \ 4 4 返回 false 。 来源:力扣(LeetCode) 链接:https://leetcode-cn....
e25812819dce5f85098b444e39b2697617abeaa5
yiming1012/MyLeetCode
/LeetCode/5388. 重新格式化字符串.py
1,889
3.703125
4
''' 给你一个混合了数字和字母的字符串 s,其中的字母均为小写英文字母。 请你将该字符串重新格式化,使得任意两个相邻字符的类型都不同。也就是说,字母后面应该跟着数字,而数字后面应该跟着字母。 请你返回 重新格式化后 的字符串;如果无法按要求重新格式化,则返回一个 空字符串 。 示例 1: 输入:s = "a0b1c2" 输出:"0a1b2c" 解释:"0a1b2c" 中任意两个相邻字符的类型都不同。 "a0b1c2", "0a1b2c", "0c2a1b" 也是满足题目要求的答案。 示例 2: 输入:s = "leetcode" 输出:"" 解释:"leetcode" 中只有字母,所以无法满足重新格式化的条件。 示例...
334858b597652d7b452b5dc484ccbc4b5040375d
yiming1012/MyLeetCode
/LeetCode/哈希表(hash table)/523. Continuous Subarray Sum.py
1,596
4.1875
4
""" Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer.   Example 1: Input: [23, 2, 4, 6, 7], k=6 Output: True Explanation: Because [2, 4]...
5acbfc6e1d964823ca3e76c46a1a18ccb87c5c94
yiming1012/MyLeetCode
/LeetCode/设计/381. O(1) 时间插入、删除和获取随机元素 - 允许重复.py
3,072
4.0625
4
""" 设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。 注意: 允许出现重复元素。 insert(val):向集合中插入元素 val。 remove(val):当 val 存在时,从集合中移除一个 val。 getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。 示例: // 初始化一个空的集合。 RandomizedCollection collection = new RandomizedCollection(); // 向集合中插入 1 。返回 true 表示集合不包含 1 。 collection.insert(1); // 向集合中插入另...
237b086b5de1da385f7fe8968cfcdbd27c924e38
yiming1012/MyLeetCode
/LeetCode/字符串/179. 最大数.py
1,504
4.03125
4
""" 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/largest-number 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ from functools import cmp_to_key from typing import List class LargerNumKey(str):...
e2eeb30df07c5830629bfabc534c02f2db1c2463
yiming1012/MyLeetCode
/LeetCode/贪心算法/252. 会议室.py
973
3.875
4
""" 252. 会议室 给定一个会议时间安排的数组 intervals ,每个会议时间都会包括开始和结束的时间 intervals[i] = [starti, endi] ,请你判断一个人是否能够参加这里面的全部会议。   示例 1: 输入:intervals = [[0,30],[5,10],[15,20]] 输出:false 示例 2: 输入:intervals = [[7,10],[2,4]] 输出:true   提示: 0 <= intervals.length <= 104 intervals[i].length == 2 0 <= starti < endi <= 106 来源:力扣(LeetCode) ...
d0bb2596cb14f35ac3562d2e9b4a3708c37e844e
yiming1012/MyLeetCode
/LeetCode/1418. Rotate Matrix LCCI.py
1,881
4.125
4
''' Given an image represented by an N x N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?   Example 1: Given matrix = [ [1,2,3], [4,5,6], [7,8,9] ], Rotate the matrix in place. It becomes: [ [7,4,1], [8,5,2], [9,6,3] ] Exampl...
5896922a1d8f3a5abe8ea9100828525870658ee4
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/最长公共子序列/1092. 最短公共超序列.py
2,008
3.59375
4
""" 1092. 最短公共超序列 给出两个字符串 str1 和 str2,返回同时以 str1 和 str2 作为子序列的最短字符串。如果答案不止一个,则可以返回满足条件的任意一个答案。 (如果从字符串 T 中删除一些字符(也可能不删除,并且选出的这些字符可以位于 T 中的 任意位置),可以得到字符串 S,那么 S 就是 T 的子序列) 示例: 输入:str1 = "abac", str2 = "cab" 输出:"cabac" 解释: str1 = "abac" 是 "cabac" 的一个子串,因为我们可以删去 "cabac" 的第一个 "c"得到 "abac"。 str2 = "cab" 是 "cabac" 的一个子串...
f674850694b817e809eb5dd8c071993710cb7d1b
yiming1012/MyLeetCode
/LeetCode/字符串/459. 重复的子字符串.py
3,663
3.703125
4
""" 459. 重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 示例 1: 输入: "abab" 输出: True 解释: 可由子字符串 "ab" 重复两次构成。 示例 2: 输入: "aba" 输出: False 示例 3: 输入: "abcabcabcabc" 输出: True 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/repeated-substring...
b3a6b50924cd556e3ec10064c0461ae0560221d8
yiming1012/MyLeetCode
/LeetCode/贪心算法/1288. 删除被覆盖区间.py
2,360
3.921875
4
""" 1288. 删除被覆盖区间 给你一个区间列表,请你删除列表中被其他区间所覆盖的区间。 只有当 c <= a 且 b <= d 时,我们才认为区间 [a,b) 被区间 [c,d) 覆盖。 在完成所有删除操作后,请你返回列表中剩余区间的数目。   示例: 输入:intervals = [[1,4],[3,6],[2,8]] 输出:2 解释:区间 [3,6] 被区间 [2,8] 覆盖,所以它被删除了。   提示:​​​​​​ 1 <= intervals.length <= 1000 0 <= intervals[i][0] < intervals[i][1] <= 10^5 对于所有的 i != j:interva...
60c22c6051a4ff15d4c522fc4e069655920902e7
yiming1012/MyLeetCode
/LeetCode/数学/204. 计数质数.py
1,501
4.15625
4
""" 204. 计数质数 统计所有小于非负整数 n 的质数的数量。   示例 1: 输入:n = 10 输出:4 解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 示例 2: 输入:n = 0 输出:0 示例 3: 输入:n = 1 输出:0   提示: 0 <= n <= 5 * 106 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/count-primes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ class Solution: def countPrimes1(self, n: ...
7aeab75712d471857ad5957641ac92491f06e878
yiming1012/MyLeetCode
/LeetCode/1389. Create Target Array in the Given Order.py
1,998
4.125
4
''' Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty. From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array. Repeat the previous step until there are no elements to read in nums...
4e1b9faa2b3b181aae8fa54d4a1a3d7ca5a161af
yiming1012/MyLeetCode
/LeetCode/石子游戏/1510. 石子游戏 IV.py
2,234
3.65625
4
""" Alice 和 Bob 两个人轮流玩一个游戏,Alice 先手。 一开始,有 n 个石子堆在一起。每个人轮流操作,正在操作的玩家可以从石子堆里拿走 任意 非零 平方数 个石子。 如果石子堆里没有石子了,则无法操作的玩家输掉游戏。 给你正整数 n ,且已知两个人都采取最优策略。如果 Alice 会赢得比赛,那么返回 True ,否则返回 False 。   示例 1: 输入:n = 1 输出:true 解释:Alice 拿走 1 个石子并赢得胜利,因为 Bob 无法进行任何操作。 示例 2: 输入:n = 2 输出:false 解释:Alice 只能拿走 1 个石子,然后 Bob 拿走最后一个石子并赢得胜利(2 ...
eea78b41d13fd2e6fe825e1fa64e8e2cceb5b260
yiming1012/MyLeetCode
/LeetCode/位运算/1386. 安排电影院座位.py
2,226
3.671875
4
""" 如上图所示,电影院的观影厅中有 n 行座位,行编号从 1 到 n ,且每一行内总共有 10 个座位,列编号从 1 到 10 。 给你数组 reservedSeats ,包含所有已经被预约了的座位。比如说,researvedSeats[i]=[3,8] ,它表示第 3 行第 8 个座位被预约了。 请你返回 最多能安排多少个 4 人家庭 。4 人家庭要占据 同一行内连续 的 4 个座位。隔着过道的座位(比方说 [3,3] 和 [3,4])不是连续的座位,但是如果你可以将 4 人家庭拆成过道两边各坐 2 人,这样子是允许的。 示例 1: 输入:n = 3, reservedSeats = [[1,2],[1,3]...
e47fa6f5c442114b3d8051bdf7514dd16b467deb
yiming1012/MyLeetCode
/LeetCode/会员题/259. 较小的三数之和.py
1,273
3.71875
4
""" 259. 较小的三数之和 给定一个长度为 n 的整数数组和一个目标值 target,寻找能够使条件 nums[i] + nums[j] + nums[k] < target 成立的三元组  i, j, k 个数(0 <= i < j < k < n)。 示例: 输入: nums = [-2,0,1,3], target = 2 输出: 2 解释: 因为一共有两个三元组满足累加和小于 2:   [-2,0,1] [-2,0,3] 进阶:是否能在 O(n2) 的时间复杂度内解决? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/3sum-smaller...
3328d3f925de69b49af515dab1ce2641309366b4
yiming1012/MyLeetCode
/LeetCode/并查集/1202. 交换字符串中的元素.py
2,299
3.5625
4
""" 1202. 交换字符串中的元素 给你一个字符串 s,以及该字符串中的一些「索引对」数组 pairs,其中 pairs[i] = [a, b] 表示字符串中的两个索引(编号从 0 开始)。 你可以 任意多次交换 在 pairs 中任意一对索引处的字符。 返回在经过若干次交换后,s 可以变成的按字典序最小的字符串。   示例 1: 输入:s = "dcab", pairs = [[0,3],[1,2]] 输出:"bacd" 解释: 交换 s[0] 和 s[3], s = "bcad" 交换 s[1] 和 s[2], s = "bacd" 示例 2: 输入:s = "dcab", pairs = [[0,3],[1,2...
4b0c5eb07fd4d075732e62357699570afa03bfdf
yiming1012/MyLeetCode
/LeetCode/差分法/1094. 拼车.py
2,267
3.546875
4
""" 假设你是一位顺风车司机,车上最初有 capacity 个空座位可以用来载客。由于道路的限制,车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向,你可以将其想象为一个向量)。 这儿有一份乘客行程计划表 trips[][],其中 trips[i] = [num_passengers, start_location, end_location] 包含了第 i 组乘客的行程信息: 必须接送的乘客数量; 乘客的上车地点; 以及乘客的下车地点。 这些给出的地点位置是从你的 初始 出发位置向前行驶到这些地点所需的距离(它们一定在你的行驶方向上)。 请你根据给出的行程计划表和车子的座位数,来判断你的车是否可以顺利完成接送所有乘客...
4e08bf394d5214d711f15933f38ee0e33c42e9d4
yiming1012/MyLeetCode
/LeetCode/排序算法(sort)/桶排序/164. 最大间距.py
1,717
3.90625
4
""" 164. 最大间距 给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。 如果数组元素个数小于 2,则返回 0。 示例 1: 输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。 示例 2: 输入: [10] 输出: 0 解释: 数组元素个数小于 2,因此返回 0。 说明: 你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内。 请尝试在线性时间复杂度和空间复杂度的条件下解决此问题。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problem...
e6c458c868901c4407bc4a08d802b97f56c5e7b7
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/1025. 除数博弈.py
1,720
3.65625
4
""" 爱丽丝和鲍勃一起玩游戏,他们轮流行动。爱丽丝先手开局。 最初,黑板上有一个数字 N 。在每个玩家的回合,玩家需要执行以下操作: 选出任一 x,满足 0 < x < N 且 N % x == 0 。 用 N - x 替换黑板上的数字 N 。 如果玩家无法执行这些操作,就会输掉游戏。 只有在爱丽丝在游戏中取得胜利时才返回 True,否则返回 false。假设两个玩家都以最佳状态参与游戏。   示例 1: 输入:2 输出:true 解释:爱丽丝选择 1,鲍勃无法进行操作。 示例 2: 输入:3 输出:false 解释:爱丽丝选择 1,鲍勃也选择 1,然后爱丽丝无法进行操作。   提示: 1 <= N <= 100...
2326c818ad99270e83deed40a38d03591d1f5268
yiming1012/MyLeetCode
/LeetCode/动态规划法(dp)/410. 分割数组的最大值.py
1,931
3.84375
4
""" 410. 分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。 注意: 数组长度 n 满足以下条件: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) 示例: 输入: nums = [7,2,5,10,8] m = 2 输出: 18 解释: 一共有四种方法将nums分割为2个子数组。 其中最好的方式是将其分为[7,2,5] 和 [10,8], 因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/probl...
bbad555af4db26ede90b10b374307aa9cf8a6750
yiming1012/MyLeetCode
/LeetCode/数组/5493. 删除最短的子数组使剩余数组有序.py
1,580
3.921875
4
""" 给你一个整数数组 arr ,请你删除一个子数组(可以为空),使得 arr 中剩下的元素是 非递减 的。 一个子数组指的是原数组中连续的一个子序列。 请你返回满足题目要求的最短子数组的长度。 示例 1: 输入:arr = [1,2,3,10,4,2,3,5] 输出:3 解释:我们需要删除的最短子数组是 [10,4,2] ,长度为 3 。剩余元素形成非递减数组 [1,2,3,3,5] 。 另一个正确的解为删除子数组 [3,10,4] 。 示例 2: 输入:arr = [5,4,3,2,1] 输出:4 解释:由于数组是严格递减的,我们只能保留一个元素。所以我们需要删除长度为 4 的子数组,要么删除 [5,4,3,2]...