05 2018 档案
线性筛素数、欧拉函数
摘要:判断一个数n是否是素数,众所周知可以用O(sqrt(n))的方法。 但是如果要求很多个数,这个方法就不太好了。(比如所有小于n的数,复杂度就是O(n1.5)。) 埃拉托斯特尼筛法,大家都听说过。从2到n,去掉每个数的倍数,剩下来的就是质数。 不过这个方法会重复删除,比如6是2、3的倍数,会被删2次, 阅读全文
posted @ 2018-05-27 11:34 苛性氢 阅读(410) 评论(0) 推荐(0)
数学女孩1 笔记
摘要:知识点如下: 质因数分解定理。 算术基本定理,又称为正整数的唯一分解定理,即:每个大于1的自然数均可写为质数的积,而且这些素因子按大小排列之后,写法仅有一种方式。 因此,1不是质数。 给定一个正整数n,求n的所有约数的和。 首先进行质因数分解。 将正整数 n 进行质因数分解,一般都可以写成以下形式。 阅读全文
posted @ 2018-05-21 22:55 苛性氢 阅读(309) 评论(0) 推荐(0)
leetcode 198 打家劫舍 (House Robber)
摘要:class Solution { public: int rob(vector<int>& nums) { int last,next2Last,ans; if(nums.empty()){ return 0; } if(nums.size()==1){ return nums[0]; } next 阅读全文
posted @ 2018-05-21 20:55 苛性氢 阅读(348) 评论(0) 推荐(0)
leetcode 53 最大子序和 (Maximum Subarray)
摘要:class Solution { public: int maxSubArray(vector<int>& nums) { int pre=nums[0]; //int now; int maxSum=nums[0]; for(int i=1;i<nums.size();i++){ if(pre>0 阅读全文
posted @ 2018-05-21 20:48 苛性氢 阅读(79) 评论(0) 推荐(0)
leetcode 121 买卖股票的最佳时机 (Best Time to Buy and Sell Stock)
摘要:class Solution { public: int maxProfit(vector<int>& prices) { if(prices.empty()){ return 0; } int minpri=prices[0]; int prof=0; for(int i=0;i<prices.s 阅读全文
posted @ 2018-05-21 20:42 苛性氢 阅读(180) 评论(0) 推荐(0)
leetcode 70 爬楼梯 (Climbing Stairs)
摘要:最后一步走1、2个台阶。 dp[i+1]=dp[i]+dp[i-1] class Solution { public: int climbStairs(int n) { int *dp=new int[n+1]; dp[1]=1; dp[2]=2; for(int i=3;i<=n;i++){ dp 阅读全文
posted @ 2018-05-21 20:40 苛性氢 阅读(156) 评论(0) 推荐(0)
leetcode 88 合并两个有序数组 (Merge Sorted Array)
摘要:归并。不过是从后向前。因为不能再开数组。 class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i=n+m-1; n--; m--; for(;i>=0;i--) 阅读全文
posted @ 2018-05-20 21:53 苛性氢 阅读(141) 评论(0) 推荐(0)
leetcode 278 第一个错误的版本 (First Bad Version)
摘要:很简单的二分查找。 弱智一开始直接复制mid=(1+n)/2 结果TLE,愣是没发现错误。 以为方法错了。 然后复制别人代码,int溢出。 // Forward declaration of isBadVersion API. bool isBadVersion(int version); clas 阅读全文
posted @ 2018-05-13 22:16 苛性氢 阅读(231) 评论(0) 推荐(0)
leetcode 102 二叉树的层次遍历 (Binary Tree Level Order Traversal)
摘要:我的方法。每个队列保存一层的node: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v 阅读全文
posted @ 2018-05-13 18:29 苛性氢 阅读(178) 评论(0) 推荐(0)
leetcode 108 将有序数组转换为二叉搜索树 (Convert Sorted Array to Binary Search Tree)
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r 阅读全文
posted @ 2018-05-13 16:52 苛性氢 阅读(88) 评论(0) 推荐(0)
leetcode 101 对称二叉树
摘要:一开始想中序遍历,然后左右判断。 但是是不对的。如果有空节点的话。 于是,空节点加数。 [1,2,3,3,null,2,null] 还是错。两个空加两个0.0,3,0,2,0,1,0,2,0,3,0 再改:只有一个空才加。(指针不能异或,转成longlong)。 [5,4,1,null,1,null 阅读全文
posted @ 2018-05-11 20:50 苛性氢 阅读(114) 评论(0) 推荐(0)
《图解TCP/IP》 疑问 数据链路 p78 79
摘要:p78 为什么要反转顺序? p79 既然可以自己设置mac地址,如何检测未知厂商网卡?冒充已知的会有破绽吗? 阅读全文
posted @ 2018-05-10 19:42 苛性氢 阅读(128) 评论(0) 推荐(0)
艾佛森括号
摘要:艾佛森括号 阅读全文
posted @ 2018-05-08 22:55 苛性氢 阅读(383) 评论(0) 推荐(0)
leetcode 104 二叉树的最大深度 (Maximum Depth of Binary Tree)
摘要:别人的代码,不用vector,不过每次要比左右两个。 相当于数组中每个元素和前后比较一下。需要确定前后,i.e.,min,max。 不对。不过,递归到当前节点,不考虑下面时,确实是相当于前后。至于当前节点和下面节点的关系,递归到下面的时候会判断。 有道词典 相当于数组中每个 ... 详细X Equa 阅读全文
posted @ 2018-05-08 22:03 苛性氢 阅读(129) 评论(0) 推荐(0)
leetcode 234 回文链表
摘要:public class Hello { public static void main(String[] args) { ListNode head=new ListNode(0); ListNode head1=new ListNode(1); ListNode head2=new ListNo 阅读全文
posted @ 2018-05-07 16:11 苛性氢 阅读(142) 评论(0) 推荐(0)
leetcode 141 环形链表
摘要:1.我想到的用set。 2. 双指针,不使用额外空间。 一个速度为2,一个速度为1。 https://blog.csdn.net/nomasp/article/details/51598735 作者没给出证明。我的证明如下: node == n car = (2i+1) //%n man= i // 阅读全文
posted @ 2018-05-04 22:56 苛性氢 阅读(108) 评论(0) 推荐(0)
计算机 数学 实际 美
摘要://不知道起什么标题好…… 计算机更注重实际,我以前是这样想的。 求一个数的所有约数的和,有公式。 但是和求出所有因子再相加比起来,哪个快? 因此我觉得数学好不一定就算法好。 公式美,但是实际怎么做的? Fibonacci数列也有通项公式。书上讲递归的时候经常用Fibonacci举例。 和用通项公式 阅读全文
posted @ 2018-05-04 20:29 苛性氢 阅读(151) 评论(0) 推荐(0)
是否有无穷多组基本勾股数
摘要:是。我的方法如下: 设a为任意大于1的奇数,所以a2也是奇数,b=(a2-1)/2,c=(a2+1)/2, => (c-b)(c+b)=1*a2 => a2+b2=c2 因为b+1=c,所以b,c互质。 a,b也互质,否则b,c不互质。a,c同理。 因为a为(……),所以有无穷多组基本勾股数。 很多 阅读全文
posted @ 2018-05-04 19:31 苛性氢 阅读(790) 评论(0) 推荐(0)
旋转矩阵
摘要:/* to be edited 回去找找4-2,4-4 */ 在《数学女孩》中看到三角和矩阵,这才想起有个叫旋转矩阵的东西。 我是在哪学到的呢,4-2?4-4?线代? 阅读全文
posted @ 2018-05-04 18:56 苛性氢 阅读(162) 评论(0) 推荐(0)
基础知识学习:unordered_map
摘要:是容器,所以有 迭代器:begin(), end(). 容量:empty(),size() 插入:insert 直接赋值:up[x]=y; 查找:find,count 阅读全文
posted @ 2018-05-01 15:10 苛性氢 阅读(128) 评论(0) 推荐(0)