xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js ~~ double bitwise not symbol All In One

js ~~ double bitwise not symbol All In One

The bitwise NOT operator (~) inverts the bits of its operand. Like other bitwise operators, it converts the operand to a 32-bit signed integer

按位非运算符 (~) 反转其操作数的位。与其他位运算符一样,它将操作数转换为 32 位有符号整数

const nums = [...''.padStart(10, 0)].map((item, i) => i);
for (let i of nums) {
    // console.log('\ni =', i);
    console.log('\n~i =', ~i);
    console.log('~~i =', ~~i);
}
const nums = [...''.padStart(10, 0)].map((item, i) => i);
for (let i of nums) {
    console.log('~i =', ~i);
}
console.log('\n');
for (let i of nums) {
    console.log('~~i =', ~~i);
}

[] + 1;
// '1'
-([] + 1);
//-1
~[]
//-1

~[] == -([] + 1);
// true
~[] === -([] + 1);
// true


{} + 1;
//1
-({} + 1);
//NaN ❓ 位运算,对非数字不适用, 优先级不同
~{}
//-1

~{} == -({} + 1);
// false
~{} === -({} + 1);
// false

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT

// 字符串相加 / 大数相加
const addStrings = function(num1, num2) {
  let res = '';
  let temp = 0;
  const arr1 = num1.split('');
  const arr2 = num2.split('');
  while (arr1.length || arr2.length || temp) {
    // `~~` double bitwise not 双非位运算, 字符串转换成整数
    temp += ~~arr1.pop() + ~~arr2.pop();
    // 字符串相加,要注意先后顺序
    res = (temp % 10) + res;
    // 进位
    temp = temp > 9 ? 1 : 0;
  }
  // console.log('res =', res, typeof res);
  return res === "0" ? res : res.replace(/^0+/, '');
}

~~ double bitwise not 双非位运算, 字符串转换成整数

let s = `3`;
// '3'

~s;
// -4

~~s;
// 3

+s;
// 3

对任一数值 x 进行按位非操作的结果为 -(x + 1) ❓

// 对任一数值 x 进行按位非操作的结果为 -(x + 1)

const a = 5;
// 00000000000000000000000000000101
const b = -3;
// 11111111111111111111111111111101

console.log(~a);
// 11111111111111111111111111111010
// -6
console.log(~b);
// 00000000000000000000000000000010
// 2

// 对任一数值 x 进行两次按位非操作的结果为 -(-(x + 1) + 1) === x ❓
// 不全对,仅仅适用于数字 ✅
console.log(~~a);
// 5
console.log(~~b);
// 3

demo

leetcode 415: Add Strings / 字符串相加


/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var addStrings = function(num1, num2) {
  let res = '';
  let temp = 0;
  const arr1 = num1.split('');
  const arr2 = num2.split('');
  while (arr1.length || arr2.length || temp) {
    // ~~ ??? bitwise not 双非位运算
    temp += ~~arr1.pop() + ~~arr2.pop();
    // 字符串相加,要注意先后顺序
    res = (temp % 10) + res;
    // 进位
    temp = temp > 9;
  }
  // console.log('res =', res, typeof res);
  return res === "0" ? res : res.replace(/^0+/, '');
}

https://leetcode-cn.com/problems/add-strings/

https://leetcode.com/problems/add-strings/

思路与算法

本题我们只需要对两个大整数模拟「竖式加法」的过程。
竖式加法就是我们平常学习生活中常用的对两个整数相加的方法,将相同数位对齐,从低到高逐位相加,如果当前位和超过 10,则向高位进一位; 因此我们只要将这个过程用代码写出来即可。

具体实现也不复杂,我们定义两个指针 i 和 j 分别指向 num1 和 num2 的末尾,即最低位,同时定义一个变量 add 维护当前是否有进位,然后从末尾到开头逐位相加即可。
你可能会想两个数字位数不同怎么处理,这里我们统一在指针当前下标处于负数的时候返回 0,等价于对位数较短的数字进行了补零操作,这样就可以除去两个数字位数不同情况的处理,具体可以看下面的代码。

var addStrings = function(num1, num2) {
    let i = num1.length - 1, j = num2.length - 1, add = 0;
    const ans = [];
    while (i >= 0 || j >= 0 || add != 0) {
        const x = i >= 0 ? num1.charAt(i) - '0' : 0;
        const y = j >= 0 ? num2.charAt(j) - '0' : 0;
        const result = x + y + add;
        ans.push(result % 10);
        add = Math.floor(result / 10);
        i -= 1;
        j -= 1;
    }
    return ans.reverse().join('');
};

复杂度分析

时间复杂度:O(max(len1,len2)),其中 len1=num1.length,len 2 =num2.length。
竖式加法的次数取决于较大数的位数。
空间复杂度:O(1)。
除答案外我们只需要常数空间存放若干变量。
在 Java 解法中使用到了 StringBuffer,故 Java 解法的空间复杂度为 O(n)。

https://leetcode-cn.com/problems/add-strings/solution/zi-fu-chuan-xiang-jia-by-leetcode-solution/

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-02 11:21  xgqfrms  阅读(35)  评论(2编辑  收藏  举报