素数筛法和哥德巴赫猜想
2019-01-03
起因是看到了这个帖子如果高中生能证明哥德巴赫猜想,会被清华北大数学系保送吗? - 知乎,emmm… 本文的目标是筛选 32 位无符号整数(…
CSAPP 信息的表示和处理
2018-12-25
2.1 信息存储 2.1.1 十六进制表示法 十六进制转换窍门:记住 A、C 和 F 对应的值,B、D 和 E 可通过计算它们与前三个值的相对关系来完成。 对于 2 的非负整数 n 次…
CSAPP 计算机系统漫游
2018-12-24
1.1 信息就是二进制位+上下文 例如 C 语言源码: hello.c #include <stdio.h> int main() { printf("hello, world\n"); return 0; } 用 vim 以二进制方式打开 vim -b hello.c,由于是纯 ASCII 码写成的,所以和以文本方式…
幂运算 pow(x, n) 的一个迭代实现
2018-08-24
求一个浮点数的整数次幂: https://leetcode.com/problems/powx-n/ Implement pow(x, n), which calculates x raised to the power n (i.e. xn). Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 Example 3: Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Constraints: -100.0…
第一个大于等于某个正整数的 2 的幂
2018-07-13
在计算机中,2 的幂是神奇的数,很多运算可以用位操作来完成。 求第一个大于等于某个正整数的 2 的幂,例如 1023 则返回 1024,这个算法在 Java 中哈希表分配…
通配符匹配 Wildcard Matching
2018-06-12
通配符匹配问题: https://leetcode.com/problems/wildcard-matching/ Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?’ and ‘*’. ‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains…
数组中首个缺失的正整数 First Missing Positive
2018-06-08
找出数组中首个缺失的正整数: https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1 Note: Your algorithm should run in O(n) time and uses constant extra space. 看起来很简单,首先想到排序…
在 Java 中使用 Map 计数的几种姿势
2018-06-02
一个老生常谈的问题:在 Java 中,如何使用 Map 给对象计数,例如统计字符串出现的次数? 姿势一:containsKey() Map<String, Integer> map = new HashMap<>(); for (String word : words) { if (map.containsKey(word)) map.put(word,…