题解 | #牛群的编码II#
牛群的编码II
https://www.nowcoder.com/practice/ed764a3284744317a787ea8218eea880
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 转为10进制
* 减法计算
* 转为三进制
* @param a string字符串
* @param b string字符串
* @return string字符串
*/
public String subtractTernary (String a, String b) {
// write code here
int num1 = Integer.parseInt(a,3);
int num2 = Integer.parseInt(b,3);
String res = Integer.toString(num1-num2,3);
//这里应对出现前导0的情况(应该是偷懒了,或许本题是模拟计算)
while(res.length()<a.length()){
res = "0"+res;
}
return res;
}
}
面试高频TOP202 文章被收录于专栏
面试高频TOP202题解
