02 2018 档案

leetcode 504. Base 7
摘要:转成7进制,辗转相除。 阅读全文

posted @ 2018-02-27 18:18 willaty 阅读(137) 评论(0) 推荐(0)

leetcode输入输出加速
摘要:C++兼容C的输入输出,即cin与scanf混用文件指针不会出错,cout亦同,导致cin有额外开销。 可以用std::ios::sync_with_stdio(false);手动关闭。 cin.tie(NULL)用于解除cin与cout绑定。 阅读全文

posted @ 2018-02-27 14:57 willaty 阅读(1071) 评论(0) 推荐(0)

leetcode 437. Path Sum III
摘要:方法一: 直接dfs。 方法二: 用哈希保存从根节点到当前节点的和,减去sum,看是否在哈希中出现过,出现过则加上。 阅读全文

posted @ 2018-02-27 11:48 willaty 阅读(144) 评论(0) 推荐(0)

leetcode 434. Number of Segments in a String
摘要:int countSegments(string s) { int ret = 0; bool flag = false; for (auto i : s) { if (!flag && i != ' ') { ret++; flag = true; ... 阅读全文

posted @ 2018-02-26 11:12 willaty 阅读(105) 评论(0) 推荐(0)

leetcode 415. Add Strings
摘要:string addStrings(string num1, string num2) { bool carry = false; int len1 = num1.size(); int len2 = num2.size(); int max_len = len1 >= len2 ? len1 : len2; ... 阅读全文

posted @ 2018-02-26 11:05 willaty 阅读(117) 评论(0) 推荐(0)

leetcode 412. Fizz Buzz
摘要:vector fizzBuzz(int n) { vector ret; for (int i = 1; i <= n; i++) { bool three = i % 3 == 0; bool five = i % 5 == 0; if (three && five... 阅读全文

posted @ 2018-02-18 14:08 willaty 阅读(134) 评论(0) 推荐(0)

leetcode 409. Longest Palindrome
摘要:int longestPalindrome(string s) { unordered_map m; for (char i : s) m[i]++; bool flag = false; int ret = 0; for (auto i : m) { ... 阅读全文

posted @ 2018-02-18 14:07 willaty 阅读(102) 评论(0) 推荐(0)

leetcode 405. Convert a Number to Hexadecimal
摘要:辗转相除法。对于负数,转为unsigned int即可。 阅读全文

posted @ 2018-02-11 16:44 willaty 阅读(97) 评论(0) 推荐(0)

leetcode 404. Sum of Left Leaves
摘要:int sumOfLeftLeaves(TreeNode* root) { if (root == NULL) return 0; int sum = 0; if (root->left && root->left->left == NULL && root->left->right == NULL... 阅读全文

posted @ 2018-02-11 16:14 willaty 阅读(84) 评论(0) 推荐(0)

leetcode 400. Nth Digit
摘要:题意: 将1,2,3,4...无限序列,组成一个视作一个连续序列,取第n位。 通俗点,就是123456789101112...,比如第11位就是0,由于第11位在10里面。 思路: 换算为实际数字及位数,就是麻烦点。 阅读全文

posted @ 2018-02-11 15:44 willaty 阅读(169) 评论(0) 推荐(0)

leetcode 389. Find the Difference
摘要:注意,两个字符串顺序可以是乱的。 两种思路: 直接双哈希。异或。 阅读全文

posted @ 2018-02-11 11:15 willaty 阅读(174) 评论(0) 推荐(0)

leetcode 387. First Unique Character in a String
摘要:哈希存索引,再遍历即可。 阅读全文

posted @ 2018-02-11 10:58 willaty 阅读(148) 评论(0) 推荐(0)

leetcode 383. Ransom Note
摘要:用magazine中的字母拼接处ransomNote,都是小写。 对magazine的字母哈希,保存出现的次数,再用ransomNote遍历减小。 这里有两个注意的点。 1.一开始用unordered_map,居然比直接遍历还慢。 2.这里优化了一下,不直接对magazine全哈希,需要才哈希,避免 阅读全文

posted @ 2018-02-11 10:16 willaty 阅读(121) 评论(0) 推荐(0)

leetcode 383. Ransom Note
摘要:bool canConstruct(string ransomNote, string magazine) { unordered_map m; for (auto i : magazine) m[i]++; for (auto i : ransomNote) { if (m... 阅读全文

posted @ 2018-02-09 18:36 willaty 阅读(80) 评论(0) 推荐(0)

leetcode 374. Guess Number Higher or Lower
摘要:二分。 阅读全文

posted @ 2018-02-09 18:25 willaty 阅读(89) 评论(0) 推荐(0)

leetcode 371. Sum of Two Integers
摘要:异或其实是无进位加法,与取进位。 阅读全文

posted @ 2018-02-09 18:15 willaty 阅读(88) 评论(0) 推荐(0)

leetcode 367. Valid Perfect Square
摘要:二分。 阅读全文

posted @ 2018-02-09 16:26 willaty 阅读(93) 评论(0) 推荐(0)

leetcode 350. Intersection of Two Arrays II
摘要:两个哈希。 阅读全文

posted @ 2018-02-09 13:03 willaty 阅读(140) 评论(0) 推荐(0)

leetcode 349. Intersection of Two Arrays
摘要:找出两个数组相同的元素。返回相同的元素集合即可。 两个思路: 1. 先将num1转为集合set1(去重),再遍历num2,查找set1中有没有出现。 2. 哈希,用两个unordered_map<int, bool>,再遍历即可。 这里用方法2: 阅读全文

posted @ 2018-02-09 12:14 willaty 阅读(107) 评论(0) 推荐(0)

leetcode 345. Reverse Vowels of a String
摘要:string reverseVowels(string s) { for (int i = 0, j = s.size() - 1; i < j;) { bool l = isVowels(s[i]); bool r = isVowels(s[j]); if (l && r) ... 阅读全文

posted @ 2018-02-08 15:45 willaty 阅读(107) 评论(0) 推荐(0)

leetcode 344. Reverse String
摘要:string reverseString(string s) { int size = (s.size() >> 1); for (int i = 0; i < size; i++) { swap(s[i], s[s.size() - i - 1]); } return move(s); } ... 阅读全文

posted @ 2018-02-08 15:37 willaty 阅读(85) 评论(0) 推荐(0)

leetcode 342. Power of Four
摘要:没想出来不用循环的。记录下。 如果是2的次方,必有num & (nums - 1) 或者第二步排除,直接(num - 1) % 3为0也可以,如果是4的倍数,减1后必能被3整除,从二进制角度考虑下即可。 不建议用对数,涉及浮点数,精度问题,见:https://discuss.leetcode.com 阅读全文

posted @ 2018-02-08 15:27 willaty 阅读(98) 评论(0) 推荐(0)

leetcode 326. Power of Three
摘要:判断是否是3的次方。 找了下二进制和十进制规律,没有明显规律。 要么对数;要么利用int下最大3的次方:哈希或整除。 阅读全文

posted @ 2018-02-08 14:59 willaty 阅读(94) 评论(0) 推荐(0)

leetcode 303. Range Sum Query - Immutable
摘要:关键在于,想出类似最大子数组和中的一种解法。 缓存每个数前的所有和,需要的时候,减一下即可。 阅读全文

posted @ 2018-02-08 14:42 willaty 阅读(89) 评论(0) 推荐(0)

leetcode 292. Nim Game
摘要:博弈论经典。 阅读全文

posted @ 2018-02-08 14:19 willaty 阅读(95) 评论(0) 推荐(0)

leetcode 290. Word Pattern
摘要:普通的做法就是两个哈希。 这里用了一点小技巧,比如a和b双射,不相互保存,而是都保存一个数字,之后每次增加映射,数字都增加1。 节省空间用。 阅读全文

posted @ 2018-02-08 14:15 willaty 阅读(104) 评论(0) 推荐(0)

leetcode 278. First Bad Version
摘要:int firstBadVersion(int n) { // long start = 1; // long end = n; int start = 1; int end = n; int last = -1; while (start <= end) { ... 阅读全文

posted @ 2018-02-08 11:55 willaty 阅读(104) 评论(0) 推荐(0)

leetcode 283. Move Zeroes
摘要:第一次写的代码,实际效率是O(n)的,但判断多了一点。 其实可以很简洁: 阅读全文

posted @ 2018-02-08 10:58 willaty 阅读(109) 评论(0) 推荐(0)

leetcode 268. Missing Number
摘要:原地哈希。代码没怎么优化,思路清晰。 阅读全文

posted @ 2018-02-08 09:54 willaty 阅读(101) 评论(0) 推荐(0)

leetcode 263. Ugly Number
摘要:bool isUgly(int num) { while (num < 1) return false; while (num % 2 == 0) num /= 2; while (num % 3 == 0) num /= 3; while (num %... 阅读全文

posted @ 2018-02-07 15:00 willaty 阅读(83) 评论(0) 推荐(0)

leetcode 258. Add Digits
摘要:给个整数,每位相加,如果位数大于1,重复,知道剩一位。 附加要求:要求不用递归循环,O(1)。 看到这要求,肯定有规律: - D,从10开始推,一下子就看出来了。 阅读全文

posted @ 2018-02-07 14:09 willaty 阅读(94) 评论(0) 推荐(0)

leetcode 257. Binary Tree Paths
摘要:返回所有根到叶子的路径。用个“全局”的ret代替合的过程。 阅读全文

posted @ 2018-02-07 13:29 willaty 阅读(84) 评论(0) 推荐(0)

leetcode 242. Valid Anagram
摘要:哈希。 阅读全文

posted @ 2018-02-07 11:15 willaty 阅读(105) 评论(0) 推荐(0)

leetcode 237. Delete Node in a Linked List
摘要:由于题目已经限定范围,直接覆盖即可。 阅读全文

posted @ 2018-02-06 17:18 willaty 阅读(92) 评论(0) 推荐(0)

leetcode 234. Palindrome Linked List
摘要:快慢指针找到中点,反转后半段,比较。 其实慢指针走的时候就可以开始反转。 阅读全文

posted @ 2018-02-06 14:20 willaty 阅读(112) 评论(0) 推荐(0)

leetcode 232. Implement Queue using Stacks
摘要:class MyQueue { public: /** Initialize your data structure here. */ stack r, b; MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { ... 阅读全文

posted @ 2018-02-06 13:12 willaty 阅读(88) 评论(0) 推荐(0)

leetcode 231. Power of Two
摘要:bool isPowerOfTwo(int n) { if (n < 1) return false; int m = 1; for (int i = 0; i < 32; i++) { int j = n & m; if (j == n) return tru... 阅读全文

posted @ 2018-02-06 11:50 willaty 阅读(77) 评论(0) 推荐(0)

leetcode 226. Invert Binary Tree
摘要:TreeNode* invertTree(TreeNode* root) { if (root == NULL) return NULL; auto temp = root->left; root->left = root->right; root->right = temp; invert... 阅读全文

posted @ 2018-02-06 11:27 willaty 阅读(83) 评论(0) 推荐(0)

leetcode 225. Implement Stack using Queues
摘要:可以用两个队列倒来倒去,保留最后一个,pop效率成了线性的。 我这里用一个队列,自己倒自己,pop和top都是线性,时间换空间吧。 阅读全文

posted @ 2018-02-06 11:23 willaty 阅读(95) 评论(0) 推荐(0)

leetcode 219. Contains Duplicate II
摘要:哈希,注意第一个为0的情况。 阅读全文

posted @ 2018-02-06 11:01 willaty 阅读(92) 评论(0) 推荐(0)

leetcode 217. Contains Duplicate
摘要:哈希一下即可。 阅读全文

posted @ 2018-02-06 09:58 willaty 阅读(82) 评论(0) 推荐(0)

leetcode 206. Reverse Linked List
摘要:头插法,每次取出后插入新链表的头部。 阅读全文

posted @ 2018-02-06 09:49 willaty 阅读(73) 评论(0) 推荐(0)

字节序--大端,小端的缘由
摘要:基础: As we all know,字节序有大端和小端。 大端:低地址位存高位,如实际是(十六进制,左边是低地址):12 34,储存是12 34。 小端则相反。 对比: 各执一词,计算机处理是按偏移地址来,从低地址往高地址处理,各有各方便。 大端方便人阅读(没啥用)。 网络中: 网络上传输都用大端 阅读全文

posted @ 2018-02-02 13:29 willaty 阅读(595) 评论(0) 推荐(0)

转)MySQL日期与时间函数
摘要:以上转自:http://blog.csdn.net/qinshijangshan/article/details/72874667 类型有: 取当前时间 从date字段中取出部分 增减date日期 日期转换格式 字符串和日期转换 不同国家格式转换 时间比较 阅读全文

posted @ 2018-02-01 17:26 willaty 阅读(28577) 评论(0) 推荐(4)

SQL中的case when then else end用法
摘要:简洁: case属于控制流函数,属于mysql函数的范围。类似的还有ifnull,nullif等。 使用方法: 注意,只匹配第一个正确答案,后面的不管。 使用场景: 接select后面,修改展示内容;接group by后面,修改分组依据。 update的更新条件。 代码来源于:https://www 阅读全文

posted @ 2018-02-01 14:58 willaty 阅读(2468) 评论(0) 推荐(0)

leetcode 627. Swap Salary
摘要:将f改为m,m改为f。 注意: case在select后面,group by,update等地方都可以使用。 阅读全文

posted @ 2018-02-01 14:32 willaty 阅读(320) 评论(0) 推荐(0)

导航