当前位置:网站首页>LeetCode热题 HOT1-50
LeetCode热题 HOT1-50
2022-06-22 05:21:00 【A snicker】
1、两数之和
经典两数之和,可以通过暴力枚举解决(时间复杂度O(n^2)),当然也可以通过哈希表,hashtable.containsKey(),来选择判断x和target-x
class Solution {
public int[] twoSum(int[] nums, int target) {
int m=nums.length;
int[] re=new int [2];
for(int i=0;i<m;i++){
for(int j=i+1;j<m;j++){
if(nums[i]+nums[j]==target){
re[0]=i;
re[1]=j;
}
}
}
return re;
}
}
2、两数相加
遍历两个链表,逐位计算它们的和
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode root = new ListNode(0);
ListNode curNode = root;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int l1val = l1 != null ? l1.val : 0;
int l2val = l2 != null ? l2.val : 0;
int sumval = l1val + l2val + carry;
carry = sumval / 10;
ListNode sumNode = new ListNode(sumval % 10);
curNode.next = sumNode;
curNode = sumNode;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
return root.next;
}
}
3、无重复字符的最长子串
通过滑动窗口来做,start是当前字符的前一个相同字符的位置加一,然后res存储最大的长度。
class Solution {
public int lengthOfLongestSubstring(String s) {
//字符上一次出现的位置
int[] last = new int[128];
for(int i=0;i<128;i++){
last[i] = -1;
}
int n = s.length();
int res = 0;
//滑动窗口开始的位置
int start = 0;
for(int i=0;i<n;i++){
int index = s.charAt(i);
start = Math.max(start,last[index] + 1);
res = Math.max(res,i-start+1);
last[index] = i;
}
return res;
}
}
5、最长回文子串
找到字符串s中的最长回文子串。
class Solution {
public String longestPalindrome(String s) {
int size = s.length();
boolean[][] dp = new boolean[size][size];
int max = 1;
int left = 0;
int right = 0;
if(size<2){
return s;
}
for(int i=0;i<size;i++){
dp[i][i] = true;
}
char[] str = s.toCharArray();
for(int L=2;L<=size;L++){
for(int i=0;i<size;i++){
int j = i+L-1;
if(j>=size){
break;
}
if(str[i]!=str[j]){
dp[i][j] = false;
}else{
if(j-i<3){
dp[i][j] = true;
}else{
dp[i][j] = dp[i+1][j-1];
}
}
if(dp[i][j] && j-i+1>max){
max = j-i+1;
left = i;
right = j;
}
}
}
return s.substring(left,right+1);
}
}
边栏推荐
- Graduation feedback! All contributors of Apache Doris community come to receive gifts!
- 新手开店货源怎么找,怎么找到优质货源?
- 在Vs Code中搭建JSP开发环境
- DTS migration script sqlserver
- In 2022, the third batch (principal) of Guangdong Provincial Safety Officer a certificate was found and analyzed, and the third batch (principal) of Guangdong Provincial Safety Officer a certificate w
- DTS迁移秘籍-Oracle篇
- 新建本地内容上传至码云分支
- mysql day02课堂笔记
- Service migration when deploying SuperMap iserver war package
- Kubernetes -- setting up an environment using minicube
猜你喜欢

Topic selection system of college graduation design based on SSM

Sort ten integers using selection

Contents of 2022 tea master (intermediate) examination and tea master (intermediate) examination

Analysis of T elevator repair in 2022 and simulation test questions of T elevator repair

Rétroaction sur la remise des diplômes! Tous les contributeurs de la communauté Apache Doris sont ici pour recevoir des cadeaux!

Storage mode and lifetime of C language variables

图扑软件2D与2.5D案例合集|智慧园区、数据中心、SMT 生产线...

Squoosh - Google's free open source image compression tool, reducing the image size by 90%! Support API development calls

7. Gateway global filter

【chrome】 谷歌小技巧 谷歌浏览器 自带 滚动截图(全屏截图)
随机推荐
北京密云区知识产权贯标的好处,补贴5-10万
Understanding of service container, service provider and facade of laravel
9 practical shell scripts, recommended collection!
Detailed explanation of OpenCV function usage 11~20, including code examples
Lua notes
VirtualBox 6.1.34 release
Service migration when deploying SuperMap iserver war package
What are the high availability designs of yarn?
postmanUtils工具类,模拟postman的get,post请求
Yarn application submission process
Squoosh - 谷歌出品的免费开源图片压缩工具,图片大小减少90%!支持 API 开发调用
Some notes on the use of C language strings
Kubernetes - bare metal cluster environment
rambbmitmq推送方
Force buckle 33 [search rotation sort array]
js正则表达式实现千分位符
Gateway uses global filter for token verification
The postmanutils tool class simulates the get and post requests of postman
Topic selection system of college graduation design based on SSM
JS regular expression to implement the thousands separator