【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.249java

方法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;code

類中有一個方法,方法名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;rem

類中有一個方法,方法名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;string

類中有一個方法,方法名withoutString;it

給定兩個字符串參數base和remove,返回刪除了remove字符串的base字符串(不區分大小寫),
而且返回的base字符串不含有remove的重疊事例。
例如:("This is a FISH", "IS") -> "Th a FH" (注意Th和a之間有兩個空格哦)
注意: 指望值裏的一個空格能夠表明多個空格.io

提示:

方法調用 指望值
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"));
    }

}
相關文章
相關標籤/搜索