火车进站、二叉树镜像

1.变量a是一个64位有符号的整数,初始值用16进制表示为:0Xf000000000000000; 变量b是一个64位有符号的整数,初始值用16进制表示为:0x7FFFFFFFFFFFFFFF。 则a-b的结果用10进制表示为多少?(C)
A.1
B.-(262+261+2^60+1)
C.262+261+2^60+1
D.259+(255+254+…+22+21+20)
2.有这么一段程序:

public class Test{
public String name="abc";
public static void main(String[] args){
    Test test=new Test();
    Test testB=new Test();
    System.out.println(test.equals(testB)+","+test.name.equals(testB.name));
    }
}

请问以上程序执行的结果是(C)
A.true,true
B.true,false
C.false,true
D.false,false
3.标题:火车进站
给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-
9编号。要求以字典序排序输出火车出站的序列号。
输入描述:
有多组测试用例,每一组第一行输入一个正整数N(0<N<10),第二行包括N个正整数,范围为1到9。
输出描述:
输出以字典序从小到大排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample。
示例1:
输入
3
1 2 3
输出
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { //输入火车数量
            int n = in.nextInt(); //输入火车编号 
            int[] A = new int[n];
            for (int i = 0; i < n; i++) {
                A[i] = in.nextInt();
            }
            int start = 0;
            //计算n个火车的出站的编号的排列组合 
            ArrayList<int[]> result = new ArrayList<int[]>();
            Permutation(A, start, n, result);
            //出栈的结果,一个元素一个记录,例如:1 2 3 ; 1 3 2 
            Set<String> sortResult = new TreeSet<String>();
            //循环排列组合 
            for (int[] out : result) {
                //判断是否满足出栈要求(后进先出) 
                if (isLegal(A, out, n)) {
                    //满足的组合,输入结果,每一个编号用空格分隔 
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < n - 1; i++) {
                        sb.append(out[i] + " ");
                    }
                    sb.append(out[n - 1]);
                    sortResult.add(sb.toString());
                }
            }
            //最后输出所有的符合出栈要求的组合 
            for (String list : sortResult) {
                System.out.println(list);
            }
        }
        in.close();
    }
    /*in : 火车编号数组 out : 火车出站顺序 n : 火车数量 */
    private static boolean isLegal(int[] in, int[] out, int n) {
        //栈:存储进站的火车编号 
        LinkedList<Integer> stack = new LinkedList<Integer>();
        int i = 0;
        int j = 0;
        while (i < n) {
            // in 还有元素的时候都需要判断 
            if (in[i] == out[j]) {
                //相等时候就不用入栈,直接后移 
                i++;
                j++;
            } else {
                if (stack.isEmpty()) {
                    //空stack 就只有入栈了 
                    stack.push(in[i]);
                    i++;
                } else {
                    int top = stack.peek(); // 栈顶元素相等,进行出栈 
                    if (top == out[j]) {
                        j++;
                        stack.pop();
                    } else if (i < n) {
                        //不相等时候入栈,说明还有待进站的车 
                        stack.push(in[i]);
                        i++;
                    }
                }
            }
        }
        while (!stack.isEmpty() && j < n) {
            // in 的结束后,栈中元素进程出栈序列应该和out剩余 的元素相同 
            int top = stack.pop();
            if (top == out[j]) {
                j++;
            } else {
                return false;
            }
        }
        return true;
    }
    /*** 求出所有排列 * @param A * @param start * @param n * @param result */
    private static void Permutation(int[] A, int start, int n, ArrayList<int[]> result) {
        if (start == n) {
            return;
        }
        if (start == n - 1) {
            int[] B = A.clone();
            result.add(B);
            return;
        }
        for (int i = start; i < n; i++) {
            swap(A, start, i);
            Permutation(A, start + 1, n, result);
            swap(A, start, i);
        }
    }
    private static void swap(int[] A, int i, int j) {
        int t = A[i];
        A[i] = A[j];
        A[j] = t;
    }
}

4.标题:二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树

public class Solution {
    public void Mirror(TreeNode root) {
        TreeNode node = root;
        if(node==null){
            return;
        }
        if(node.left==null&&node.right==null){
            return;
        }
        TreeNode t = node.left;
        node.left = node.right;
        node.right = t;
        if(node.left!=null){
            Mirror(node.left);
        }
        if(node.right!=null){
            Mirror(node.right);
        }
    }
}
//方法二
public class Solution {
    public void Mirror(TreeNode root) {
        //节点为null 不处理
        if (root == null) return;
        //节点的左右子节点为null(即就是节点为叶子节点)同样不处理
        if (root.left == null && root.right == null) return;
        //节点的左右子节点交换 
        TreeNode pTemp = root.left;
        root.left = root.right;
        root.right = pTemp;
        //递归处理 
        if (root.left != null) Mirror(root.left);
        if (root.right != null) Mirror(root.right);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值