最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

最后一天了(再见了)

IT圈 admin 2浏览 0评论

最后一天了(再见了)

我测你们码,这么快就最后一天了,虽然昨天星期六去浪了,属实是放纵了一天

1.最长平衡子串

package zfc;

/*
 * 最长平衡子串
 * 1000111 :6
 * 101001
 */
import java.util.*;
public class phzc {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        char c [] = str.toCharArray();
        int num =0;int num1=0;
        int ans =0;
        for(int i=0;i<c.length;i++) {
            if(str.charAt(i) == '0') {
                num++;
                if(num>0) {
                    num1=0;
                    num=1;
            }else {
                num++;
            }
        }else {
                num1++;
                ans = Math.max(num, num1);
            }
        }
        System.out.println(ans * 2);


    }
}

2.转换为二维数组

/*
 * 将一维数组中的元素,转换为行数尽可能小的二维数组
 * nums = [1113342]
 * 1342
 * 13
 * 1
 */

import java.util.*;

public class leetc {
    public static List<List<Integer>> findMatrix(int[] nums) {
        List<List<Integer>> list=new ArrayList<>();
        
        
        
        for (int i = 0; i < nums.length; i++) {
            boolean flag=true;
            
            //创建一个表
            if (list.size()<1) {
                list.add(new ArrayList<>());
            }
            
            //遍历子表
            for(List<Integer> list3:list) {
                flag=true;
            
                //判断子表和数组有无相同元素
                for (int j = 0; j < list3.size(); j++) {
                    if (list3.get(j)==nums[i]) {
                        flag=false;
                        break;
                    }
                }
                
                //第一次出现的所有的不同元素则放到第一行
                if (flag) {
                    list3.add(nums[i]);
                    break;
                }
            }
            
            //第二次出现相同的元素就会放置到第二行,第几次出现相同的元素放到第几行
            if (!flag) {
                List<Integer> list2=new ArrayList<>();
                list2.add(nums[i]);
                list.add(new ArrayList<>(list2));
            }
            
        }
        
        return list;
    }
    
public static void main(String[] args) {
    Scanner  sc = new Scanner(System.in);
    int n = sc.nextInt();
    int nums[]=new int[n];

    System.out.println(findMatrix(nums));
    
    }
    
}

最后一天了(再见了)

我测你们码,这么快就最后一天了,虽然昨天星期六去浪了,属实是放纵了一天

1.最长平衡子串

package zfc;

/*
 * 最长平衡子串
 * 1000111 :6
 * 101001
 */
import java.util.*;
public class phzc {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        char c [] = str.toCharArray();
        int num =0;int num1=0;
        int ans =0;
        for(int i=0;i<c.length;i++) {
            if(str.charAt(i) == '0') {
                num++;
                if(num>0) {
                    num1=0;
                    num=1;
            }else {
                num++;
            }
        }else {
                num1++;
                ans = Math.max(num, num1);
            }
        }
        System.out.println(ans * 2);


    }
}

2.转换为二维数组

/*
 * 将一维数组中的元素,转换为行数尽可能小的二维数组
 * nums = [1113342]
 * 1342
 * 13
 * 1
 */

import java.util.*;

public class leetc {
    public static List<List<Integer>> findMatrix(int[] nums) {
        List<List<Integer>> list=new ArrayList<>();
        
        
        
        for (int i = 0; i < nums.length; i++) {
            boolean flag=true;
            
            //创建一个表
            if (list.size()<1) {
                list.add(new ArrayList<>());
            }
            
            //遍历子表
            for(List<Integer> list3:list) {
                flag=true;
            
                //判断子表和数组有无相同元素
                for (int j = 0; j < list3.size(); j++) {
                    if (list3.get(j)==nums[i]) {
                        flag=false;
                        break;
                    }
                }
                
                //第一次出现的所有的不同元素则放到第一行
                if (flag) {
                    list3.add(nums[i]);
                    break;
                }
            }
            
            //第二次出现相同的元素就会放置到第二行,第几次出现相同的元素放到第几行
            if (!flag) {
                List<Integer> list2=new ArrayList<>();
                list2.add(nums[i]);
                list.add(new ArrayList<>(list2));
            }
            
        }
        
        return list;
    }
    
public static void main(String[] args) {
    Scanner  sc = new Scanner(System.in);
    int n = sc.nextInt();
    int nums[]=new int[n];

    System.out.println(findMatrix(nums));
    
    }
    
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论