【Q13】

Roman numerals are represented by seven different symbols: IVXLCD 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 before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (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 = 0
for x in reversed(s):
if x=='I':
op += 1 if op<5 else -1
elif x=='V':
op += 5
elif x=='X':
op += 10 if op<50 else -10
elif x=='L':
op += 50
elif x=='C':
op += 100 if op<500 else -100
elif x=='D':
op += 500
else:
op += 1000
return 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 list
for 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 abc 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]:
continue target = 0-nums[k]
i,j = k+1,N-1 while i<j:
if nums[i]+nums[j]==target:
result.append([nums[k],nums[i],nums[j]])
i += 1
j -= 1
while i<j and nums[i]==nums[i-1]:
i += 1
elif nums[i]+nums[j]<target:
i += 1
else:
j -= 1
return result

 

【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum的更多相关文章

  1. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  2. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  3. 【leetcode刷题笔记】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  4. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  5. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  6. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  7. 【算法】LeetCode算法题-Roman To Integer

    这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...

  8. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  9. LeetCode算法题-Design HashSet(Java实现)

    这是悦乐书的第298次更新,第317篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第166题(顺位题号是705).不使用任何内建的hash表库设计一个hash集合,应包含 ...

随机推荐

  1. ES6新特性5:类(Class)和继承(Extends)

    本文摘自ECMAScript6入门,转载请注明出处. 一.类(Class) 1.基本语法 JavaScript语言的传统方法是通过构造函数,定义并生成新对象.下面是一个例子 function Poin ...

  2. Java反射学习三

    反射与数组 java.lang.Array类提供了动态创建和访问数组元素的各种静态方法. 例程ArrayTester1类的main()方法创建了一个长度为10的字符串数组,接着把索引位置为5的元素设为 ...

  3. servlet使用

    一.使用IDEAL创建项目 1) 2) 3) 4) 5) 6) 7) 8) 9) 二.路径介绍: 配置文件: servlet配置文件: package ser_Test; import javax.s ...

  4. Angular动态表单生成(六)

    动态表单之根据Json生成表单 我们在实际的使用中,动态表单往往是由服务器端的一系列配置,然后返回数据给客户端,最后客户端根据数据来动态的生成表单.那么怎么像我们上面所描述的这样,生成一个可以让我们的 ...

  5. css3 学习图谱

    摘要 最近看了一篇文章,建立最近的知识体系,对于工作了几年的大牛需要,但对学生学习同样也需要,我试着做了自己的css知识总图,类似目录,引导我的学习路线. 3-28日总结 根据该图谱,我学习得更有方向 ...

  6. 一份可以落地靠谱iOS开发规范

    列出来的都是个人觉得在团队合作,代码阅读,代码维护中比较重要的一些点,没有什么空格 间距华而不实的东西在里面.涉及 命名规范.编码规范.代码管理规范 命名规范 项目名都遵循大驼峰命名.例如:MSMob ...

  7. iOS开发网络篇—发送GET和POST请求(使用NSURLSession) - 转

    说明: 1.该文主要介绍如何使用NSURLSession来发送GET请求和POST请求 2.本文将不再讲解NSURLConnection的使用,如有需要了解NSURLConnection如何发送请求. ...

  8. php如何实现统计一个数字在排序数组中出现的次数(代码)

    统计一个数字在排序数组中出现的次数. 博客 www.51msk.cn 1.有序的数组查找,使用二分法2.二分法查找第一次出现的位置,二分法查找最后一次出现的位置,end - start +1 left ...

  9. 【10.15总结】绕过CSRF的Referer保护

    今天下午可能要出远门,所以现在就把总结写好了. Write-up地址:[Critical] Bypass CSRF protection on IBM 这个CSRF漏洞存在于IBM的修改邮箱页面,修改 ...

  10. 【转】SQL中的锁、脏读、不可重复的读及虚读

    原文:http://blog.csdn.net/wjeson/article/details/9382067 锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题 ...