【LeetCode算法题库】Day5:Roman to Integer Longest Common Prefix 3Sum
【Q13】
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: "III" Output: 3
Example 2:
Input: "IV" Output: 4
Example 3:
Input: "IX" Output: 9
Example 4:
Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 5:
Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 解法:从高位至低位倒序遍历,遇到遇到I/X/C时判断此时数值,若此时数值大于5/50/500,则对数值依次减去1/10/100,其余情况下加上对应数字即可。
class Solution:def romanToInt(self, s):""":type s: str:rtype: int"""op = 0for x in reversed(s):if x=='I':op += 1 if op<5 else -1elif x=='V':op += 5elif x=='X':op += 10 if op<50 else -10elif x=='L':op += 50elif x=='C':op += 100 if op<500 else -100elif x=='D':op += 500else:op += 1000return op
【Q14】
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 ""
.
Example 1:
Input: ["flower","flow","flight"] Output: "fl"
Example 2:
Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
解法:先找到最短的字符串,以此为基准。遍历整个字符串数组,取每个单词依次与该最短字符串比较。
class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs)==0:return ""shortestStr = min(strs,key=len) # find shortest string in the listfor i in range(len(shortestStr)):for s in strs:if s[i]!=shortestStr[i]:return shortestStr[:i]return shortestStr
【Q15】
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],A solution set is: [[-1, 0, 1],[-1, -1, 2] ]
解法:先固定一个数A,则任务变成寻找数组里的另外两个数,使得这两个数的和Target=0-A,此时问题变成2Sum问题。需要注意的是可能存在数组内有重复元素的问题,此时可通过while语句直接跳过重复元素。
class Solution:def threeSum(self, nums):""":type nums: List[int]:rtype: List[List[int]]"""nums.sort()N = len(nums)result = []for k in range(N):if k>0 and nums[k]==nums[k-1]:continuetarget = 0-nums[k]i,j = k+1,N-1while i<j:if nums[i]+nums[j]==target:result.append([nums[k],nums[i],nums[j]])i += 1j -= 1while i<j and nums[i]==nums[i-1]:i += 1elif nums[i]+nums[j]<target:i += 1else:j -= 1return result
转载于:https://www.cnblogs.com/YunyiGuang/p/10351374.html
【LeetCode算法题库】Day5:Roman to Integer Longest Common Prefix 3Sum相关推荐
- 【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- python【力扣LeetCode算法题库】136-只出现一次的数字
只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 ...
- python【力扣LeetCode算法题库】面试题 01.07- 旋转矩阵
面试题 01.07. 旋转矩阵 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节.请你设计一种算法,将图像旋转 90 度. 不占用额外内存空间能否做到? 示例 1: 给定 mat ...
- python【力扣LeetCode算法题库】289- 生命游戏
生命游戏 根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞都具有 ...
- python【力扣LeetCode算法题库】面试题 17.16- 按摩师(DP)
面试题 17.16. 按摩师 一个有名的按摩师会收到源源不断的预约请求,每个预约都可以选择接或不接.在每次预约服务之间要有休息时间,因此她不能接受相邻的预约.给定一个预约请求序列,替按摩师找到最优的预 ...
- python【力扣LeetCode算法题库】409-最长回文串(数学 计数器)
最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字 ...
- python【力扣LeetCode算法题库】1160-拼写单词
拼写单词 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们 ...
- python【力扣LeetCode算法题库】300 最长上升子序列(动态规划)
最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的 ...
- python【力扣LeetCode算法题库】121-买卖股票的最佳时机
买卖股票的最佳时机 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股 ...
最新文章
- python pandas DataFrame 字符串转日期格式
- 从源码了解spring bean实例化过程
- 《软件需求模式》阅读笔记04
- 大数据和个性化设计是用户体验(UX)的未来
- 自然语言15_Part of Speech Tagging with NLTK
- spring3依赖包下载
- pygame render怎么显示中文_Pygame游戏——贪吃蛇(完结)
- 汽车常识全面介绍 - 悬挂系统
- [置顶] 怎么对待重复的代码
- JAVA加载 编译 运行,在Java 7中编译的加载/运行类6
- 如何用100美元和TensorFlow来造一个能“看”东西的机器人
- less 使用小结!笔记!
- 杨辉三角形(简明易懂)
- WPF学习笔记(6):DataSet更新后台数据库个别列失败的问题
- 一文带你了解夜间灯光数据
- struct字节计算
- 杂牌蓝牙在2003系统使用新驱动的破解方法!
- SK创新在2019年下半年将实现柔性显示器核心材料FCW量产
- Errors accessing files.There may be spaces in your image‘s filename (已解决)
- Oracle存储过程打印输出错误信息、行号,快速排查
热门文章
- 蝴蝶曲线python_ProE常用曲线方程:Python Matplotlib 版本代码(蝴蝶曲线)
- flash activex java_Adobe flash player ActiveX和NPAPI和PPAPI 这三个软件有什么区别?
- 国际导航网二开php源码下载,国际网址导航系统整站源码 v3.5.2
- php date 毫秒_swoole+PHP自动取消订单he还原库存
- python正则表达式匹配模式屠夫之桥_Python 编程快速上手 第 7章 模式匹配与正则表达式...
- matlab数学实验课件4,数学实验4_数学实验_doc_大学课件预览_高等教育资讯网
- 用力和应变片计算弹性模量_实验力学实验讲义(08.9).doc
- 计算机与材料化学应用背景介绍,计算机在材料工程中的应用.ppt
- c语言在线编译器_C语言和汇编语言是什么?他们之间可以有怎样的合作?为你解析...
- java 打印心形图案_简单漂亮的心形礼品盒折纸手工教程