档案日期2015的10

2015年3月9日 - 2015年3月15日

JavaScript的变量声明与声明提前

JavaScript的变量声明

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 ...

继续阅读

不使用乘除和pow计算一个数的平方

Calculate square of a number without using *, / and pow()

不使用乘除和pow计算一个数的平方 

给定一个整数n,在不使用乘除和pow函数的前提下计算其平方的值。

Examples:

Input: n = 5
Output: 25

Input: 7
Output: 49

Input: n = 12
Output: 144

一个直观的解法是重复地向结果加n。下面是该思路的C++实现。

// Simple solution to calculate square without
// using * and pow()
#include<iostream> ...

继续阅读

[LeetCode]Number of 1 Bits

题目描述:

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3 ...

继续阅读

每日归档

上周

下周

归档