462. Minimum Moves to Equal Array Elements II\520. Detect Capital\383. Ransom Note

462. Minimum Moves to Equal Array Elements II

题目描述

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.

You may assume the array’s length is at most 10,000.

Example:

Input:
[1,2,3]

Output:
2

Explanation:
Only two moves are needed (remember each move increments or decrements one element):

[1,2,3]  =>  [2,2,3]  =>  [2,2,2]

代码实现

因为数组中某个项可以加一或者减一,那么找到中位数。计算各个数与中位数的差距之和就是最少的次数。

class Solution {
public:
    int minMoves2(vector<int>& nums) {
        long long int s = 0, med = 0;
        int n = nums.size();
        if(!n || n == 1) return 0;
        sort(nums.begin(), nums.end());
        if(n & 1) med = nums[n>>1];
        else med = round((nums[n>>1] + nums[(n>>1) - 1])*1.0/2.0);
        int res = 0;
        for(auto i:nums) {
            res += abs(med - i);
        }
        return res;
    }
};

520. Detect Capital

题目描述

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

All letters in this word are capitals, like “USA”.
All letters in this word are not capitals, like “leetcode”.
Only the first letter in this word is capital if it has more than one letter, like “Google”.
Otherwise, we define that this word doesn’t use capitals in a right way.

Example 1:
Input: "USA"
Output: True
Example 2:
Input: "FlaG"
Output: False

代码实现

判断字符串是否符合写的规范。

class Solution {
public:
    bool detectCapitalUse(string word) {
        int slen = word.size(), cnt = 0, fst = false;
        fst = slen?(word[0] >= 'A' && word[0] <= 'Z' ?true:false):false;
        for(int i = 0; i < slen; i++) 
            if(word[i] >= 'A' && word[i] <= 'Z') cnt++;  
        return cnt == 0 || cnt == slen?true:(cnt == 1 && fst)?true:false;
    }
};

383. Ransom Note

题目描述

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

代码实现

计算ransomNote能不能从magaZine中提取。就是计算每种字符的个数的大小问题:magazine每种字符都比ransomNote的大。

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int rlen = ransomNote.size(), mlen = magazine.size();
        map<char, int> m;
        for(int i = 0; i < mlen; i++) {
            if(m.count(magazine[i])) m[magazine[i]]++;   else m[magazine[i]] = 1;
        }
        for(int i = 0; i < rlen; i++) {
            if(m.count(ransomNote[i]))  { m[ransomNote[i]]--; if(m[ransomNote[i]] < 0)  return false; }
            else return false;
        }
        return true;    
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值