【244天】我爱刷题系列(3)

叨叨两句

  1. 未来我爱刷题系列将与跃迁之路系列绑定,同步更新,即每天我都将运用跃迁之路的方法进行刻意练习。
  2. 总结套路,用乘法思路跃迁。

题8:编写程序把这些IP按数值大小,从小到大排序并打印出来

编写程序把这些IP按数值大小,从小到大排序并打印出来。
61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249

方法1:用循环嵌套进行二次切割

package com.heima_xxxxxxxxx;

import java.util.ArrayList;
import java.util.Collections;

public class Test5_4 {
    public static void main(String[] args) {
        mySort("61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249");
    }
    
    public static void mySort(String str) {
        ArrayList<Integer> list = new ArrayList<>(); 
        String[] arr1 = str.split(" ");
        for(String sTemp1 : arr1) {
            String[] arr2 = sTemp1.split("\\.");
            for(String sTemp2 : arr2) {
                list.add(Integer.parseInt(sTemp2));
            } 
        }
        Collections.sort(list);
        System.out.println(list);
    }
    
}

方法2:用正则实现一次切割

package com.heima_xxxxxxxxx;

import java.util.ArrayList;
import java.util.Collections;
public class Test5_4 {
    public static void main(String[] args) {
        mySort("61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249");
    }
    
    public static void mySort(String str) {
        ArrayList<Integer> list = new ArrayList<>(); 
        String[] arr1 = str.split("[^0-9]");//或使用\\D
        for(String s : arr1) {
            list.add(Integer.parseInt(s));
        }
        Collections.sort(list);
        System.out.println(list);
    }
    
}

题9:给定一字符串,截取出该字符串开头和结尾相同的内容且不可重叠,并返回

题目描述:

书写一个类,类名为Itheima;

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

给定一字符串,截取出该字符串开头和结尾相同的内容且不可重叠,并返回。
例如:("javaXYZjava") -> "java",否则返回空。

提示:

方法调用期望值
sameEnds("abXYab")"ab"
sameEnds("xx")"x"
sameEnds("xxx")"x"
package com.heima_6;

public class Itheima_01 {

    public String sameEnds(String str) {
        //遍历字符
        //索引0字符串 尝试 endsWith  
            //为true 则截取返回
            //为false pass
        //索引0 + 1字符串 尝试endsWith
        //。。。
        //一直尝试到0+1+...+length-2索引字符(不能尝试倒数第1个了)
    
        String newStr = "";
        for(int i = 0; i < str.length() - 1 ; i++) {
            newStr = newStr + str.charAt(i);
            if(str.endsWith(newStr)) {
                return newStr;
            }
        }
        
        return "";
        
    }

}
package com.heima_6;

public class Test_01 {

    public static void main(String[] args) {
        Itheima_01 it = new Itheima_01();
        
        System.out.println(it.sameEnds("abXYab"));
        System.out.println(it.sameEnds("xx"));
        System.out.println(it.sameEnds("xxx"));
    }

}

题10: 给定一字符串,求出现在字符串中的数字之和

题目描述:

书写一个类,类名为Itheima;

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

给定一字符串,求出现在字符串中的数字之和。
例如:sumNumbers("abc123xyz") → 123

提示:

方法调用期望值
sumNumbers("abc123xyz")123
sumNumbers("aa11b33")44
sumNumbers("7 11")18
package com.heima_3;

public class Itheima_07 {
    public int sumNumbers(String str) {
        int sum = 0;
        String regex = "\\D+";
        String[] arr = str.split(regex);
        
        for(String s : arr) {
            if(s.length() != 0) {
                sum = sum + Integer.parseInt(s);
            }
        }
        return sum;
    }
}
package com.heima_3;

public class Test_07 {
    public static void main(String[] args) {
        Itheima_07 it = new Itheima_07();
        System.out.println(it.sumNumbers("abc123xyz")); 
        System.out.println(it.sumNumbers("aa11b33")); 
        System.out.println(it.sumNumbers("7 11")); 
    }
}

题11: 移除指定的字符串

题目描述:

书写一个类,类名为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"

方法1

package com.heima_6;

public class Itheima_07 {

    public String withoutString(String base,String remove) {
        String newStr = base.replaceAll(remove,"");
        return newStr;
        
    }
}

方法2

package com.heima_6;

public class Itheima_07 {

    public String withoutString(String base,String remove) {
        for(int i = 0; i <= base.length() - remove.length(); i++) {
            String newStr = base.substring(i, i + remove.length());
            if(newStr.equalsIgnoreCase(remove)) {
                base = base.replace(newStr, "");
            }
        }
        return base;
    }
}
package com.heima_6;

public class Test_07 {

    public static void main(String[] args) {
        Itheima_07 it = new Itheima_07();
        System.out.println(it.withoutString("Hello there", "llo"));
        System.out.println(it.withoutString("Hello there", "e"));
        System.out.println(it.withoutString("Hello there", "x"));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值