数学与位运算

来源:互联网 发布:网络锁是什么 编辑:程序博客网 时间:2024/05/18 17:04

正负 溢出 除零

https://oj.leetcode.com/tag/math/

溢出相关的问题

Integer.MAX_VALUE:2^31 - 1=2147483647

Integer.MIN_VALUE:-2^31=    -2147483648

求绝对值会溢出

判断:使用long解决int溢出问题

二分查找计算中点避免溢出:m = (l + r) / 2 = l + (r - l) / 2

Java逻辑与位操作

#1数学运算

加减:

E11:https://oj.leetcode.com/problems/add-two-numbers/ (链表)

H2:https://oj.leetcode.com/problems/add-binary/ (字符串)

F3:https://oj.leetcode.com/problems/plus-one/ (数组)

乘除:

H3:https://oj.leetcode.com/problems/multiply-strings/ (字符串)

J8:https://oj.leetcode.com/problems/fraction-to-recurring-decimal/ (字符串循环小数)

J1:https://oj.leetcode.com/problems/divide-two-integers/ (比特试探) (

幂与开方:

J2:https://oj.leetcode.com/problems/sqrtx/ (二分查找)

J3:https://oj.leetcode.com/problems/powx-n/ (分治)

#2String格式

H4:https://oj.leetcode.com/problems/string-to-integer-atoi/ 

H6:https://oj.leetcode.com/problems/roman-to-integer/
         https://oj.leetcode.com/problems/integer-to-roman/

J4:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ (逆波兰式)

#3数字pattern

J5:https://oj.leetcode.com/problems/reverse-integer/

J6:https://oj.leetcode.com/problems/palindrome-number/ (基于J5)

#4二进制bit操作

https://oj.leetcode.com/tag/bit-manipulation/

比特位累计(不同运算只是公式不同)

因为int数值大小有限,所以分治与比特位累计复杂度一样

乘法:a * 7 = a (2^2 + 2^1 + 2^0) = a<<2 + a<<1 + a<<0 (此法因为CPU可以直接移位)

            a * 7 = a*(1 +2 + 4) = a + a*2 + a*4 (DP比分治加法快)

乘方:a ^ 7 = a^(1 + 2 + 4) = a * a^2 * a^4 (DP)

J3:https://oj.leetcode.com/problems/powx-n/ 

比特位试探 (不同运算只是公式不同)

因为int数值大小有限,所以二分查找与比特位累计复杂度一样

除法:a/b=x => a-bx=0 => 将x视为二进制数 移位相减试探

J1:https://oj.leetcode.com/problems/divide-two-integers/ (除了溢出还要小心除0;bit操作比二分查找快)

开方:a^1/2=x => a-x^2=0 =>将x视为二进制数 乘方相减试探

J2:https://oj.leetcode.com/problems/sqrtx/ (bit操作比二分查找快)

log:loga=x => a-2^x=0

比特操作

J12: https://oj.leetcode.com/problems/single-number/

J13: https://oj.leetcode.com/problems/single-number-ii/

https://leetcode.com/problems/reverse-bits/

https://leetcode.com/problems/number-of-1-bits/

一个正整数int有多少bit位为一

#5进制相关

J7:https://oj.leetcode.com/problems/excel-sheet-column-title/

         https://oj.leetcode.com/problems/excel-sheet-column-number/

https://oj.leetcode.com/problems/factorial-trailing-zeroes/

J16: https://oj.leetcode.com/problems/gray-code/

#6几何

J15: https://oj.leetcode.com/problems/max-points-on-a-line/

0 0
原创粉丝点击