keep alternate characters & get deletions




二、使用步骤

1.引入库

代码如下 :

import java.util.ArrayList;
import java.util.Scanner;

public class HackerRankTest1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String lineNumStr = scanner.nextLine();
        String trim = lineNumStr.trim();
        Integer integer = Integer.valueOf(trim);

        int count=0;
        StringBuilder stringBuilder = new StringBuilder();
        while (true){

            String nextLine = scanner.nextLine();
            stringBuilder.append(nextLine);
            stringBuilder.append("\n");

            count++;
            if (count>=integer){
                break;
            }
        }

        String toString = stringBuilder.toString();

        String[] splitStrArr = toString.split("\\n");
        ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
        for (int i = 0; i < splitStrArr.length; i++) {
            String tempStr = splitStrArr[i];
            Integer deletionsFromQueStr = getDeletionsFromQueStr(tempStr);

            integerArrayList.add(deletionsFromQueStr);
        }

        System.out.println(integerArrayList);

    }

    /**
     * keep alternate characters
     * get deletions
     * string length 1 10to5
     * queries count minimum 1 maximum 10
     *
     * @param s
     * @return
     */
    public static Integer getDeletionsFromQueStr(String s) {
        if (s == null) {
            return null;
        }
        if (s.isEmpty()) {
            return null;
        }

        int retVal = 1;
        for (int i = 0; i < 5; i++) {
            retVal *= 10;
        }

        int length = s.length();
        if (length < 1 || length > retVal) {
            return null;
        }
        for (int i = 0; i < s.length(); i++) {
            char charAt = s.charAt(i);
            if (charAt == 'A' || charAt == 'B') {
                continue;
            } else {
                return null;
            }
        }

        /**
         * input    output      output(test required)
         * 5
         * AAAA     A           3
         * BBBBB    B           3
         * ABABABAB ABABABAB    0
         * BABABA   BABABA      0
         * AAABBB   AB          4
         */

        String strTemp=s;
        int length1 = strTemp.trim().length();
        int count=0;
        for (int i = 0; i < length1; i++) {
            char charAt = strTemp.charAt(0);
            char charAt1 = strTemp.charAt(1);
            if (charAt==charAt1) {
                String substring = strTemp.trim().substring(i + 1);
                strTemp = substring;

                count++; // deletions

                length1 = strTemp.length();
                i=0;

            }
        }

        return count;
    }

}

2.读入数据

代码如下 :

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员是干活的

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

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

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

打赏作者

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

抵扣说明:

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

余额充值