d********w 发帖数: 363 | 1 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的
字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。
给字符串计算它代表的值,
Example:
"Queue" = 21 * 5 * 21 * 5 = 11025.
"myopia" = 25 * 15 + 9 * 1 = 384.
“I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is
preceded by a space,
the value is 9 + 5 * 5 + 9 + 21 + 25 = 89.
"gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
the second y a vowel. | m*******l 发帖数: 12782 | 2 好踢
is
【在 d********w 的大作中提到】 : 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的 : 字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。 : 给字符串计算它代表的值, : Example: : "Queue" = 21 * 5 * 21 * 5 = 11025. : "myopia" = 25 * 15 + 9 * 1 = 384. : “I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is : preceded by a space, : the value is 9 + 5 * 5 + 9 + 21 + 25 = 89. : "gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
| d********w 发帖数: 363 | 3 需要逻辑缜密阿,
我实现的好像有bug,大家能想到比较好的办法么
static int encode(String str) {
int num = 0;
int sum = 0;
int cur = 0;
boolean vowelBefore = false;
boolean vowel;
str = str.toLowerCase();
for ( int i=0; i< str.length(); i++) {
if (map.get(str.charAt(i)) != null) {
cur = map.get(str.charAt(i));
vowel = true;
} else {
vowel = false;
}
if (str.charAt(i) == 'y') {
if (vowelBefore) {
vowelBefore = false;
sum += num;
num = 0;
continue;
}
}
if (vowel || (!vowelBefore && str.charAt(i) == 'y')) {
if (num == 0)
num = 1;
num *= cur;
} else if (vowelBefore && !vowel) {
sum += num;
num = 0;
}
vowelBefore = vowel;
}
return sum + num;
}
is
【在 d********w 的大作中提到】 : 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的 : 字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。 : 给字符串计算它代表的值, : Example: : "Queue" = 21 * 5 * 21 * 5 = 11025. : "myopia" = 25 * 15 + 9 * 1 = 384. : “I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is : preceded by a space, : the value is 9 + 5 * 5 + 9 + 21 + 25 = 89. : "gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
| q****x 发帖数: 7404 | 4 烂题一道。
is
【在 d********w 的大作中提到】 : 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的 : 字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。 : 给字符串计算它代表的值, : Example: : "Queue" = 21 * 5 * 21 * 5 = 11025. : "myopia" = 25 * 15 + 9 * 1 = 384. : “I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is : preceded by a space, : the value is 9 + 5 * 5 + 9 + 21 + 25 = 89. : "gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
| p*****2 发帖数: 21240 | 5 using System;
using System.Text;
using System.Collections.Generic;
public class Calculate
{
private Dictionary ht =new Dictionary();
public Calculate()
{
ht.Add('a', 1);
ht.Add('e', 5);
ht.Add('i', 9);
ht.Add('o', 15);
ht.Add('u', 21);
ht.Add('y', 25);
}
public int Calcu(string input,int start)
{
input=input.ToLower();
int i = start;
while (i < input.Length)
if (!ht.ContainsKey(input[i]))
i++;
else
break;
if (i == input.Length)
return 0;
else
{
int result = 0;
int j = i;
while (j < input.Length)
{
if (ht.ContainsKey(input[j]))
{
if (j < input.Length-1 && input[j]=='y' && input[j + 1]
== 'y')
break;
if (j != 0 && input[j] == 'y' && input[j - 1] == ' ')
break;
if (result == 0)
result = ht[input[j]];
else
result *= ht[input[j]];
j++;
}
else
break;
}
if (j < input.Length)
return result + Calcu(input,j+1);
else
return result;
}
}
} | p*****2 发帖数: 21240 | 6
不是什么面试的好题。不需要什么技巧,就是对规则的实现。
【在 q****x 的大作中提到】 : 烂题一道。 : : is
| b******c 发帖数: 70 | 7 这种题纯粹考编程
写一个bool IsVowel(char c, bool isPrevVowel)
Loop through the string, using one flag to indicate if the previous one is
vowel or not, if it is, do mult, else reset current value
is
【在 d********w 的大作中提到】 : 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的 : 字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。 : 给字符串计算它代表的值, : Example: : "Queue" = 21 * 5 * 21 * 5 = 11025. : "myopia" = 25 * 15 + 9 * 1 = 384. : “I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is : preceded by a space, : the value is 9 + 5 * 5 + 9 + 21 + 25 = 89. : "gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
| H***e 发帖数: 476 | 8 这种絮絮叨叨的题最烦人le
is
【在 d********w 的大作中提到】 : 告诉元音字母map到数字 (a=1, e=5, i=9, o=15, u=21, y=25),y特殊些,当 前面的 : 字母是不是元音才计算值。如果是连续的元音使用乘法,否则把几段值相加。 : 给字符串计算它代表的值, : Example: : "Queue" = 21 * 5 * 21 * 5 = 11025. : "myopia" = 25 * 15 + 9 * 1 = 384. : “I'm Feeling Yucky” split into "I", "ee", "i", "u", and "y" ,first 'Y' is : preceded by a space, : the value is 9 + 5 * 5 + 9 + 21 + 25 = 89. : "gayyou" has a score of 1 + 25 * 15 * 21 = 7876, since the first y makes
| z****c 发帖数: 602 | 9 用一个deque存元音,每次碰到辅音或字符串结束清元音队列计算值。元音队列为空时
才把Y加入队列。 |
|