百度面试总结
来源:互联网 发布:淘宝买家版下载 编辑:程序博客网 时间:2024/06/03 15:31
- 项目介绍
- 介绍一下linux命令
- 线程进程的区别
- 线程和进程共享什么资源
线程共享的环境包括:进程代码段、进程的公有数据(利用这些共享的数据,线程很容易的实现相互之间的通讯)、进程打开的文件描述符、信号的处理器、进程的当前目录和进程用户ID与进程组ID。 - 设计模式,单例双重检查其实并发有问题
- 大数加法(+-号)
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")); }}
- 怎么才能最小次数使一个字符串变成回文的字符串
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
- 百度面试及总结
- 百度面试总结
- 百度面试及总结
- 百度实习面试总结
- 百度面试题目总结
- 百度面试总结
- 百度面试总结
- 百度面试总结
- 百度电话面试总结
- 百度面试总结
- 百度面试总结
- 百度面试总结
- 面试百度总结
- 百度面试总结
- 百度面试总结
- 百度 iOS 面试总结
- 面试百度总结
- 百度面试总结
- UVa227 谜题 Puzzle
- 希尔排序
- opencv 学习笔记第一天
- android 导入第三方包出错
- 颠倒的价牌
- 百度面试总结
- [OpenGL ES 02]OpenGL ES渲染管线与着色器
- “ 鸡尾酒会问题”(cocktail party problem)
- DBUtils框架的应用(小巧的数据库连接工具)
- kmp算法
- Django-Model字段选项
- 240. Search a 2D Matrix II
- 大文件排序
- Jquery之事件绑定(bind)实现加减乘除运算