题目描述:
Given a text file file.txt, transpose its content.
You may assume that each row has the same number of columns and each field is separated by the ' ' character.
For example, if file.txt has the following ...
Given a text file file.txt, transpose its content.
You may assume that each row has the same number of columns and each field is separated by the ' ' character.
For example, if file.txt has the following ...
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.
You may assume that a valid phone number must appear in one ...
Write a bash script to calculate the frequency of each word in a text file words.txt.
For simplicity sake, you may assume:
Reverse bits of an unsigned integer.
将无符号整数按位反转
有几种方法可以实现一个无符号整数的按位反转。在此,我们设计一个使用异或交换(XOR swap)技巧的算法,然后使用分治法对其进行优化。
怎样实现第i位与第j位的对调?想想能否使用异或操作实现。
按位反转可以通过将低n/2位与高位对换实现。技巧就是实现一个名为swapBits(i, j)的函数,将第i位与第j位对换。回忆一下异或运算的原理:0 ^ 0 == 0, 1 ^ 1 == 0, 0 ^ 1 == 1, and 1 ^ 0 == 1。
我们只需要在第i位与第j位不同时执行对换 ...
JavaScript的变量声明语句无论出现在何处,都会先于其他代码首先被执行。使用var关键词声明变量的作用域是当前的执行上下文,有可能是外围函数,或者,当变量声明在函数体之外时,则为全局变量。
向一个未声明变量赋值会隐式地将其创建为一个全局变量(它变成了全局对象的一个属性)。声明变量与未声明变量之间的区别为:
1. 声明变量的作用范围限定在其执行的上下文环境中。未声明的变量总是全局的。
function x() {
y = 1; // Throws a ReferenceError in strict mode
var z = 2;
}
x();
console.log(y); // logs "1"
console.log(z); // Throws a ReferenceError: z ...