作业111

作业111,第1张

作业111

Java

第四题:

package com.taobao;

import java.util.Scanner;

public class Main {

    public static String helper(String s, String t){
        int flag1 = s.length()-1, flag2 = t.length()-1;
        StringBuilder strBuilder = new StringBuilder();
        int carry = 0;
        do{
            int x = flag1 >= 0? s.charAt(flag1--)-'0':0;
            int y = flag2 >= 0? t.charAt(flag2--)-'0':0;
            strBuilder.append((x+y+carry)%10);
            carry = (x+y+carry)/10;
        }while(flag1 >=0 || flag2 >=0);
        if(carry!=0){
            strBuilder.append(carry);
        }
        return strBuilder.reverse().toString();
    }


    public static String solve(String s, String t){
        if(s.equals("0") || t.equals("0")) return "0";
        char[] nums1 = s.toCharArray();
        char[] nums2 = t.toCharArray();
        String ans = "0";
        int zeronum = 0;
        for(int i = nums2.length-1; i>=0;i--){
            StringBuilder strBuilder = new StringBuilder();
            int carry = 0;
            for(int j=0;j=0;j--){
                int x = nums1[j]-'0';
                strBuilder.append((x*y+carry)%10);
                carry = (x*y+carry)/10;
            }
            if(carry!=0){
                strBuilder.append(carry);
            }
            ans = helper(ans, strBuilder.reverse().toString());
        }
        return ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s, t;
        s = scanner.nextLine();
        t = scanner.nextLine();
        System.out.println(solve(s, t));
    }
}

第五题:

package com.taobao;

import java.util.Scanner;

class TreeNode {
    char val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(char val) {
        this.val = val;
    }

    TreeNode(char val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


public class Main {

    public static TreeNode helper(String s, int inLeft, int inRight, String t, int postLeft, int postRight){
        if(inRight < inLeft || postRight < postLeft) return null;
        TreeNode root = new TreeNode(t.charAt(postRight));
        int index = 0;
        for(int i=0;i 

第六题:

package com.taobao;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        if(n == 0 && m == 0) return;
        int[] nums = new int[n+1];
        for(int i=0;i 

第七题:

package com.taobao;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] str = s.split(" ");
        int[] nums = new int[str.length];
        for(int i=0;i< str.length;i++){
            nums[i] = Integer.parseInt(str[i]);
        }
        Arrays.sort(nums);
        int target = nums[str.length/2];
        int i= str.length/2, j = str.length/2;
        while(nums[i] == target){
            i--;
            if(i < 0) break;
        }
        while(nums[j] == target){
            j++;
            if(j >= str.length) break;
        }
        if(j-i-1 < str.length/2){
            System.out.println(-1);
        }
        else {
            System.out.println(target);
        }
    }
}

第八题:

package com.taobao;

import java.util.linkedList;
import java.util.Queue;
import java.util.Scanner;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Main {

	public static int maxDepth(TreeNode root) {
        if(root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public static TreeNode stringToTreeNode(String input) {
        input = input.trim();
        if (input.length() == 0) {
            return null;
        }
        String[] parts = input.split(" ");
        String item = parts[0];
        TreeNode root = new TreeNode(Integer.parseInt(item));
        Queue nodeQueue = new linkedList<>();
        nodeQueue.add(root);

        int index = 1;
        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.remove();

            if (index == parts.length) {
                break;
            }

            item = parts[index++];
            item = item.trim();
            if (!item.equals("None")) {
                int leftNumber = Integer.parseInt(item);
                node.left = new TreeNode(leftNumber);
                nodeQueue.add(node.left);
            }

            if (index == parts.length) {
                break;
            }

            item = parts[index++];
            item = item.trim();
            if (!item.equals("None")) {
                int rightNumber = Integer.parseInt(item);
                node.right = new TreeNode(rightNumber);
                nodeQueue.add(node.right);
            }
        }
        return root;
    }

    public static void main(String[] args)  {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();;
        TreeNode root = stringToTreeNode(line);
        int ret = maxDepth(root);
        String out = String.valueOf(ret);
        System.out.println(out);
    }
}

第九题:

package com.taobao;

import java.util.Scanner;

public class Main {

    public static int solve(String s, String t) {
        if (t.length() == 0) return 0;
        int left = 0, right = 0;
        int ans = -1;
        while (left < s.length() && right < t.length()) {
            if (s.charAt(left) == t.charAt(right)) {
                left+=1;
                right+=1;
            } else {
                ans = left - right;
                left = ans + 1;
                right = 0;
            }
            if (right == t.length()) return ans+1;
        }
        return -1;
    }

    public static void main(String[] args)  {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String t = scanner.nextLine();
        System.out.println(solve(s, t));
    }
}

第十题:

package com.taobao;

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args)  {
        Scanner scanner = new Scanner(System.in);
        int[][] nums = new int[6][6];
        String[] StringArray = new String[6];
        for(int i=0;i<6;i++){
            StringArray[i] = scanner.nextLine();
            String[] s = StringArray[i].split(" ");
            for(int j=0;j					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691076.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存