LeetCode 刷题记录(6-10题)
6 Z 字形变换(题目链接)
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
lists = []
for i in range(numRows):
lists.append('')
numMids = numRows - 2
if numMids < 0:
numMids = 0
for i_s in range(len(s)):
rest = i_s % (numMids + numRows)
if rest < numRows:
lists[rest] = ''.join([lists[rest], s[i_s]])
else:
lists[numMids + numRows - rest] = ''.join([lists[numMids + numRows - rest], s[i_s]])
s_result = ''.join(lists)
return s_result
7.整数反转(题目链接)
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
y = 0
z = 1
if x < 0:
z = -1
x = -x
while x != 0:
item = x % 10
x = x // 10
if 2147483647 - 10 * y <= item:
return 0
y = y * 10 + item
return y*z
8.字符串转换整数 (atoi)(题目链接)
class Solution:
def myAtoi(self, str: 'str') -> 'int':
sign = 0
num = 0
for i in str:
if sign == 0 and i == ' ':
continue
elif sign == 0 and i == '-':
sign = -1
elif sign == 0 and i == '+':
sign = 1
elif i in ['','','','','','','','','','']:
if (sign == 1 or sign == 0) and (2147483647 - int(i)) // 10 < num:
return 2147483647
elif sign == -1 and (2147483648 - int(i)) // 10 < num:
return -2147483648
else:
if sign == 0:
sign = 1
flag = 1
num = num * 10 + int(i)
else:
break
if sign == 0:
return num
return sign * num
LeetCode 刷题记录(6-10题)的更多相关文章
- Leetcode第1题至第10题 思路分析及C++实现
笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC.(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了, ...
- LeetCode 刷题记录(1-5题)
1 两数之和(题目链接) class Solution: # 一次哈希法 def twoSum(self, nums, target): """ :type nums: ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题模板(1):《我要打10个》之二分法
Author : 叨陪鲤 Email : vip_13031075266@163.com Date : 2021.01.23 Copyright : 未 ...
- leetcode刷题--两数之和(简单)
一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
随机推荐
- unity学习 5.x解包
using System.Collections;using System.Collections.Generic;using UnityEngine; public class bundleload ...
- linux中ftp中文名乱码问题
问题触发环境 1. java中使用org.apache.commons.net.ftp.FTPClient包 2. 通过chrome浏览器的file标签上传文件 3. 在windows上部署的File ...
- SAP AM:固定资产采购的预算管理
对于很多公司来说,购买资产是公司年度支持的主要部分,因此需要用预算管理来防止过度支出.这项支出被列为资本支出,所以很多公司都需要对购买过程和安全防范进行良好的控制.以下文中说明如何在购买资产时使用预算 ...
- 元组(tuple)的用途(基础)
>>>a = 123,456,'jia',['jia','xiang'] >>>a (123, 456, 'jia', ['jia', 'xiang']) 这个带括 ...
- PAT甲级——1140.Look-and-say Sequence (20分)
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...
- 爬虫笔记(十三)——lxml库的使用
HTML示例代码: text = ''' <div> <ul> <li class="item-0"><a href="link ...
- C实现日志等级控制
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h&g ...
- ubuntu或者raspbian清理软件使用痕迹
拿最常用的nginx举例 删除nginx–purge包括配置文件 sudo apt-get --purge remove nginx 开始使用上面这条,后来发现还是有很多相关联没有删除 首先需要停止n ...
- 远程关机 (Windows shutdown Windows)
在某些场景,可使用远程关机控制整个局域网中的所有电脑进行一键关机或重启,便于管理,以提高工作效率. 从远程系统强制关机,首先需要进行一些必要的设置. 1.使用 win + R 打开运行,输入gpedi ...
- Linux svn 服务器配置--转
转自 http://my.oschina.net/lionel45/blog/298305 留存备份. Linux搭建SVN 服务器 作者: 沈小然 日期:2014年 8月 5日 1 ...