第170场周赛

5303. 解码字母到整数映射

写代码的过程中有两个地方没注意到,一直在改BUG
1、int 和char转换,自然是想到Integer.parseInt,但是这个方法前提是要将char转换成String,char转String,用String.valueOf()
2、还有 char类型和Int类型相加时,会默认转换为int类型,所以如果要赋值给char要进行强制转换
3、String类的一些常用方法,当没有使用IDE写代码的时候,这些常用方法老是写错了,比如substring()

class Solution {
    public String freqAlphabets(String s) {
        StringBuilder res =new StringBuilder();
        if(null==s|| 0==s.length()){
            return null;
        }
        int left =0;
        int right = 1;
        int count = 0;
        while(left<s.length()){
            while(right<s.length() && s.charAt(right)!='#'){
                right++;
            }
            
            if(right>=s.length()){
                break;
            }
            while(right-left>2){
                count = Integer.parseInt(String.valueOf(s.charAt(left)))-1;
                char temp = (char)('a'+count);
                res.append(temp);
                left++;
    
            }
            
            if(right-left==2){

                count = Integer.parseInt(s.substring(left,right)) -1;
                char temp =(char)('a'+count);
                res.append(temp);
                left= right+1;
                right= left+1;

            }
            
        
        }
        while(left<s.length()){
        
            count = Integer.parseInt(String.valueOf(s.charAt(left)))-1;
            char temp = (char)('a'+count);
            res.append(temp);
            left++;
        }
        
        return new String(res);
    }
}

5304. 子数组异或查询

class Solution {
    public int[] xorQueries(int[] arr, int[][] queries) {
        int length = queries.length;
        int [] res = new int[length];
        for(int i=0;i<length;i++){
            int left = queries[i][0];
            int right = queries[i][1];
            int temp = arr[left];
            for(int j= left+1;j<=right;j++){
                temp ^= arr[j];
            }
            res[i]=temp;
        }
        return res;
    }
}

5305. 获取你好友已观看的视频

Collection.sort中重写compare的方法不太熟悉,花了很长时间
然后Entry老是提示cannot find symbol class entry,后来Import一下库就好了,之前想了半天不知道错在哪里。

题目想法是简单的,主要是卡在排序那儿了,没想出更好的办法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
class Solution {
boolean [] visited = null;
	 public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends, int id, int level) {
	        int length = friends.length;
	        visited = new boolean[length];
	        //获取第K层的好友
	        visited[id] = true;
	        Queue<Integer> q = new LinkedList<Integer>();
	        q.offer(id);
	        int[] eachQcount = new int[level+1];
	        eachQcount[0] = 1;
	        for(int i=1;i<=level;i++){
	            for(int count = 0;count < eachQcount[i-1];count++ ){
	                int [] currFriends = friends[q.poll()];
	                for(int j =0 ;j<currFriends.length;j++){
	                    if(visited[currFriends[j]]==false){
	                        q.offer(currFriends[j]);
	                        visited[currFriends[j]] = true;
	                        eachQcount[i]++;
	                    }
	                }
	            }
	        }
	        //获取k层朋友的播放列表,并排序
	        
	        
	        Map<String,Integer> maps = new HashMap<String,Integer>();
	        while(!q.isEmpty()){
	            int index = q.poll();
	            List<String> videoList = watchedVideos.get(index);
	            for(int i =0 ;i<videoList.size();i++){
	                if(maps.containsKey(videoList.get(i))){
	                    maps.put(videoList.get(i),maps.get(videoList.get(i))+1);
	                }else{
	                    maps.put(videoList.get(i),1);
	                }
	            }
	        }
	        List<Map.Entry<String,Integer>> lists = new ArrayList<Map.Entry<String,Integer>>(maps.entrySet());
	        Collections.sort(lists,new Comparator<Map.Entry<String,Integer>>(){
	

				@Override
				public int compare(Entry<String, Integer> o1,
						Entry<String, Integer> o2) {
					// TODO Auto-generated method stub
					if (o1.getValue().intValue() == o2.getValue().intValue()) {
		                return o1.getKey().compareTo(o2.getKey());
		            } else {
		                return o1.getValue() - o2.getValue();
		            }
				}
	        });
    
	        
	        List<String> res = new ArrayList<String>();
	        for(Entry<String,Integer> e:lists){
	           // System.out.println("s: "+s+" ");
	            res.add(e.getKey());
	        }
	        
	        return res;
	       
	        
	    }
    
}

今天在第三题中卡太久,第四题还是没写。There is a long way to go.

发布了21 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Tracy_Yuan2014/article/details/103844281