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 ...
随机推荐
- 如何清除SQL 的登录名
复制路径 找到SqlStudio.bin 文件删除即可 SQL Server 2016 版本: C:\Users\%username%\AppData\Roaming\Microsoft\SQL Se ...
- Exchange2016 & Skype for business 集成之三统一联系人存储
Exchange2016&Skype for business集成之二统一联系人存储 利用统一的联系人存储库,用户可以维护单个联系人列表,然后使这些联系人适用于多个应用程序,包括 Skype ...
- POJ | Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...
- December 09th 2016 Week 50th Friday
In books lies the soul of the whole past time. 书中有所有先贤的全部灵魂. I must know that if I run my business i ...
- J2EE项目异常处理(转)
为什么要在J2EE项目中谈异常处理呢?可能许多java初学者都想说:“异常处理不就是try….catch…finally吗?这谁都会啊!”.笔者在初学java时也是这样认为的.如何在一个多层的j2e ...
- jq双日历--最终版(功能兼容IE5,样式兼容IE6)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- jq判断鼠标滚轴向上滚动还是向下滚动
$(document).on("mousewheel DOMMouseScroll", function (e) { var delta = (e.originalEvent.wh ...
- 自定义控件(视图)2期笔记12:View的滑动冲突之 外部拦截法
1. 外部拦截法: 点击事件通过父容器拦截处理,如果父容器需要就拦截,不需要就不拦截. 这种方法比较符合事件分发机制.外部拦截法需要重写父容器的onInterceptTouchEvent方法,在内部做 ...
- [SDOI2016]数字配对
题目 发现要求配对的条件是这样 \[a_j|a_i,\frac{a_i}{a_j}=p_1\] 我们考虑一下再来一个\(a_k\),满足 \[a_k|a_j,\frac{a_j}{a_k}=p_2\] ...
- luogu P2742 【模板】二维凸包
嘟嘟嘟 没错,我开始学凸包了. 其实挺简单的. 前置技能: 1.极坐标系 2.向量叉积 1.极坐标系 就是一种二维坐标系.只不过两个坐标分别表示向量和极轴的角度和自身的长度.对于不同的问题,极轴可以自 ...