
python
文章平均质量分 52
slbyzdgz
这个作者很懒,什么都没留下…
展开
-
LeetCode刷题记录
@[TOC](文章预览:)##两数之和给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例1输入:nums = [2,7,11,15], target = 9输出:[0,1]解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。解法:暴力解...原创 2021-12-10 18:02:59 · 261 阅读 · 0 评论 -
1到n!的和
sum = 0shu = int(input("请输入所求的一个数阶乘的累加和: "))for i in range(1, shu+1): s = 1 for j in range(2, i+1): s *= j sum += sprint("1!+2!+.....+%d!=%d" % (shu, sum))...原创 2019-10-29 18:46:20 · 407 阅读 · 0 评论 -
LeetCode---Max Consecutive Ones、 Fibonacci Number、Array Partition I、Reshape the Matrix
485.Max Consecutive OnesGiven a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last thr...原创 2019-07-02 21:42:11 · 172 阅读 · 0 评论 -
LeetCode---Rotate Array、Missing Number
189.Rotate ArrayGiven an array, rotate the array to the right byksteps, wherekis non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps...原创 2019-06-25 19:55:57 · 186 阅读 · 0 评论 -
LeetCode---Contains Duplicate
217.Contains DuplicateGiven an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return...原创 2019-06-25 19:21:38 · 168 阅读 · 0 评论 -
LeetCode---Merge Sorted Array
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may assume tha...原创 2019-06-21 21:48:05 · 118 阅读 · 0 评论 -
LeetCode---Plus One
Given anon-emptyarray of digitsrepresenting a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element...原创 2019-06-21 21:24:47 · 133 阅读 · 0 评论 -
LeetCode---Valid Sudoku
Determine if a9x9 Sudoku boardis valid.Only the filled cells need to be validatedaccording to the following rules:Each rowmust contain thedigits1-9without repetition. Each column must conta...原创 2019-06-17 19:39:17 · 101 阅读 · 0 评论 -
LeetCode---Pow(x, n)
Implementpow(x,n), which calculatesxraised to the powern(xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, ...原创 2019-06-20 16:04:10 · 142 阅读 · 0 评论 -
LeetCode---Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1-...原创 2019-05-31 21:47:33 · 138 阅读 · 0 评论 -
LeetCode---Roman to Integer
Roman numerals are represented by seven different symbols:I,V,X,L,C,DandM.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2019-05-31 21:22:59 · 167 阅读 · 0 评论 -
LeetCode---Pascal's Triangle
Given a non-negative integernumRows, generate the firstnumRowsof Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output:...原创 2019-06-23 21:43:19 · 167 阅读 · 0 评论 -
LeetCode---Pascal's Triangle II
Given a non-negativeindexkwherek≤33, return thekthindex row of the Pascal's triangle.Note that the row index starts from0.In Pascal's triangle, each number is the sum of the two numbers ...原创 2019-06-23 21:59:27 · 149 阅读 · 0 评论 -
LeetCode---Move Zeroes、Third Maximum Number、Find All Duplicates in an Array、Find All Numbers Disappe
283.Move ZeroesGiven an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,...原创 2019-06-26 21:50:47 · 141 阅读 · 0 评论 -
LeetCode--- Non-decreasing Array
665.Non-decreasing ArrayGiven an array withnintegers, your task is to check if it could become non-decreasing by modifyingat most1element.We define an array is non-decreasing ifarray[i] <...原创 2019-07-05 22:07:49 · 118 阅读 · 0 评论 -
LeetCode---Valid Mountain Array、Sort Array By Parity II
941.Valid Mountain ArrayGiven an arrayAof integers, returntrueif and only if it is avalid mountain array.Recall that A is a mountain array if and only if:A.length >= 3 There exists som...原创 2019-07-11 08:44:56 · 127 阅读 · 0 评论 -
LeetCode---Can Place Flowers、Shortest Unsorted Continuous Subarray、Maximum Product of Three Numbers
605.Can Place FlowersSuppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water a...原创 2019-07-04 22:46:59 · 124 阅读 · 0 评论 -
LeetCode--- Squares of a Sorted Array、Find Common Characters
977.Squares of a Sorted ArrayGiven an array of integersAsorted in non-decreasing order,return an array of the squares of each number,also in sorted non-decreasing order.Example 1:Input: [-...原创 2019-07-12 21:21:47 · 127 阅读 · 0 评论 -
LeetCode---Largest Number At Least Twice of Others、Toeplitz Matrix、Positions of Large Groups
747.Largest Number At Least Twice of OthersIn a given integer arraynums, there is always exactly one largest element.Find whether the largest element in the array is at least twice as much as ev...原创 2019-07-08 22:29:00 · 143 阅读 · 0 评论 -
LeetCode---Longest Continuous Increasing Subsequence、1-bit and 2-bit Characters、 Find Pivot Inde
674.Longest Continuous Increasing SubsequenceGiven an unsorted array of integers, find the length of longestcontinuousincreasing subsequence (subarray).Example 1:Input: [1,3,5,4,7]Output: 3...原创 2019-07-08 21:33:44 · 261 阅读 · 0 评论 -
LeetCode---Majority Element
169.Majority ElementGiven an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.You may assume that the array is non-empty and th...原创 2019-06-24 21:33:33 · 135 阅读 · 0 评论 -
LeetCode---Best Time to Buy and Sell Stock I和II
121.Best Time to Buy and Sell StockSay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (i.e., buy...原创 2019-06-24 20:43:17 · 138 阅读 · 0 评论 -
LeetCode---Integer to Roman
Example 1:Input: 3Output: "III"Example 2:Input: 4Output: "IV"Example 3:Input: 9Output: "IX"Example 4:Input: 58Output: "LVIII"Explanation: L = 50, V = 5, III = 3.Example 5:...原创 2019-05-28 21:12:13 · 148 阅读 · 0 评论 -
LeetCode---Longest Substring Without Repeating Characters
Given a string, find the length of thelongest substringwithout repeating characters.给定一个字符串,在不重复字符的情况下找出最长子字符串的长度Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with ...原创 2019-05-22 21:31:58 · 276 阅读 · 0 评论 -
LeetCode---Implement strStr()
ImplementstrStr().Return the index of the first occurrence of needle in haystack, or-1if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example ...原创 2019-05-26 21:07:50 · 116 阅读 · 0 评论 -
快速排序
快速排序是一种非常高效的排序算法,它采用“分而治之”的思想,把大的拆分成小的,小的再拆分为更小的。其原理是:对于一组给定的记录,通过一趟排序后,将原序列分为两部分,其中前部分的所有记录均比后部分的所有记录小,然后再依次对前后两部分的记录进行快速排序,递归该过程,直到序列中的所有记录均有序为止。def quick_sort(lists,left,right): if left>=...原创 2019-02-22 16:25:23 · 229 阅读 · 0 评论 -
归并排序
归并排序算法的原理如下:对于给定的一组记录(假设共有n个记录),首先将每两个相邻的长度为1的子序列进行归并,得到n/2(向上取整)个长度为2或1的有序子序列,再将其两两归并,反复执行此过程,直到得到一个有序序列为止。def merge(left,right): i,j = 0,0 result=[] while i<len(left) and j<len(...原创 2019-02-22 15:59:53 · 155 阅读 · 0 评论 -
冒泡排序
冒泡排序顾名思义就是整个过程像气泡一样往上升,单向冒泡排序的基本思想是(假设由小到大排序):对于给定的n个记录,从第一个记录开始依次对相邻的两个记录进行比较,当前面的记录大于后面的记录时时,交换其位置,进行第一轮比较和换位后,n个记录中的最大记录将位于第n位;然后对前(n-1)个记录进行第二轮比较;重复该过程直到进行比较的记录只剩下一个时为止。def bubble_sort(lists):...原创 2019-02-22 14:10:49 · 121 阅读 · 0 评论 -
插入排序---直接插入排序、折半插入排序、希尔排序
直接插入排序基本原理:对于给定的一组记录,初始时假设第一个记录自成一个有序序列,其余的记录为无序序列;接着从第二个记录开始,按照记录的大小依次将当前处理的记录插入到其之前的有序的序列中,直至最后一个记录插入到有序序列中为止。def insert_sort(lists): for i in range(1,len(lists)): key=lists[i] ...原创 2019-02-22 11:02:17 · 202 阅读 · 0 评论 -
选择排序
选择排序的基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录进行交换;接着对不包括第一个记录以外的其他记录进行第二轮比较,得到最小的记录并与第二个记录位置交换;重复该过程,直到进行比较的记录只有一个为止。def select_sort(lists): count = len(lists) for i in range(0,count): ...原创 2019-02-22 10:35:08 · 112 阅读 · 0 评论 -
Python os.listdir() 方法
os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。 它不包括 '.' 和'..' 即使它在文件夹中。只支持在 Unix, Windows 下使用。参数: listdir()方法语法格式如下:os.listdir(path) path--需要列出的目录路径返回值:返回指定路径下的文件和文件夹列表。import ospath = "./data...原创 2018-12-10 19:55:56 · 1452 阅读 · 0 评论 -
整数加法
题目描述请设计一个算法能够完成两个用字符串存储的整数进行相加操作,对非法的输入则返回error输入描述:输入为一行,包含两个字符串,字符串的长度在[1,100]。输出描述:输出为一行。合法情况输出相加结果,非法情况输出error示例1输入复制123 123abd 123输出复制246Errorm,n=input().strip()...原创 2018-12-05 21:52:23 · 254 阅读 · 1 评论 -
Python绘制简单的散点图
# 导入模板pyplot并指定别名pltimport matplotlib.pyplot as plt# 创建2个列表x_values = [2, 4, 6, 8, 10]y_values = [4, 16, 36, 9, 25]# 使用scatter()绘制散点plt.scatter(x_values, y_values, s=100)# 设置图表标title()题并给坐标轴...原创 2018-10-22 16:42:31 · 1112 阅读 · 0 评论 -
Python绘制简单的折线图
import csvimport mathimport numpy as np# 导入模板pyplot并指定别名plt,pyplot包含很多生成图表的函数import matplotlib.pyplot as plt# 创建2个列表input_values = [2, 4, 6, 8, 10]squares = [4, 16, 36, 9, 25]# 传输入值x和输出y值给s...原创 2018-10-22 16:04:14 · 592 阅读 · 0 评论 -
python编程练习
实现列表的逆序def reverse(list): for i in range(0,len(list)//2): temp = list[i] list[i] = list[-i-1] list[-i-1] = templ= [2,6,8,9,13,7]reverse(l)print(l)[7, 13, 9, 8, 6,...原创 2019-02-28 16:58:42 · 246 阅读 · 0 评论 -
基本数字运算---如何判断一个自然数是否是某个数的平方
如何判断一个自然数是否是某个数的平方:设计一个算法,判断给定的一个数n是否是某个数的平方,不能使用开方运算1 直接计算法def ispower(n): if n<=0: print(n+'不是自然数') return False i = 1 while i<n: m = i*i if m=...原创 2019-03-04 22:13:04 · 1449 阅读 · 0 评论 -
如何实现栈
实现一个栈的数据结构,使其具有以下方法:压栈、弹栈、取栈顶元素、判断栈是否为空以及获取栈中数据元素。数组实现在采用数组来实现栈的时候,栈空间是一段连续的空间。class MyStack: # 模拟栈 def __init__(self): self.items = [] # 判断栈是否为空 def isEmpty(self): ...原创 2019-03-07 21:58:10 · 224 阅读 · 0 评论 -
LeetCode---Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2019-05-25 21:26:29 · 131 阅读 · 0 评论 -
LeetCode---Remove Element
Given an arraynumsand a valueval, remove all instances of that valuein-placeand return the new length.Do not allocate extra space for another array, you must do this bymodifying the input arra...原创 2019-05-25 20:57:29 · 151 阅读 · 0 评论 -
LeetCode---Remove Duplicates from Sorted Array
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-05-25 20:02:08 · 112 阅读 · 0 评论