本文主要是介绍第187场周赛,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第187场周赛
- 1436. 旅行终点站
- 1437. 是否所有 1 都至少相隔 k 个元素
- 1438. 绝对差不超过限制的最长连续子数组
1436. 旅行终点站
class Solution {public String destCity(List<List<String>> paths) {Set<String> in = new HashSet<String>();Set<String> out = new HashSet<String>();for(List<String> path:paths) {in.add(path.get(0));out.add(path.get(1));}Iterator<String> it = in.iterator();while(it.hasNext()) {out.remove(it.next());}String res;Iterator<String> ot = out.iterator();while(ot.hasNext()) {res = ot.next();return res;}return null;}
}
1437. 是否所有 1 都至少相隔 k 个元素
class Solution {public boolean kLengthApart(int[] nums, int k) {int left = 0;while(left<nums.length && nums[left]==0) {left++;}if(left>=nums.length) {return true;}int right = left+1;while(right<nums.length) {if(nums[right]==0) {right++;}else if(nums[right]==1) {if(right-left<=k) {return false;}left = right;right = left+1;}}return true;}
}
1438. 绝对差不超过限制的最长连续子数组
class Solution {public int longestSubarray(int[] nums, int limit) {if (nums ==null || nums.length==0)return 0;int curr_max = nums[0]; // 当子数组下最大值 这里初始化为第一个数int curr_min = nums[0]; // 当子数组下最大值 这里初始化为第一个数Queue<Integer> sub_nums = new LinkedList<Integer>();for(int num:nums){if (Math.abs(num - curr_max) <= limit && Math.abs(num - curr_min) <= limit && Math.abs(curr_max - curr_min) <= limit) {curr_max = Math.max(num,curr_max);curr_min = Math.min(num,curr_min);sub_nums.offer(num);}else{sub_nums.offer(num);sub_nums.poll();curr_max = Collections.max(sub_nums); // 当子数组最大值curr_min = Collections.min(sub_nums); // 当前子数组最小值}}return sub_nums.size();}
}
这篇关于第187场周赛的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!