上一篇推文给你们留下的习题来自于《剑指 Offer》第 29 题:数组中超过一半的数字,不知道各位去思考了么?java
面试题:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字并输出。好比 {1,2,3,2,2,2,1} 中 2 的次数是 4,数组长度为 7,因此输出 2。要求不能修改输入的数组。面试
这道题能思考到的测试用例比较简单。算法
第二步即是咱们的思考程序逻辑了,题目要求查找出现次数超过一半的数字。比较容易想到的思路是直接对数组排序,那中间那个值就是咱们想要的值,但这样的想法明显排序后会对输入的数组顺序有影响,因此咱们可能须要换一种思路。数组
再看一遍题干,咱们不难思考到,咱们是否能够对每一个数字进行计数,最后返回计数次数最多的值。存储次数采用 map 作映射处理。测试
public class Test19 {
private static int moreThanHalfNums(int[] nums) {
if (nums == null || nums.length == 0)
throw new RuntimeException("the length of array must be large than 0");
int len = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num))
map.put(num, map.get(num) + 1);
else
map.put(num, 1);
}
int times = len / 2;
// 查找 map 中 value 最大的值
for (Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > times)
return entry.getKey();
}
throw new RuntimeException("invalid input!");
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 2, 2, 4, 2, 2, 5};
System.out.println(moreThanHalfNums(nums1));
int[] nums2 = {1};
System.out.println(moreThanHalfNums(nums2));
int[] nums3 = {2, 1, 2, 1, 2, 2, 3, 2, 1};
System.out.println(moreThanHalfNums(nums3));
int[] nums4 = {1, 2, 3, 4, 5};
System.out.println(moreThanHalfNums(nums4));
}
}
复制代码
写毕后进行测试用例的验证,无不例外,目前都经过,因而咱们把这样的代码解法递交给面试官。spa
面试官看了这样的算法,表示他更期待的是不使用任何辅存空间的算法。因而咱们得换个角度思考。code
数组中有一个数字出现的次数超过数组长度的一半,也就是说它出现的次数比其余全部数字出现次数的和还要多。所以咱们能够考虑在遍历数组的时候保存两个值: 一个是数组中的一个数字, 一个是次数。当咱们遍历到下一个数字的时候,若是下一个数字和咱们以前保存的数字相同,则次数加 1 ;若是下一个数字和咱们以前保存的数不一样,则次数减 1。若是次数为 0,咱们须要保存下一个数字,并把次数设为 1 。因为咱们要找的数字出现的次数比其余全部数字出现的次数之和还要多,那么要找的数字确定是最后一次把次数设为 1 时对应的数字。cdn
咱们来看这样的思路用代码怎么实现。排序
public class Test19 {
private static int moreThanHalfNums(int[] nums) {
if (nums == null || nums.length == 0)
throw new RuntimeException("the length of array must be large than 0");
int result = nums[0];
int times = 1;
int len = nums.length;
for (int i = 1; i < len; i++) {
if (times == 0) {
result = nums[i];
times = 1;
} else if (result == nums[i])
times++;
else
times--;
}
times = 0;
for (int num : nums) {
if (num == result)
times++;
}
if (times > len / 2)
return result;
throw new RuntimeException("invalid input!");
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 2, 2, 4, 2, 2, 5};
System.out.println(moreThanHalfNums(nums1));
int[] nums2 = {1};
System.out.println(moreThanHalfNums(nums2));
int[] nums3 = {2, 1, 2, 1, 2, 2, 3, 2, 1};
System.out.println(moreThanHalfNums(nums3));
int[] nums4 = {1, 2, 3, 4, 5};
System.out.println(moreThanHalfNums(nums4));
}
}
复制代码
写毕后,验证测试用例,一样所有经过。get
本题最后的思路,但愿你们刻意去思考和记忆一下,由于也许变一下题意,这样的想法还能够用到。
咱们下一次推文的题目来源于《剑指 Offer》第 31 题:计算连续子数组的最大和。
面试题:输入一个整型数组,数组中有正数也有负数。数组中一个或多个整数造成一个子数组,求全部子数组的和的最大值,要求时间复杂度为 O(n)。 好比输入 {1, -2, 3, 10, -4, 7, 2, -5},能产生子数组最大和的子数组为 {3,10,-4,7,2},最大和为 18。
我是南尘,只作比心的公众号,欢迎关注我。
南尘,GitHub 7k Star,各大技术 Blog 论坛常客,出身 Android,但不单单是 Android。写点技术,也吐点情感。作不完的开源,写不完的矫情,你就听听我吹逼,不会错~