编程之美初赛是在codeHunt上进行的,就是给一些输入和输出让后让你猜要做什么然后编程实现,通过测试用例即可
第一道要实现的是消除字符串前后的空格,中间的空格如果有多个替换成一个,由空格隔开的字符串序列倒序输出,比如" LLTTL TLT LTLT "经过转化后的结果应该是
"LTTLL TLT TLTL",代码实现如下:
public class Program3 {
public static String Puzzle(String s) {
List<Integer> emptyPos = new ArrayList<Integer>();
for (int i=0; i<s.length(); ++i) {
if (s.charAt(i) == ' ')
emptyPos.add(i);
}
StringBuilder resultBuilder = new StringBuilder(s.length());
if (emptyPos.size() == 0) {
for (int i=s.length() - 1; i>=0; --i)
resultBuilder.append(s.charAt(i));
return resultBuilder.toString();
} else {
int lastPos = emptyPos.get(0);
if (lastPos != 0) {
for (int i=lastPos -1; i>=0; --i)
resultBuilder.append(s.charAt(i));
resultBuilder.append(' ');
}
for (int count=1; count<emptyPos.size() - 1; ++count) {
int nextPos = emptyPos.get(count);
for (int i=nextPos - 1; i>lastPos; --i)
resultBuilder.append(s.charAt(i));
resultBuilder.append(' ');
lastPos = nextPos;
}
int finalPos = emptyPos.get(emptyPos.size() - 1);
for (int i = s.length() - 1; i>finalPos; --i)
resultBuilder.append(s.charAt(i));
return resultBuilder.toString();
}
}
public static void main(String[] args) {
System.out.println(Puzzle("1T sd"));
System.out.println(Puzzle(" LLTT LLTL TLLTLTLT "));
}
}
public class Program {
private static final int T = -1;
private static final int _1 = 1;
private static final int _0 = 0;
public static String Puzzle(String a, String b) {
String shortOne = a.length() > b.length() ? b : a;
String longerOne = shortOne == a ? b : a;
StringBuilder result = new StringBuilder(a.length() + b.length());
int carryBit = _0;
for (int count = 0; count < longerOne.length(); ++count) {
char c1 = longerOne.charAt(longerOne.length() - 1 - count);
char c2;
if (count < shortOne.length())
c2 = shortOne.charAt(shortOne.length() - 1 - count);
else
c2 = '0';
int[] addResult = addChar(c1, c2, carryBit);
carryBit = addResult[1];
result.append(toCharValue(addResult[0]));
}
if (carryBit != _0)
result.append(toCharValue(carryBit));
return reverseAndSlim(result);
}
private static int[] addChar(char c1, char c2, int carryBit) {
int[] addResult = addChar(c1, c2);
int[] addCarryBitResult = addOpValue(addResult[0], carryBit);
return new int[]{addCarryBitResult[0], addResult[1] + addCarryBitResult[1]};
}
private static String reverseAndSlim(StringBuilder result) {
int noneZeroPos = result.length() - 1;
while (noneZeroPos >= 0) {
if (result.charAt(noneZeroPos) != '0')
break;
noneZeroPos--;
}
char[] slimResult = new char[noneZeroPos + 1];
for (int i=0; i<=noneZeroPos; ++i)
slimResult[i] = result.charAt(noneZeroPos - i);
return new String(slimResult);
}
private static int toOpValue(char c) {
if (c == '1')
return _1;
else if (c == 'T')
return T;
else
return _0;
}
private static char toCharValue(int opValue) {
if (opValue == _1)
return '1';
else if (opValue == T)
return 'T';
else
return '0';
}
private static int[] addOpValue(int op1, int op2) {
int result = op1 + op2;
int carryBit;
if (result == T + T) {
result = _1;
carryBit = T;
} else if (result == _1 + _1) {
result = T;
carryBit = _1;
} else {
carryBit = _0;
}
return new int[]{result, carryBit};
}
private static int[] addChar(char c1, char c2) {
int op1 = toOpValue(c1);
int op2 = toOpValue(c2);
return addOpValue(op1, op2);
}
public static void main(String[] args) {
System.out.println(Puzzle("111", "TTT"));
System.out.println(Puzzle("1101", "10T"));
System.out.println(Puzzle("1T01", "10T"));
System.out.println(Puzzle("T", "0"));
System.out.println(Puzzle("T", "0T"));
System.out.println(Puzzle("T", "0110"));
System.out.println(Puzzle("T", "011T0"));
System.out.println(Puzzle("T", "0110TT"));
System.out.println(Puzzle("T0TT1011", "11"));
}
}
希望能摸到西决地板!