这边文章简单记录leetcode的简单数学题目。
Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example:
Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
这道题目其实很简单,原先没有想到开始的逻辑,后来看到网上的介绍是说可以利用 HashmapSet数据结构来结束无限循环。好是好,但是我利用c语言目前还没有具体实现,所以,先偷个懒,利用一点数学的技巧。
数学上,非快乐数一定会出现4,至于为什么,我也没有搞明白。那么,以后遇到这样的问题怎么办?我觉得最好尝试着写出几个数字,来看看其中有没有相同点。 下面是第一版的解决方法。
bool isHappy(int n){
int sum;
while ( n != 1 && n != 4){
sum = 0;
while(n){
sum += (n % 10) * (n % 10);
n /= 10;
}
n = sum;
}
return n == 1;
}
上面的代码段中有一点是如何统计一个整数的各位数字,这个思想值利用,还有一个就是先除再乘。
计算一个整数范围内质数的个数. 版本1:
bool is_prime(int n){
int i;
int flag = 1;
if (n <= 1)
return false;
for(i = 2; i * i <= n; i++){
if(n % i == 0){
flag = 0;
break;
}
}
return flag;
}
int countPrimes(int n){
int res = 0;
int i;
for(i = 1; i < n;i++){
if(is_prime(i))
res++;
}
return res;
}
求一个int的sqrt,我首先使用的暴力求解, 简直惨不忍睹,
class Solution {
public:
int mySqrt(int x) {
if (x == 0) {
return 0;
}
int i = 0, ret = 0;
while(i * i < x){
i++;
if (i == 46340)
break;
}
if (i * i > x)
return i - 1;
else
return i;
}
};
One: Accept the invitation to peer review
Two: Read the paper
Three: write a brief summary of the article and its contribution
Four: write out your major criticisms of the paper
Five: Review
A: Is well-organized ?
B: Does the article contain all of the compentions you would expect?
C: Are the sections well-developed?
D: Does the author do a good job of synthesizing the literature?
E: Does the author answer the question he/she sets out to answer?
F: Is the methodologe clearly explained?
G: Does the theory connect to the data?
I: Is the article well-written and easy understding?
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
其中的一个解决方案为:
int reverse(int x) {
long int ps = 0;
while(x){
if(ps > INT_MAX/10 || ps < INT_MIN/10)
return 0;
ps = x % 10 + ps * 10;
x /= 10;
}
return ps;
}
这里值得注意的就是,INT_MAX与INT_MIN是定义在limits.h头文件中
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
所以,如果输入1534236469,期待结果为0(有点看不懂),文章来自here解释的非常清楚:
We want to repeatedly “pop” the last digit off of xx and “push” it to the back of the \text{rev}rev. In the end, \text{rev}rev will be the reverse of the xx. To “pop” and “push” digits without the help of some auxiliary stack/array, we can use math.
//pop operation:
pop = x % 10;
x /= 10;
//push operation:
temp = rev * 10 + pop;
rev = temp;
However, this approach is dangerous, because the statement \text{temp} = \text{rev} \cdot 10 + \text{pop}temp=rev⋅10+pop can cause overflow. Luckily, it is easy to check beforehand whether or this statement would cause an overflow. To explain, lets assume that \text{rev}rev is positive.
https://blog.csdn.net/twt520ly/article/details/53038345
这两个leetcode 题目基本是很简单的一个类型,只要你把key自动填充到对应的数组位置即可, 但是,这样太直接了,效率上来说应该会有问题。所以,这并不是最优解。
#define NUM 1000010
typedef struct {
int *arr;
} MyHashMap;
/** Initialize your data structure here. */
MyHashMap* myHashMapCreate() {
MyHashMap *obj = malloc(sizeof(MyHashMap));
if(!obj)
return ;
obj->arr = (int *)malloc(sizeof(int) * NUM);
if(!obj->arr) // here may be a problem
return ;
memset(obj->arr, -1, NUM);
return obj;
}
/** value will always be non-negative. */
void myHashMapPut(MyHashMap* obj, int key, int value) {
obj->arr[key] = value;
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int myHashMapGet(MyHashMap* obj, int key) {
if(obj->arr[key] != -1)
return obj->arr[key];
return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void myHashMapRemove(MyHashMap* obj, int key) {
if(obj->arr[key] == -1)
return;
obj->arr[key] = -1;
}
void myHashMapFree(MyHashMap* obj) {
free(obj);
}
/**
* Your MyHashMap struct will be instantiated and called as such:
* MyHashMap* obj = myHashMapCreate();
* myHashMapPut(obj, key, value);
* int param_2 = myHashMapGet(obj, key);
* myHashMapRemove(obj, key);
* myHashMapFree(obj);
*/
gcc作为一个编译器,在unix编程占有绝对重要的地位,目前本人对他的研究极少,不过有时候在内核中看到 一些古怪的用法,特地记录下来。
There is one error:
libbpf.c:68:26: error: format string is not a string literal [-Werror,-Wformat-nonliteral] return vfprintf(stderr, format, args); ^~~~~~ Solved:
+/* vsprintf() in __base_pr() uses nonliteral format string. It may break
+ * compilation if user enables corresponding warning. Disable it explicitly.
+ */
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+