题解 | #记录点赞用户#
记录点赞用户
https://www.nowcoder.com/practice/19a766a67cdc4eb0a354d70597cf008b
class LikeRecorderImpl implements LikeRecorder { private HashSet<String> usernames; //实现HashSet public LikeRecorderImpl(){ this.usernames=new HashSet<>(); } @Override public void like(String username){ if(!usernames.contains(username)){ usernames.add(username); }else{ usernames.remove(username); } } @Override public String[] getLikeUsers(){ return usernames.toArray(new String[1]); //String[0],还是String[1],都无所谓,传入的数组小,HashSet就重新创建一个大的,不小,就用传入的数组,主要是用来限定类型的 } }