【254天】我爱刷题系列(13)

叨叨两句

  1. 戒骄戒躁!
  2. 不要小看任何一个知识点!

题26:正则表达式的一个标准写法(Pattern)

题目描述

书写一个类,类名为Itheima;
类中有一个方法,方法名bobThere;
给你一个字符串,如果包含bob就返回true
注意如果bob中的字符o,可以是任何字符
例如:bob返回true, bpb返回true

提示

方法调用期望值
bobThere("abcbob")true
bobThere("b9b")true
bobThere("bac")false
public class Itheima {
    public boolean bobThere(String str){
        String regex = "b.b";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }
}

题27:查找满足特定条件的指定字符

题目描述

书写一个类,类名为Itheima;类中有一个方法,方法名countYZ;

给定一个字符串,找出里面以“y”或者“z”结尾的单词的个数,也就是说,跟在“y”或者“z”后面的字符不是英文单词。
“y”在“heavy”中和“z”在“fez”中计数,而“y”在“yellow”中不计数,(不区分大小写)

提示

方法调用期望值
countYZ("fez day")2
countYZ("day fez")2
countYZ("day fyyyz")2
public class Itheima {
    public int countYZ(String str){
        
        int count = 0;
        String str1 = str + " ";
        char[] c = str1.toCharArray();
        for (int i = 0; i < c.length - 1; i++) {
            if(c[i] == 'y' || c[i] == 'z' || c[i] == 'Y' || c[i] == 'Z'){
                char t = c[i + 1];
                if(!((t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z'))){
                    count++;
                }
            }
        }
        
    /*    if(c[c.length - 1] == 'y' || c[c.length - 1] == 'z' || c[c.length - 1] == 'Y' || c[c.length - 1] == 'Z'){
            count++;
        }*/
        
        return count;
    }
}

题28:取int类型数字指定位数形成int数组

题目描述

书写一个类,类名为Itheima;
类中有一个方法,方法名makePi;
返回一个包含pi(参考Math.PI)的前n位数字的整数数组长度,n为方法接收的参数。
例如:n为3,则返回{3,1,4}。

提示

方法调用期望值
makePi(1)[3]
makePi(2)[3,1]
makePi(3)[3,1,4]
public class Itheima {

    public int[] makePi(int n) {
        
        String str = Math.PI + "";
        String str2 = str.replaceAll("\\.", "");
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(str2.charAt(i)+"");
        }
        return arr;
        
        /*int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (int) (Math.PI * Math.pow(10, i) % 10);
        }
        return arr;*/
    }
}

题29:字符串前后加空格+累积标记

题目描述

书写一个类,类名为Itheima;

类中有一个方法,方法名gHappy;

如果字符串中的’g’的相邻左边或者右边有另外一个’g’,则’g’在这个字符串中是happy的,
如果字符串中所有的’g’都是happy的则返回true,否则返回false。

提示

方法调用 期望值
gHappy("xxggxx") true
gHappy("xxgxx") false
gHappy("xxggyygxx") false

public class Itheima {
    public boolean gHappy(String str){
        
        boolean sumFlag = true;
        boolean flag = true;
        boolean hasG = false;
        String newStr = " " + str + " ";
        
        for (int i = 1; i < newStr.length() - 1; i++) {
            char cBefore = newStr.charAt(i - 1);
            char c = newStr.charAt(i);
            char cLater = newStr.charAt(i + 1);
            if(c == 'g'){
                if(c == cBefore || c == cLater){
                    flag = true;
                } else {
                    flag = false;
                }
                hasG = true;
            } 
            sumFlag = sumFlag && flag;
        }
        return sumFlag && hasG;
    }
}

题30:连续出现字符计数

题目描述

书写一个类,类名为Itheima;

类中有一个方法,方法名maxBlock;

给定一个字符串,返回该字符串中连续出现个数最多的字符的数量。

提示

方法调用期望值
maxBlock("hoopla")2
maxBlock("abbCCCddBBBxx")3
maxBlock("")0
int count = 1;//用于作为中间变量,记录正在遍历的字符的个数
        HashMap<Character,Integer> map = new HashMap<>();//Key:字符 Value:最大连续出现次数
        for (int i = 0; i < str.length() - 1; i++) {
            char c1 = str.charAt(i);
            char c2 = str.charAt(i + 1);
            map.put(c1,count);
            if(c1 == c2){
                count++;
                if(count > map.get(c1)){
                    map.put(c1, count);
                }
            } else {
                count = 1;
            }
        }
        
        int totalMax = 0;
        for (Character c : map.keySet()) {
            if(map.get(c) >= totalMax){
                totalMax = map.get(c);
            }
        }
        
        return totalMax;

题31: 排除特定数组索引

题目描述

书写一个类,类名为Itheima;
类中有一个方法,方法名sum13;
有一个整型数组,返回的结果是数组所有元素的总和,如果数组中出现整型数13的时候,
那么元素13和13后面的一个元素也不计入总和里面。

提示

方法调用期望值
sum13([1,2,2,1])6
sum13([1,1])2
sum13([1,2,2,1,13])6
public class Itheima {
    public int sum13(int[] arr){
        HashSet<Integer> hs = new HashSet<>();  
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == 13){
                hs.add(i);
                hs.add(i + 1);
            }
        }
        
        for(int i = 0; i < arr.length; i++){
            if(hs.contains(i)){
                continue;
            }
            sum += arr[i];
        }
        return sum;

题32:使用标记切换使用代码

题目描述

书写一个类,类名为Itheima;
类中有一个方法,方法名sum67;
有一个整型数组,返回数组中的数字的总和,如果数组含有数6和7
那么忽略不计入从6开始并延伸到有7的数字段,7后面的数字需要参与运算(每6个将跟随至少一个7)。
返回0表示没有数字。

提示

方法调用期望值
sum67([1,2,2])5
sum67([1,2,2,6,99,99,7])5
sum67([1,1,6,7,2])4
public class Itheima {
    public int sum67(int[] arr){
        boolean switch67 = true;
        boolean sumFlag = true;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == 6 && switch67){
                switch67 = false;
                sumFlag = false;
            } 
            
            if(arr[i] == 7 && !switch67){
                switch67 = true;
                sumFlag = true;
                sum -= 7;
            }
            
            if(sumFlag){
                sum += arr[i];
            }
        }
        
        return sum;
        

题33:删除不区分大小写的子字符串

题目描述

书写一个类,类名为Itheima;

类中有一个方法,方法名withoutString;

给定两个字符串参数base和remove,返回删除了remove字符串的base字符串(不区分大小写),
并且返回的base字符串不含有remove的重叠事例。
例如:("This is a FISH", "IS") -> "Th a FH" (注意Th和a之间有两个空格哦)
注意: 期望值里的一个空格可以代表多个空格.

提示

方法调用期望值
withoutString("Hello there","llo")"He there"
withoutString("Hello there","e")"Hllo thr"
withoutString("Hello there","x")"Hello there"
public class Itheima {
    public String withoutString(String base,String remove){
        for (int i = 0; i < base.length() - remove.length(); i++) {
            String str = base.substring(i,remove.length()+ i);
            if(str.equalsIgnoreCase(remove)){
                base = base.replace(str, "");
            }
        }
        return base;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值