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
Comments
二进制的话就可以异或了吧 |
var a; |
function interchange(x) { |
~x + 2 |
如果没其他更深层次的意思的话,,, function foo (val) {
return val === 1 ? 0 : 1
} |
function calc (val) {
return val === 1 ? 0 : 1
}
let str = "0101"
// =>
str = "1010"
function change(str){
return ~str
} 这个问题不清楚考察什么 ? |
有问题 |
不考虑浮点数的话 |
应该是考察异或运算把,记得以前学过 1^num就可以完成,但是这仅仅限于num为数字的情况 |
function foo (val) { |
const reverse = num=>+!num const reverseNum = str => str.replace(/\d/g, m => +!+m); |
function six(str) { |
感觉是二进制取反的操作,可以使用 ~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)); |
直接异或操作就行了 const convert = num => num^1;
convert(0); // 1
convert(1); // 0 |
function interChange (num) {
return ~num+2
}
console.log('interChange',interChange(1)) |
|
function foo(n) {
return -(n + 1) + 2
} 和 |
|
function interchange(x) { |
reverse = (x: number) => x^1 |
function test(str){ |
//假设为字符串的1 0转换,数字的话使用toString就可以转为字符串,然后再外面套个parseInt就可以转会number类型 console.log(changeNum('1100011qwezx10')) |
let a = 1; //一般方法 //三木运算符 //位运算 |
console.log(!0 ? 1 : 0); |
1 similar comment
console.log(!0 ? 1 : 0); |
mark一下 |
|
我看了半天都没看到我经常用的方法 用1减去那个数就行了! 1 = 1 - 0
0 = 1 - 1 |
第31天 写一个方法把0和1互转(0置1,1置0)
The text was updated successfully, but these errors were encountered: