算法(四十二)

1、给你一个包含数字的数组,返回所有可能组成ip的结果。如果没有返回false
输入:数组a为[2,5,5,2,5,5,1,1,1,3,5]
输出:[255.255.11.135, 255.255.111.35]

import java.util.ArrayList;
import java.util.List;

public class test001 {
    public static void main (String[] args) {
        test035 ip = new test035();
        int[] a = new int[]{2,5,5,2,5,5,1,1,1,3,5};
//        String s = a.toString();
        String b="";
        //对整型数组进行遍历,高级for循环
        for(int i:a) {
            //将整型数据变为字符串,valueof方法
            String s1=String.valueOf(i);
            //拼接字符串
            b=b+s1;
        }
        List<String> res = ip.restoreIpAddresses(b);
        if(res==null || res.isEmpty()){
            System.out.println(false);
        }else{
            System.out.println(res);
        }
    }
    public List<String> restoreIpAddresses(String s) {
        List<String> ans = new ArrayList<>();//保存最终的所有结果
        backtrack(s,0,new StringBuilder(),ans,0);
        return ans;
    }

    private void backtrack(String s,int st,StringBuilder t,List<String> ans,int c){
        //如果剩余的长度大于剩下的部分都取 3 位数的长度,那么直接结束
        if ( (s.length() - st) > (3*(4 - c))){
            return;
        }
        //当前刚好到达了末尾
        if (st == s.length()){
            if (c == 4) {//加入结果
                ans.add(new String(t.substring(0,t.length()-1)));
            }
            return;
        }
        //当前超过末位,或者已经到达了 4 部分结束掉
        if (st>s.length()||c==4){
            return;
        }
        //保存的当前的解
        StringBuilder builder = new StringBuilder(t);
        //加入1
        t.append(s.charAt(st)+""+'.');
        backtrack(s,st+1,t,ans,c+1);

        if (s.charAt(st)=='0')return;
        //加入2#10 33
        if(st+1<s.length()){
            t = new StringBuilder(builder);
            t.append(s.substring(st,st+2)+""+'.');
            backtrack(s,st+2,t,ans,c+1);
        }
        //加入3 #225
        if(st+2<s.length()){
            t = new StringBuilder(builder);
            int n =Integer.parseInt(s.substring(st,st+3));
            if(n>=0&&n<=255){
                t.append(s.substring(st,st+3)+""+'.');
                backtrack(s,st+3,t,ans,c+1);
            }

        }
    }
}

2、两个有序的数组A,B。A的数组长度是B数组长度两倍。A数组的后半部分是空的,数据都在前半部分。现在要求把元素都合并到A里面,并且保持有序。
方法一:
//用-1代替空值部分
public class test002 {
    public static void main(String[] args) {
        int[] a = new int[]{1,3,5,7,9,-1,-1,-1,-1,-1};
        int[] b = new int[]{0,2,4,6,8};
        for(int i=0; i<b.length; i++){
            a[a.length/2+i]=b[i];
        }
        for(int j=0; j<a.length; j++){
            for(int k=0; k<a.length-1-j; k++){
                if(a[k]>a[k+1]){
                    int temp = a[k];
                    a[k] = a[k+1];
                    a[k+1] = temp;
                }
            }
        }
        for(int i=0; i<a.length; i++){
            System.out.print(a[i]+" ");
        }
    }
}
方法二:
public class test002 {
    public static void main(String[] args) {
        int[] a = new int[]{1,3,5,7,9,-1,-1,-1,-1,-1};
        int[] b = new int[]{0,2,4,6,8};
        sort(a,b,a.length/2, b.length);
        for(int j=0; j<a.length; j++){
            System.out.print(a[j]+" ");
        }
    }

    private static void sort(int[] a, int[] b, int m, int n) {
        int k = m+n-1;
        int j= n -1;
        int i = m - 1;

        while (k >= 0) {
            if(i < 0){
                a[k] = b[j];
                j--;
            }
            else if(j < 0 || a[i] > b[j]){
                a[k] = a[i];
                i--;
            }else{
                a[k] = b[j];
                j--;
            }
            k--;
        }
    }
}
方法三:
public class test002 {
    public static void main(String[] args) {
        int[] a = new int[]{1,3,5,7,9,-1,-1,-1,-1,-1};
        int[] b = new int[]{0,2,4,6,8};
        int[] c = new int[a.length];
        sort(a,b,c,a.length/2, b.length);
        for(int j=0; j<c.length; j++){
            a[j] = c[j];
        }
        for(int j=0; j<a.length; j++){
            System.out.print(a[j]+" ");
        }
    }

    private static void sort(int[] a, int[] b, int[] c, int m, int n) {
        int i=0;
        int j=0;
        while (i<m && j<n){
            if(a[i]<b[j]){
                c[i+j]=a[i];
                i++;
            }else {
                c[i + j] = b[j];
                j++;
            }
        }

        while (i<m){
            c[i+j]=a[i];
            i++;
        }

        while (j<n){
            c[i+j] = b[j];
            j++;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值