随笔分类 - Leetcode
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: int numDecodings(string s) { int n = s.length(); if (n == 0) return 0; vector dp(n+1, 0); dp[0] = 1; for (int i = 0; i 0) { ...
阅读全文
摘要:// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasation Log() { id = 0; timestamp = 0; comp = 0; ...
阅读全文
摘要:class Solution { public: int leastInterval(vector& tasks, int n) { int freq[26] = {0}; for (auto t : tasks) { freq[t-'A']++; } int max_freq = 0, max_fr...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: int leastBricks(vector>& wall) { unordered_map m; for (int i = 0; i < wall.size(); i++) for (int j = 0, t = 0; j < wall[i].size() - 1; j++) { ...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: long max_id = 0; unordered_map id_long; // Encodes a URL to a shortened URL. string encode(string longUrl) { id_long[max_id++] = longUrl; ...
阅读全文
摘要:class Solution { public: bool checkSubarraySum(vector& nums, int k) { unordered_map m; // 从头元素开始的sum,取模为key的index。 m[0] = -1; int _sum = 0; for (int i = 0; i < n...
阅读全文
摘要:class Solution { public: unordered_map dp; int findTargetSumWays(vector& nums, int S) { return helper(nums, S, 0); } int helper(vector& nums, int S, int start) { strin...
阅读全文
摘要:class Solution { public: int totalHammingDistance(vector& nums) { int res = 0; for (int i = 0; i < 32; i++) { int ones = 0; for (int n : nums) { ...
阅读全文
摘要:class Solution { public: int hammingDistance(int x, int y) { int r = x ^ y; return bitCount(r); } int bitCount(int x) { int res = 0; while (x) { ...
阅读全文
摘要:class Solution { public: int splitArray(vector& nums, int m) { int _max = -1, _sum = 0; for (auto n : nums) { if (n > _max) _max = n; _sum += n; ...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class RandomizedSet { public: vector data; unordered_map m; // /** Initialize your data structure here. */ RandomizedSet() { } /** Inserts a value to the set....
阅读全文
摘要:/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Return ...
阅读全文
摘要:class Solution { public: bool increasingTriplet(vector& nums) { int i = INT_MAX, j = INT_MAX; for (int n : nums) { if (n <= i) { i = n; } ...
阅读全文
摘要:class Solution { public: unordered_set res; // DFS may produce dup result, use set to filter. vector removeInvalidParentheses(string s) { // Calculate rmL and rmR for the numbers o...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
浙公网安备 33010602011771号