辣鸡刘的Leetcode之旅7【Length of Last Word,Maximum Subarray,count-and-say,Plus One】

产业园区在推动科技成果转化过程中,如何实现企业与高校的精准对接?.docx 科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。 立即下载

58. Length of Last Word

题目描述:

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

这个很简单:

class Solution:
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        str = s.split()
        if len(str)==0:
            return 0
        else:
            return len(str[-1])

一行代码也可以:

def lengthOfLastWord(self, s):
    return len(s.rstrip(' ').split(' ')[-1])

## 53. Maximum Subarray ##```
题目描述:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.


动态规划,这个我不太会:

class Solution:
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        for i in range(1,len(nums)):
            if nums[i-1]>0:
                nums[i]+=nums[i-1]
        return max(nums)
        

注释:动态规划,汉诺塔之类的字眼,师兄师姐做笔试题成天遇到,貌似很重要,嘿嘿

## count-and-say ##

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 111221
    

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

题目稍微有点不容易理解,就是1读作1个1,11读作2个1既是:21;21读作1个2和1个1:1211,以此类推......
class Solution:
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s = '1'
        for _ in range(n - 1):
            s = ''.join(str(len(list(group))) + digit
                        for digit, group in itertools.groupby(s))
        return s

``

66. Plus One

题目:

Given a non-empty array of digits representing 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 in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

简单翻译:将给出的列表转为数字,然后加1,再转为列表;
重点注意str和int之间的转换;
我的代码如下:

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        if digits[0]==0 and len(digits)>1:
            digits=digits[1:len(digits)+1]
        num=int(''.join(str(i) for i in digits))
        num+=1
        nums=list(str(num))
        int_nums=[int(i) for i in nums]
        print(int_nums)
        return int_nums

s=Solution()
s.plusOne([0,1,2,3])

最快的是大神用指针做的,学习一下:

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        ans = []
        rem = 1
        
        while digits:
            val = digits.pop()
            temp = val + rem
            next = temp % 10
            rem = temp // 10
            ans.insert(0, next)
            
            if not digits:
                while rem:
                    temp = rem
                    next = temp % 10
                    rem = temp // 10
                    ans.insert(0, next)
                
        return ans
        
项目实战:用西门子S7协议开发基于PLC的上位机.pdf 立即下载

相关推荐

产业园区如何高效挖掘企业技术需求并促进精准匹配?.docx

产业园区如何高效挖掘企业技术需求并促进精准匹配?

You're given a string of lower-case Latin letters. Your task is to find the length of its longest su

C - You're Given a String...  代码:import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String[] args){  int count=1,k=0,max=0,i=0;  int[] sum=new int[100];  Sca...

L427630的博客 3369

该数据集收录了国内主要期货商品和股指指数的日频行情 商品期货覆盖有色、黑色、能化、农产品等板块,包含铜、螺纹钢、原油、豆粕等活跃合约 股指指数部分涵盖沪深300、上证50、中证500等品种 每个品种提

该数据集收录了国内主要期货商品和股指指数的日频行情。商品期货覆盖有色、黑色、能化、农产品等板块,包含铜、螺纹钢、原油、豆粕等活跃合约。股指指数部分涵盖沪深300、上证50、中证500等品种。每个品种提供统一字段:合约代码、交易日期、开盘价、最高价、最低价、收盘价、结算价、成交量和持仓量。数据按交易日顺次排列,未做复权处理,直接反映交易所原始发布信息。 数据源:CMES金融数据库

A. You're Given a String...

A. You're Given a String... time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You're given a string of lower-case Latin

sdjzqing的专栏 898

KernelSU.zip

KernelSU.zip

产业园区运营负责人如何精准匹配产业需求与科技成果?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind

题目:Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s ="aab", Return [     ["a

咖啡与唾液淀粉酶 1883

单相PWM整流器基于直流电流控制+虚拟dq两种闭环控制仿真【2个仿真+参考资料】

内容概要:本文介绍了单相PWM整流器的两种闭环控制策略仿真研究,即基于直流电流控制和虚拟dq变换的闭环控制方法。资源包含两个独立的Simulink仿真模型及相关参考资料,旨在对比和分析两种控制策略在动态响应、稳态精度及抗干扰能力等方面的性能差异。通过构建完整的主电路、控制回路与信号处理模块,展示了如何实现单位功率因数运行、直流侧电压稳定控制以及交流侧电流正弦化等关键目标,适用于电力电子变换器控制系统的学习与科研验证。; 适合人群:电气工程、自动化及相关专业的研究生、科研人员以及从事电力电子与新能源系统开发的工程师,具备一定的电路理论、自动控制原理和Simulink仿真基础者更佳。; 使用场景及目标:①深入理解单相PWM整流器的工作原理及其闭环控制机制;②掌握直流电流控制与虚拟dq控制两种方法的设计思路与实现步骤;③为相关课题的仿真建模、算法优化与论文写作提供参考依据和技术支持; 阅读建议:建议结合仿真模型与参考资料同步学习,重点分析控制器参数整定过程、坐标变换环节的实现方式,并尝试修改负载条件或输入扰动以测试系统鲁棒性,从而加深对控制策略本质的理解。

视频编码----H.264到H.265的转码

背景及意义 最新的高效视频编码 (HEVC) 标准相比H. 264高级视频编码标准实现了显著的压缩效率改进(50%),由于其优越的压缩性能, 它在许多应用中迅速被采用。 与H.264 标准相比,HEVC编码的计算复杂度非常高,使得难以在广泛用到的多媒体转码中的通用处理器上实现实时的高质量编码。 由于目前H.264广泛而深入的应用,大量现有的内容已经使用 H.264标准编码, 这时候如果...

lwj的小站 1万+

SPAD单光子雪崩二极管:从基础原理到前沿应用

本文深入解析SPAD单光子雪崩二极管的基础原理、硬件实现关键及核心性能参数,并探讨其在激光雷达、量子通信和生物医学成像等前沿领域的创新应用。SPAD凭借超高灵敏度和精确的时间分辨率,正在推动自动驾驶、量子技术和医学诊断等领域的革命性发展。

weixin_30335575的博客 664

峰岹FU6832L双核电机控制芯片实战:从FOC算法到BLDC/PMSM驱动开发

在嵌入式系统和电机控制领域,磁场定向控制(FOC)是实现无刷直流电机(BLDC)和永磁同步电机(PMSM)高效、平稳运行的核心算法。其原理是通过坐标变换将三相交流量分解为直流量,实现类似直流电机的转矩与磁场解耦控制,从而提升效率、降低噪音与振动。这项技术的工程价值在于能以更低的处理器开销实现高性能驱动,广泛应用于消费电子、工业自动化与智能家电。本文以峰岹FU6832L芯片为例,深入解析其独特的双核架构——将实时性要求极高的FOC运算交由专用硬件引擎处理,解放主控CPU处理应用逻辑,并结合PID参数整定、电流

weixin_29570795的博客 515

高速视觉伺服系统(一)

最近在学习相关知识,做个简要总结: 背景介绍 国内外研究现状 算法介绍 研究问题及实施方案 总结展望 背景介绍 研究表明,人类获得的80%以上的感官信息来自于视觉器官。借鉴同样的原理,借助视觉伺服系统,机器人可以广泛地用于掌握变化的外部环境。从而做出相应的反应。这就是伺服系统的背景。最典型的一个例子就是图中实况转播中乒乓球的高速追踪。 如果摄像头需要捕捉某一运动物体,常需要...

lwj的小站 7923

嵌入式学习(29)-ASM330LHH驱动程序

ASM330LHH是一种系统级封装器件,具有3D数字加速度计和3D数字陀螺仪,扩展温度范围上限为+105 °C,适用于汽车非安全领域的应用。ASM330LHH 的满量程加速范围为±2 g、±4 g、±8 g 和 ±16 g,角速度范围扩展至 ±125 dps、±250 dps、±500 dps、±1000 dps、± 2000 dps 和 ±4000 dps,适合各种汽车应用。

剑藏锋的博客 1176

高速视觉伺服系统(二)

接着上期:https://blog.youkuaiyun.com/power1_power2/article/details/80084544 算法对比 误差公式 D<W/16 : status A+: 成功追踪 W/16 <D<3W/16 : status A-: 可以追踪 D>W/16 : status B: 追踪失败 Invisible : status...

lwj的小站 2360

Kerberos基础及KDC服务(理论知识)

kerberos基础理论讲解+KDC服务

Siobhan_Mxin的博客 9715

EndNote导入文献出现带有大括号{}乱码的解决办法

进入word中的Endnote工具栏——Bibliography Preferences(位于Bibliography区块的右下角),就是那个输入杂志名的小三角形,在Temporary citation delimiters处修改变量的默认符号。将默认的大括号{}修改为文章中不回使用到的符号。原因:因为Endnote插入的参考文献在Word中的代码就是以大括号隔开的,当文章中出现了该符号,endnote就会自动比对Library中的书目,而此时endnote出现错误。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 4万+

辣鸡刘的Leetcode之旅2【最长公共前缀,有效的括号,合并链表,删除排序数组中的重复项】

Longest Common Prefix 2. 最长公共前缀 问题描述: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string ” “. 编写一个函数来查...

lwj的小站 372

Altium Designer系列: 软件系统参数配置保存

【Altium Designer实践】 软件系统参数配置保存 作者 CodeAllen ,转载请注明出处 目录设置中文菜单高亮模式设置文件关联pcb设置布线设置保存配置文件(复用) 设置中文菜单 DXP->Preferences 高亮模式设置 文件关联 pcb设置 布线设置 保存配置文件(复用) ......

CodeAllen嵌入式 2019

辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】

Add Binary 题目描述; Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "

lwj的小站 371

低轨通信 | 手机直连低轨卫星通信:架构、关键技术与未来展望

手机直连低轨卫星通信正推动人类进入"全域连接"时代。当中国星网GW星座与千帆星座的万颗卫星点亮夜空,当普通智能手机在珠峰之巅仍能流畅视频通话,当远洋渔船通过卫星物联网实现精准捕捞——这些场景不仅重构通信产业格局,更将深刻改变人类生产生活方式。正如工信部《指导意见》所规划,到2030年我国将发展超千万卫星通信用户,催生万亿级新市场[1][5]。在这场太空基建竞赛中,技术创新与标准制定的双重突破,将决定未来数字文明的星空版图。

尘世冰封的专栏 940

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

辣鸡刘的Leetcode之旅6【移除元素, 实现strStr() ,寻找插入的位置,最长无重复子串, 最长回文字符 】

27. Remove Element 题目描述: Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this ...

lwj的小站 359

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

辣鸡刘的Leetcode之旅3(SQL Part)【两表合并查询,第二高薪,挣钱比经理多, 重复值查询】

1. 两表合并查询(Combine Two Tables) 题目描述: Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | v...

lwj的小站 349

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

辣鸡刘的LeetCode之旅-1 [two Sum算法,反转整数算法,回文数,罗马数转整数]

从今天开始,辣鸡刘学习一下搞搞算法,确定了leetcode网站,从最简单的开始学习和思考。 入门算法, 1. Two Sum 描述: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that ea...

lwj的小站 325

辣鸡刘的Leetcode之旅4 (Weekly Contest 100)【单调序列,】

Monotonic Array 题目描述: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is mono...

lwj的小站 312
上一篇: 辣鸡刘的Leetcode之旅6【移除元素, 实现strStr() ,寻找插入的位置,最长无重复子串, 最长回文字符 】
下一篇: 辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】
powerlwj
博客等级 码龄12年 116粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值