利扣刷题 day4 760 1290 1165

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值