牛顿-拉弗森方法(Newton-Raphson_method)求平方根
牛顿法(英语:Newton’s method)又称为牛顿-拉弗森方法(英语:Newton-Raphson method),它是一种在实数域和复数域上近似求解方程的方法。方法使用函数 {\displaystyle f(x)} f(x)的泰勒级数的前面几项来寻找方程 {\displaystyle f(y)=0} {\displaystyle f(y)=0}的根。
leetcode之Counting_Bits
Counting_Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
Follow up:
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
Space complexity should be O(n).
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
Credits:
Special thanks to @ syedee for adding this problem and creating all test cases.
浅谈立即执行函数
发现一个有意思的问题,有下面一段代码1
2
3function f(x, y){
console.log(x + y)
}(3,4)
我们把它输入到firebug下的命令控制台里面,回车之后会输出什么?
计算素数个数的三段进阶
今天坐leetcode上的Count Primes这道题,题目要求比较的简单:Count the number of prime numbers less than a non-negative number, n. 就是给一个数字,然后输出从0到这个数字内有多少个素数。
老办法先实现后优化,实现之后发现优化真不容易,借助外界各种资源后,差不多算是达到了自己想达到的效果,故记录一下此次一波三折的优化。
JavaScript的递归以及浅谈尾调用
一个递归引发的血案
案发现场
今天看到了递归,测试的时候必须是经典的 Fibonacci (斐波纳契) 数列,然后我做的时候的代码如下:1
2
3
4function Fibonacci (n) {
if ( n <= 1 ) {return 1};
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
这是一个很简答的递归函数,单纯且不做作。他很简单,看一眼就知道他是如何运算的,单纯到到计算一个Fibonacci(100)就给我来个栈溢出错误(stack overflow),然后我很奇葩的算了一下Fibonacci(25),然后就卡-住-了,这不行啊,所以我就去搜原因去咯。
发现个异或的妙用
有个题目是这样子的:Single Number
题目要求如下:Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
意思就是一个数组[2,2,3,3,5] 5就是那个 single numble了
然后自己想了半天也就会笨办法,倒也能通过,然后看了一下前面人的解法,又一次打开了脑洞
从Power_of_Two所得
开始刷leetcode上的题目了,毕竟写程序也是需要开脑洞的,刷题目就能让自己脑洞越来越大的说,这不发现了一个题目: Power of Two
题目要求:
Given an integer, write a function to determine if it is a power of two.
其实就是让你看一个数是不是2的幂次方