[Q7]  把数倒过来

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21 Solution: https://leetcode.com/problems/reverse-integer/discuss/229800/Python3-Faster-than-100
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
""" label = 1
if x<0:
label = -1
s = str(abs(x))
S = label*int(s[-1:None:-1])
if S<-2**31 or S>2**31-1:
return 0
else:
return S

【Q8】在给定列表中搜索数字,且数字必须在最开头位置

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
  Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
  digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
  Thefore INT_MIN (−231) is returned. Solution:正则表达式
class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
""" import re x = re.search('^\s*[-\+]?\d+',str)
if not x:
return 0
x = int(x.group(0))
if x>2**31-1:
return 2**31-1
elif x<-2**31:
return -2**31
else:
return x

【Q9】判断是否为回文

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Solution:从两边同时搜索
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
x = str(x)
if len(x)==0:
return False
i = 0
j = len(x)-1
while i<=j:
if x[i]!=x[j]:
return False
i += 1
j -= 1
return True

【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number的更多相关文章

  1. 【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 Valu ...

  2. 【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 ...

  3. 【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 ...

  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算法题-Reverse Integer

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

  7. LeetCode算法题-Reverse Words in a String III(Java实现)

    这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...

  8. LeetCode算法题-Reverse String II(Java实现)

    这是悦乐书的第256次更新,第269篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第123题(顺位题号是541).给定一个字符串和一个整数k,你需要反转从字符串开头算起的 ...

  9. LeetCode算法题-Reverse String(Java实现)

    这是悦乐书的第205次更新,第217篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第73题(顺位题号是344).编写一个以字符串作为输入并返回字符串的函数.例如: 输入: ...

随机推荐

  1. 【整理】close 和 shutdown 的原理

    http://stackoverflow.com/questions/14740852/linux-socket-close-vs-shutdown shutdown(sd, SHUT_WR)  发送 ...

  2. 1003. [ZJOI2006]物流运输【区间DP+最短路】

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转 停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严 ...

  3. 1934. [SHOI2007]善意的投票【最小割】

    Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...

  4. 【洛谷】【堆+结论】P4597 序列sequence

    [题目背景:] 原题cf13c 数据加强版(就是说原来能用DP做现在不行了QwQ) [题目描述:] 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.而且要求修改后的数列只能出现修改 ...

  5. Day10 API

    String类 String是不可变类:值一旦确定了,就不会更改. public static void main(String[] args) { String s1 = "hello&q ...

  6. 【绝密】为什么现在的CAN收发器通信距离越来越短?

    [绝密]为什么现在的CAN收发器通信距离越来越短?   CAN收发器的改良和隔离器件引入,大大提高了通信的可靠性,但同时也引入了额外的延时,导致通信距离变短,或总线错误帧增加,本文以1Mbps波特率下 ...

  7. Animate.css介绍

    Animate.css简介 animate.css 动画库,预设了抖动(shake).闪烁(flash).弹跳(bounce).翻转(flip).旋转(rotateIn/rotateOut).淡入淡出 ...

  8. 虹软人脸识别iOS SDK2.0

    最近公司要在APP上添加一个人脸识别功能,在网上搜了一圈,发现虹软的人脸识别SDK挺好用的,而且还免费,所以就下载了他们的SDK研究了一下.总的来看功能挺好用的,只是demo上面部分功能不是很完善,所 ...

  9. Django:settings中关于static静态文件目录的设置

    django项目settings中关于静态资源存放位置的设置 主要涉及以下3项:STATIC_URL.STATICFILES_DIR和STATIC_ROOT 1.STATIC_URL 这项是必须配置的 ...

  10. Json转Scala对象一个问题

    今天与第三方对接一个接口,由于我们是用Scala语言,对方的返回体Json需要转换为一个对象,对象里面包含一个数组也可以说是集合,于是乎就用List接收,看似没问题,编译也没报错,自测调用的时候就报了 ...