百度面试总结

  1. 项目介绍
  2. 介绍一下linux命令
  3. 线程进程的区别
  4. 线程和进程共享什么资源
    线程共享的环境包括:进程代码段进程的公有数据(利用这些共享的数据,线程很容易的实现相互之间的通讯)、进程打开的文件描述符信号的处理器进程的当前目录进程用户ID与进程组ID
  5. 设计模式,单例双重检查其实并发有问题
  6. 大数加法(+-号)
    public class Main {

    /**
     * 两个大数相加
     *
     * @param a
     * @param b
     * @return
     * @throws Exception
     */
    private static String Add(String a, String b) throws Exception {
        // 处理空
        if (a == null || b == null || "".equals(a) || "".equals(b)) {
            throw new Exception("number is null.");
        }
        // 处理负号
        boolean aIsNegative = false, bIsNegative = false;
        if (a.startsWith("-")) {
            aIsNegative = true;
            a = a.substring(1);
        }
        if (b.startsWith("-")) {
            bIsNegative = true;
            b = b.substring(1);
        }
        // 处理正号
        if (a.startsWith("+")) a = a.substring(1);
        if (b.startsWith("+")) b = b.substring(1);
        if (!check(a) || !check(b)) {
            throw new Exception("number is error.");
        }
        if (aIsNegative == bIsNegative) {
            String ans = AddCore(a, b);
            if (aIsNegative) {
                return "0".equals(ans) ? "0" : "-" + ans;
            } else {
                return ans;
            }
        } else {
            if (!(a.length() > b.length() || a.length() == b.length() && a.compareTo(b) >= 0)) {
                // swap
                String stmp = a;
                a = b;
                b = stmp;
                boolean btmp = aIsNegative;
                aIsNegative = bIsNegative;
                bIsNegative = btmp;
            }
            String ans = SubtractCore(a, b);
            if (aIsNegative && !bIsNegative) {
                // -2+1
                return "0".equals(ans) ? "0" : "-" + ans;
            } else {
                // 2+-1
                return ans;
            }
        }
    }

    /**
     * 正数加法
     *
     * @param a
     * @param b
     * @return
     */
    private static String AddCore(String a, String b) throws Exception {
        if (a == null || b == null || "".equals(a) || "".equals(b)) {
            throw new Exception("number is null.");
        }
        int maxx = Math.max(a.length(), b.length());
        StringBuilder asb = new StringBuilder(a);
        for (int i = a.length(); i < maxx; i++) {
            asb.insert(0, '0');
        }
        a = asb.toString();
        StringBuilder bsb = new StringBuilder(b);
        for (int i = b.length(); i < maxx; i++) {
            bsb.insert(0, '0');
        }
        b = bsb.toString();
        StringBuilder ans = new StringBuilder();
        int e = 0;
        for (int i = maxx - 1; i >= 0; i--) {
            e = e + (a.charAt(i) - '0') + (b.charAt(i) - '0');
            ans.insert(0, e % 10);
            e /= 10;
        }
        while (e != 0) {
            ans.insert(0, e % 10);
            e /= 10;
        }
        String c = handlePrefixZero(ans.toString());
        return "".equals(c) || c.length() == 0 ? "0" : c;
    }

    /**
     * 参数为a>b
     * 正数减法,返回结果为大的数减小的数
     *
     * @param a
     * @param b
     * @return
     */
    private static String SubtractCore(String a, String b) throws Exception {
        if (a == null || b == null || "".equals(a) || "".equals(b)) {
            throw new Exception("number is null.");
        }
        int maxx = Math.max(a.length(), b.length());
        StringBuilder asb = new StringBuilder(a);
        for (int i = a.length(); i < maxx; i++) {
            asb.insert(0, '0');
        }
        a = asb.toString();
        StringBuilder bsb = new StringBuilder(b);
        for (int i = b.length(); i < maxx; i++) {
            bsb.insert(0, '0');
        }
        b = bsb.toString();
        StringBuilder ans = new StringBuilder();
        int e = 0;
        for (int i = maxx - 1; i >= 0; i--) {
            e = (e + (a.charAt(i) - '0')) - (b.charAt(i) - '0');
            if (e < 0) {
                ans.insert(0, e + 10);
                e = -1;
            } else {
                ans.insert(0, e);
                e = 0;
            }
        }
        String c = handlePrefixZero(ans.toString());
        return c;
    }

    /**
     * 正数乘法
     *
     * @param a
     * @param b
     * @return
     */
    private static String MultCore(String a, int b) throws Exception {
        if (a == null || "".equals(a)) {
            throw new Exception("number is null.");
        }
        if (b == 0) return "0";
        StringBuilder ans = new StringBuilder();
        int e = 0;
        for (int i = a.length() - 1; i >= 0; i--) {
            e = e + (a.charAt(i) - '0') * b;
            ans.insert(0, e % 10);
            e /= 10;
        }
        while (e != 0) {
            ans.insert(0, e % 10);
            e /= 10;
        }
        String c = handlePrefixZero(ans.toString());
        return "".equals(c) || c.length() == 0 ? "0" : c;
    }

    /**
     * 处理前缀0
     *
     * @param c
     * @return
     */
    private static String handlePrefixZero(String c) {
        if (c.startsWith("0")) {
            int pos = 0;
            while (pos < c.length() && c.charAt(pos) == '0') {
                pos++;
            }
            c = c.substring(Math.min(pos, c.length() - 1));
        }
        return c;
    }

    /**
     * 判断是否为数字
     *
     * @param s
     * @return
     */
    private static boolean check(String s) {
        if (s == null || "".equals(s)) return false;
        for (int i = 0, len = s.length(); i < len; i++) {
            char ch = s.charAt(i);
            if (ch >= '0' && ch <= '9') continue;
            else return false;
        }
        return true;
    }

    /**
     * 大整数乘法
     *
     * @param a
     * @param b
     * @return
     */
    private static String Mult(String a, String b) throws Exception {
        // 处理空
        if (a == null || b == null || "".equals(a) || "".equals(b)) {
            throw new Exception("number is null.");
        }
        // 处理负号
        boolean aIsNegative = false, bIsNegative = false;
        if (a.startsWith("-")) {
            aIsNegative = true;
            a = a.substring(1);
        }
        if (b.startsWith("-")) {
            bIsNegative = true;
            b = b.substring(1);
        }
        // 处理正号
        if (a.startsWith("+")) a = a.substring(1);
        if (b.startsWith("+")) b = b.substring(1);
        if (!check(a) || !check(b)) {
            throw new Exception("number is error.");
        }
        if (!(a.length() > b.length() || a.length() == b.length() && a.compareTo(b) >= 0)) {
            // swap
            String stmp = a;
            a = b;
            b = stmp;
            boolean btmp = aIsNegative;
            aIsNegative = bIsNegative;
            bIsNegative = btmp;
        }
        String ans = "0";
        for (int i = b.length() - 1, j = 0; i >= 0; i--, j++) {
            StringBuilder tmp = new StringBuilder(MultCore(a, b.charAt(i) - '0'));
            for (int k = 0; k < j; k++) {
                tmp.append("0");
            }
            ans = Add(ans, tmp.toString());
        }
        if (aIsNegative == bIsNegative) return ans;
        else return "-" + ans;
    }

    /**
     * 大整数减法
     *
     * @param a
     * @param b
     * @return
     */
    private static String Subtract(String a, String b) throws Exception {
        // 处理空
        if (a == null || b == null || "".equals(a) || "".equals(b)) {
            throw new Exception("number is null.");
        }
        // 处理负号
        boolean aIsNegative = false, bIsNegative = false;
        if (a.startsWith("-")) {
            aIsNegative = true;
            a = a.substring(1);
        }
        if (b.startsWith("-")) {
            bIsNegative = true;
            b = b.substring(1);
        }
        // 处理正号
        if (a.startsWith("+")) a = a.substring(1);
        if (b.startsWith("+")) b = b.substring(1);
        if (!check(a) || !check(b)) {
            throw new Exception("number is error.");
        }
        if (!(a.length() > b.length() || a.length() == b.length() && a.compareTo(b) >= 0)) {
            // swap
            String stmp = a;
            a = b;
            b = stmp;
            boolean btmp = aIsNegative;
            aIsNegative = bIsNegative;
            bIsNegative = btmp;
        }
        if (aIsNegative && bIsNegative) {
            // 2-1
            return SubtractCore(a, b);
        } else if (aIsNegative && !bIsNegative) {
            // 2-(-1)=2+1
            return AddCore(a, b);
        } else if (!aIsNegative && bIsNegative) {
            // -2-1 = -(2+1)
            String ans = AddCore(a, b);
            return "0".equals(ans) ? "0" : "-" + ans;
        } else {
            //-2-(-1)=-2+1=-(2-1)
            String ans = SubtractCore(a, b);
            return "0".equals(ans) ? "0" : "-" + ans;
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(111*(-12));
        System.out.println(Mult("111", "-12"));
    }

}
  1. 怎么才能最小次数使一个字符串变成回文的字符串
import java.util.Scanner;

public class Main {

    private static int dp[][] = null;

    private static int dp(char chs[], int start, int end) {
        if (chs == null || chs.length == 0
                || start >= chs.length || end >= chs.length
                || start >= end) {
            return 0;
        }
        if (chs[start] == chs[end]) {
            return dp[start][end] = dp(chs, start + 1, end - 1);
        } else {
            int leftPop = -1, rightPop = -1;
            if (dp[start + 1][end] == -1) dp[start + 1][end] = dp(chs, start + 1, end);
            if (dp[start][end - 1] == -1) dp[start][end - 1] = dp(chs, start, end - 1);
            leftPop = dp[start + 1][end];
            rightPop = dp[start][end - 1];
            return dp[start][end] = Math.min(leftPop, rightPop) + 1;
        }
    }

    private static void init(int dp[][]) {
        for (int i = 0, len = dp.length; i < len; i++) {
            for (int j = i; j < dp[0].length; j++) {
                dp[i][j] = dp[j][i] = -1;
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            int n = cin.nextInt();
            for (int i = 0; i < n; i++) {
                String s = cin.next();
                dp = new int[s.length()][s.length()];
                init(dp);
                System.out.println(dp(s.toCharArray(), 0, s.length() - 1));
            }

        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值