760 不用去重 又开始暴力刷题了,
public int[] anagramMappings(int[] A, int[] B) {
int length=A.length;
int[] out=new int[length];
for(int i=0;i<length;i++) {
for(int j=0;j<length;j++) {
if(A[i]==B[j]) {
out[i]=j;
}
}
}
return out;
}
特优解 想到了hashmap 但是想反了
public int[] anagramMappings(int[] A, int[] B) {
Map<Integer, Integer> D = new HashMap();
for (int i = 0; i < B.length; ++i)
D.put(B[i], i);
int[] ans = new int[A.length];
int t = 0;
for (int x: A)
ans[t++] = D.get(x);
return ans;
}
1290 继续暴力
public int getDecimalValue(ListNode head) {
StringBuffer sb=new StringBuffer();
while(head!=null) {
sb.append(head.val);
head=head.next;
}
return Integer.parseInt(sb.toString(), 2);
}
左移更快 又给忘了!
public int getDecimalValue(ListNode head) {
int ans = 0;
while (head != null) {
ans <<= 1;
ans += head.val;
head = head.next;
}
return ans;
}
1165 暴力 我是暴力狂吗
public int calculateTime(String keyboard, String word) {
char[] keys=keyboard.toCharArray();
Map<Character,Integer> map=new HashMap();
for(int i=0;i<26;i++){
map.put(keys[i],i);
}
char[] words=word.toCharArray();
int count=Math.abs(map.get(words[0])-map.get(keys[0]));
for(int i=1;i<words.length;i++){
count+=Math.abs(map.get(words[i])-map.get(words[i-1]));
}
return count;
}
没有什么更好解 最多剩下一个hashmap