Leecode刷题之旅-C语言/python-349两整数之和
/*
* @lc app=leetcode.cn id=371 lang=c
*
* [371] 两整数之和
*
* https://leetcode-cn.com/problems/sum-of-two-integers/description/
*
* algorithms
* Easy (55.04%)
* Total Accepted: 8.1K
* Total Submissions: 14.8K
* Testcase Example: '1\n2'
*
* 不使用运算符 + 和 - ,计算两整数 a 、b 之和。
*
* 示例 1:
*
* 输入: a = 1, b = 2
* 输出: 3
*
*
* 示例 2:
*
* 输入: a = -2, b = 3
* 输出: 1
*
*/
int getSum(int a, int b) {
if(a && b) return getSum(a^b, (a&b) << );
else return a|b;
}
这里对a和b进行二进制上的相加,然后递归中处理进位。
(不过这里一直会溢出。。。。。。尴尬)
-------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=371 lang=python3
#
# [371] 两整数之和
#
# https://leetcode-cn.com/problems/sum-of-two-integers/description/
#
# algorithms
# Easy (55.04%)
# Total Accepted: 8.1K
# Total Submissions: 14.8K
# Testcase Example: '1\n2'
#
# 不使用运算符 + 和 - ,计算两整数 a 、b 之和。
#
# 示例 1:
#
# 输入: a = 1, b = 2
# 输出: 3
#
#
# 示例 2:
#
# 输入: a = -2, b = 3
# 输出: 1
#
#
class Solution:
def getSum(self, a: int, b: int) -> int:
while b != 0:
carry = a & b
a = (a ^ b) % 0x100000000
b = (carry << 1) % 0x100000000
return a if a <= 0x7FFFFFFF else a | (~0x100000000+1)
这里模拟32位的int 左移位,python左移位是不会溢出的。
Leecode刷题之旅-C语言/python-349两整数之和的更多相关文章
- Leecode刷题之旅-C语言/python-88合并两个有序数组
/* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...
- Leecode刷题之旅-C语言/python-21.合并两个有序链表
/* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...
- Leecode刷题之旅-C语言/python-13.罗马数字转整数
/* * @lc app=leetcode.cn id=13 lang=c * * [13] 罗马数字转整数 * * https://leetcode-cn.com/problems/roman-to ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
随机推荐
- models的单表操作
orm创建数据库 from django.db import models # Create your models here. class Book(models.Model): name = mo ...
- August 24th 2017 Week 34th Thursday
If you have choices, choose the best. If you have no choice, do the best. 如果有选择,那就选择最好的:如果没有选择,那就努力做 ...
- 洛谷 P4841 城市规划
构造简单无向图的EGF: \[ G(x)=\sum_{i}^{\infty}2^{\binom{i}{2}}\cdot\frac{x^i}{i!} \] 构造简单无向连通图的EGF: \[ F(x)= ...
- python strip()
函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头 ...
- CF284A Cows and Primitive Roots
嘟嘟嘟 这道题就是求一个奇素数\(p\)的原根数量. 公式是\(\varphi(\varphi(p))\).又因为\(p\)是质数,所以就是\(\varphi(p - 1)\). (证明啥的我不会-- ...
- 关于numpy mean函数的axis参数
import numpy as np X = np.array([[1, 2], [4, 5], [7, 8]]) print np.mean(X, axis=0, keepdims=True) pr ...
- 使用MVVM设计模式构建WPF应用程序
使用MVVM设计模式构建WPF应用程序 本文是翻译大牛Josh Smith的文章,WPF Apps With The Model-View-ViewModel Design Pattern,译者水平有 ...
- 使用arcpy替换栅格数据异常
使用arcpy替换mxd中的栅格图层数据源时,发现导出的图片异常. 修改代码把修改后的mxd保存后发现修改后的数据源不是我传入参数的数据源 比如我设置的参数是 “2019_3_2_2f8091e2d4 ...
- Notepad++格式化JSON字符串
窗口 -> 插件 -> Plugin Manager -> Show Plugin Manager,在弹出层中找到"JSON Viewer"选项,将踏勾选上,然后 ...
- 启动 NFS 守护进程:rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
启动 NFS 守护进程:rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused) rpc.nfsd: unable t ...