笔试训练(5)

在正式开始进行今天的笔试训练之前,先做一道开胃小菜:

在字符串中找出连续最长的数字串的长度:

输入样例:abcd12345ed125ss123456789

输出样例:9

package com.example.demo;

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean IsTrue(char ch){
        if(ch>='0'&&ch<='9'){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        String str=scanner.nextLine();
        int max=0;
        int count=0;
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(IsTrue(ch)){//这个是最终遇不到字母比如说全是数字的情况
                count++;
                if(count>max){
                    max=count;
                }
            }else{//这个是遇到字母停下来的情况
                if(count>=max){
                    max=count;
                }
                count=0;
            }
        }
        System.out.println(max);
    }
}

之前我的代码是这样子写的:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean IsTrue(char ch){
        if(ch>='0'&&ch<='9'){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
         Scanner scanner =new Scanner(System.in);
         String str=scanner.nextLine();
         int max=0;
         int count=0;
         for(int i=0;i<str.length();i++){
             char ch=str.charAt(i);
                 if(IsTrue(ch)){
                     count++;
                 }else{
                     if(count>=max){
                         max=count;
                         count=0;
                     }
                 }
             }
             System.out.println(max);
         }
        }
    

这样子写也是错误的:

1)遇到字母才会更新最大值

2)不超过最大值不会将count置为0

输入样例:abcd12345ed125ss123456789

错误输出样例:5,况且最终count的值加到了11,因为count的值在走过中间那一段的时候已经加到了再次遇到字母没有手动清成0

笔试训练1: 字符串中找出连续最长的数字串字符串中找出连续最长的数字串_牛客题霸_牛客网 

输入:abcd12345ed125ss123456789

输出:123456789

思路:

1)我们可以定义两个字符串,一个是临时字符串,一个是数字长度最大的字符串

2)我们一旦发现临时字符串比最大的字符串大,那么就自动进行更新最大字符串里面的值



import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean IsTrue(char ch){
        if(ch>='0'&&ch<='9'){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        String str=scanner.nextLine();
        int count=0;
        String max="";
        String temp="";
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(IsTrue(ch)){
                temp+=ch;
                if(temp.length()>max.length()){
                    max=temp;
                }
                if((i+1<str.length())&&(!IsTrue(str.charAt(i+1))))
//看看i指向的下一个字符是不是字母,如果是直接把本次临时字符串里面的字符清空
                {
                    temp="";
                }
            }
        }
        System.out.println(max);
    }
}

3)但是我们在这里面不能用StringBuiler来做:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean IsTrue(char ch){
        if(ch>='0'&&ch<='9'){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        String str=scanner.nextLine();
        int count=0;
        StringBuilder temp=new StringBuilder();
        StringBuilder max=new StringBuilder();
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(IsTrue(ch)){
                temp.append(ch);
                if(temp.length()>max.length()){
                    max=temp;
                }
                if((i+1<str.length())&&(!IsTrue(str.charAt(i+1))))
                {
                    temp.setLength(0);
                }
            }
        }
        System.out.println(max);
    }
}

原因就是把temp的长度设置成0了,此时max的长度也被变成0了

笔试训练2:数组中出现次数超过一半的数字_牛客题霸_牛客网

1)使用hashmap来进行统计每一个数字出现的次数:

在这里面注意一下:遍历HashMap的时候,调用entrySet方法的时候,最终的返回值是Set<Map.Entry<K,V>>,这里面的Entry不要写成小写

class Solution {
    public int majorityElement(int[] array) {
       HashMap<Integer,Integer> result=new HashMap<>();
             for(int i=0;i<array.length;i++){
                 if(!result.containsKey(array[i])){
                     result.put(array[i],1);
                 }else{
                     int count=result.get(array[i]);
                     result.put(array[i],count+1);
                 }
             }
        Set<Map.Entry<Integer,Integer>> entrySet=result.entrySet();
        for(Map.Entry<Integer,Integer> set:entrySet){
            if(set.getValue()>array.length/2){
                return set.getKey();
            }
         }
         return -1;
       }
    }

2)将数组进行排序,直接返回中间下标的数字,但是你给的是1,2,3,4,5,直接返回中间的数字也不对呀?

2.1)判断数组长度是否为空或者数组中是否有数据

2.2)我们可以找到中间的数字X

2.3)我们可以进行遍历原来的数组,看看X出现了几次,看看可能是否超过了出现次数超过一半的

import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
       if(array==null||array.length==0){
           return -1;
       }
       Arrays.sort(array);
       int len=array.length;
       int midNum=len/2;
       int count=0;
       for(int i=0;i<array.length;i++){
           if(array[i]==array[midNum]){
               count++;
           }
       }
       if(count>len/2){
           return array[midNum];
       }else{
           return -1;
       }
    }
}

3)使用投票法:

3.1)如果说两个数不相等,那么就消去这两个数,最坏情况下,每一次都消去一个众数和一个非众数,如果说最后存在众数,最后留下来的数一定是众数,大不了两个非众数也在相互抵消

3.2)我们假设相同的数字是一个帮派,如果说两个数字是不同的帮派,如果发现两个数字不一样,那么两个数字那么直接进行互杀,大不了互相消亡,count就表示牛逼的那个帮派的人数

import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
if(array==null||array.length==0) return -1;
//这里面的if里面的条件是不可以进行互换的
        int count=0;
        int temp=array[0];
        for(int i=0;i<array.length;i++){
            if(array[i]==temp){
                count++;
            }else{
                count--;
            }
              if(count==0){
                  temp=array[i+1];//前面的帮派都已经死了,该立新的帮派为王了
              }
        }
        return temp;
    }
}

但是假设有现在的一种情况:

1,2,3,2,1

1)假设我们现在使用投票法来解决这个问题,现在i走到数组的最后一个位置了,按理说最后返回的应该是1,但是此时我们发现1并不是在数组中出现次数大于数组长度的数所以说我们根本就不知道数组中是否存在众数(符合题目要求的数)

2)所以说我们应该此时再次遍历一次数组,看看这个数是否出现次数大于数组长度的一半

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值