From b19f59f71a0e43ddfadf23ba8d074769e5bdbafa Mon Sep 17 00:00:00 2001 From: zouwx2cs Date: Mon, 22 Oct 2018 01:38:27 +0800 Subject: [PATCH] add 318 folder & cpp, add 427 folder & cpp --- .../Solution.cpp | 16 ++++ solution/427.Construct Quad Tree/Solution.cpp | 74 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 solution/318.Maximum Product of Word Lengths/Solution.cpp create mode 100644 solution/427.Construct Quad Tree/Solution.cpp diff --git a/solution/318.Maximum Product of Word Lengths/Solution.cpp b/solution/318.Maximum Product of Word Lengths/Solution.cpp new file mode 100644 index 0000000000000..0e235f5f808ee --- /dev/null +++ b/solution/318.Maximum Product of Word Lengths/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxProduct(vector& words) { + vector v(words.size(), 0) ; + for (int i = 0; i < words.size(); ++i) + for (auto ch: words[i]) + v[i] |= 1 << (ch-'a') ; + + int M = 0 ; + for (int i = 0; i < words.size(); ++i) + for (int j = i+1; j < words.size(); ++j) + if ((v[i] & v[j]) == 0) + M = max(M, (int)words[i].size() * (int)words[j].size()) ; + return M ; + } +}; \ No newline at end of file diff --git a/solution/427.Construct Quad Tree/Solution.cpp b/solution/427.Construct Quad Tree/Solution.cpp new file mode 100644 index 0000000000000..3e05c0c1d642c --- /dev/null +++ b/solution/427.Construct Quad Tree/Solution.cpp @@ -0,0 +1,74 @@ +/* +// Definition for a QuadTree node. +class Node { +public: + bool val; + bool isLeaf; + Node* topLeft; + Node* topRight; + Node* bottomLeft; + Node* bottomRight; + + Node() {} + + Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) { + val = _val; + isLeaf = _isLeaf; + topLeft = _topLeft; + topRight = _topRight; + bottomLeft = _bottomLeft; + bottomRight = _bottomRight; + } +}; +*/ +class Solution { +public: + Node* construct(vector>& grid) { + return build(grid, 0, grid.size(), 0, grid.size()) ; + } + Node *build(vector> &g, int l, int r, int t, int b) + { + Node *node = new Node ; + node->topLeft = NULL ; + node->topRight = NULL ; + node->bottomLeft = NULL ; + node->bottomRight = NULL ; + node->isLeaf = false ; + + bool tl, tr, bl, br ; + if (l + 1 == r) + { + node->val = g[t][l] ; + node->isLeaf = true ; + return node ; + } + + int vmid = (l+r)>>1 ; + int hmid = (t+b)>>1 ; + node->topLeft = build(g, l, vmid, t, hmid) ; + node->topRight = build(g, vmid, r, t, hmid) ; + node->bottomLeft = build(g, l, vmid, hmid, b) ; + node->bottomRight = build(g, vmid, r, hmid, b) ; + + if (node->topLeft->isLeaf && node->topRight->isLeaf && node->bottomLeft->isLeaf && node->bottomRight->isLeaf) + { + if (node->topLeft->val && node->topRight->val && node->bottomLeft->val && node->bottomRight->val + || !(node->topLeft->val || node->topRight->val || node->bottomLeft->val || node->bottomRight->val)) + { + node->val = node->topLeft->val ; + node->isLeaf = true ; + + delete(node->topLeft) ; + delete(node->topRight) ; + delete(node->bottomLeft) ; + delete(node->bottomRight) ; + + node->topLeft = NULL ; + node->topRight = NULL ; + node->bottomLeft = NULL ; + node->bottomRight = NULL ; + } + } + return node ; + } +}; \ No newline at end of file