上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 28 下一页
摘要: 一个数可以使用多次图: 节点:x(当前的和,当前要考虑的数a[i]) 边:x-> y1(当前的和,下一个要考虑的数a[i+1]) y2(当前的和+a[i],下一个要考虑的数a[i+1])BFS 如何求具体解? 队列里放全部的“部分解”——浪费空间 每个节点存放到它的前一个节点—... 阅读全文
posted @ 2015-12-02 13:53 ZH奶酪 阅读(302) 评论(0) 推荐(0) 编辑
摘要: C++Traverse 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode... 阅读全文
posted @ 2015-12-02 13:12 ZH奶酪 阅读(321) 评论(0) 推荐(0) 编辑
摘要: C++Binary Search陷阱:找到index最小的。 1 class Solution { 2 public: 3 /** 4 * @param nums: The integer array. 5 * @param target: Target number t... 阅读全文
posted @ 2015-12-02 12:47 ZH奶酪 阅读(555) 评论(0) 推荐(0) 编辑
摘要: 开始没看懂题目的意思,以为是输入一个单链表,删掉链表中间的那个节点。实际的意思是,传入的参数就是待删节点,所以只要把当前节点指向下一个节点就可以了。C++ 1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 ... 阅读全文
posted @ 2015-12-02 12:34 ZH奶酪 阅读(277) 评论(0) 推荐(0) 编辑
摘要: C++ 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val... 阅读全文
posted @ 2015-12-02 11:44 ZH奶酪 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 给定字符串序列和一个字典,问给定的字符串能否用字典中的单词拼出来?图: 节点:字符串的前缀长度 边:前缀x如果加一个字典里边的单词能形成新前缀x',则有一条边 例如:字符串IAMxxxxx,字典里有I和AM 则有(0,1)一条边,(1,3)一条边解:从(0,?)到(?,n)找一条路径... 阅读全文
posted @ 2015-12-02 11:19 ZH奶酪 阅读(636) 评论(0) 推荐(0) 编辑
摘要: 分析:经典连通分量问题图: 节点:所有1的位置 边:两个相邻的1的位置有一条边BFS/DFS (DFS使用递归,代码较短) 选一个没标记的点,然后搜索,扩展4个邻居(如果有),直到不能扩展 每一次是一个连通分量 难点:标记节点——判重C++DFS 1 class Solution { 2 ... 阅读全文
posted @ 2015-12-02 10:35 ZH奶酪 阅读(292) 评论(0) 推荐(0) 编辑
摘要: 输入n,输出n个左括号和n个右括号的合法括号序列关键:当前位置的左括号不少于右括号图: 节点:当前位置左括号和右括号的个数(x, y)(x>=y) 边:从(x, y)到(x+1, y)或(x,y+1) x==y时,只有(x+1,y)这条边解:从(0,0)出发到(n,n)的全部路径=======... 阅读全文
posted @ 2015-12-02 10:06 ZH奶酪 阅读(320) 评论(0) 推荐(0) 编辑
摘要: C++使用hash map存储每个子串出现的次数 1 #include 2 #include 3 #include 4 using namespace std; 5 6 pair fun(const string& str) { 7 int len = str.length(); 8... 阅读全文
posted @ 2015-12-01 18:42 ZH奶酪 阅读(746) 评论(0) 推荐(0) 编辑
摘要: C++暴力搜索两个游标一个长度i遍历aj遍历blen遍历公共子串长度 1 class Solution { 2 public: 3 /** 4 * @param A, B: Two string. 5 * @return: the length of the lo... 阅读全文
posted @ 2015-12-01 12:18 ZH奶酪 阅读(301) 评论(0) 推荐(0) 编辑
摘要: C++去掉二进制最右边的1 1 class Solution { 2 public: 3 /* 4 * @param n: An integer 5 * @return: True or false 6 */ 7 bool checkPowerOf2(i... 阅读全文
posted @ 2015-12-01 12:02 ZH奶酪 阅读(271) 评论(0) 推荐(0) 编辑
摘要: C++ 1 class Solution { 2 public: 3 /** 4 * param n: As description. 5 * return: A list of strings. 6 */ 7 vector fizzBuzz(int n... 阅读全文
posted @ 2015-12-01 11:54 ZH奶酪 阅读(274) 评论(0) 推荐(0) 编辑
摘要: C++把3个数求和,转变为2个数求和1. 把数组排序2. 注意过滤重复值3. 从前到后遍历,游标i4. 从后边数中找start + end = -arr[i]的2 sum5. start + end -arr[i], end--7. start + end = -arr[i], insert i... 阅读全文
posted @ 2015-12-01 11:48 ZH奶酪 阅读(356) 评论(0) 推荐(0) 编辑
摘要: C++hash map把查找2个数的过程转换为查找1个数借用STL容器 unordered_map 1 class Solution { 2 public: 3 /* 4 * @param numbers : An array of Integer 5 * @param ... 阅读全文
posted @ 2015-12-01 11:31 ZH奶酪 阅读(270) 评论(0) 推荐(0) 编辑
摘要: 通过交换,对0,1,2排序使用三个标记[循环不变式]i从前向后,记录最后一个0的位置j从后向前,记录第一个2的位置k从前向后,是遍历用的游标[0..i-1]是0[i..k-1]是1[k,j-1]是未探测[j..n-1]是2初始k=0时,0,1,2的区域都是空,所有区域都是未探测,循环k=0..n-1... 阅读全文
posted @ 2015-12-01 11:19 ZH奶酪 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 求第k个值1.归并排序归并到第k个值为止时间复杂度:O(k) 1 class Solution { 2 public: 3 // merge-sort to find K-th value 4 double helper(vector A, vector B, int lenA, i... 阅读全文
posted @ 2015-12-01 10:10 ZH奶酪 阅读(290) 评论(0) 推荐(0) 编辑
摘要: 1.设查找的数位y,第一行最后一列的数位x如果xy,x是最后一列最小的,所以最后一列都大于y,删除最后一列;这样保证x永远在可能有解的矩阵的第一行,最后一列。时间复杂度:O(m+n) 1 class Solution { 2 public: 3 /** 4 * @param mat... 阅读全文
posted @ 2015-12-01 10:06 ZH奶酪 阅读(305) 评论(0) 推荐(0) 编辑
摘要: C++ 1 class Solution { 2 public: 3 /** 4 * @param x: An integer 5 * @return: The sqrt of x 6 */ 7 int sqrt(int x) { 8 /... 阅读全文
posted @ 2015-11-30 13:03 ZH奶酪 阅读(295) 评论(0) 推荐(0) 编辑
摘要: C++逆推 1 class Solution { 2 public: 3 /** 4 * @param triangle: a list of lists of integers. 5 * @return: An integer, minimum path sum. 6 ... 阅读全文
posted @ 2015-11-30 12:35 ZH奶酪 阅读(271) 评论(0) 推荐(0) 编辑
摘要: C++双向递推空间O(1)时间O(n) 1 class Solution { 2 public: 3 /** 4 * @param A an array of Integer 5 * @return an integer 6 */ 7 int long... 阅读全文
posted @ 2015-11-30 12:08 ZH奶酪 阅读(188) 评论(0) 推荐(0) 编辑
上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 28 下一页