462. Minimum Moves to Equal Array Elements II\520. Detect Capital\383. Ransom Note
来源:互联网 发布:python javascript 编辑:程序博客网 时间:2024/06/06 19:23
- Minimum Moves to Equal Array Elements II
- 题目描述
- 代码实现
- Detect Capital
- 题目描述
- 代码实现
- 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:2Explanation: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") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("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; }};
- 462. Minimum Moves to Equal Array Elements II\520. Detect Capital\383. Ransom Note
- Minimum Moves to Equal Array Elements II
- Minimum Moves to Equal Array Elements II
- Minimum Moves to Equal Array Elements II
- 【leetcode】462. Minimum Moves to Equal Array Elements II【M】
- 462. Minimum Moves to Equal Array Elements II
- 462. Minimum Moves to Equal Array Elements II
- LeetCode 462. Minimum Moves to Equal Array Elements II
- 462. Minimum Moves to Equal Array Elements II
- LeetCode笔记:462. Minimum Moves to Equal Array Elements II
- 462. Minimum Moves to Equal Array Elements II 难度:medium
- LeetCode 462. Minimum Moves to Equal Array Elements II
- Leetcode-462. Minimum Moves to Equal Array Elements II
- 【LeetCode】 462. Minimum Moves to Equal Array Elements II
- 462. Minimum Moves to Equal Array Elements II**
- LeetCode 462. Minimum Moves to Equal Array Elements II
- LeetCode 462. Minimum Moves to Equal Array Elements II
- 462. Minimum Moves to Equal Array Elements II
- babel总结
- linux 下 select 编程
- idea for mac快捷键
- 1.2 产品最终定位的心得体会
- 集成模型的五个基础问题
- 462. Minimum Moves to Equal Array Elements II\520. Detect Capital\383. Ransom Note
- C~K的奖券
- 关于实现ps3手柄无线连接电脑
- 杭电ACM 2002.计算球体积
- 算法训练 大小写转换
- 不使用sizeof,如何求出数据类型的字节数
- 初学Linux
- allegro 贴片与过孔焊盘制作流程
- 负载均衡之DNS域名解析,实现一个域名对应多个IP地址