刷题记录02

题目1

在这里插入图片描述
解析:
遍历字符串,使用cur去记录连续的数字串,
如果遇到不是数字字符,则表示一个连续的数字串结束了,
则将数字串跟之前的数字串比较,如果更长,则更新更长的数字串更新到ret。
具体代码:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String ret = "";
        String cur = "";
        int i = 0;
        for (; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= '0' && ch <= '9') {
                cur = cur + ch + "";
            } else {
                if (ret.length() < cur.length()) {
                    ret = cur;
                }
                cur = "";
            }
        }
        //处理123abc123456这种情况
        if (i == str.length() && ret.length() < cur.length()) {
            ret = cur;
        }

        System.out.println(ret);
    }
}

题目2在这里插入图片描述

思路一:

  1. 定义一个HashMap来统计每个数字出现的次数
  2. 遍历数组,对每个数字:
  • 如果map中不存在该数字,将其加入map,value设为1
  • 如果map中存在该数字,将其value加1
  1. 遍历map,找到value最大的数字及其次数maxCount
  2. 如果maxCount > nums.length / 2,说明找到了出现次数超过数组长度一半的数字,返回该数字
  3. 否则返回0

具体代码:

 public static int MoreThanHalfNum_Solution1 (int[] numbers) {
        // write code here
        Map<Integer, Integer> count = new HashMap<>();
        for (int num:numbers){
            count.put(num, count.getOrDefault(num,0)+1);
        }
        int maxCount=0;
        int majority=0;
        for (Map.Entry<Integer,Integer>entry : count.entrySet()){
            if (entry.getKey() >maxCount){
                maxCount=entry.getValue();
                majority=entry.getKey();
            }

        }
        if (maxCount >numbers.length / 2){
            return majority;
        }
        return 0;
    }

思路二:
数组排序后,如果符合条件的数存在,则一定是数组中间那个数。
具体过程:
在这里插入图片描述

具体代码:

 public static int MoreThanHalfNum_Solution(int[] numbers){
        if (numbers == null || numbers.length == 0){
            return 0;
        }
        Arrays.sort(numbers);
        int len=numbers.length;
        int midNum=numbers[len/2];
        int count=0;
        for (int i=0;i<len;i++){
            if (numbers[i] == midNum){
                count++;
            }
        }
        if (count > len/2){
            return midNum;
        }
        return 0;

    }

思路三
众数:就是出现次数超过数组长度一半的那个数字
如果两个数不相等,就消去这两个数,最坏情况下,每次消去一个众数和一个非众数,那么如果存在众数,最后留下的数肯定是众数

在这里插入图片描述
实际的过程就是找众数

 public int MoreThanHalfNum_Solution3(int [] array) {
        if(array == null || array.length==0) {
            return 0;
        }
        int result = array[0];
        int times = 1; // 次数
        for(int i=1;i<array.length;++i){
            if(times != 0){
                if(array[i] == result) {
                    ++times; // 相同则加1
                }else{
                    --times; // 不同则减1
                }
            }
            else {
// 更新result的值为当前元素,并置次数为1
                result = array[i];
                times = 1;
            }
        }
// 判断result是否符合条件,即出现次数大于数组长度的一半
        times = 0;
        for(int i=0;i<array.length;++i){
            if(array[i] == result) {
                ++times;
                }
            }
        return (times > array.length/2) ? result : 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忘忧记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值