Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第31天 写一个方法把0和1互转(0置1,1置0) #112

Open
haizhilin2013 opened this issue May 16, 2019 · 28 comments
Open

[js] 第31天 写一个方法把0和1互转(0置1,1置0) #112

haizhilin2013 opened this issue May 16, 2019 · 28 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第31天 写一个方法把0和1互转(0置1,1置0)

@haizhilin2013 haizhilin2013 added the js JavaScript label May 16, 2019
@alery123456
Copy link

二进制的话就可以异或了吧

@undefinedYu
Copy link
Contributor

var a;
!a && 1 || 0 ;
~a+2

@ghost
Copy link

ghost commented May 17, 2019

function interchange(x) {
return +!x;
}

@qq674785876
Copy link

~x + 2

@tzjoke
Copy link

tzjoke commented May 28, 2019

如果没其他更深层次的意思的话,,,

function foo (val) {
  return val === 1 ? 0 : 1
}

@myprelude
Copy link

function calc (val) {
  return val === 1 ? 0 : 1
}
let str = "0101"  
// =>
str = "1010"
function change(str){
 return ~str
}

这个问题不清楚考察什么 ?

@Damon99999
Copy link

var a;
!a && 1 || 0 ;
~a+2

有问题

@chenyouf1996
Copy link

不考虑浮点数的话
function change(val) {
if (typeof val === 'number') {
val += ''
let newStrArr = val.split('').map((item, index) => {
return item == '1' ? '0' : '1'
})
let newStr = newStrArr.join('')
return parseInt(newStr)
}
return val.split('').map((item, index) => item == '1' ? '0' : '1').join('')
}
let str='10101'
console.log(change(str)) //01010
str=10101
console.log(change(str)) //1010

@zhuyuedlut
Copy link

应该是考察异或运算把,记得以前学过 1^num就可以完成,但是这仅仅限于num为数字的情况

@FFairy
Copy link

FFairy commented Jul 25, 2019

function foo (val) {
var arr = val.split("");
var a = "";
for(let i=0;i<arr.length;i++){
if( arr[i] == 1){
a += 0;
}else if(arr[i] == 0){
a += 1;
}else{
a += arr[i]
}
}
console.log(a);
}
foo("48522220000333331111")

@Vi-jay
Copy link

Vi-jay commented Jul 29, 2019

const reverse = num=>+!num
const reverseNum = str => str.replace(/\d/g, m => +!+m);

@hc951221
Copy link

hc951221 commented Aug 7, 2019

function six(str) {
let arr = str.split('')
let brr = arr.map(item => {
if (item !== '0' && item !== '1') {
return item
} else if (item === '0') {
return 1
} else if (item === '1') {
return 0
} else if (item === ' ') {
return ' '
}
})
console.log(brr.join(''))
}
six(0110..32..1) // 1001..32..0

@Konata9
Copy link

Konata9 commented Aug 25, 2019

感觉是二进制取反的操作,可以使用 ~ 符号进行取反。

~1; // -2
~-2; // 1

如果只是单纯的 0 和 1 互换的话。

function switchOneZero(a) {
  const str = `${a}`;
  return str
    .split("")
    .map(s => (s === "0" ? 1 : s === "1" ? 0 : s))
    .join("");
}

console.log(switchOneZero(2310110));

@zhuang13
Copy link

zhuang13 commented Sep 13, 2019

直接异或操作就行了

const convert = num => num^1;
convert(0); // 1
convert(1); // 0

@ZindexYG
Copy link

function interChange (num) {
  return ~num+2
}
console.log('interChange',interChange(1))

@15190408121
Copy link

function foo (val) {
  return val === 1 ? 0 : 1
}

@qiqingfu
Copy link

function foo(n) {
    return -(n + 1) + 2
}

~x + 2 一样

@blueRoach
Copy link

function change12(number){
  if(!typeof number){
    new Error('not number')
  }
  return number ? 0 : 1
}

@MrZ2019
Copy link

MrZ2019 commented Oct 9, 2020

function interchange(x) {
return +!x;
}

@followfire
Copy link

reverse = (x: number) => x^1

@YU-zhao-jun
Copy link

function test(str){
let length =str.length
let i = 0
let arr = []
const newStr = str.toString()
while(length>0){
if(!!+newStr[i]){
arr.push(0)
}else{
arr.push(1)
}
length--;
i++;
}
return arr
}

@zxcdsaqwe123
Copy link

//假设为字符串的1 0转换,数字的话使用toString就可以转为字符串,然后再外面套个parseInt就可以转会number类型
function changeNum(str) {
let arr = str.split('').map((x) => {
if (x === '0') {
return '1'
} else if (x === '1') {
return '0'
} else {
return x
}
})
let newstr = arr.reduce((x, y) => {
return x+y
})
return newstr
}

console.log(changeNum('1100011qwezx10'))
console.log('1100011qwezx10')

@YangWenLong123
Copy link

let a = 1;

//一般方法
if(a) {
a = 0;
} else {
a = 1;
}

//三木运算符
a = a ? 0 : 1;

//位运算
a ^= 1; --> 0

@github-cxtan
Copy link

console.log(!0 ? 1 : 0);
console.log(~0 + 2);

1 similar comment
@github-cxtan
Copy link

console.log(!0 ? 1 : 0);
console.log(~0 + 2);

@xiaoqiangz
Copy link

mark一下

@PaleBlue0601
Copy link

function conversion(str) {
  const resArr = [...str].map((ch) => {
    return  ch === '0' 
      ? '1' 
      : ch === '1' 
      ? '0' 
      : ch;
  })
  return resArr.join('');
}

@wangyuanjian
Copy link

我看了半天都没看到我经常用的方法

用1减去那个数就行了!

1 = 1 - 0
0 = 1 - 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests