Java练习day3

一、验证回文串

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean judge(char ch){if((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')){return true;}return false;}public boolean judge1(char ch1, char ch2){if(ch1 == ch2){         return true;}return false;}public boolean isPalindrome(String s) {int left = 0;int n = s.length();int right = n - 1;s = s.toLowerCase();while(left <= right){while(left < n && (judge(s.charAt(left)) == false)){++left;}while(right >= 0 && (judge(s.charAt(right)) == false)){--right;}if(left > right){break;}if(judge1(s.charAt(left), s.charAt(right)) == false){return false;} ++left;--right;}return true;}
}

3.知识点

(1) Java如何将字符串从小写转化为大写,从大鞋转化为小写。
(2) 回文的基本概念。
(3) 利用双指针来解决问题。

二、环形链表

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {ListNode head1 = head;ListNode head2 = head;while(head1 != null && head1.next != null){head1 = head1.next;if(head1 == head2){return true;}head1 = head1.next;if(head1 == head2){return true;}head2 = head2.next;}return false;}
}

3.知识点

(1) 链表方面的题目
(2) 使用快慢指针来解决问题。

三、二叉树的前序遍历

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public void dfs(TreeNode root, List<Integer> res){if(root == null){return ;}res.add(root.val);dfs(root.left, res);dfs(root.right, res);}public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();dfs(root, res);return res;}
}

3.知识点

(1) 树的先序遍历,记住是根左右的顺序。
(2) 利用递归直接解决。

四、二叉树的后序遍历

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public void dfs(TreeNode root, List<Integer> res){if(root == null){return; }dfs(root.left, res);dfs(root.right, res);res.add(root.val);}public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList();dfs(root, res);return res;}
}

3.知识点

(1) 树的后序遍历,记住是左右根的顺序。
(2) 利用递归直接解决。

五、相交链表

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode head1 = headA;ListNode head2 = headB;int flag1 = 0;int flag2 = 0;while(head1 != null && head2 != null){if(head1 == head2){return head1;}if(head1.next == null && flag1 == 0){head1 = headB;flag1 = 1;} else{head1 = head1.next;} if(head2.next == null && flag2 == 0){head2 = headA;flag2 = 1;} else{head2 = head2.next;}}return null;}
}

3.知识点

(1) 使用双指针从两个链表头开始形式,然后到达终点后再到达另一条链表的头,再到另一条链表的结尾。
(2) 如果该过程两个指针相遇则代表有公共结点。

六、Excel表列名称

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public String convertToTitle(int columnNumber) {StringBuffer res = new StringBuffer();while(columnNumber != 0){columnNumber--;int num = columnNumber % 26;columnNumber /= 26;res.append((char)(num  + 'A'));}return res.reverse().toString();}
}

3.知识点

(1) 比较充满数学性的题目。

七、多数元素

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int majorityElement(int[] nums) {Arrays.sort(nums);return nums[nums.length / 2];}
}

3.知识点

(1) 排序,取中位数即可。

八、Excel 表列序号

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int titleToNumber(String columnTitle) {int res = 0;int n = columnTitle.length();for(int i = 0; i < n; ++i){res *= 26;res += (columnTitle.charAt(i) - 'A' + 1);}return res;}
}

3.知识点

(1) 和第七条一样,反向推理。

九、颠倒二进制位

1、题目链接

点击跳转到题目位置

2、代码

public class Solution {// you need treat n as an unsigned valuepublic int reverseBits(int n) {int res = 0;for(int i = 0; i < 32; ++i){if(((1 << (31 - i)) & n) != 0){res += (1 << i);}}return res;}
}

3、知识点

(1) 考察了关于位运算相关知识。

十、位1的个数

1、题目链接

点击跳转到题目位置

2、代码

public class Solution {// you need to treat n as an unsigned valuepublic int hammingWeight(int n) {int ret = 0;while(n != 0){n &= (n - 1);ret++;}return ret;}
}

3、知识点

(1) 关于汉明编码的一些优化。

十一、快乐数

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public int get_num(int n){int sum = 0;while(n != 0){sum += (n % 10) * (n % 10);n /= 10;}return sum;}public boolean isHappy(int n) {Set<Integer> hash1 = new HashSet<>();while(n != 1 && !hash1.contains(n)){hash1.add(n);n = get_num(n);}return n == 1;}
}

3、知识点

(1) 利用哈希表,每次模拟过程,如果模拟的过程中到达1或者到达过去已经到达过的数字就停止。

十二、移除链表元素

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode removeElements(ListNode head, int val) {if(head == null){return head;}ListNode dummyHead = new ListNode();dummyHead.next = head;head = dummyHead;while(head.next != null){if(head.next.val == val){head.next = head.next.next;} else{head = head.next;}}return dummyHead.next;}
}

3、知识点

(1) 链表中增删改查中删的操作。

十三、同构字符串

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean isIsomorphic(String s, String t) {Map<Character, Character> hash1 = new HashMap<Character, Character>();Map<Character, Character> hash2 = new HashMap<Character, Character>();for(int i = 0; i < s.length(); ++i){char ch1 = s.charAt(i);char ch2 = t.charAt(i);if(hash1.containsKey(ch1) && hash1.get(ch1) != ch2 || hash2.containsKey(ch2) && hash2.get(ch2) != ch1){return false;}hash1.put(ch1, ch2);hash2.put(ch2, ch1);}return true;}
}

3、知识点

(1) 实际上是利用两个哈希表来判断函数是否是双射关系。

十四、反转链表

1、题目链接

点击跳转到题目位置

2、代码

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {ListNode dummyHead = new ListNode();while(head != null){ListNode temp = head.next;head.next = dummyHead.next;dummyHead.next = head;head = temp;}return dummyHead.next;}
}

3、知识点

(1) 链表的头插法得到相反的序列。

十五、存在重复元素

1、题目链接

点击跳转到题目位置

2、代码

class Solution {public boolean containsDuplicate(int[] nums) {Set<Integer> hash1 = new HashSet<>();for(int i = 0; i < nums.length; ++i){if(hash1.contains(nums[i])){return true;}hash1.add(nums[i]);}return false;}
}

3、知识点

(1) 使用哈希表解决。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/148055.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

竞赛选题 机器视觉 opencv 深度学习 驾驶人脸疲劳检测系统 -python

文章目录 0 前言1 课题背景2 Dlib人脸识别2.1 简介2.2 Dlib优点2.3 相关代码2.4 人脸数据库2.5 人脸录入加识别效果 3 疲劳检测算法3.1 眼睛检测算法3.2 打哈欠检测算法3.3 点头检测算法 4 PyQt54.1 简介4.2相关界面代码 5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#x…

【Java 进阶篇】JDBC PreparedStatement 详解

在Java中&#xff0c;与关系型数据库进行交互是非常常见的任务之一。JDBC&#xff08;Java Database Connectivity&#xff09;是Java平台的一个标准API&#xff0c;用于连接和操作各种关系型数据库。其中&#xff0c;PreparedStatement 是 JDBC 中一个重要的接口&#xff0c;用…

RAID知识点总结

目录 RAID类型 RAID的数据组织及存取方式 RAID热备与重构 RAID逻辑卷 常见的RAID RAID0 RAID 1 RAID3 RAID 5 RAID 6 RAID组合 RAID 10 RAID 50 总结 RAID技术对比 RAID的应用场景 RAID2.0 使用RAID2.0的原因 RAID2.0的发展 RAID2.0技术&#xff1a;两层虚拟…

K8s架构简述

以部署一个nginx服务说明kubernetes系统各个组件调用关系&#xff1a; 一旦kubernetes环境启动之后&#xff0c;master和node都会将自身的信息存储到etcd数据库中 一个nginx服务的安装请求会首先被发送到master节点的apiServer组件 apiServer组件会调用scheduler组件来决定到底…

【强化学习】05 —— 基于无模型的强化学习(Prediction)

文章目录 简介蒙特卡洛算法时序差分方法Example1 MC和TD的对比偏差&#xff08;Bias&#xff09;/方差&#xff08;Variance&#xff09;的权衡Example2 Random WalkExample3 AB 反向传播(backup)Monte-Carlo BackupTemporal-Difference BackupDynamic Programming Backup Boot…

请求转发与请求作用域

创建input.jsp页面&#xff0c;通过表单输入学号、姓名后&#xff0c;单击登录按钮&#xff0c;控制转发到FirstServlet对其进行处理&#xff0c;然后通过请求对象的getRequestDispartcher()获得RequestDispartcher对象&#xff0c;将请求转发至SecondServlet&#xff0c;在Sec…

SpringBoot 可以同时处理多少请求

一、前言 首先&#xff0c;在Spring Boot应用中&#xff0c;我们可以使用 Tomcat、Jetty、Undertow 等嵌入式 Web 服务器作为应用程序的运行容器。这些服务器都支持并发请求处理的能力。另外&#xff0c;Spring Boot 还提供了一些配置参数&#xff0c;可以对 Web 服务器进行调…

北大硕士7年嵌入式学习经验分享

阶段 1 大一到大三这个阶段我与大多数学生相同&#xff1a; 学习本专业知识&#xff08;EE专业&#xff09;&#xff0c;学习嵌入式软件开发需要的计算机课程&#xff08;汇编原理&#xff0c;计算机组成原理&#xff0c;操作系统&#xff0c;C语言等&#xff09;&#xff0c…

常见web信息泄露

一、源码(备份文件)泄露 1、git泄露 Git是一个开源的分布式版本控制系统&#xff0c;在执行git init初始化目录的时候&#xff0c;会在当前目录下自动创建一个.git目录&#xff0c;用来记录代码的变更记录等。发布代码的时候&#xff0c;如果没有把.git这个目录删除&#xff…

SpringBoot 中使用JPA

最近忙里偷闲&#xff0c;想写一点关于JPA的东西&#xff0c;另外也加深下对JPA的理解&#xff0c;才有了此篇博文。 一、JPA JPA &#xff08;Java Persistence API&#xff09;Java持久化API&#xff0c;是一套Sun公司Java官方制定的ORM 规范&#xff08;sun公司并没有实现…

mfc140u.dll是什么文件?mfc140u放在哪个文件夹?详细修复教程

今天我想和大家分享一个非常常见的问题——mfc140u.dll丢失的困扰以及解决方法。 首先&#xff0c;让我们来了解一下什么是mfc140u.dll。这是一个非常重要的动态链接库文件&#xff0c;它是Microsoft Foundation Class Library的一个组件。许多软件和游戏都需要这个文件的支持才…

github搜索技巧

指定语言 language:java 比如我要找用java写的含有blog的内容 搜索项目名称包含关键词的内容 vue in:name 其他如项目描述跟项目文档&#xff0c;如下 组合使用 vue in:name,description,readme 根据Star 或者fork的数量来查找 总结 springboot vue stars:>1000 p…

(三)激光线扫描-中心线提取

光条纹中心提取算法是决定线结构光三维重建精度以及光条纹轮廓定位准确性的重要因素。 1. 光条的高斯分布 激光线条和打手电筒一样,中间最亮,越像周围延申,光强越弱,这个规则符合高斯分布,如下图。 2. 传统光条纹中心提取算法 传统的光条纹中心提取算法有 灰度重心法、…

漏洞扫描环境:win10系统用VMware Workstation打开虚拟机若干问题

win10系统用VMware Workstation打开虚拟机若干问题 一 .VMware打开虚拟机就蓝屏重启怎么解决&#xff1f;一. VMware打开虚拟机就蓝屏重启怎么解决&#xff1f;方法一&#xff1a;1、同时按下CTRLSHIFTESC打开任务管理器功能&#xff0c;之后依次点击-详细信息-性能后出现下列界…

苹果双系统和虚拟机哪个好用?

苹果不能直接使用windows系统中的软件&#xff0c;但windows系统较为全面&#xff0c;为了解决苹果电脑不能使用windows系统软件的问题&#xff0c;使用双系统和类虚拟机是非常不错的解决方案。那么&#xff0c;苹果双系统和虚拟机哪个好&#xff1f;这两种解决方案各有千秋。苹…

ubuntu18.04 OpenGL开发(显示YUV)

源码参考&#xff1a;https://download.csdn.net/download/weixin_55163060/88382816 安装opengl库 sudo apt install libglu1-mesa-dev freeglut3-dev mesa-common-dev 安装opengl工具包 sudo apt install mesa-utils 检查opengl版本信息&#xff08;桌面终端执行&#xff09…

ubuntu 18.04 LTS安装opencv 3.4.16 + opencv_contrib 3.4.16

1.下载 opencv 3.4.16 opencv_contrib 3.4.16 其中&#xff0c;opencv_contrib解压后的多个文件夹复制到opencv内、合并 2.安装 参考博文&#xff1a; https://zhuanlan.zhihu.com/p/650792342 https://zhuanlan.zhihu.com/p/87197806 其中 &#xff08;1&#xff09;cmake前…

【AI视野·今日Sound 声学论文速览 第十七期】Tue, 3 Oct 2023

AI视野今日CS.Sound 声学论文速览 Tue, 3 Oct 2023 Totally 15 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Sound Papers DiffAR: Denoising Diffusion Autoregressive Model for Raw Speech Waveform Generation Authors Roi Benita, Michael Elad, Joseph Kes…

大数据-玩转数据-Flink 海量数据实时去重

一、海量数据实时去重说明 借助redis的Set&#xff0c;需要频繁连接Redis&#xff0c;如果数据量过大, 对redis的内存也是一种压力&#xff1b;使用Flink的MapState&#xff0c;如果数据量过大, 状态后端最好选择 RocksDBStateBackend&#xff1b; 使用布隆过滤器&#xff0c;…

结构型设计模式——桥接模式

摘要 桥接模式(Bridge pattern): 使用桥接模式通过将实现和抽象放在两个不同的类层次中而使它们可以独立改变。 一、桥接模式的意图 将抽象与实现分离开来&#xff0c;使它们可以独立变化。 二、桥接模式的类图 Abstraction: 定义抽象类的接口Implementor: 定义实现类接口 …