Fork me on GitHub
hazelnutsgz

Why don't you come to your senses?


  • Home

  • About

  • Tags

  • Archives

Subsets II

Posted on 2018-09-27

Description

https://leetcode.com/problems/subsets-ii/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int> > ret;
unordered_map<int, int> counter;
for (int i = 0; i < nums.size(); i++) counter[nums[i]] += 1;
unordered_map<int, int>::iterator iter = counter.begin();
vector<int> current;
this->getSet(counter, current, iter, ret);
return ret;
}

void getSet(unordered_map<int, int>& counter, vector<int>& current, \
unordered_map<int, int>::iterator iter, vector<vector<int>>& ret) {
ret.push_back(current);
if (iter == counter.end()) return;
for (auto it = iter; it != counter.end(); it++) {
int count = it->second;
for (int i = 0; i < count; i++) {
current.push_back(it->first);
auto nextIter = next(it, 1);
this->getSet(counter, current, nextIter, ret);
}
for (int i = 0; i < count; i++) current.pop_back();
}
return;
}
};

4SUM II

Posted on 2018-09-27

Description

https://leetcode.com/problems/4sum-ii/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27


class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> X, Y;
for (int indexA = 0; indexA < A.size(); indexA++) {
for (int indexB = 0; indexB < B.size(); indexB++) {
X[A[indexA] + B[indexB]] += 1;
}
}
for (int indexC = 0; indexC < C.size(); indexC++) {
for (int indexD = 0; indexD < D.size(); indexD++) {
Y[C[indexC] + D[indexD]] += 1;
}
}

int ret = 0;
for (auto iter = X.begin(); iter != X.end(); iter++) {
auto iterD = Y.find(-iter->first);
if (iterD != Y.end())
ret += iter->second * iterD->second;
}

return ret;
}
};

Paper Review: Low-resource Multi-task Audio Sensing for Mobile and Embedded Devices via Shared Deep Neural Network Representations

Posted on 2018-09-26

Linkage

https://www.cl.cam.ac.uk/~cm542/papers/Ubicomp17-Georgiev.pdf

Background

This paper proposed a multi-task framework to handle audio recongnition problem on embedded or mobile devices.

Former work

The former work focused on the single network optimization or the multi-task network on large devices like server. While our work combined them together.

Feature

1.Change the input feature.

2.Proposed a new search-mechanism

3.Less energy consumed

Implementation

Multi-task with shared network structure. We us

Task Type

The number of tasks can be scale up and down dynamcally using our multi -task framework.

  • Speaker Identi!cation.

  • Emotion Recognition

  • Stress Detection.

  • Ambient Scene Analysis.

Customize the Input

a. extract filter bank summaries, normalize the features across individual datasets and randomize the samples.

Using Filter Bank rather than the PLP, MFCC

  1. Reducing Input Feature Complexity.

  2. Adjust the input data to the same size regardless of the size of voice data.

Hidden Layer

Topology of Framework

DBN Stacked RBM(two stage)

unsupervised pre-training(RBM)

fine tuning

DNN(BP + SGD)

Optimize Configuration of Structure

1.The node in each layer is the powers of 2 (Should not exceed the memory limitation)

2.The searching scopre for topology is restricted.

3.Make sure the accuracy is high enough.

Output Layer

Represent the probability by Softmaxing

Evalution

We evaluate the performance from different aspects.

a. Accuracy : similiar to the simple network

b.Runtime and Energy Reductions. (Runtime and Energy is similar to simple network)

c.handcrafted feture & filter bank

d.Cluster size

e.memory footpritnt

Binary Tree Postorder Traversal

Posted on 2018-09-26

Description

https://leetcode.com/problems/binary-tree-postorder-traversal/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Node {
public:
bool l;
bool r;
TreeNode* treeNode;
Node(TreeNode* node) {
this->treeNode = node;
this->l = false;
this->r = false;
}
};

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
stack<Node*> s;
Node* start = new Node(root);
if (root == NULL) return ret;
s.push(start);
while (s.empty() == false) {
Node* currentNode = s.top();
if (currentNode->l == false && currentNode->treeNode->left != NULL) {
currentNode->l = true;
TreeNode* traverse = currentNode->treeNode->left;
while(traverse != NULL) {
Node* newNode = new Node(traverse);
newNode->l = true;
s.push(newNode);
traverse = traverse->left;
}
continue;
}
if (currentNode->r == true || currentNode->treeNode->right == NULL) {
s.pop();
ret.push_back(currentNode->treeNode->val);
continue;
}
//Default case, find the node in the right subtree.
currentNode->r = true;
s.push(new Node(currentNode->treeNode->right));
}
return ret;
}
};

Insert Delete GetRandom O(1)

Posted on 2018-09-26

Description

https://leetcode.com/problems/insert-delete-getrandom-o1/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class RandomizedSet {
public:

/** Initialize your data structure here. */
RandomizedSet() {
}

/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if(this->hash.find(val) != this->hash.end()) return false;
this->hash[val] = this->nums.size();
this->nums.push_back(val);
return true;
}

/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
auto iter = this->hash.find(val);
if(iter == this->hash.end()) return false;
int originIndex = iter->second;
this->swap(nums, originIndex, this->nums.size() - 1);
this->hash[this->nums[originIndex]] = originIndex;
this->nums.pop_back();
this->hash.erase(val);
return true;
}

void swap(vector<int>& nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}

/** Get a random element from the set. */
int getRandom() {
if (this->nums.size() == 0) return -1;
return this->nums[rand() % (this->nums.size())];
}

private:
unordered_map<int, int> hash;
vector<int> nums;
};

/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* bool param_1 = obj.insert(val);
* bool param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/

Increasing Triplet Subsequence

Posted on 2018-09-25

Description

https://leetcode.com/problems/increasing-triplet-subsequence/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {

if (nums.size() < 3) return false;
int lower = INT_MAX;
int mid = INT_MAX;
for (int i = 0; i < nums.size(); i++) {
int item = nums[i];
if (item < lower) {
lower = item;
continue;
}
if (item < mid && item > lower) {
mid = item;
continue;
}
if (item > mid) return true;
}

return false;
}
};

Construct Binary Tree from Preorder and Inorder Traversal

Posted on 2018-09-25

Description

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0) return NULL;
return this->build(preorder, inorder, 0, inorder.size() - 1, 0, preorder.size() - 1);
}

TreeNode* build(vector<int>& preorder, vector<int>& inorder, int inLeft, int inRight, int preLeft, int preRight) {
if (inLeft > inRight || preLeft > preRight) return NULL;
TreeNode* root = new TreeNode(preorder[preLeft]);
int preIndex = preLeft;
int inIndex = inLeft;
while (inIndex <= inRight) {
if (inorder[inIndex] == preorder[preLeft]) {
break;
}
preIndex += 1;
inIndex += 1;
}
root->left = this->build(preorder, inorder, inLeft, inIndex - 1, preLeft + 1, preIndex);
root->right = this->build(preorder, inorder, inIndex + 1, inRight, preIndex + 1, preRight);
return root;
}
};

Symmetric Tree

Posted on 2018-09-25

Description

https://leetcode.com/problems/symmetric-tree/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
TreeNode* left = root->left;
TreeNode* right = root->right;

return check(left, right);
}

bool check(TreeNode* l, TreeNode* r) {
if (l == NULL && r == NULL) return true;
if (l == NULL || r == NULL) return false;
if (l->val != r->val) return false;

return this->check(l->left, r->right) && this->check(l->right, r->left);
}
};

Basic Calculator

Posted on 2018-09-25

Description

https://leetcode.com/problems/basic-calculator/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int calculate(string s) {
int index = 0;
return this->calculateFrom(index, s);
}

int calculateFrom(int& index, string& s) {
int ret = 0;
int sign = 1;
while(index < s.size()) {
while(s[index] == ' ') index += 1;
switch (s[index]) {
case '-':
index += 1;
sign = -1;
break;
case '(':
index += 1;
ret += sign * this->calculateFrom(index, s);
continue;
case ')':
index += 1;
return ret;
case '+':
index += 1;
sign = 1;
continue;
default:
//Number
ret += sign * this->extractNumber(s, index);
}
}
return ret;
}

int extractNumber(string& s, int& index) {
int ret = 0;
while (index < s.size() && s[index] <= '9' && s[index] >= '0') {
ret = ret * 10 + (s[index] - '0');
index += 1;
}
return ret;
}
};

Evaluate Reverse Polish Notation

Posted on 2018-09-25

Description

https://leetcode.com/problems/evaluate-reverse-polish-notation/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
int evalRPN(vector<string>& tokens) {
if (tokens.size() == 0) return 0;

stack<int> numberStack;
int index = 0;

while(index < tokens.size()) {
int left, right;
string token = tokens[index];
index += 1;

if (token == "+"){
left = numberStack.top();
numberStack.pop();
right = numberStack.top();
numberStack.pop();
numberStack.push(left + right);
continue;
}

if (token == "-" and token.length() == 1) {
left = numberStack.top();
numberStack.pop();
right = numberStack.top();
numberStack.pop();
numberStack.push(right - left);
continue;
}

if(token == "/") {
left = numberStack.top();
numberStack.pop();
right = numberStack.top();
numberStack.pop();
numberStack.push(right / left);
continue;
}

if(token == "*") {
left = numberStack.top();
numberStack.pop();
right = numberStack.top();
numberStack.pop();
numberStack.push(right * left);
continue;
}

int number = stoi(token);
numberStack.push(number);
}
return numberStack.top();
}
};
1…111213…21

hazelnutsgz

A normal coder

210 posts
9 tags
Github LinkedIn
© 2019 hazelnutsgz
本站访客数:
|
Theme — NexT.Muse v5.1.4
总访问量次 | 总访客人 |