辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】
- Add Binary
题目描述;
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
简单描述:二进制相加
代码如下:
class Solution:def addBinary(self, a, b):""":type a: str:type b: str:rtype: str"""print(bin(int(a,2)+int(b,2))[2:])return bin(int(a,2)+int(b,2))[2:]s=Solution()
s.addBinary("1010","1011")
python的函数真他娘的多。
本题中用到的:
bin() 返回一个整数 int 或者长整数 long int 的二进制表示。
示例如下:
>>>bin(10)
'0b1010'
>>> bin(20)
'0b10100'
上面的例子也说明了代码中为什么用到了[2:],就是从第3位开始;
然后让我们重新认识以下int这个函数:
int(x, base=10)
x -- 字符串或数字。
base -- 进制数,默认十进制。
举例如下:
>>>int() # 不传入参数时,得到结果0
0
>>> int(3)
3
>>> int(3.6)
3
>>> int('12',16) # 如果是带参数base的话,12要以字符串的形式进行输入,12 为 16进制
18
>>> int('0xa',16)
10
>>> int('10',8)
8
69. Sqrt(x)
题目描述:
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
翻译:求所给数字的平方根;
我做的第一种方法:
class Solution:def mySqrt(self, x):""":type x: int:rtype: int"""mid=x//2if x == 0 or x == 1:return xfor i in range(mid+1):if i * i <= x and (i + 1) * (i + 1) > x:print(i)return i
这种方法在编译器上可以AC,但是到了LeetCode的平台就提示:
351 / 1017 test cases passed.
Status: Time Limit Exceeded
也就是说时间复杂度太大了,毕竟是一个一个遍历。
第二种方法【二分思想】:
class Solution:def mySqrt(self, x):""":type x: int:rtype: int"""low,high=0,xwhile low <= high:mid = (low + high) / 2if mid ** 2 <= x:low = mid + 1else:high = mid - 1return low - 1s=Solution()
s.mySqrt(4)
88. Merge Sorted Array
题目描述:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
就是把两个已经排好序的数组,在不借助其他内存的基础上合并到一起(也是排好序的)
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
我的解法:
起初我是这样写的:
class Solution:def merge(self, nums1, m, nums2, n):""":type nums1: List[int]:type m: int:type nums2: List[int]:type n: int:rtype: void Do not return anything, modify nums1 in-place instead."""for i in range(len(nums2)):nums1.append(nums2[i])nums1.sort()s=Solution()
s.merge([1,2,3,0,0,0],3,[2,5,6],3)
可是输出有0没法处理;
最后这样写的:
for i in range(len(nums2)):nums1[i+m]=nums2[i]
nums1.sort()
还有一种写法,值得学习,利用了堆栈的思维;
while(len(nums1)>m):nums1.pop(m)
while(len(nums2)>n):nums2.pop(n)
for i in nums2:nums1.append(i)
nums1.sort()
辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】相关推荐
- 辣鸡刘的Leetcode之旅4 (Weekly Contest 100)【单调序列,】
896. Monotonic Array 题目描述: An array is monotonic if it is either monotone increasing or monotone dec ...
- LeetCode之Merge Sorted Array
1.问题 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. No ...
- leetcode python3 简单题88. Merge Sorted Array
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十八题 (1)题目 英文: Given two sorted integer a ...
- 超级辣鸡的编程学习之旅 之 第一篇~~
背景介绍:博主前十八年前只用过电脑看电视聊QQ,其他计算机知识一窍不通,高考后确定读计算机相关的专业之后就收藏了很多编程学习视频,but初高中时我的理科成绩一直比较差,所以对编程这类知识心存畏惧,迟迟 ...
- LeetCode:1287. Element Appearing More Than 25% In Sorted Array - Python
1287. 有序数组中出现次数超过25%的元素 问题描述: 给你一个非递减的有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的25%. 请你找到并返回这个整数 示例 : 输入 ...
- [LeetCode]Merge Sorted Array
题目:给定两个int型有序数组A和B,将B数组合并到A数组中(A数组空间足够) 算法:模拟插入排序,从右往左推断并往A数组中插入B数组元素 public class Solution {public ...
- LeetCode 之 Merge Sorted Array(排序)
[问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- [Leetcode] Merge Sorted Array 合并数组
Merge Sorted Array 最新更新请见:https://yanjia.me/zh/2019/02/... Given two sorted integer arrays nums1 and ...
- 【LeetCode从零单排】No88.Merge Sorted Array
题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume ...
最新文章
- Java程序员3面小米,被俩算法题难倒,微软员工6分钟解决,真丢脸
- 2017年10月07日普及组 单元格
- php7 mcrypt windows,Windows下php安装mcrypt扩展问题
- 976 AlvinZH想回家(背包DP大作战T)
- ABP vNext分布式事件总线RabbitMQ注意事项
- php crypt mysql password_php使用crypt()函数进行加密
- MySQL学习——标识符语法和命名规则
- vivo X30 5G版现身Geekbench:搭载三星Exynos 980
- 7月共处理钓鱼网站1921个:非CN域名达1911 个
- java 判断两个时间段是不是有交集
- dat文件读取_比特币blk.dat文件结构及读取软件
- 我写了一个“文本转手写”神器来搞定作业!
- 数据结构入门(二)固定容量数组
- JSP实用教程(3)——Tag文件和Tag标记
- html切西瓜游戏源码,html5切水果源码(水果忍者)
- 基于springboot2.0.6版本的TX-LCN分布式事务搭建说明
- MIPI.DSI.LCD點屏筆記_AT070TN92(800x480)_THC63LVDF84B_深圳富元智FX6
- oracle 的insert into的详解
- 北航计算机控制系统实验报告,北航计算机控制系统实验报告详细分解.doc
- NCBI|16S原始数据上传
热门文章
- PCB设计当中<过孔>的设计规范
- anaconda 安装第三方库
- 生活中动听歌曲旋律-music
- yuv从4k缩小到1080p
- 147.文本编辑工具:Markdown+Typora+幕布
- 元计算模拟宇宙人生by剑桥大学材料学博士段晓明 (公号回复“元计算”下载PDF典藏版资料,欢迎转发、赞赏支持科普)
- MSP430 精准延时之定时器 TIMERA 0.5实现LED闪烁
- 每日一课 | 如何编写完美的Python命令行程序?
- 光通信器件与系统(西交大朱京平笔记) -2-4 半导体激光器的工作特性
- STM32 USB CDC 虚拟多串口