学习笔记
weixin_45823711
这个作者很懒,什么都没留下…
展开
-
二分查找法解题思路
一般二分查找法是针对数组中的排列规律进行操作,也就是对索引的规律进行操作。 切记,尽量不要对值的变化进行操作。 最后return 才能是nums[index] 假设你有 n 个版本 [1, 2, …, n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。 在 public int firstBadVersion(int n) { int l = 1;原创 2021-11-19 15:32:15 · 196 阅读 · 0 评论 -
在一个数组中找到target的两数之和,返回两个数的下标
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 为了节约时间,可以将哈希表中的键设为数组的值,哈希表中的值设为输出目标(也就是返回的数的下标)。 Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>(); for (int i = 0; i < nums原创 2021-11-16 14:56:16 · 239 阅读 · 0 评论 -
字符串中找不同的元素
lc389 int[] cnt = new int[26]; for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); cnt[ch - 'a']++; } for (int i = 0; i < t.length(); ++i) { char ch = t.charAt(i);原创 2021-11-16 13:22:44 · 145 阅读 · 0 评论