刷题笔记-面试
1.面试题01.01.判定字符是否唯一
https://leetcode-cn.com/problems/is-unique-lcci/
public boolean isUnique(String astr) {
int mark=0;
boolean flag = true;
for(int i=0;i<astr.length();i++) {
int lm = astr.charAt(i)-'a';
if((mark & (1<<lm)) > 0) {
flag = false;
break;
} else {
mark |= (1<<lm);
}
}
return flag;
}
2.面试题 01.02. 判定是否互为字符重排
https://leetcode-cn.com/problems/check-permutation-lcci/
class Solution {
public boolean CheckPermutation(String s1, String s2) {
if(s1.length()!=s2.length())return false;
Map<Character,Integer> mps1 = new HashMap<>();
Map<Character,Integer> mps2 = new HashMap<>();
for(int i=0;i<s1.length();i++) {
if(mps1.get(s1.charAt(i)) == null) {
mps1.put(s1.charAt(i),1);
}else {
mps1.put(s1.charAt(i),mps1.get(s1.charAt(i))+1);
}
if(mps2.get(s2.charAt(i)) == null) {
mps2.put(s2.charAt(i),1);
}else {
mps2.put(s2.charAt(i),mps2.get(s2.charAt(i))+1);
}
}
for(Map.Entry<Character,Integer> k:mps1.entrySet()){
if(mps2.get(k.getKey()) != k.getValue())return false;
}
return true;
}
}
面试题 01.03. URL化
一开始是直接尝试用字符串拼接,如果是空字符就拼接%20,否则就拼接原来的字符。但是超时。
题目描述
class Solution {
public String replaceSpaces(String S, int length) {
String res = "";
int m=0;
char[] s = S.toCharArray();
for(int i=0;i<length;i++) {
if(s[i] == ' '){
m++;
}
}
m = m*2+length;
char[] arr = new char[m];
int j=0;
for(int i=0;i<length;i++) {
if(s[i] == ' '){
arr[j++] = '%';
arr[j++] = '2';
arr[j++] = '0';
}else{
arr[j++] = s[i];
}
}
return new String(arr);
}
}