文章目录
代码直接复制进代码测试区域即可
2235. 两整数相加
我的代码
class Solution {
public int sum(int num1, int num2) {
return num1 + num2;
}
}
2469. 温度转换
我的代码
class Solution {
public double[] convertTemperature(double celsius) {
double[] ans = {0,0};
ans[0] = celsius + 273.15;
ans[1] = celsius * 1.80 + 32.00;
return ans;
}
}
答案
直接return的时候创建新数组并赋值,
这个倒是无所谓一样的思路,代码要易读。
注意:写法没等号
class Solution {
public double[] convertTemperature(double celsius) {
return new double[]{celsius+273.15,celsius*1.80+32.00};
}
}
2413. 最小偶倍数
根据题意,当 n 为奇数时,答案为 2n,当 n 为偶数时,答案为 n。
我的代码
class Solution {
public int smallestEvenMultiple(int n) {
if (n % 2 == 0) {//偶数
return n;
} else {//奇数
return n * 2;
}
}
}
答案
直接三元运算符问一下
class Solution {
public int smallestEvenMultiple(int n) {
return n%2==0?n : n*2;
}
}
答案
新思路但是,不能一眼懂
return (n % 2 + 1) * n;
2236. 判断根结点是否等于子结点之和
我的代码
class Solution {
public boolean checkTree(TreeNode root) {
return root.left.val + root.right.val == root.val ? true : false;
}
}
1486. 数组异或操作
做对这道题,首先要明确有关异或的基本知识
手动计算异或的方法是:
每个数字转换为二进制,然后进行按位运算,相同为0,不同为1。对每一个数字进行这样的操作,最后0 ^ 2 ^ 4 ^ 6 ^ 8 结果得8。
我的代码
这道题的错误在与不知道异或的基本知识:a^0=a;所以给ans赋初值为0就很重要,因为nums[1] ^ ans = nums[1];
错误代码
对start理解不清,对边界分析不清
正确代码:
public int xorOperation(int n, int start) {
int[] nums = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {//赋值
nums[i] = start + 2*i;
//ans =start ^ nums[i];错误
ans ^= nums[i];//0^a=a
}
return ans;
}
答案
思路与答案是相同的
优点:没有创建数组,节省空间
public int xorOperation(int n, int start) {
int ans = 0;
for (int i = 0; i < n; i++) {//赋值
ans ^= (start + 2*i);//0^a=a
}
return ans;
}
1512. 好数对的数目
我的代码
拿两个for循环嵌套遍历即可,我知道这个方法时间复杂度肯定大:n的平方。但是我第一时间想到的确实是这个。
class Solution {
public int numIdenticalPairs(int[] nums) {
int ans = 0;
for (int i = 0;i < nums.length;i++){
for (int j = i+1;j < nums.length;j++){
if (nums[i] == nums[j]){
ans += 1;
}
}
}
return ans;
}
}
答案
核心思路
- 哈希表的作用:记录每个数字在遍历过程中已经出现的次数,避免双重循环。
例如,当遍历到 nums[j] 时,map[x] 表示 x 在 [0, j-1] 范围内的出现次数。 - 好数对的统计逻辑:
如果 nums[j] 的值 x 之前出现过 c 次,那么这 c 个 x 都可以与 nums[j] 组成好数对。
因此,直接 ans += c。 - 时间复杂度:
O(n)(只需遍历一次数组)。
比暴力解法(O(n²))高效。
class Solution {
public int numIdenticalPairs(int[] nums) {
int ans = 0;
Map<Integer,Integer> map = new HashMap<>();//key是数字的值(nums[i]);value是该数字在当前遍历位置之前已经出现的次数
for (int x : nums){//nums[j];nums[i]相当于已经在hash表里了
int c = map.getOrDefault(x,0);//如果x在hash表中,返回对应的value;如果x不在hash表中,返回默认值0
ans += c;
//c表示x 在 [0, j-1] 范围内已经出现的次数
//这些c个x每一个都可以,与当前遍历到的nums[j]组成好数队,
//因此直接累加 而不是ans += 1;
map.put(x,c+1);
}
return ans;
}
}
代码解释
----
1534. 统计好三元组
我的代码
- 暴力解法思路(三个for嵌套,if判断)没问题,但真是太笨的方法。
仍要注意边界值问题,我对边界值不能分析,而是看到报错才开始检查边界值。 - 而且在这道题上,case没通过的时候我进行打印输出,检查问题
class Solution {
public static int countGoodTriplets(int[] arr, int a, int b, int c) {
int ans = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
for (int k = j+1; k < arr.length; k++) {
if((Math.abs(arr[i] - arr[j]) <= a) && (Math.abs(arr[j] - arr[k]) <= b ) && (Math.abs(arr[i] - arr[k] ) <= c)){
ans++;
}
}
}
}
return ans;
}
}
暴力解法优化,这个只优化了一点: 先快速跳过所有不满足前两个条件的元素。但是第一种方法在思路上更好理解。
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int n = arr.length;
int count = 0;
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
if (Math.abs(arr[i] - arr[j]) > a) {
continue;
}
for (int k = j + 1; k < n; k++) {
if (Math.abs(arr[j] - arr[k]) > b) {
continue;
} else if (Math.abs(arr[i] - arr[k]) <= c) {
count++;
}
}
}
}
return count;
}
}
答案
方法二
前缀和,还不会
709. 转换成小写字母
我的代码
直接使用String的API
class Solution {
public String toLowerCase(String s) {
return s.toLowerCase();
}
}
答案
也可以不使用API自行实现
class Solution {
public String toLowerCase(String s) {
//调用API
// return s.toLowerCase();
//自己实现
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
//如果是大写,那么转换成小写然后添加到stringBuffer里;如果不是大写(即直接就是小写),那么直接加入stringBuffer中
if(ch >= 65 && ch <= 90){
// ch |= 32;//这里用的是一种更底层的位运算方法,也是有关为什么a和A之间差32而不是26
//也可以使用Java自带的方法
ch = Character.toLowerCase(ch);
}
stringBuffer.append(ch);
}
return stringBuffer.toString();
}
}
1281. 整数的各位积和之差
我的代码
class Solution {
public static int subtractProductAndSum(int n) {
int product = 1;
int sum = 0;
while(n > 0) {
product = product * (n % 10);
sum = sum + (n % 10);
n /= 10;
}
return product-sum;
}
}
231. 2的幂
我的代码
思路:
class Solution {
public boolean isPowerOfTwo(int n) {
if(n == 0){
return false;
}
while(n % 2 == 0){
n /= 2;
}
return n==1;
}
}
答案
思路:
使用位运算
class Solution {
public boolean isPowerOfTwo(int n) {
//方法二:不使用循环/递归解决此问题
return (n > 0) && (n &(n-1))==0;
}
}
326. 3的幂
我的代码
思路:
和上面2的幂思路相同
class Solution {
public boolean isPowerOfThree(int n) {
if(n == 0){
return false ;
}
while (n %3 == 0){
n /= 3;
}
return n == 1;
}
}
答案
思路:
作者:灵茶山艾府
public boolean isPowerOfThree(int n) {
/*方法一:
if(n == 0){
return false ;
}
while (n %3 == 0){
n /= 3;
}
return n == 1;*/
//方法二 模运算:本题范围内:只要是3的幂数,必然是1162261467的因子
return n > 0 && 1162261467 % n == 0;
}
- 遇到幂运算先想最大公约数性质
- 质数问题优先考虑模运算特性
263. 丑数
我的代码
丑数定义是只能被2/3/5整除,不是做除法游戏
class Solution {
public boolean isUgly(int n) {
/*
n=14时会出错,因为int的除法运算直接省略小数
if(n/5==2 || n/5==3 || n/2==3 || n == 1){
return true;
}else {
return false;
}
*/
if(n <=0 )return false;
if(n == 1)return true ;
while(n % 2 == 0)n /= 2;
while(n % 3 == 0)n /= 3;
while(n % 5 == 0)n /= 5;
return n == 1;
}
}
总结:
- 不写if (n == 1) return true;虽然也能通过编译,但是数学定义和代码逻辑是两回事
- 因数和质因数
被ai骂了我要笑死了哈哈哈哈哈
2的幂,3的幂,丑数总结
1470.重新排列数组
我的代码
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[nums.length];
for (int i = 0, j = nums.length/2,k=0; i < nums.length/2 && j < nums.length; i++,j++){
ans[k] = nums[i];
ans[k+1] = nums[j];
k += 2;
}
return ans;
}
}
更精简的
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[nums.length];
for (int i = 0, k = 0; i < nums.length/2; i++) {
ans[k++] = nums[i]; // 前半段元素
ans[k++] = nums[i + nums.length/2]; // 后半段元素
}
return ans;
}
}
答案
class Solution {
public int[] shuffle(int[] nums, int n) {
//使用一个变量
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length/2; i++) {
ans[2*i] = nums[i]; // 偶数位放前半段
ans[2*i+1] = nums[i+nums.length/2]; // 奇数位放后半段
}
return ans;
}
}
我的问题
int[] ans = new int[nums.length];
int mid = nums.length/2;
for (int i = 0; i < nums.length/2; i++) {
ans[i] = nums[i];
ans[i+1] = nums[i+mid];
}
return ans;
2469.
我的代码
答案
867. 转置矩阵
我的代码
class Solution {
public int[][] transpose(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] ans = new int[n][m];
for (int i = 0; i < n; i++) {//i是行
for (int j = 0; j < m; j++) {//j是列
ans[i][j] = matrix[j][i];
}
}
return ans;
}
}
要点
我的问题
- 只处理了对角线的元素,非对角线元素没处理。
for (int i = 0,j=0; i < m && j < n; i++,j++) {
ans[i][j] = matrix[j][i];
}
- 要注意范围,下面是对的