**leetcode笔记--4 Sum of Two Integers

question:

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

my wrong answer

错误点:

显示memory error 应该是运行较大数字时导致内存不够,所以这种方法不佳

正确答案地址:(暂时不明白)

http://blog.csdn.net/mebiuw/article/details/51788817

http://www.cnblogs.com/la0bei/p/5659829.html

重点(补充):

1 合并两个list的方法:

除了直接相加(生成新的list),还有两种方法(修改其中一个list):

用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部,例如:

>>> L1 = [1, 2, 3, 4, 5]
>>> L2 = [20, 30, 40]
>>> L1.extend(L2)
>>> L1
[1, 2, 3, 4, 5, 20, 30, 40]

用切片(slice)操作,L1[len(L1):len(L1)] = L2和上面的方法等价,例如:(此方法更加灵活)

>>> L1 = [1, 2, 3, 4, 5]
>>> L2 = [20, 30, 40]
>>> L1[len(L1):len(L1)] = L2
>>> 
>>> L1
[1, 2, 3, 4, 5, 20, 30, 40]

时间: 2024-10-23 16:38:29

**leetcode笔记--4 Sum of Two Integers的相关文章

通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

昨天在leetcode做题的时候做到了371,原题是这样的: 371. 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. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,

【leetcode】371. Sum of Two Integers

题目描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 解题分析: 这种类型的题必然要用位运算,虽然自己写了关于位运算的代码,但是不够简洁. 后来参考了这篇博文: http://blog.csdn.net/zhongjiekangping/article/details/6855864 这篇博文对位运算加法的实现讲的得十分清楚,我这里就只放按此思路写

leetcode笔记:Sum Root to Leaf Numbers

一. 题目描述 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1

LeetCode之371. Sum of Two Integers

---------------------------------- 使用位运算实现加法: a^b 加不同部分 (a&b)<<1 加相同部分 递归相加 AC代码: public class Solution { public int getSum(int a, int b) { if(b==0) return a; int t1=a^b; int t2=(a&b)<<1; return getSum(t1,t2); } } 题目来源: https://leetcod

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[LeetCode] 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. 这道题是CareerCup上的一道原题,难道

LeetCode 第 371 题 (Sum of Two Integers)

LeetCode 第 371 题 (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. 不用加减法计算两个整数的和.这道题其实是考察一些基本的布尔代数知识.我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 +

【Leetcode】Sum of Two Integers

题目链接:https://leetcode.com/problems/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. 思路: 唉,这题虽然是easy,但是真好烦的一题,之前在hihocoder(还是其他oj)上好像也做过这

[leetcode] 371. 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. 用 异或 表示sum,与 表示 carry,carry左移一位后,递归调用getSum(sum,carry<<1) 一刷: public int getSum(int a, int b) { return b ==0 ? a