辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】

7 篇文章 0 订阅
4 篇文章 0 订阅
  1. 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//2
        if x == 0 or x == 1:
            return x
        for 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,x
        while low <= high:
            mid = (low + high) / 2
            if mid ** 2 <= x:
                low = mid + 1
            else:
                high = mid - 1
        return low - 1


s=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()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值