[Swift]LeetCode371. 两整数之和 | Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:
Given a = 1 and b = 2, return 3.

Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.



不使用运算符 + 和 - ???????,计算两整数 ???????a 、b ???????之和。

示例 1:

输入: a = 1, b = 2
输出: 3

示例 2:

输入: a = -2, b = 3
输出: 1

8ms
 1 class Solution {
 2     func getSum(_ a: Int, _ b: Int) -> Int {
 3         //按位取异或
 4         var res:Int = a^b
 5         //判断是否需要进位
 6         var forward = (a&b) << 1
 7         if forward != 0
 8         {
 9             //如有进位,则将二进制数左移一位,进行递归
10             return getSum(res, forward)
11         }
12         return res
13     }
14 }


8ms

 1 class Solution {
 2     func getSum(_ a: Int, _ b: Int) -> Int {
 3         if a == 0 {
 4             return b
 5         }
 6
 7         if b == 0 {
 8             return a
 9         }
10
11         var a = a
12         var b = b
13         while b != 0 {
14             let carry = a & b
15             a = a ^ b
16             b = carry << 1
17         }
18         return a
19     }
20 }


8ms

1 class Solution {
2     func getSum(_ a: Int, _ b: Int) -> Int {
3         if a&b==0 {
4             return a|b
5         }
6         return getSum(a^b, (a&b)<<1)
7     }
8 }


12ms

 1 class Solution {
 2     func getSum(_ a: Int, _ b: Int) -> Int {
 3         var carry = (a & b) << 1
 4     var result = a ^ b
 5     while carry != 0 {
 6       let carryTemp = carry
 7       carry  = (result & carryTemp) << 1
 8       result = result ^ carryTemp
 9     }
10     return result
11     }
12 }


16ms

 1 class Solution {
 2     func getSum(_ a: Int, _ b: Int) -> Int {
 3
 4         if b == 0 {
 5             return a
 6         } else {
 7             return getSum(a ^ b, (a & b) << 1)
 8         }
 9     }
10 }

原文地址:https://www.cnblogs.com/strengthen/p/9769295.html

时间: 2024-08-30 05:13:36

[Swift]LeetCode371. 两整数之和 | Sum of Two Integers的相关文章

【leetcode 简单】 第八十七题 两整数之和

不使用运算符 + 和-,计算两整数a .b之和. 示例: 若 a = 1 ,b = 2,返回 3. class Solution: def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ # return sum([a,b]) first=a^b second=(a&b)<<1 return sum([first,second]) 参考:htt

LeetCode|371. 两整数之和

题目描述 等级: 简单 不使用运算符 和 - ,计算两整数a .b之和. 示例1: 输入: a = 1, b = 2 输出: 3 示例2: 输入: a = -2, b = 3 输出: 1 思路 对于位运算的考察. 在位运算中,异或操作获取的是两个数的无进位和,异或:相同为0,不同为1. 如, 2^3 0010 ^ 0011 ------- 0001 我们知道,2 3=5,5的二进制是0101.前面已经知道了无进位和,下面获取进位的数: 看a 原文地址:https://www.cnblogs.co

[Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 1 /**

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam

位运算————两整数之和

在位运算操作中,异或的一个重要特性是无进位加法. a = 5 = 0101 b = 4 = 0100 a ^ b 如下: 0 1 0 1 0 1 0 0 ------- 0 0 0 1 得到了一个无进位加法结果,如果要得到 a + b 的最终值,我们还要找到进位的数,把这二者相加.在位运算中,我们可以使用与操作获得进位: a = 5 = 0101 b = 4 = 0100 a & b 如下: 0 1 0 1 0 1 0 0 ------- 0 1 0 0 由计算结果可见,0100 并不是我们想要

给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的位置,如果sum=x则返回true, 3,找到位置后,保持i不变,从k处向前遍历,直到找到A[k]+A[i]等于x,并返回TRUE,如果找不到,则返回false. 论证步骤3:当前找到的位置恰好A[k]+A[i]>x,且前一位置的sum<x: 所以A[i]前面的数(不包括A[i])无论取哪两个数都

leetcode——Two Sum 两数之和(AC)

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / 3 6 / \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / 3 6 / \ 2 4 7 Targe

找出数组中两数之和为指定值的所有整数对

一,问题描述 给定一个整型数组(数组中的元素可重复),以及一个指定的值.打印出数组中两数之和为指定值的 所有整数对 二,算法分析 一共有两种方法来求解.方法一借助排序,方法二采用HashSet 方法一: 先将整型数组排序,排序之后定义两个指针left和right.left指向已排序数组中的第一个元素,right指向已排序数组中的最后一个元素 将 arr[left]+arr[right]与 给定的元素比较,若前者大,right--:若前者小,left++:若相等,则找到了一对整数之和为指定值的元素