python刷 leetcode 题目(10)

【一】Leetcode之Python刷题之路 申请完了github帐号,又在Leetcode上发现了新大陆,从昨天开始刷题了,感觉很有意思。看视频学习Python动手不太多,现在开始刷题练习自己的代码水平,leetcode上还有很多大神,快哉美哉!   代码我都放到github里了https://github.com/Ray1225/Python_Training,欢迎大家关注并指正。话不多说,怒上代码。 1. Hamming Dista 阅读详情

206. 反转链表

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

代码如下:耗时56ms

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        cur = head 
        pre = None
        h = head
        list = []
        while cur:
            h = cur
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
            list.append(h.val)
        return list[::-1]

21. 合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

代码如下:耗时60ms

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
#         list = []
#         newHead = ListNode(0)
#         pre = newHead
#         while l1 and l2:
#             if l1.val < l2.val:
#                 pre.next = l1
#                 l1 = l1.next
#                 list.append(pre.next.val)
#             else:
#                 pre.next = l2
#                 l2 = l2.next
#                 list.append(pre.next.val)
#             pre = pre.next

#         if l1:
#             pre.next = l1
#             list.append(pre.next.val)
#         elif l2:
#             pre.next = l2
#             list.append(pre.next.val)
#         return list

        head = ListNode(0)  
        first = head  
        while l1 != None and l2 != None:  
            if l1.val > l2.val:  
                head.next = l2  
                l2 = l2.next  
            else :  
                head.next = l1  
                l1 = l1.next  
            head = head.next  
        if l1 == None:  
            head.next = l2  
        elif l2 == None:  
            head.next = l1  
        return first.next  
互相学习,互相指教
Leetcode top100题目及解答代码 (Python) 代码 代码放置在github中:https://github.com/isthegoal/leetcode_top125 使用了大量注释进行了题目 方案的解读。(类似形式如下) 题目 1 两数之和 42.20% Easy 2 两数相加 30.70% Medium 3 无重复字符的最长子串 28.00% Medium 4 寻找两个有序数组的中位数 25.60% ... 阅读详情

相关推荐

告别玄学调参:手把手教你用20元件模型搞定GaN HEMT高频仿真(附S参数提取避坑指南)

本文详细介绍了如何使用20元件模型优化GaN HEMT高频仿真,解决传统16元件模型在10GHz以上频段的误差问题。通过S参数提取避坑指南和实战案例,帮助工程师精准建模分布式电极效应和寄生反馈路径,提升射频电路设计效率。

weixin_42523693的博客 443

LeetCode第10题----正则表达式匹配

本题在LeetCode官方评定难度为Hard,本文将较为详细的阐述该题的解法和步骤。

F & F的博客 1267

多雷达协同探测技术研究进展:认知跟踪与资源调度算法

源自:雷达学报作者:易伟 袁野 刘光宏 葛建军 孔令讲 杨建宇雷达是信息感知的千里眼,具有极高的军用和民用价值,广泛应用在防空预警、遥感测绘、反恐维稳等领域[1–5]。雷达信息获取与探测技术也一直是大国竞相抢占的前沿技术,同时是信息科学的重要组成部分。随着科学技术不断进步,先进电子对抗装备的不断问世和广泛列装,雷达的生存与发展正面临日益严峻的挑战。从生存环境上看,雷达面临通信等各类民用电磁干扰以及复杂的军用电磁干扰;从探测对象上看,集群、微弱、隐身、高空、低空等多样化目标的出现也给雷达探测与识别带来困难。

renhongxia1的博客 2784

leetcode第10题python版正则匹配

class Solution: """ 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。'.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素。所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。 """ def isMatch(self, s: str, p: str) -> bool: #

前端帅小伙 677

基于DSP28335的Matlab/Simulink代码生成实验七:A/D采样

实验七:讲解如何使用DSP的ADC模块,通过代码生成技术控制DSP进行电流采样,是后续实现电机闭环控制的重要环节。通过简单的实验来熟悉代码生成的过程。最终实现复杂算法代码自动生成,进行高性能电机控制!

m0_73869369的博客 4725

2022-04-06 RK3566 上调试百兆以太网卡YT8512C 调试记录

一、原理图 二、dts &gmac1_clkin{ clock-frequency = <50000000>; status = "okay"; }; &gmac1 { phy-mode = "rmii"; clock_in_out = "input"; snps,reset-gpio = <&gpio4 RK_PC2 GPIO_ACTIVE_LOW>; snps,reset-active-low; sn...

海月汐辰 1万+

LeetCode-10

10. 正则表达式匹配 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。 ‘.’ 匹配任意单个字符 ‘*’ 匹配零个或多个前面的那一个元素 所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。 说明: s 可能为空,且只包含从 a-z 的小写字母。 p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。 不考虑 p 中会出现连续*出现 Example input s = "aa",p = "a"

扬弃 943

机械臂控制的基础知识

在现代工业中,机械臂的自动化控制已经成为智能制造的重要一环。控制机械臂涉及多个学科,包括机械设计、运动学、控制理论和编程等。本文将从以下几个方面介绍机械臂控制的核心知识点:机械臂的构成、运动学分析、动力学建模、控制方法及编程实现。

qq_45728381的博客 5470

LeetCode10

如果饿了就吃,困了就睡,渴了就喝,人生就太无趣了 题源:https://leetcode-cn.com/problems/regular-expression-matching/ 问题描述:正则表达式匹配 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。 '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配,是要涵盖整...

可耳(keer)的博客 524

自助结账商品检测(Scanned Item Detection)——基于YOLOv8的RPC数据集实战

RPC数据集是由中国科学院自动化研究所发布的零售商品结账检测公开数据集,广泛用于自助结账和零售场景下的目标检测与识别。YOLOv8是YOLO系列最新版本,由Ultralytics推出,继承了YOLO家族的高效快速和高准确性,适用于实时检测场景。本文系统介绍了基于YOLOv8的RPC数据集自助结账商品检测方案,涵盖数据准备、训练、推理及UI界面设计。未来,可结合多模态感知、RFID融合技术,打造更加智能和稳定的自助结账系统。

YOLO 252

【实战教程】BMAD-METHOD 入门教程:用 AI 组建你的“七人开发团队”

简单说,BMAD-METHOD 就像一个会自己运转的“AI 软件公司”。它不是某个单一应用,而是一种AI 工作流方法论—— 把一个项目拆解成多个角色(产品、架构、前后端、测试、运维等),每个角色由一个 AI 智能体负责协作,就像你真的在带一个 7 人团队。GitHub 项目地址 👉目前 Star 数已经达到了恐怖的 20k,可见这套方法的影响力不小。BMAD-METHOD 的核心理念不是“生成代码”,而是“自动化协作”。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1624

[每日一题]101:正则表达式匹配(动归、递归)

题目描述 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。 ‘.’ 匹配任意单个字符 ‘*’ 匹配零个或多个前面的那一个元素 所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。 说明: s 可能为空,且只包含从 a-z 的小写字母。 p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。 示例 1: 输入: s = "aa" p = "a" 输出: false 解释: "a" 无法匹配 "aa" 整个字符串。 示例 2: 输入

AngelDg的博客 506

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

10.24-10.28 leetcode基于python刷题

10.24-10.28 leetcode基于python刷题 20. Valid Parentheses(Easy) 3. Longest Substring Without Repeating Characters(Medium) 27. Remove Element(Easy) 16. 3Sum Closest(Medium) 58. Length of Last Word(Easy)5. Longest Palindromic Substring(Medium) 6. Zigzag Conversi

CR1820的博客 364

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

力扣学习day1

学习目标: 1.在2021年2月底刷完力扣10%题目。 2.从零开始学习C,C++,python。 3.用多种方法写出解题思路,以及理解代码。 学习内容: 1.两数之和 网址 题目:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 你可以按任意顺序返回答案。 思考:说真的,最开始的时候我还以为是真的就是a+b的入门级题目,然后看完题目后才知道想

写点有意思的博客 449

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

[C++]Leetcode超高效刷题顺序及题目详解笔记(持续更新中)

一.引言 博主最近在LeetCode上用C++练习。一路走来踩过一些坑,做了一些总结和笔记,分享给需要的人。 LeetCode网址:https://leetcode.com/ LeetCode中文版:https://leetcode-cn.com/ 二.刷题指南 LeetCode 是一个非常棒的平台,收集了许多公司的面试题目。相对其他平台而言,有着下面的几个优点: 题目全部来自业内大公司的真实面试不用处理输入输出 精力全放在解决具体问题上题目有丰富的讨论 可以参考别人的思路精确了解自己代码在所有提交代码中

cpb____的博客 2万+

【python力扣刷题经验分享】——从初入力扣到上分变强!!!

在力扣的异世界里不断升级!

2301_80269840的博客 6410

耗时3个月,近10W字的刷题笔记,多语言通吃,秒杀 80% Leetcode 题目!

学会了,offer收割机就是你~

Jack-Cui 1万+

【PythonCode】力扣Leetcode6~10题Python版

Leetcode6-10

weixin_43790276的博客 1859

[刷leetcode神器] IDEA 插件 leetcode editor

点击上方 Java学习之道,选择 设为星标每天18点,干货准时奉上!我们都知道,eclipse 已经被大多 Java 程序员抛弃,路转粉 IDEA 了。为什么会有这样子的趋势,除了 IDE...

Java学习之道 4万+

LeetCode刷题python详解

LeetCode刷题 最近想要练一练代码能力,得知LeetCode这个网站挺不错的,所以便开始刷题。 LeetCode中国网址 在这里主要就是练习一下python,其它编程语言可能基本不会涉及到。 从最简单的开始题——两数相加开始。 先附带一下题目: 首先是最简单也是最容易想到的解法: class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :

w0714的博客 1万+

刷力扣

刷力扣序言350. 两个数组的交集 II 序言 蓝桥杯的题目没有题解,力扣不仅有的题有官方题解而且还有大量用户的题解,所以就转战力扣。虽说力扣有题解,但毕竟不是我自己写的,我会把自己写的代码贴到这里,留作以后复习。为了适应蓝桥杯,Python环境是3.6.5,没有安装任何第三方库。 350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集。 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2,2] 示例 2: 输入:nums1 = [4,9,5

超小墨的博客 2373

leetcode(力扣)刷题笔记(c++、python)【上】

力扣刷题顺序以及用不同语言解答过程

qq_43050258的博客 3700

【PythonCode】力扣Leetcode26~30题Python版

力扣26-30

weixin_43790276的博客 2028

【PythonCode】力扣Leetcode46~50题Python版

力扣46-50

weixin_43790276的博客 1663

LeetCode刷题指南

你是否在刷LeetCode的时候不知道怎么正确的提交代码?不管怎样提交都不对,可是在自己的编译器上却是能运行成功并且答案正确,又或是你不知道刷题顺序,一来就倒在了第一题两数之和面前了。看来你和最初的我一样,也遇到了这样的问题,那么可以看看我的这篇文章能否帮助到你,有什么不对的地方敬请大家批评改正。

qq_63215420的博客 1万+
上一篇: python刷 leetcode 题目(9)
下一篇: python 刷 leetcode 题目(11)
凌疯墨子
博客等级 码龄11年 47粉丝 81原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值