1,问题简述
统计一个数字在排序数组中出现的次数。
2,示例
示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 示例 2: 输入: nums = [5,7,7,8,8,10], target = 6 输出: 0 限制: 0 <= 数组长度 <= 500003,题解思路
正常的逻辑思路,比对
4,题解程序
public class SearchTest3 { public static void main(String[] args) { int [] nums={5,7,7,8,8,10}; int target=8; int search = search(nums, target); System.out.println("search = " + search); } public static int search(int[] nums, int target) { if (nums == null) { return 0; } int count = 0; for (int num : nums ) { if (num == target) { count++; } } return count; } }5,题解程序图片版
6,总结
这道题之前的用法竟然是使用键值对集合HashMap来做的,现在看有点大材小用吧,时间复杂度为O(n),空间复杂度为O(1)就可以了,这或许就是一点个人的思考吧,不同的时间做法就不一样了