LeetCode
Mr_W1997
这个作者很懒,什么都没留下…
展开
-
137. Single Number II
class Solution {public: int singleNumber(vector& nums) { int sz = nums.size(); int result = 0; for(int i = 0; i < 32; ++i){ int temp = 1 << i, times = 0;原创 2017-08-15 09:12:17 · 209 阅读 · 0 评论 -
203. Remove Linked List Elements
class Solution {public: ListNode* removeElements(ListNode* head, int val) { while(head && head->val == val) head = head->next; ListNode *p = head; while(p && p原创 2017-09-06 13:37:54 · 212 阅读 · 0 评论 -
216. Combination Sum III
class Solution {private: vector> ans; vector s;public: vector> combinationSum3(int k, int n) { vector visited(10, false); dfs(visited, 1, k, n); return ans; }原创 2017-09-14 09:11:02 · 215 阅读 · 0 评论 -
191. Number of 1 Bits
class Solution {public: int hammingWeight(uint32_t n) { int count = 0; while (n) { if(n & 1) ++count; n >>= 1; } return cou原创 2017-08-24 15:12:44 · 219 阅读 · 0 评论 -
Linux网络常用命令
ifconfig 查看ip Mac 掩码ifdown 网卡名 禁用网卡ifup 网卡名 启用网卡netstat-t 列出TCP协议端口-u 列出udp协议端口-a 所有-n 不使用域名与服务名 而使用ip与端口号-l 查看监听端口原创 2017-09-06 16:41:03 · 257 阅读 · 0 评论 -
205. Isomorphic Strings
class Solution {public: int smap[256] = {0}; int tmap[256] = {0}; bool isIsomorphic(string s, string t) { for(int i = 0; i < s.size(); ++i){ if(smap[s[i]] == 0)原创 2017-09-07 08:41:30 · 220 阅读 · 0 评论 -
155. Min Stack
class MinStack {public: /** initialize your data structure here. */ stack s; stack sMin; MinStack() { sMin.push(0x7fffffff); } void push(int x) { s.push(x);原创 2017-08-29 19:42:04 · 206 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if(0 == headA || headB == 0) return NULL; int len1 = 0, len2 = 0; Lis原创 2017-08-29 19:59:35 · 245 阅读 · 0 评论 -
165. Compare Version Numbers
class Solution {public: queue s1, s2; int m[256]; int compareVersion(string version1, string version2) { m['0'] = 0; m['1'] = 1;m['2'] = 2;m['3'] = 3;m['4'] = 4; m['5'] =原创 2017-08-29 21:18:48 · 271 阅读 · 0 评论 -
202. Happy Number
class Solution {public: bool isHappy(int n) { vector v; long long res = n; v.push_back(res); while(1){ res = getSum(res); if(res == 1)原创 2017-09-06 13:23:23 · 218 阅读 · 0 评论 -
201. Bitwise AND of Numbers Range
class Solution {public: int rangeBitwiseAnd(int m, int n) { return n == m ? m : rangeBitwiseAnd(m>>1, n>>1) << 1; }};原创 2017-09-05 18:07:36 · 190 阅读 · 0 评论 -
168. Excel Sheet Column Title
class Solution {public: stack s; char str[100]; string convertToTitle(int n) { while(n > 0){ int last = n % 26; s.push(last); n = last==0? n/26原创 2017-08-22 09:02:21 · 263 阅读 · 0 评论 -
141. Linked List Cycle
class Solution {public: bool hasCycle(ListNode *head) { if(!head) return false; ListNode *p1 = head, *p2 = head->next; while(p1 && p2){ if(p1 == p2)原创 2017-08-15 11:24:23 · 212 阅读 · 0 评论 -
198. House Robber
class Solution {public: int rob(vector& nums) { if(0 == nums.size()) return 0; if(1 == nums.size()) return nums[0]; if(2 == nums.size())原创 2017-09-03 22:09:36 · 203 阅读 · 0 评论 -
199. Binary Tree Right Side View
class Solution {public: queue q; vector ans; vector rightSideView(TreeNode* root) { if(!root) return ans; q.push(root); bfs(); return ans;原创 2017-09-03 22:40:05 · 178 阅读 · 0 评论 -
171. Excel Sheet Column Number
class Solution {public: int titleToNumber(string s) { int t = 1; int ans = 0; for(int i = s.size() - 1; i >= 0; --i){ int n = s[i]; n -= 64;原创 2017-08-22 12:03:31 · 265 阅读 · 0 评论 -
190. Reverse Bits
class Solution {public: uint32_t ans = 0; uint32_t reverseBits(uint32_t n) { for(int i = 0; i < 32; ++i){ if(n & (1 << i)){ ans |= (1 << (32 - i -原创 2017-08-25 21:45:01 · 168 阅读 · 0 评论 -
189. Rotate Array
class Solution {public: void rotate(vector& nums, int k) { if(nums.size() < k) k = k % nums.size(); vector content(k, 0); copy(nums.end() - k, nums.end(), cont原创 2017-08-25 22:16:30 · 286 阅读 · 0 评论 -
162. Find Peak Element
class Solution {public: int findPeakElement(vector& nums) { int sz = nums.size(); if(sz <= 1) return 0; if(nums[0] > nums[1]) return 0; if(nums[sz-1] >原创 2017-08-23 16:38:03 · 200 阅读 · 0 评论 -
230. Kth Smallest Element in a BST
class Solution {public: int ans; int step; stack s; Solution(){ step = 0; } int kthSmallest(TreeNode* root, int k) { TreeNode *p = root; while(p || !s.empty()){ while(p){原创 2017-09-23 21:19:42 · 214 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root) return NULL; if(!p) return q; if(!q) return q; TreeNode *r = root; int原创 2017-09-24 07:47:06 · 223 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || root == p || root == q) return root; TreeNode *left = lowestCommonAncestor(root-原创 2017-09-24 09:55:51 · 200 阅读 · 0 评论 -
222. Count Complete Tree Nodes
class Solution {public: int countNodes(TreeNode* root) { if(!root) return 0; int hl = 0, hr = 0; TreeNode *l = root, *r = root; while(l){ l = l->left; ++hl; } while(r){原创 2017-09-20 11:43:07 · 272 阅读 · 0 评论 -
223. Rectangle Area
class matrix{private: static const int TOTOP = 0x8; static const int TOBOTTOM = 0x4; static const int TOLEFT = 0x2; static const int TORIGHT = 1; static const int TOMARK = 0x0f; static const in原创 2017-09-20 14:04:29 · 247 阅读 · 0 评论 -
228. Summary Ranges
class Solution {public: vector summaryRanges(vector& nums) { vector ans; int start = 0, ending = 0; for(int i = 0; i < nums.size(); ++i){ start = nums[i]; while(i < nums.size()-1 && n原创 2017-09-21 09:01:28 · 226 阅读 · 0 评论 -
300. Longest Increasing Subsequence
class Solution {public: int lengthOfLIS(vector& nums) { if(0 == nums.size()) return 0; int maxVal = 1; vector dpVal(nums.size(), 0x80000000); dpVal[0]原创 2017-10-08 14:49:42 · 241 阅读 · 0 评论 -
303. Range Sum Query - Immutable
class NumArray {public: vector sums; NumArray(vector nums) { sums.push_back(0); for(auto i : nums){ sums.push_back(sums.back()+i); } } int sum原创 2017-10-09 08:35:25 · 265 阅读 · 0 评论 -
304. Range Sum Query 2D - Immutable
class NumMatrix {public: vector> *sums; NumMatrix(vector> matrix) { if(0 == matrix.size()) return; sums = new vector>(matrix.size(), vector(matrix[0].size(), 0));原创 2017-10-09 09:11:05 · 315 阅读 · 0 评论 -
283. Move Zeroes
class Solution {public: void moveZeroes(vector& nums) { vector notzero(nums.size(), 0); int sz = 0; for(int i = 0; i < nums.size(); ++i){ if(nums[i] != 0)原创 2017-10-06 09:16:17 · 285 阅读 · 0 评论 -
213. House Robber II
class Solution {public: int rob(vector& nums) { if(0 == nums.size()) return 0; if(1 == nums.size()) return nums[0]; return max(robLine(nums.begin()原创 2017-09-12 11:49:14 · 294 阅读 · 0 评论 -
221. Maximal Square
class Solution{public: int maximalSquare(vector > matrix){ if(0 == matrix.size()) return 0; int row = matrix.size(), col = matrix[0].size(); vector> dp(row, vector(col, 0)); int maxn =原创 2017-09-20 10:16:33 · 280 阅读 · 0 评论 -
278. First Bad Version
// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: int firstBadVersion(int n) { long long low = 0, high = n; long long mid;原创 2017-09-28 08:55:51 · 291 阅读 · 0 评论 -
239. Sliding Window Maximum
class Solution {public: deque maxs; vector maxSlidingWindow(vector& nums, int k) { vector ans; for(int i = 0; i < nums.size(); ++i){ if(!maxs.empty() && maxs.front() == i-k) maxs.pop_原创 2017-09-24 23:23:59 · 230 阅读 · 0 评论 -
219. Contains Duplicate II
class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { set pot; for(int i = 0; i < nums.size(); ++i){ if(i > k) pot.erase(nums[i-k原创 2017-09-16 10:24:51 · 331 阅读 · 0 评论 -
220. Contains Duplicate III
class Solution {public: bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { set pot; for(int i = 0; i < nums.size(); ++i){ long long val = nums[i];原创 2017-09-16 10:57:20 · 347 阅读 · 0 评论 -
260. Single Number III
class Solution {public: vector singleNumber(vector& nums) { int res = 0; for(auto i : nums){ res ^= i; } int dif = ((res - 1) & res) ^ res; int a = 0, b = 0; for(auto i : nums){原创 2017-09-25 09:18:54 · 238 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
class Solution {public: int minSubArrayLen(int s, vector& nums) { if(0 == nums.size()) return 0; int l = 0, r = 0, ans = 0x7fffffff; int sum = nums[0];原创 2017-09-10 08:46:50 · 242 阅读 · 0 评论 -
207. Course Schedule
class Solution {public: static const int VISITING = 0; static const int VISITED = 1; static const int NOTVISITED = 2; bool canFinish(int numCourses, vector>& prerequisites) {原创 2017-09-11 08:31:02 · 287 阅读 · 0 评论 -
268. Missing Number
class Solution {public: int missingNumber(vector& nums) { int mark = 0; int maxi = nums.size(); for(int i = 0; i <= maxi; ++i) mark ^= i; for(auto i :原创 2017-09-27 21:58:10 · 287 阅读 · 0 评论 -
290. Word Pattern
class Solution {public: bool wordPattern(string pattern, string str) { map c2sm; map s2cm; stringstream ss(str); string s; for(int i = 0; i <原创 2017-10-06 10:19:44 · 311 阅读 · 0 评论