9.9头条笔试java代码(AC4.8)

package real.toutiao2;


import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		
		Scanner scan=new Scanner(System.in);
		String str=scan.next();
		
		int[] v = new int[10000]; 
	    int j = 0;
	    int i = 0;
	    int res = 0;
	    for (i = 0; i < str.length(); i++) {
	        while (j < str.length() && v[str.charAt(j)]==0) {
	            v[str.charAt(j)] = 1;
	            res = Math.max(res, j-i + 1);
	            j ++;
	        }
	        v[str.charAt(i)] = 0;
	    }
		System.out.println(res);
	}

}
package real.toutiao2;
import java.util.*;
/*这题过了0.8   不晓得为什么*/
public class Main1{
	
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int m=scan.nextInt();
		if(m<=1||m>=1000)
			return;
		boolean a[][]=new boolean[m][m];
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				if(scan.nextInt()==1)
					a[i][j]=true;
				else
					a[i][j]=false;
			}
		}
		System.out.println(findNum(a));
		
	}
	public static int findNum(boolean[][] a) {
        if (a == null || a.length == 0 || a[0].length == 0) {
            return 0;
        }
        
        int n = a.length;
        int m = a[0].length;
        int islands = 0;
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j]) {
                    markByBFS(a, i, j);
                    islands++;
                }
            }
        }
        
        return islands;
    }
    
    private static void markByBFS(boolean[][] a, int x, int y) {
        // magic numbers!
        int[] directionX = {0, 1, -1, 0};
        int[] directionY = {1, 0, 0, -1};
        
        Queue<Point> queue = new LinkedList<Point>();
        
        queue.offer(new Point(x, y));
        a[x][y] = false;
        
        while (!queue.isEmpty()) {
            Point p = queue.poll();
            for (int i = 0; i < 4; i++) {
                Point adj = new Point(
                    p.x + directionX[i],
                    p.y + directionY[i]
                );
                if (!inBound(adj, a)) {
                    continue;
                }
                if (a[adj.x][adj.y]) {
                    a[adj.x][adj.y] = false;
                    queue.offer(adj);
                }
            }
        }
    }
    
    private static boolean inBound(Point p, boolean[][] a) {
        int n = a.length;
        int m = a[0].length;
        
        return p.x >= 0 && p.x < n && p.y >= 0 && p.y < m;
    }
		
	
		
}

class Point {
    int x, y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}



package real.toutiao2;
import java.util.*;

/*有效ip*/
public class Main3 {
    public int  numOfIP(String s) {
        List<String> result = new ArrayList<String>();
        List<String> list = new ArrayList<String>();
        
        if(s.length() <=3 || s.length() > 12)
            return result.size();
        find(result, list, s , 0);
        	return result.size();
    }
    
    public void find(List<String> result, List<String> list, String s, int begin){
        if(list.size() == 4){
            if(begin != s.length())
                return;
            
            StringBuffer sb = new StringBuffer();
            for(String tmp: list){
                sb.append(tmp);
                sb.append(".");
            }
            sb.deleteCharAt(sb.length()-1);
            result.add(sb.toString());
            return;
        }
        
        for(int i=begin; i<s.length() && i < begin+3; i++){
            String tmp = s.substring(begin, i+1);
            if(isvalid(tmp)){
                list.add(tmp);
                find(result, list, s, i+1);
                list.remove(list.size()-1);
            }
        }
    }
    
    private boolean isvalid(String s){
        if(s.charAt(0) == '0')
            return s.equals("0"); 
        int number = Integer.valueOf(s);
        return number >= 0 && number <= 255;
    }
    
    public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		String str=scan.next();
		Main3 m=new Main3();
		System.out.println(m.numOfIP(str));
		
	}
    
}

package real.toutiao2;

import java.util.Scanner;
/*utf-8*/
public class Main4 {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++){
			a[i]=scan.nextInt();
		}

		System.out.println(numOfUtf(a));
		
	}

	public static int numOfUtf(int[] a) {
		int i = 0;
		while (i < a.length) {

			int ones = 0;
			while (((a[i] >> (7 - ones)) & 1) == 1) {
				ones++;
			}
			if (ones == 1 || ones > 4)
				return 0;
			i++;
			while (ones-- > 1) {
				if (i >= a.length || ((a[i] >> 6) & 3) != 2)
					return 0;

				i++;
			}
		}

		return 1;
	}
}
package real.toutiao2;


import java.util.*;
/*抖音红人*/
class edge{

	int from,to,next;

	public edge(int a, int b, int c){
		from=a;to=b;next=c;
	}

}

class Solution{

	int n,m;
	int header[];
	edge edges[];
	int cnt = 0;
	boolean vis[];
    void solver(){    
    	Scanner scan=new Scanner(System.in);
        n=scan.nextInt();
        m=scan.nextInt();
        header = new int[n+1];
        Arrays.fill(header, -1);
        edges = new edge[m];
        vis = new boolean[n+1];
        for(int i=0;i<m;i++){
        	int f = scan.nextInt();
        	int t =scan.nextInt();
        	edges[i] = new edge(t,f,header[t]);

        	header[t] = cnt++;

        }

        int total = 0;

        for(int i=1;i<=n;i++){

        	Arrays.fill(vis, false);
        	vis[i] = true;

        	int tmp = dfs(i);

                if(tmp==n) total++;

        }      

        System.out.println(total);

    }

    int dfs(int cur){

        int s = 1;

    	for(int i=header[cur];i!=-1;i=edges[i].next){

    		edge t = edges[i];

    		if(!vis[t.to]){

    			vis[t.to] = true;

    			s+=dfs(t.to);

    		}

    	}

        return s;

    }

}

public class Main5{

    public static void main (String [] args) throws Exception {

    	Solution s = new Solution();

        s.solver();

    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值