一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

一亩三分地官方iOS手机应用下载
查看: 1557|回复: 0
收起左侧

Hulu

[复制链接] |试试Instant~ |关注本帖
106 发表于 2015-5-23 11:57:26 | 显示全部楼层 |阅读模式

2015(1-3月) 码农类 硕士 全职@Hulu - 内推 - 技术电面 |Passfresh grad应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
Hulu电面. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
随便问一下我的实习经历, 讲了一分钟就直接coding了。 isAngram 两种方法及其变体。
// Ransom Note
// magazine = "cdhjkscjgcdjkshvfhjgdu..." "themet"
// note = "Meet me somewhere" "meet" a-z A-Z 0-9

boolean canMakeRansomNote(String magazine, String note) {
//assume the input is not null
Map<Character, Frequence> map = new HashMap<>();
for(int i = 0; i < note.length(); i++){
char c = note.charAt(i);
if(isValid(c)) //ignore any space or illegal chars{
int freq = map.containsKey(c) ? map.get(c)+1 : 1;
map.put(c, freq);
} . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
}. From 1point 3acres bbs

for(int i = 0; i < magazine.length(); i++){. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
char c = magazine.charAt(i);
if(isValid(c) && map.containsKey(c){ //care about the chars occured in notes.
int newFreq = map.get(c)-1; //found a char which is contained in notes
if(newFreq <= 0){
map.remove(c);
if(map.isEmpty())
return true;.鏈枃鍘熷垱鑷1point3acres璁哄潧
}.鐣欏璁哄潧-涓浜-涓夊垎鍦
else
map.put(c, newFreq);


. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴}

return false;
}
}

public boolean isValid(char c){ //c is Uni-8, int = 65 ('A'). 鍥磋鎴戜滑@1point 3 acres
if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')
return true;
return false;
}

// "abc", "cab" -> true "[a, b , c] [a, b, c]
// "abc", "def" -> false
. From 1point 3acres bbs

void isAnagram(String string1, String string2) {
//first idea: sort them and then compare if it is equals
//assume input is not null.鏈枃鍘熷垱鑷1point3acres璁哄潧
if(string1.length() != string2.length())
return false;

Map<Character, Integer> map = new HashMap<>();
//iterate string1 and store it in map
for(int i = 0; i < string1.length(); i++){. 1point 3acres 璁哄潧
char c = string1.charAt(i);
int freq = map.containsKey(c) ? map.get(c)+1 : 1;
map.put(c, freq);
}

//iterate the other string to check
for(int i = 0; i < string2.length(); i++){
char c = string2.charAt(i);
if(!map.containsKey(c))
return false;
//decrease freq
int newFreq = map.get(c)-1;
. 鍥磋鎴戜滑@1point 3 acresif(newFreq < 0)
return false;

map.put(c, newFreq);. Waral 鍗氬鏈夋洿澶氭枃绔,
}
. more info on 1point3acres.com
return true;
}

鏉ユ簮涓浜.涓夊垎鍦拌鍧.
. Waral 鍗氬鏈夋洿澶氭枃绔,

void isAnagram(String string1, String string2) {
//first idea: sort them and then compare if it is equals
//assume input is not null. visit 1point3acres.com for more.
if(string1.length() != string2.length())
return false;

char[] S1 = string1.toCharArray();
char[] S2 = string2.toCharArray()

Arrays.sort(S1);
Arrays.sort(s2);

return isEqual(S1, S2); // Arrays.toStint(s1).equals(Arrays.toString(S2));
}.1point3acres缃

public boolean isEqual(char[] A, char[] B){
//cornet case
if(A == null && B == null)
return true;

.鐣欏璁哄潧-涓浜-涓夊垎鍦//only one of them is null
if(A == null || B == null)
returnf false;. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴

for(int i = 0; i < A.length; i++){
if(A != B)
return false;
}
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
return true;. Waral 鍗氬鏈夋洿澶氭枃绔,
}


评分

1

查看全部评分

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2017-3-27 03:21

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表