
亚马逊
文章平均质量分 63
bjzhaoxiao
这个作者很懒,什么都没留下…
展开
-
239. Sliding Window Maximum
/* Memory Limit Exceeded Jacob Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. is OK*//*class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { ...原创 2018-05-17 09:28:11 · 171 阅读 · 0 评论 -
711. Number of Distinct Islands II
// Acception of others// After we get coordinates for an island, compute all possible rotations/reflections (https://en.wikipedia.org/wiki/Dihedral_group) of it //and then sort them using the default ...原创 2018-05-14 13:27:53 · 660 阅读 · 0 评论 -
126. Word Ladder II
//very interesting problem//It can be solved with standard BFS. The tricky idea is doing BFS of paths instead of words!//Then the queue becomes a queue of paths.class Solution {public: vector<vecto...原创 2018-05-14 12:26:57 · 198 阅读 · 0 评论 -
五大常用算法&实例列举
1.分治法·话说递归与HANOI塔·二分法求方程近似解·用C++实现合并排序·求最大值和最小值的分治算法2.动态规划法·动态规划求0/1背包问题·最长公共子串问题的实现·用动态规划实现导弹拦截·最大化投资回报问题的实现3.贪心算法·最小生成树之Prim算法·最小生成树之kruskal算法·贪心算法在背包中的应用·汽车加油问题之贪心算法4.回溯法·回溯法之数的划分·回溯法求解运动员最佳配对问题·回溯...转载 2018-05-11 10:17:28 · 6886 阅读 · 0 评论 -
五大常用算法概述
分治法基本思想将一个问题,分解为多个子问题,递归的去解决子问题,最终合并为问题的解适用情况问题分解为小问题后容易解决问题可以分解为小问题,即最优子结构分解后的小问题解可以合并为原问题的解小问题之间互相独立实例二分查找快速排序合并排序大整数乘法循环赛日程表动态划分算法基本思想将问题分解为多个子问题(阶段),按顺序求解,前一个问题的解为后一个问题提供信息适用情况最优化原理:问题的最优解所包含的子问题的...转载 2018-05-11 10:09:44 · 215 阅读 · 0 评论 -
[回溯算法] 五大常用算法之回溯法
算法入门6:回溯法一. 回溯法 – 深度优先搜素 1. 简单概述 回溯法思路的简单描述是:把问题的解空间转化成了图或者树的结构表示,然后使用深度优先搜索策略进行遍历,遍历的过程中记录和寻找所有可行解或者最优解。基本思想类同于:图的深度优先搜索二叉树的后序遍历 【 分支限界法:广度优先搜索 思想类同于:...转载 2018-05-11 10:09:03 · 983 阅读 · 0 评论 -
545. Boundary of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*1. One simp...原创 2018-05-11 09:29:31 · 164 阅读 · 0 评论 -
675. Cut Off Trees for Golf Event
/*Since we have to cut trees in order of their height, we first put trees (int[] {row, col, height}) into a priority queue and sort by height.Poll each tree from the queue and use BFS to find out step...原创 2018-05-11 09:23:29 · 224 阅读 · 0 评论 -
234 Palindrome Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//*//Time Limit Exceeded class Solution {public: ...原创 2018-05-17 10:57:38 · 157 阅读 · 0 评论 -
[leetcode] 517. Super Washing Machines
这道题是为洗衣机阵列做负载均衡,题目难度为Hard。如果衣服总数不能被洗衣机总数整除,表明不能均分所有衣服,返回-1;如果能整除,拿avg表示最终每个洗衣机中衣服数。如果我们能够得到任意位置k上洗衣机最少需要的操作数,则遍历整个数组即可得到最终需要的最小操作数,因为所有位置互不相关,可以同时进行操作。对位置k上的洗衣机来说,如果左边k个洗衣机中(下标从0开始)原有衣服总数小于avg*k,表明左边...转载 2018-05-14 13:48:47 · 156 阅读 · 0 评论 -
742. Closest Leaf in a Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*题意是寻找离k最近的叶...原创 2018-05-14 17:33:39 · 160 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
/*We need to find out the maximum difference (which will be the maximum profit) between two numbers in the given array. Also, the second number (selling price) must be larger than the first one (buyin...原创 2018-05-15 17:37:09 · 210 阅读 · 0 评论 -
5. Longest Palindromic Substring
class Solution {public: string longestPalindrome(string s) { if (s.size() == 1 || s.size() == 0) return s; int min_start=0, max_len=0, new_len = 0; int i = 0; for (i = 0...原创 2018-05-15 17:36:38 · 119 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
/*Acception!*/class Solution {public:TreeNode * dfsTraverse(TreeNode * root, TreeNode * p , TreeNode * q){ if( root == p || root == q || root == NULL) return root; TreeNode * parent1 = df...原创 2018-05-15 16:55:27 · 145 阅读 · 0 评论 -
138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list./*思想:head->p1->p2->p3 ...原创 2018-05-15 16:53:23 · 137 阅读 · 0 评论 -
438. Find All Anagrams in a String
/*Acception of mine using template.1. //create a hashmap to save the Characters of the target substring. //(K, V) = (Character, Frequence of the Characters)2. //maintain a counter to check whether...原创 2018-05-15 16:30:15 · 158 阅读 · 0 评论 -
48. Rotate Image
/*观察上面数据,需要做两步完成旋转1. a[i][j] = a[j][i]2. 然后对每行反序。 */class Solution {public: void rotate(vector<vector<int>>& matrix) { //transfor int n = matrix.size(); for(in...原创 2018-05-15 14:13:58 · 134 阅读 · 0 评论 -
20. Valid Parentheses
/*1. 第一个字符入栈2. 第二个字符看看和栈的top是否匹配,如果匹配,出栈。如果不匹配,入栈。3. 循环1,24. 栈空,整体匹配,否则,整体不匹配。*/class Solution {public: bool isValid(string s) { if(s.size() == 0) return true; if(s.size()%2 != 0) ret...原创 2018-05-15 13:55:37 · 140 阅读 · 0 评论 -
1. Two Sum
/*1. 一个unordered_map结构,key是nums[i],值是i2. 遍历nums[i],如果target-nums[i]不存在,把nums[i],i 插入到map中去,如果找到,返回结果。*/class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { ...原创 2018-05-15 13:50:50 · 145 阅读 · 0 评论 -
200. Number of Islands
/*1. 找到一个grid[i][j]==1的节点,2. 把它置位23. 从这个节点向外扩展,(递归),建岛完成。4. 找第二个grid[i][j] == 1的节点,转2.*/class Solution {public: int numIslands(vector<vector<char>>& grid) { int m = g...原创 2018-05-15 13:46:38 · 131 阅读 · 0 评论 -
49. Group Anagrams
class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<string,vector<string>> mp; for(auto m :strs){ str...原创 2018-05-17 10:44:05 · 153 阅读 · 0 评论 -
240. Search a 2D Matrix II
class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); if (m == 0) return false; int n = matrix[0].size(); int i = 0, j...原创 2018-05-17 10:25:25 · 142 阅读 · 0 评论 -
141. Linked List Cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode...原创 2018-05-10 11:39:11 · 101 阅读 · 0 评论 -
449. Serialize and Deserialize BST
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*class Codec...原创 2018-05-10 10:59:17 · 118 阅读 · 0 评论 -
155. Min Stack
/* Acception of mine!*//*#include <vector>using namespace std;class MinStack {public: //initialize your data structure here. MinStack() { m_data.clear(); } void ...原创 2018-05-10 11:36:22 · 111 阅读 · 0 评论 -
206. Reverse Linked List
* struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//* acception of mine*//*class Solution {public: ListNode* reverseList(ListNode* head) ...原创 2018-05-03 11:09:12 · 115 阅读 · 0 评论 -
127. Word Ladder
/*Time Limit Exceeded*//*class Solution {public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { queue<string> tovisit; tovisit.push(beginWord); int d...原创 2018-05-10 10:54:29 · 146 阅读 · 0 评论 -
460. LFU Cache
/*unordered_map<int, my_val_t*> m_node; // map node map<int, my_val_t*> m_fr; // map frenquency int m_cap; // capacity算法思想: 另个map,1. 1个...原创 2018-05-10 11:21:31 · 113 阅读 · 0 评论 -
297. Serialize and Deserialize Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*[1,2,3,1,3,...原创 2018-05-10 11:32:57 · 135 阅读 · 0 评论 -
146. LRU Cache
// Acception of mine Jacobtypedef struct my_val my_val_t;struct my_val { int key; int val; my_val_t* pre; my_val_t* next; my_val(int v) { val = v;pre = NULL; next = NULL; }; my_val(int key, int v) { k...原创 2018-05-08 09:10:36 · 143 阅读 · 0 评论 -
1. Two Sum
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> ret; unordered_map<int,int> mp; for(int i = 0;i<nums.size...原创 2018-05-10 11:30:58 · 138 阅读 · 0 评论 -
763. Partition Labels
class Solution {public: vector<int> partitionLabels(string S) { vector<int> ret; help(ret,S); return ret; }private: void help(vector<int>& ret, stri...原创 2018-05-15 13:40:14 · 141 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*Acception o...原创 2018-05-17 10:21:11 · 211 阅读 · 0 评论 -
682. Baseball Game
class Solution {public: int calPoints(vector<string>& ops) { stack<int> s; int ans = 0; int num = 0; for(int i = 0;i<ops.size();i++) { ...原创 2018-05-17 10:06:26 · 205 阅读 · 0 评论 -
2. Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//* not acception!*//* because overflow for long l...原创 2018-05-03 10:42:42 · 144 阅读 · 0 评论 -
387. First Unique Character in a String
class Solution {public: int firstUniqChar(string s) { int idx = s.size(); map<int, pair<int, int>> mp; for (int i = 0; i < s.size();i++) { mp[s[i]].first++; mp[s[i]].second = i; ...原创 2018-05-17 10:02:17 · 201 阅读 · 0 评论 -
17 Letter Combinations of a Phone Number
/*String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};*/class Solution {public: vector<string&原创 2018-05-17 09:58:37 · 188 阅读 · 0 评论 -
23. Merge k Sorted Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */// Acception of Mineclass Solution {public: Li...原创 2018-05-03 11:24:17 · 102 阅读 · 0 评论 -
238. Product of Array Except Self
class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { int product = 1; int n = nums.size(); vector<int> fromBegin(n,1); vector...原创 2018-05-17 09:54:44 · 142 阅读 · 0 评论 -
42. Trapping Rain Water
/*[4,2,3] is KO 0,should be 1class Solution {public: int trap(vector<int>& height) { if (height.size()<3) return 0; int i = 0; int j = 0; int ans = 0; for (i = 0; i<height.size()-...原创 2018-05-17 09:30:03 · 129 阅读 · 0 评论