
Leecode
文章平均质量分 61
bjzhaoxiao
这个作者很懒,什么都没留下…
展开
-
692. Top K Frequent Words
/*Acception of mine use buket*/class Solution {public: vector<string> topKFrequent(vector<string>& words, int k) { vector<string> ans; unordered_map<string, int> mp; vec...原创 2018-05-09 13:08:07 · 194 阅读 · 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-09 16:24:01 · 152 阅读 · 0 评论 -
1. Two Sum
#include <vector>#include <algorithm>using namespace std;/*class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int nLeft = 0; int t...原创 2018-05-03 10:41:11 · 114 阅读 · 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 评论 -
3. Longest Substring Without Repeating Characters
/*acception of mine*//*class Solution {public: int lengthOfLongestSubstring(string s) { int result = 0; for(int i = 0; i<s.size();i++){ map<char,int> mp; ...原创 2018-05-03 10:43:39 · 135 阅读 · 0 评论 -
136. Single Number
class Solution {public: int singleNumber(vector<int>& nums) { int ans=0; for(int i = 0; i<nums.size();i++) { ans ^= nums[i]; } ret...原创 2018-05-03 11:01:20 · 121 阅读 · 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 评论 -
47. Permutations II
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> ret; vector<bool> visited(nums.size()); ...原创 2018-05-10 10:55:03 · 158 阅读 · 0 评论 -
119. Pascal's Triangle II
class Solution {public: vector<int> getRow(int rowIndex) { vector<vector<int>> v(rowIndex+1,vector<int>()); v[0].push_back(1); if(rowIndex==0) r...原创 2018-05-03 11:10:56 · 125 阅读 · 0 评论 -
771. Jewels and Stones
/*class Solution {public: int numJewelsInStones(string J, string S) { int ans = 0; unordered_set<char> s; for(auto m:J){ s.insert(m); } f...原创 2018-05-03 11:14:32 · 133 阅读 · 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-03 11:19:50 · 120 阅读 · 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 评论 -
186. Reverse Words in a String II
//acception of mine Jacob!class Solution {public: void reverseWords(vector<char>& str) { for(int i = 0; i<str.size();) { int j = i; while(str[j]!=' ' &a...原创 2018-05-03 11:25:39 · 204 阅读 · 0 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
355. Design Twitter
/*整个数据是一个map,key是userid,value是一个user对象user中有自己的fans, follows 有自己的消息链取前10个 浏览自己的fans表 (自己是自己的fans) 把每个fans的消息入堆 然后从堆中找出10个最新的。*/typedef struct user user_t;typedef struct news news_t;str...原创 2018-05-10 10:49:30 · 164 阅读 · 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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论 -
[leetcode] 517. Super Washing Machines
这道题是为洗衣机阵列做负载均衡,题目难度为Hard。如果衣服总数不能被洗衣机总数整除,表明不能均分所有衣服,返回-1;如果能整除,拿avg表示最终每个洗衣机中衣服数。如果我们能够得到任意位置k上洗衣机最少需要的操作数,则遍历整个数组即可得到最终需要的最小操作数,因为所有位置互不相关,可以同时进行操作。对位置k上的洗衣机来说,如果左边k个洗衣机中(下标从0开始)原有衣服总数小于avg*k,表明左边...转载 2018-05-14 13:48:47 · 156 阅读 · 0 评论 -
776. Split BST
class Solution {public:vector<TreeNode*> splitBST(TreeNode* root, int V) { vector<TreeNode *> res(2, NULL); if(root == NULL) return res; if(root->val > V...原创 2018-05-14 16:09:18 · 17727 阅读 · 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 评论 -
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 评论 -
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 评论 -
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 评论 -
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 评论