标签:new auto -- 目录 contains 次数 ble 出现 否则
目录
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。如果小数部分为循环小数,则将循环的部分括在括号内。
输入: numerator = 2, denominator = 3
输出: "0.(6)"
思路:
long rem = (num % den) * ,10;
来判断是否可以整除,之后会不断用到。// 分母、分子为0的情况
if (denominator == 0) return "";
if (numerator == 0) return "0";
StringBuilder res = new StringBuilder();
// 结果是否为负数
if ((numerator < 0) ^ (denominator < 0)) res.append("-");
// 取绝对值,方便处理
long num = numerator, den = denominator;
num = Math.abs(num);
den = Math.abs(den);
// 整数部分
res.append(num / den);
long rem = (num % den) * 10;
if (rem == 0) return res.toString();
// 负数部分
res.append(".");
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
while (rem != 0) {
if (map.containsKey(rem)) {
Integer loc = map.get(rem);
String p1 = res.substring(0, loc);
String p2 = res.substring(loc);
res = new StringBuilder(p1 + "(" + p2 + ")");
return res.toString();
}
map.put(rem, res.length());
res.append(rem / den);
rem = (rem % den) * 10;
}
return res.toString();
169次数大于一半才是众数
思路:
int maj = 0, cnt = 0;
// 遍历
for (int num : nums){
if (num == maj){
cnt++;
}
else if (cnt == 0) {
maj = num;
cnt++;
}
else cnt--;
}
229超过n/3属于众数
思路
int m = 0, n = 0, cm = 0, cn = 0;
for (auto &a : nums) {
if (a == m) ++cm;
else if (a ==n) ++cn;
else if (cm == 0) m = a, cm = 1;
else if (cn == 0) n = a, cn = 1;
else --cm, --cn;
}
cm = cn = 0;
for (auto &a : nums) {
if (a == m) ++cm;
else if (a == n) ++cn;
}
if (cm > nums.size() / 3) res.push_back(m);
if (cn > nums.size() / 3) res.push_back(n);
return res;
输入: [1,2,3,4]
输出: [24,12,8,6]
思路:题目不能用除法
注意边界初始化record[0] = 1和right = 1,再根据例子列出左累乘[1,1,2,6]就可已解出
int n = nums.length;
int[] record = new int[n];
record[0] = 1;
for (int i = 1; i < n; i++){
record[i] = record[i-1] * nums[i-1];
}
int right = 1;
for (int i = n - 1; i >=0; i--){
record[i] *= right;
right *= nums[i];
}
return record;
if (x == 0) return 0;
double res = (double) x;
double last = 0.0;
while (res != last) {
last = res;
res = res - x / (2 * res); // 求解看下面
}
return (int) res;
f(x) = x^2^ - n
(f(x) - f(x~i~))/ (x - x~i~) = f‘(x~i~)
求出x~i+1~ = g(x)即可,x表示上面res,f(x)表示上面的参数x
n > 0 && (n & (n-1)) == 0
标签:new auto -- 目录 contains 次数 ble 出现 否则
原文地址:https://www.cnblogs.com/code2one/p/10100195.html