当前位置: 首页 > news >正文

java练习 day5

一、Nim 游戏

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean canWinNim(int n) {if(n % 4 == 0){return false;}return true;}
}

3、知识点

(1) 通过模拟来寻找 规律。

二、区域和检索 - 数组不可变

1、题目链接

点击跳转到题目位置

2、代码

class NumArray {int[] sums;public NumArray(int[] nums) {int n = nums.length;sums = new int[n+1];for(int i = 0; i < n; ++i){sums[i + 1] = sums[i] + nums[i];}}public int sumRange(int left, int right) {return sums[right + 1] - sums[left];}
}/*** Your NumArray object will be instantiated and called as such:* NumArray obj = new NumArray(nums);* int param_1 = obj.sumRange(left,right);*/

3、知识点

(1) 使用前缀和来解决问题。

三、3 的幂

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean isPowerOfThree(int n) {if(n <= 0){return false;}while(n > 1){if(n % 3 == 0){n /= 3;} else{return false;}}   return true;}
}

3、知识点

(1) 模拟试除。

四、比特位计数

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int count(int n){int res = 0;while(n > 0){n &= (n - 1);++res;}return res;}public int[] countBits(int n) {int[] res = new int[n+1];for(int i = 0; i <= n; ++i){res[i] = count(i);}return res;}
}

3、知识点

(1) 位运算,知识点同2的幂。

五、4的幂

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean isPowerOfFour(int n) {return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;}
}

3、知识点

(1) 位运算,如何判断一个数是2的幂,4的幂除以3的余数等于1。

六、反转字符串

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public void reverseString(char[] s) {int left = 0;int right = s.length - 1;while(left < right){char temp = s[left];s[left] = s[right];s[right] = temp;++left;--right;}}
}

3、知识点

(1) 双指针

七、反转字符串中的元音字母

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean judge(char ch){switch(ch){case 'a':case 'o':case 'i':case 'e':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':return true;}return false;}public String reverseVowels(String s) {int n = s.length();int left = 0;int right = n - 1;StringBuffer sb = new StringBuffer(s);while(left < right){while(left < n && judge(sb.charAt(left)) == false){++left;}while(right >= 0 && judge(sb.charAt(right)) == false){--right;}if(left > right){break;}char temp = sb.charAt(left);sb.setCharAt(left, sb.charAt(right));sb.setCharAt(right, temp);++left;--right;}  return sb.toString();}
}

3、知识点

(1) 字符串中进行遍历,交换两个字符。

(2) java中switch操作。

八、 两个数组的交集

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int[] getIntersection(Set<Integer> set1, Set<Integer> set2){if(set1.size() < set2.size()){return getIntersection(set2, set1);}Set<Integer> intersectionSet = new HashSet<Integer>();for(int num : set1){if(set2.contains(num)){intersectionSet.add(num);}}int []res = new int[intersectionSet.size()];int index = 0;for(int num : intersectionSet){res[index++] = num;} return res;}public int[] intersection(int[] nums1, int[] nums2) {Set<Integer> set1 = new HashSet<Integer>();Set<Integer> set2 = new HashSet<Integer>();for(int i = 0; i < nums1.length; ++i){set1.add(nums1[i]);} for(int i = 0; i < nums2.length; ++i){set2.add(nums2[i]);}return getIntersection(set1, set2);}
}

3、知识点

(1) java中的集合。

九、两个数组的交集 II

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int[] intersect(int[] nums1, int[] nums2) {int len1 = nums1.length;int len2 = nums2.length;int[] hash1 = new int[1005];int[] hash2 = new int[1005];int[] res = new int[Math.min(len1, len2)];for(int i = 0; i < len1; ++i){hash1[nums1[i]]++;}for(int i = 0; i < len2; ++i){hash2[nums2[i]]++;}int index = 0;for(int i = 0; i <= 1000; ++i){int num = Math.min(hash1[i], hash2[i]);while(num > 0){--num;res[index] = i;++index;}}return Arrays.copyOfRange(res, 0, index);}
}

3、知识点

(1) 哈希表解决。

十、有效的完全平方数

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean isPerfectSquare(int num) {if((int)Math.sqrt(num) * (int)Math.sqrt(num) != num){return false;}return true;}
}

3、知识点

(1) 利用**内置函数sqrt()**即可。

十一、猜数字大小

1、题目链接

点击跳转到题目位置

2、代码

/** * Forward declaration of guess API.* @param  num   your guess* @return 	     -1 if num is higher than the picked number*			      1 if num is lower than the picked number*               otherwise return 0* int guess(int num);*/public class Solution extends GuessGame {public int guessNumber(int n) {int left = 1;int right = n;while(left <= right){int mid = ((right - left) >> 1) + left;if(guess(mid) == -1){right = mid - 1;} else if(guess(mid) == 0){return mid;} else{left = mid + 1;}}return 0;}
}

3、知识点

(1) 二分搜索即可。

十二、赎金信

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean canConstruct(String ransomNote, String magazine) {int[] hash1 = new int[26];int[] hash2 = new int[26];for(int i = 0; i < ransomNote.length(); ++i){hash1[ransomNote.charAt(i) - 'a']++;}for(int i = 0; i < magazine.length(); ++i){hash2[magazine.charAt(i) - 'a']++;}for(int i = 0; i < 26; ++i){if(hash1[i] > hash2[i]){return false;}}return true;}
}

3、知识点

(1) 用数组来模拟哈希表

十三、字符串中的第一个唯一字符

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int firstUniqChar(String s) {int[] hash1 = new int[26];for(int i = 0; i < s.length(); ++i){hash1[s.charAt(i) - 'a']++;}for(int i = 0; i < s.length(); ++i){if(hash1[s.charAt(i) - 'a'] == 1){return i;}}return -1;}
}

3、知识点

(1) 哈希表来统计字符数量。

十四、找不同

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public char findTheDifference(String s, String t) {int[] hash1 = new int[26];int[] hash2 = new int[26];for(int i = 0; i < s.length(); ++i){hash1[s.charAt(i) - 'a']++;}for(int i = 0; i < t.length(); ++i){hash2[t.charAt(i) - 'a']++;}for(int i = 0; i < 26; ++i){if(hash1[i] != hash2[i]){return (char)(i + 'a'); }}return ' ';}
}

3、知识点

(1) 哈希表统计字符串。

十五、判断子序列

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean isSubsequence(String s, String t) {int i = 0;int j = 0;int m = s.length();int n = t.length();while(i < m && j < n){if(s.charAt(i) == t.charAt(j)){++i;++j;} else{++j;}}if(i != m){return false;}return true;}
}

3、知识点

(1) 双指针解决问题。

http://www.lryc.cn/news/185571.html

相关文章:

  • 腾讯云轻量和CVM有啥区别?怎么选择服务器配置?
  • 服务器or虚拟机安装SSH和虚拟机or服务器设置远程服务权限
  • Sentinel入门
  • Mac解压缩软件BetterZip免费版注册码下载
  • 在win10里顺利安装了apache2.4.41和php7.4.29以及mysql8.0.33
  • 云服务仿真:完全模拟 AWS 服务的本地体验 | 开源日报 No.45
  • css实现不规则图片文字环绕效果
  • Day-05 CentOS7.5 安装 Docker
  • 激光雷达:自动驾驶的眼睛
  • Scratch3.0下载
  • 多功能频率计周期/脉宽/占空比/频率测量verilog,视频/代码
  • img标签src动态绑定资源失败问题
  • 【自学笔记】网络安全——黑客技术
  • Rust 技术文档及详细使用命令
  • 建立HTTP代理IP池的技术和工具支持
  • 【机器学习】数据格式csv/txt/pkl
  • unity脚本_Input鼠标键盘 c#
  • 解析‘找不到msvcp140.dll无法继续执行代码’这个问题的解决方法
  • 练[FBCTF2019]RCEService
  • php实战案例记录(21)sprintf函数
  • 【数据结构-二叉树 九】【树的子结构】:树的子结构
  • 七张图解锁Mybatis整体脉络,让你轻松拿捏面试官
  • 力扣之删除有序数组中的重复项
  • pnpm、npm、yarn 包管理工具『优劣对比』及『环境迁移』
  • 【AntDesign】多环境配置和启动
  • Unix Network Programming Episode 78
  • 学习笔记(css穿透、vue-cookie、拦截器、vuex、导航守卫、token/Cookie、正则校验)
  • Day4:Linux系统编程1-60P
  • 【HuggingFace】Transformers(V4.34.0 稳定)支持的模型
  • oracle 导入数据泵常用语句