- 项目介绍
- 介绍一下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)) {
String stmp = a;
a = b;
b = stmp;
boolean btmp = aIsNegative;
aIsNegative = bIsNegative;
bIsNegative = btmp;
}
String ans = SubtractCore(a, b);
if (aIsNegative && !bIsNegative) {
return "0".equals(ans) ? "0" : "-" + ans;
} else {
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)) {
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)) {
String stmp = a;
a = b;
b = stmp;
boolean btmp = aIsNegative;
aIsNegative = bIsNegative;
bIsNegative = btmp;
}
if (aIsNegative && bIsNegative) {
return SubtractCore(a, b);
} else if (aIsNegative && !bIsNegative) {
return AddCore(a, b);
} else if (!aIsNegative && bIsNegative) {
String ans = AddCore(a, b);
return "0".equals(ans) ? "0" : "-" + ans;
} else {
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));
}
}
}
}