【242天】我爱刷题系列(2)

叨叨两句

  1. 害怕与人的冲突,本质是不知如何应对,解决的办法不是逃避,而是学习高手如何应对【人是具备连续性的生物,行为可预测,高手的方法可信度高】。

  2. 不要小看任何你未搞清楚的事物。

题4:删除原字符串del

题目描述

书写一个类,类名为Tencent;

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

给定一个字符串,如果字符串“del”从索引1开始出现
返回一个新字符串,其中新字符串中“del”已被删除。 否则,返回字符串不变。

提示:

方法调用期望值
delDel("adelbc")"abc"
delDel("adelHello")"aHello"
delDel("adedbc")"adedbc"


public class Tencent{
    public String delDel(String str) {
        if((str.length() >= 4) && (str.substring(1,4).equals("del"))) {
            return "" + str.charAt(0) + str.substring(4,str.length());
        } else {
            return str;
        }
    }
}

注意区分:equals() 与 ==

题5:统计以"y"或"z"结尾的单词个数

题目描述:

书写一个类,类名为Tencent;

类中有一个方法,方法名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 Test01 {
    public int countYZ(String str) {
        int count = 0;
        String temp = str.toUpperCase();
        String[] words = temp.split("\\W|\\d");
        for (String word : words) {
            if(word.endsWith("Z")||word.endsWith("Y")) {
                count++;
            }
        }
        return count;
    }
}

题6: 判断g是否为happy字

题目描述:

书写一个类,类名为Tencent;

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

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

提示:

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

法1
public class Test01 {
    public boolean gHappy(String str) {
        if(str.length()==0) {
            return false;
        }
        String regex = "gg+";
        return !(str.replaceAll(regex, "").contains("g"));
    }
}
法2
package com.baidu;

public class Test01 {
    public boolean gHappy(String str) {
        if(str.length()==1&&str.equals("g")||str.length()==0) {
            return false;
        }
        
        String temp = "a"+str+"a";
        
        for (int i = 1; i < temp.length() - 1; i++) {
            if(temp.charAt(i-1)!='g'&&temp.charAt(i)=='g'&&temp.charAt(i+1)!='g') {
                return false;
            }
        }
        
        return true;
        
    }
}

题7: 求字符串中连续出现的个数最多的字符的数量

题目描述:

书写一个类,类名为Tencent;

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

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

提示:

方法调用 期望值
maxBlock("hoopla") 2
maxBlock("abbCCCddBBBxx") 3
maxBlock("") 0


package com.baidu;

import java.util.HashMap;

public class Test01 {
    public int maxBlock(String str) {
        int max = 0;
        HashMap<Character, Integer> hm = new HashMap<>();
        int count = 1;
        for (int i = 0; i < str.length() - 1; i++) {
            char c1 = str.charAt(i);
            char c2 = str.charAt(i + 1);
            hm.put(c1, count);
            if(c1 == c2) {
                count++;
                if(count > hm.get(c1)) {
                    hm.put(c1, count);
                }
            } else {
                count = 1;
                hm.put(c2, count);
            }

            for (Character c : hm.keySet()) {
                if(hm.get(c) >= max) {
                    max = hm.get(c);
                }
            }
        }
        return max;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值