500. Keyboard Row
题目描述
Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.
把一个字符串中的字符来源于键盘同一行的字符串保留下来。
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
代码实现
这里使用map记录出现的类别,如果不是需要的类别就放弃这一个字符串,判断下一个字符串。
class Solution {
public:
vector<string> findWords(vector<string>& words) {
int wsize = words.size();
vector<string> res;
if(!wsize) return res;
map<int, int> alpha;
char keyboard[] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a','s', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
for(int i = 0; i < 10; i++) alpha[keyboard[i]] = 1;
for(int i = 10; i < 19; i++) alpha[keyboard[i]] = 2;
for(int i = 19; i < 26; i++) alpha[keyboard[i]] = 3;
for(int i = 0; i < wsize; i++) {
int flg = 0;
for(int j = 0, n = words[i].size(); j < n; j++) {
char lc = alpha[tolower(words[i][j])];
if(!flg) flg = lc;
else if(flg != lc) { flg = -1; break; }
}
if(flg != -1) res.push_back(words[i]);
}
return res;
}
};
当然使用位操作也可以:
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<int> dict(26);
vector<string> rows = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
for (int i = 0; i < rows.size(); i++) {
// 键盘上每一行的字符用1,2,4表示
for (auto c : rows[i]) dict[c-'A'] = 1 << i;
}
vector<string> res;
for (auto w : words) {
int r = 7;
// r = 2'b111
// 判断每个字符串
for (char c : w) {
// 若出现不一样的字符,那么r为0. 1 & 2 & 7 || 1 & 4 & 7 || 2 & 4 & 7 = 0
r &= dict[toupper(c)-'A'];
if (r == 0) break;
}
if (r) res.push_back(w);
}
return res;
}
};
205. Isomorphic Strings
题目描述
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note:
You may assume both s and t have the same length.
Subscribe to see which companies asked this question.
代码实现
开始的时候我以为只有a,b,c所以代码为:
class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.size();
vector<int> hash(24, 0);
vector<int> rec(len, 0);
for(int i = 0; i < len; i++) {
if(!rec[i]) {
if(!hash[tolower(s[i])-'a']) {
char tmp = t[i];
hash[tolower(s[i])-'a'] = t[i];
for(int j = i; j < len; j++) {
if(t[j] == tmp && !rec[j]) {
if(s[j] != s[i]) return false;
else { rec[j] = 1;
t[j] = s[i];
}
}
}
}
else {
if(hash[tolower(s[i])-'a'] != t[i]) return false;;
}
}
}
return s == t;
}
};
改成使用map,那就是:整个解题思路就是把字符串B每个字符替换成字符串A的对应的字符。然后比较这两个字符串是否相等,如果相等,则模式一样。如果不相等,那么模式不一样。举个例子:
字符串A为“abb”, 字符串B为“bdd”。首先是’a’和’b’比较,看有没有存在以’a’为键的映射存在。如果存在,则判断键值是不是’b’,不是’b’则说明同一个键对应了不同的键值,模式不一样。返回false,否则继续。如果以’a’为键的映射不存在,则建立a->b的映射,并把B字符串改位置值为’a’。同时在字符串B中往后遍历,存在值为’b’的情况就判断同一位置字符串A是否为之前的键’a’,如果不是那么就是模式不一样。所以从判断是否模式一样的情况,既从A到B,也有B到A这个相互的判断。在这个基础上,因为我的算法里涉及修改字符串,比如A ‘abab’, B ‘baba’ 那么在第一次修改以后B那就是 ‘aaaa’, 建立了a->b。 那么现在在到了第二个字符b的时候,就建立了b->a,但是往后遍历的话,B的第三个字符串的逆映射就是a->a,和b->a发生了冲突。所以在这里,我们需要把之前已经访问过的B中的字符做一个标记。就可以避免这一个冲突。
class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.size();
map<char, char> hash;
vector<int> rec(len, 0);
for(int i = 0; i < len; i++) {
if(!rec[i]) {
if(!hash.count(s[i])) {
char tmp = t[i];
hash[s[i]] = t[i];
for(int j = i; j < len; j++) {
if(t[j] == tmp && !rec[j]) {
if(s[j] != s[i]) return false;
else { rec[j] = 1;
t[j] = s[i];
}
}
}
}
else {
if(hash[s[i]] != t[i]) return false;;
}
}
}
return s == t;
}
};
453. Minimum Moves to Equal Array Elements
题目描述
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Example:
Input:
[1,2,3]
Output:
3
Explanation:
Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
代码实现
直接模拟加数字过程,每次加完之后排序,比较首尾是否一样。如果一样则返回加的次数,不然重复上面步骤。这样做就超时了。
class Solution {
public:
int minMoves(vector<int>& nums) {
int n = nums.size(), max = INT_MIN, s = 0;
if(n == 1 || !n) return 0;
sort(nums.begin(), nums.end());
if(nums[n-1] == INT_MAX || nums[0] == INT_MIN) return nums[n-1] - nums[0];
while(nums[0] != nums[n-1]) {
for(int i = 0; i < n - 1; i++) nums[i]++;
sort(nums.begin(), nums.end());
s++;
}
return s;
}
};
换一种思路,n-1个数加一其实就是某个数减一。那么这样的话,计算超过最小值的总数就是最后的答案。
class Solution {
public:
int minMoves(vector<int>& nums) {
int n = nums.size(), min = INT_MAX, s = 0;
if(n == 1 || !n) return 0;
for(int i = 0; i < n; i++) {
if(nums[i] < min) min = nums[i];
s += nums[i];
}
while(n--) s -= min;
return s;
}
};
上面的代码使用一行搞定,使用accumulate计算总和和min_elelent计算最小值。
class Solution {
public:
int minMoves(vector<int>& nums) {
return accumulate(nums.begin(), nums.end(), 0) - *min_element(nums.begin(), nums.end()) * nums.size();
}
};