目录
1 在区间范围内统计奇数数目
解题思路是将low与high分为奇-奇、奇-偶、偶-偶、偶-奇四种类型,除了偶-偶num=(high-low)//2以外,其余都是num = (high-low)//2 +1
class Solution:
def countOdds(self, low: int, high: int) -> int:
num = (high - low)//2
if not ((high % 2 == 0) and (low % 2 == 0)):
num += 1
return num
class Solution {
public:
int countOdds(int low, int high) {
int num = (high - low) / 2;
if (!((high % 2 == 0) && (low % 2 == 0)))
{
num++;
}
return num;
}
};
c++的与&&或 ||非!
2 去掉最低工资和最高工资后的工资平均值
class Solution:
def average(self, salary: List[int]) -> float:
max_salary = max(salary)
min_salary = min(salary)
average_salary = (sum(salary) - max_salary - min_salary) / (len(salary) - 2)
return average_salary
class Solution:
def average(self, salary: List[int]) -> float:
sum_salary = max_salary = 0
min_salary = 1000001
for i in salary:
if max_salary < i:
max_salary = i
if min_salary > i:
min_salary = i
sum_salary += i
average_salary = (sum_salary - min_salary - max_salary) / (len(salary) - 2)
return average_salary
class Solution {
public:
double average(vector<int>& salary) {
double sum_salary = 0;
int max_salary = 0;
int min_salary = 1000001;
for (int i=0; i<salary.size(); ++i)
{
if(max_salary < salary[i]) max_salary = salary[i];
if(min_salary > salary[i]) min_salary = salary[i];
sum_salary += salary[i];
}
double average_salary = (sum_salary - min_salary -max_salary) / (salary.size() - 2);
return average_salary;
}
};
class Solution {
public:
double average(vector<int>& salary) {
double maxValue = *max_element(salary.begin(), salary.end());
double minValue = *min_element(salary.begin(), salary.end());
double sum = accumulate(salary.begin(), salary.end(), - maxValue - minValue);
return sum / int(salary.size() - 2);
}
};
设置两个flag分别标志最大值和最小值,最后求和的时候减去这两个值,题目给出了工资范围,要注意最大值的初始值要比最小值小,最小值的初始值要比最大值大。直接调用函数就能通过
accumulate用法:accumulate(arr.begin(), arr.end(), int val);其中val是初始值
3 位1的个数
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
if n&1:
count += 1
n = n >> 1
return count
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
while(n)
{
if (n&1) count++;
n >>= 1;
}
return count;
}
};
python和c++的按位与&、或|、异或^、取反~、左移<<、右移>>。
x&1==0是偶数;x&1==1是奇数。如果位数不一致则右对齐
4 整数的各位积和之差
class Solution:
def subtractProductAndSum(self, n: int) -> int:
mul_ = 1
sum_ = 0
while(n > 0):
tmp = n % 10
mul_ *= tmp
sum_ += tmp
n = n // 10
return mul_ - sum_
class Solution {
public:
int subtractProductAndSum(int n) {
int mul_ = 1;
int sum_ = 0;
while (n > 0)
{
int tmp = n % 10;
mul_ *= tmp;
sum_ += tmp;
n = int(n / 10);
}
return (mul_ - sum_);
}
};
取余 再相加相乘 再除以10
5 三角形的最大周长
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
nums = sorted(nums, reverse=True)
for i in range(len(nums) - 2):
if nums[i] < nums[i+1] + nums[i+2]:
return nums[i] + nums[i+1] + nums[i+2]
return 0
class Solution {
public:
int largestPerimeter(vector<int>& nums) {
sort(nums.begin(), nums.end());
reverse(nums.begin(), nums.end());
for(int i=0; i<nums.size()-2; ++i)
{
if (nums[i] < nums[i+1] + nums[i+2])
return (nums[i] + nums[i+1] + nums[i+2]);
}
return 0;
}
};
三角形成立条件为任意两边之和大于第三边,于是先将数组按照从大到小排序,最大值大于右边两个小值相加即可
C++排序sort()方法在#include <algorihm>下面
6 找到最近的有相同 X 或 Y 坐标的点
class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
min_length = 100000
point = -1
for i in range(len(points)):
if points[i][0] == x or points[i][1] == y:
tmp = abs(points[i][1] - y) + abs(points[i][0] - x)
if min_length > tmp:
min_length = tmp
point = i
return point
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int min_length =100000;
int point = -1;
for (int i=0; i<points.size(); ++i)
{
if (points[i][0] == x || points[i][1] == y)
{
int tmp = abs(points[i][0] - x) + abs(points[i][1] - y);
if (min_length > tmp)
{
point = i;
min_length = tmp;
}
}
}
return point;
}
};
本题的案例应该是没有加入多个有效点的判断。。不判断也能过
7 数组元素积的符号
class Solution:
def arraySign(self, nums: List[int]) -> int:
x = 1
for num in nums:
if num == 0:
return 0
if num < 0:
x *= -1
return x
class Solution {
public:
int arraySign(vector<int>& nums) {
int x = 1;
for(auto it = nums.begin(); it!=nums.end(); it++)
{
if(*it == 0) return 0;
if(*it < 0) x = -x;
}
return x;
}
};
不必真的计算具体数值,只计算负数= -x,正数也不用管,遇到0直接返回0