403. Snapchat面试题:青蛙过河/653. Two Sum IV

来源:互联网 发布:java开发 android app 编辑:程序博客网 时间:2024/06/05 01:00

Frog Jump

Problem Description

想象有一只青蛙要过河。河面用一个一维 0/1 数组表示,0 表示这一格是水,1 表示这一格是石头。这只青蛙不会游泳,但可以在石头之间跳跃,不过要满足:假设上一次跳跃的距离为d,这次跳跃的距离必须是 d-1、d 和 d-1 之一的数值。给定这个一维数组,请编程确定这只青蛙是否能顺利过河?注意跳跃的距离不能为负,也就是说不能回头。

这道题目也是Leetcode的403道题目:https://leetcode.com/problems/frog-jump/description/

Implementation

class Solution {public:    bool canCross(vector<int>& stones) {        unordered_map<int, bool> m;        return helper(stones, 0, 0, m);    }    bool helper(vector<int>& stones, int pos, int jump, unordered_map<int, bool>& m) {        int n = stones.size(), key = pos | jump << 11;        if (pos >= n - 1) return true;        if (m.count(key)) return m[key];        for (int i = pos + 1; i < n; ++i) {            int dist = stones[i] - stones[pos];            if (dist < jump - 1) continue;            if (dist > jump + 1) return m[key] = false;            if (helper(stones, i, dist, m)) return m[key] = true;        }        return m[key] = false;    }};

653. Two Sum IV - Input is a BST

Problem Description

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:Input:     5   / \  3   6 / \   \2   4   7Target = 9Output: True
Example 2:Input:     5   / \  3   6 / \   \2   4   7Target = 28

Output: False

Implementation

/** * 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:    void goThrough(vector<int>& in_order, TreeNode* root) {        if(!root) {            return;        }        goThrough(in_order, root->left);        in_order.push_back(root->val);        goThrough(in_order, root->right);    }    bool findTarget(TreeNode* root, int k) {        if(!root) {            return false;        }        vector<int> in_order;        goThrough(in_order, root);        int size = in_order.size();        int low  = 0;        int high = size - 1;        while(low < high) {            int tmp_s = in_order[low] + in_order[high];            if(tmp_s > k) {                high--;            }            else             if(tmp_s < k) {                low++;            }                else {                return true;            }        }        return false;    }};

655. Print Binary Tree

Problem Description

Print a binary tree in an m*n 2D string array following these rules:

The row number m should be equal to the height of the given binary tree.
The column number n should always be an odd number.
The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
Each unused space should contain an empty string “”.
Print the subtrees following the same rules.

Example 1:

Input:     1    /   2Output:[["", "1", ""], ["2", "", ""]]

Example 2:

Input:     1    / \   2   3    \     4Output:[["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]]

Example 3:

Input:      1     / \    2   5   /   3  / 4 Output:[["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""] ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""] ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""] ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]Note: The height of binary tree is in the range of [1, 10].

Implementation

Calculate depth and use recursive to store respond elements.

/** * 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:    int findDepth(TreeNode* root) {        if(!root) {            return 0;        }        int left_d  = findDepth(root->left) + 1;        int right_d = findDepth(root->right) + 1;        return left_d > right_d?left_d:right_d;    }    void insertNumber(vector<vector<string>>&res, TreeNode* root, int low, int high, int layer) {        if(!root) {            return;        }        int idx = (low+high) >> 1;        res[layer][idx] = to_string(root->val);        insertNumber(res, root->left, low, idx - 1, layer + 1);        insertNumber(res, root->right, idx + 1, high, layer + 1);    }    vector<vector<string>> printTree(TreeNode* root) {        if(!root) {            vector<vector<string>> no_ele;            return no_ele;        }        int depth = findDepth(root);        int num   = exp2(depth);         vector<vector<string>> res(depth, vector<string>(num-1, ""));        insertNumber(res, root, 0, num-1, 0);                                   return res;    }};
原创粉丝点击