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% ...
随机推荐
- Java集合详解(全)
Java的集合主要有List , Set, Map List , Set继承至Collection接口,Map为独立接口 List下有ArrayList,LinkedList,Vector Set下有 ...
- C# 类的解构
C#对类的解构,必须在该类内实现Deconstruct方法,并且返回类型为void ,并用out参数返回各个部分. using System; using System.Text; namespace ...
- USB Reverse Tether (a dirty solution)
Tether your android phone to your PC using USB cable could share your 3g Internet connection with PC ...
- CentOS7离线安装MySQL8.0
CentOS7离线安装MySQL8.0 卸载软件 rpm -e --nodeps 要卸载的软件包 root@jacky zookeeper]# rpm -e --nodeps java-1.6.0-o ...
- 远程关机 (Windows shutdown Windows)
在某些场景,可使用远程关机控制整个局域网中的所有电脑进行一键关机或重启,便于管理,以提高工作效率. 从远程系统强制关机,首先需要进行一些必要的设置. 1.使用 win + R 打开运行,输入gpedi ...
- shell_backup_MySQL
#!/bin/bash #可修改如下参数backup_filename=$(date +%Y%m%d%H%M%S)backup_tmp_dir=/data/backup/ip=10.0.1.182us ...
- ModelSerializer补充及ListSerializer
整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py """ 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验,校验的数据应该在 ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
- Office 365管理中心门户
一.使用Office 365管理员账户登陆到由世纪互联运营的Office 365 登陆地址 https://portal.partner.microsoftonline.cn 1.登陆完成后,选择左上 ...
- pip install torch出现错误
首先使用Python的pip安装命令: pip install torch 出现错误 解决办法:这时需要先下载pytorch包,根据自己的python版本选择.pytorch包链接: https:// ...