YaphetK:后来想的写法,这样写比较简洁,用了些lambda表达式     import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input = sc.nextLine().split(" ");
        TreeMap<String, Set<String>> map = new TreeMap<>();
        String[] mark;
        Set<String> set;
        for (String s : input) {
            mark = s.split("-");
            set = !map.containsKey(mark[1]) ? new HashSet<>() : map.get(mark[1]);
            set.add(mark[0]);
            map.put(mark[1], set);
        }
        List<Map.Entry<String, Set<String>>> list = new ArrayList<>(map.entrySet());
        list.sort((Map.Entry<String, Set<String>> root, Map.Entry<String, Set<String>> next) ->
                next.getValue().size() - root.getValue().size() != 0 ? next.getValue().size() - root.getValue().size() : root.getKey().compareTo(next.getKey()));
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(i).getKey() + " " + list.get(i).getValue().size());
        }
    }
}
 投递马蜂窝等公司10个岗位
投递马蜂窝等公司10个岗位0 点赞 评论 收藏   
分享
 绅蓝:不逼签三方就先拿着
0 点赞 评论 收藏   
分享
 大虾s:public class Main {
	public static void main(String[] args) {
		int[] arr = new int[] {1,2,3,4,5,6,7};
		int n = 3;
		int len = arr.length;
		int cur = arr[0];
		int index = 0;
		int newIndex = 0;
		int temp = cur;
		for(int i = 0 ; i < len ; i++) {
			newIndex = (index+n)%len;
			cur =  arr[newIndex];//更新当前值
			arr[newIndex] = temp;//保存上一个数
			temp = cur;
			index = newIndex;
		}
		for(int i : arr) {
			System.out.print(i+" ");
		}
		
	}
}
我觉得这是最好的,时间复杂度O(n),空间O(1)
 投递多益网络等公司10个岗位
投递多益网络等公司10个岗位0 点赞 评论 收藏   
分享
 创作者周榜
更多 
 关注他的用户也关注了: