Bulls and Cows 公母牛游戏

1.Bulls and Cows 公母牛游戏

 

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out the secret number.

For example:

Secret number:  1807
Friend's guess: 7810

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 01 and 7.)

class Solution {
public:
    string getHint(string secret, string guess) {
        int m[256] = {0}, bulls = 0, cows = 0;
        for (int i = 0; i < secret.size(); ++i) {
            if (secret[i] == guess[i]) ++bulls;
            else ++m[secret[i]];
        }
        for (int i = 0; i < secret.size(); ++i) {
            if (secret[i] != guess[i] && m[guess[i]]) {
                ++cows;
                --m[guess[i]];
            }
        }
        return to_string(bulls) + "A" + to_string(cows) + "B";
    }
};
class Solution {
public:
    string getHint(string secret, string guess) {
        int m[256] = {0}, bulls = 0, cows = 0;
        for (int i = 0; i < secret.size(); ++i) {
            if (secret[i] == guess[i]) ++bulls;
            else {
                if (m[secret[i]]++ < 0) ++cows;
                if (m[guess[i]]-- > 0) ++ cows;
            }
        }
        return to_string(bulls) + "A" + to_string(cows) + "B";
    }
};

2.Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。

说明: 输入可能包含了除 ( 和 ) 以外的字符。

示例 1:

输入: "()())()"
输出: ["()()()", "(())()"]
1
2
示例 2:

输入: "(a)())()"
输出: ["(a)()()", "(a())()"]
1
2
示例 3:

输入: ")("
输出: [""]
 

class Solution {
    //核心在于正反两次排除多余的'(' 和 ‘)’
    public List<String> removeInvalidParentheses(String s) {
        List<String> result = new ArrayList<String>();

        //首先正向排除多余的'('
        remove(s, result, 0, 0, new char[] { '(', ')' });
        return result;
    }

    private void remove(String s, List<String> result, int lasti, int lastj, char[] chr) {
        for (int stack = 0, i = lasti; i < s.length(); i++) {
            //最基础的通过 stack 记录 匹配的'('或')'
            if (s.charAt(i) == chr[0]) {
                stack++;
            }
            if (s.charAt(i) == chr[1]) {
                stack--;
            }
            //正向'('个数多于')' 或者 反向')'个数多于'('
            if (stack >= 0) {
                continue;
            }
            //存在多余括号
            for (int j = lastj; j <= i; j++) {
                //去掉连续多余括号中后面的那个
                if (s.charAt(j) == chr[1] && (j == lastj || s.charAt(j - 1) != chr[1])) {
                    remove(s.substring(0, j) + s.substring(j + 1, s.length()), result, i, j, chr);
                }
            }
            return;
        }

        //反转字符串,反向去掉多余的'('
        String reverse = new StringBuilder(s).reverse().toString();

        if (chr[0] == '(') {
            remove(reverse, result, 0, 0, new char[] { ')', '(' });
        } else {
            result.add(reverse);
        }
    }
}

3. Longest Increasing Subsequence 最长递增子序列

 

Given an unsorted array of integers, find the length of longest increasing subsequence.

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4 
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        vector<int> dp(nums.size(), 1);
        int res = 0;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] > nums[j]) {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            res = max(res, dp[i]);
        }
        return res;
    }
};

4. Find Median from Data Stream 找出数据流的中位数

 

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

Examples:

[2,3,4] , the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • double findMedian() - Return the median of all elements so far.

For example:

add(1)
add(2)
findMedian() -> 1.5
add(3) 
findMedian() -> 2
class MedianFinder {
public:

    // Adds a number into the data structure.
    void addNum(int num) {
        small.push(num);
        large.push(-small.top());
        small.pop();
        if (small.size() < large.size()) {
            small.push(-large.top());
            large.pop();
        }
    }

    // Returns the median of current data stream
    double findMedian() {
        return small.size() > large.size() ? small.top() : 0.5 *(small.top() - large.top());
    }

private:
    priority_queue<long> small, large;
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值