08 2015 档案

摘要:自己想了两天都没思路啊啊啊啊啊我真是太笨了。看了他的 tag,说是用到了动态规划,于是特意看了算法导论里动态规划的部分。然而只是说了其思想,第一步构建合理的数据结构,第二部以递归的形式求解。可见水无常形,动态规划并不是单纯的公式就可以解决的。那么具体如何处理呢?第一是,什么时候会用到动态规划,就是这... 阅读全文
posted @ 2015-08-28 23:40 wu_overflow 阅读(1255) 评论(0) 推荐(0)
摘要:我已开始的策略其实是直接转字符串操作一下完了:int reverse(int x) { if (x == 0){ return 0; } while (x % 10 == 0){ x /= 10; } auto&& s = to_string... 阅读全文
posted @ 2015-08-26 11:42 wu_overflow 阅读(259) 评论(0) 推荐(0)
摘要:就是字面意思,负数不算回文数。但可能是我的英语太差,所以看到 "Do this without extra space." 的时候,觉得是不能用其他的变量,因为要往栈上分配空间。所以觉得好难,以为要从回文数的数学性质入手,因此看了一些资料仍觉无从下手。直到看到了一些 Accepted 的解决方案……... 阅读全文
posted @ 2015-08-26 08:30 wu_overflow 阅读(173) 评论(0) 推荐(0)
摘要:我用的方法是,同时保存每个 string 的begin 迭代器,让他们一起移动,然后统一比较,若是不同,或其中有的已经到了 end() ,那么就返回。否则所有的迭代器共同前进一步。string longestCommonPrefix(vector& strs) { if (strs.empty... 阅读全文
posted @ 2015-08-25 18:16 wu_overflow 阅读(228) 评论(0) 推荐(0)
摘要:ListNode* removeNthFromEnd(ListNode* head, int n) { function check; check = [&](ListNode* node)->ListNode* { if (node == nullptr){ ... 阅读全文
posted @ 2015-08-25 01:49 wu_overflow 阅读(159) 评论(0) 推荐(0)
摘要:就是在一个只有括号的字符串里的括号是否合法。 所谓合法就是成对,比如 "{}[](){([])}" 但是这样就是不对的 “[((((])” 以及包含的顺序不对 “([)]” 都判为非法。bool isValid(string s) { unordered_map parenthesesDic{... 阅读全文
posted @ 2015-08-24 21:21 wu_overflow 阅读(392) 评论(0) 推荐(0)
摘要:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == nullptr){ return l2; } if (l2 == nullptr){ return l1; } ... 阅读全文
posted @ 2015-08-24 16:58 wu_overflow 阅读(118) 评论(0) 推荐(0)
摘要:从标准输入读入一系列string对象,寻找连续重复出现的单词程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现自己本身,跟踪重复次数多的单词及其重复次数.输出重复次数的最大值。没错,C++ 吧里有人问的,我觉得如果只是单纯的想知道重复次数最多的单词,可以这样:void foo(){... 阅读全文
posted @ 2015-08-24 13:37 wu_overflow 阅读(259) 评论(0) 推荐(0)
摘要:int strStr(string haystack, string needle) { if (needle.empty()){ return 0; } if (haystack.length() (it - haystack.cbegin()); ... 阅读全文
posted @ 2015-08-24 03:07 wu_overflow 阅读(372) 评论(0) 推荐(0)
摘要:数出来,确实很符合字面意思。比如 “1” 念 “一个一” 那么得出的就是 “11”; “11” 念 “两个 1” 那么得出的就是 “21”; “21” 念出来是 “一个二一个一” 那么 “1211”......我的代码很笨拙,是这样的:string countAndSay(int n) { i... 阅读全文
posted @ 2015-08-23 13:18 wu_overflow 阅读(157) 评论(0) 推荐(0)
摘要:int removeDuplicates(vector& nums) { if (nums.empty()){ return 0; } auto it = nums.begin() + 1; int currentVal = nums.front(); ... 阅读全文
posted @ 2015-08-22 18:32 wu_overflow 阅读(185) 评论(0) 推荐(0)
摘要:何为异构?内容排列的模式相同。例如 "piss" & "buzz", "OMG" & "LGD", "good" & "feet"...我之前的思路是先遍历第一个字符串,记录其中各个字符的重复情况作为一个 set> 然后遍历 set,把各个 vector 中的索引取出来对比是否相等,并由此来判断。可... 阅读全文
posted @ 2015-08-22 16:59 wu_overflow 阅读(519) 评论(0) 推荐(0)
摘要:所谓丑数,就是其素因子只包含 2,3,5 的非负数。另外, 1 也是丑数。恶心的是,当时题目说的是 positive number,这尼玛能包括 0?还就包括了。看明白定义就简单了,那就对着 2 3 5 玩命除呗,看看除完了还剩什么。bool isUgly(int num) { if (num... 阅读全文
posted @ 2015-08-22 15:17 wu_overflow 阅读(129) 评论(0) 推荐(0)
摘要:题干就是给一个非负整数,把各位数加起来,若超过一位,则继续把各位加起来,直到和是一位数。example: 39->12->3坦白说我是看了第三个提示意识到的,所以说要找规律,先要暴力列举。int addDigits(int num) { if (num == 0){ return... 阅读全文
posted @ 2015-08-22 14:22 wu_overflow 阅读(120) 评论(0) 推荐(0)
摘要:我的思路后来被证实是比较慢哈,思路很简单,递归地一行一行地往里插。是这样的:vector> generate(int numRows) { if (numRows >(); } if (numRows == 1){ return vector>{vector{... 阅读全文
posted @ 2015-08-15 22:50 wu_overflow 阅读(185) 评论(0) 推荐(0)
摘要:额,可能是我的理解能力太差吧,我还是要解释一下。例如 999, 那么传入的 vector 就是 9, 9, 9那么加一之后,返回的就是 1, 0, 0, 0vector plusOne(vector& digits) { ++*(digits.end() - 1); for (auto ... 阅读全文
posted @ 2015-08-13 23:21 wu_overflow 阅读(189) 评论(0) 推荐(0)
摘要:一开始还看错了我去,后来发现后改为:ListNode* deleteDuplicates(ListNode* head) { if (head == nullptr || head->next == nullptr){ return head; } int ... 阅读全文
posted @ 2015-08-13 22:15 wu_overflow 阅读(180) 评论(0) 推荐(0)
摘要:void merge(vector& nums1, int m, vector& nums2, int n) { if ((nums1.empty() && nums2.empty()) || (!nums1.empty() && nums2.empty())){ return;... 阅读全文
posted @ 2015-08-12 18:02 wu_overflow 阅读(165) 评论(0) 推荐(0)
摘要:其实和判断是否相同是一样的:bool isSymmetric(TreeNode* root) { if (root == nullptr) { return true; } function check; check = [&](TreeNode*& p... 阅读全文
posted @ 2015-08-12 16:00 wu_overflow 阅读(218) 评论(0) 推荐(0)
摘要:vector inorderTraversal(TreeNode* root) { vector inorderTraversalElems; function traversal; traversal = [&](TreeNode*& node) { ... 阅读全文
posted @ 2015-08-12 07:23 wu_overflow 阅读(179) 评论(0) 推荐(0)
摘要:bool isSameTree(TreeNode* p, TreeNode* q) { if (p == nullptr && q == nullptr){ return true; } else if (p == nullptr || q == nullptr){ ... 阅读全文
posted @ 2015-08-12 07:13 wu_overflow 阅读(241) 评论(0) 推荐(0)
摘要:都能求最小了,就能求最大:int maxDepth(TreeNode* root) { if (root == nullptr) { return 0; } constexpr int MIN_DEPTH = 0; constexpr int in... 阅读全文
posted @ 2015-08-11 21:50 wu_overflow 阅读(615) 评论(0) 推荐(0)
摘要:int minDepth(TreeNode* root) { if (root == nullptr) { return 0; } constexpr int MAX_DEPTH = INT16_MAX; constexpr int initLay... 阅读全文
posted @ 2015-08-11 17:23 wu_overflow 阅读(294) 评论(0) 推荐(0)
摘要:在我的 Xcode 上明明行为都是正常的,但是 leetcode 总是说测试到 “ab” 的时候,我的程序返回的是 true。我特么再试几次返回的都是 false,所以觉得是不是网站错了。首先是我一开始写的版本,简单倒是简单,只是效率低:bool isPalindrome(string s) { ... 阅读全文
posted @ 2015-08-11 16:22 wu_overflow 阅读(303) 评论(0) 推荐(0)
摘要:先安装 sublime REPL, 然后在用户自定义的键绑定中更新为以下代码:[ { "keys": ["super+shift+r"], "command": "repl_open", "caption": "Python", ... 阅读全文
posted @ 2015-08-11 13:04 wu_overflow 阅读(724) 评论(0) 推荐(0)
摘要:我一开始没想太多,觉得把标准库里现有的数据结构拼一下,完全就可以了,这是我的代码:class MinStack {private: multiset sortedElems; list stack;public: void push(int x) { sort... 阅读全文
posted @ 2015-08-11 02:26 wu_overflow 阅读(178) 评论(0) 推荐(0)
摘要:把这个像复杂了,因为之前判断链表是否回文的经验,想从递归的延迟求值的特性来从两个链表的终点递归来确定交集的头结点,甚难。后来看了一位朋友的博客,明白了原来是只要找第一个相同的头结点就行了,那这个就简单了。代码如下:ListNode *getIntersectionNode(ListNode *hea... 阅读全文
posted @ 2015-08-11 00:34 wu_overflow 阅读(411) 评论(0) 推荐(0)
摘要:uint32_t reverseBits(uint32_t n) { auto strBits = bitset(n).to_string(); return static_cast(bitset(string(strBits.crbegin(), strBits.crend())).t... 阅读全文
posted @ 2015-08-07 16:30 wu_overflow 阅读(178) 评论(0) 推荐(0)
摘要:void rotate(vector& nums, int k) { if (nums.empty() || k == 0 || nums.size() == 1){ return; } k %= nums.size(); vector newNums(... 阅读全文
posted @ 2015-08-07 15:40 wu_overflow 阅读(129) 评论(0) 推荐(0)
摘要:bool isHappy(int n) { // If not happy, it will finally recycle in the checkList. array checkList{4,16,37,58,89,145,42,20}; auto caculate ... 阅读全文
posted @ 2015-08-06 15:24 wu_overflow 阅读(281) 评论(0) 推荐(0)
摘要:链表和树都自带递归特性,我很喜欢。这一题很简单,有意思的是我是先把内部的 lambda 表达式写出来之后才发现可以直接用这个函数本身做递归。ListNode* removeElements(ListNode* head, int val) { if (head == nullptr){ ... 阅读全文
posted @ 2015-08-06 13:53 wu_overflow 阅读(131) 评论(0) 推荐(0)
摘要:bool containsNearbyDuplicate(vector& nums, int k) { unordered_map> numIndexListDic; for (size_t i = 0; i {i}; } else{ a... 阅读全文
posted @ 2015-08-05 12:35 wu_overflow 阅读(156) 评论(0) 推荐(0)
摘要:我自己的方法是用的递归,毕竟也是接触了一点点点点点点 scheme 的骚年是吧,代码如下:ListNode* reverseList(ListNode* head) { if (head == nullptr){ return nullptr; } ListN... 阅读全文
posted @ 2015-08-04 17:15 wu_overflow 阅读(164) 评论(0) 推荐(0)
摘要:bool containsDuplicate(vector& nums) { return !(nums.size() == unordered_set(nums.cbegin(), nums.cend()).size());} 阅读全文
posted @ 2015-08-04 08:15 wu_overflow 阅读(289) 评论(0) 推荐(0)
摘要:class Stack {private: queue que;public: // Push element x onto stack. void push(int x) { que.push(x); } // Removes the eleme... 阅读全文
posted @ 2015-08-04 07:47 wu_overflow 阅读(144) 评论(0) 推荐(0)
摘要:class Queue {private: stack in; stack out; void reverseStackToOut() { auto size = in.size(); for (size_t i = 0; i < size... 阅读全文
posted @ 2015-08-03 22:28 wu_overflow 阅读(186) 评论(0) 推荐(0)
摘要:def summary_ranges(nums) summary = []; if nums.size == 0 return summary end if nums.size == 1 return summary #{nums[i - 1]}"... 阅读全文
posted @ 2015-08-03 22:10 wu_overflow 阅读(203) 评论(0) 推荐(0)
摘要:思路很简单,出口是空节点,先翻转子节点,再返回。TreeNode* invertTree(TreeNode* root) { if (root == nullptr){ return root; } invertTree(root->left); ... 阅读全文
posted @ 2015-08-03 20:26 wu_overflow 阅读(229) 评论(0) 推荐(0)