随笔分类 -  problems

1 2 3 4 5 6 下一页

leetcode 443. String Compression
摘要:下面反向遍历,还是正向好。 其他答案: 阅读全文

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

leetcode 441. Arranging Coins
摘要:解方程: 阅读全文

posted @ 2018-03-09 09:57 willaty 阅读(115) 评论(0) 推荐(0)

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

posted @ 2018-02-27 18:18 willaty 阅读(135) 评论(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 阅读(1067) 评论(0) 推荐(0)

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

posted @ 2018-02-27 11:48 willaty 阅读(141) 评论(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 阅读(101) 评论(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 阅读(115) 评论(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 阅读(132) 评论(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 阅读(100) 评论(0) 推荐(0)

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

posted @ 2018-02-11 16:44 willaty 阅读(96) 评论(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 阅读(168) 评论(0) 推荐(0)

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

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

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

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

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

posted @ 2018-02-11 10:16 willaty 阅读(116) 评论(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 阅读(76) 评论(0) 推荐(0)

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

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

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

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

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

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

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

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

1 2 3 4 5 6 下一页

导航