当前位置:网站首页>LeetCode136+128+152+148
LeetCode136+128+152+148
2022-07-04 03:53:00 【想进阿里的小菜鸡】
思路
因为是用O(n)复杂度的算法,所以不能用自带的排序算法来解决这个问题。所以就尝试用空间换时间来解决问题。就用了哈希表法。
首先将所有元素存入带哈希表中,然后遍历每个元素,去寻找哈希表中是否有该元素相邻的元素,然后找到上限和下限,取二者的差并减一,因为每次都是先减或者先加再去找哈希表的元素。
代码
class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null || nums.length == 0) return 0;
HashSet<Integer> set = new HashSet<>();
int res = 0;
for(int num:nums){
set.add(num);
}
for(int i = 0;i<nums.length;i++){
int down = nums[i]-1;
while(set.contains(down)){
set.remove(down);
down--;
}
int up = nums[i] + 1;
while(set.contains(up)){
set.remove(up);
up++;
}
res = Math.max(res,up-down-1);
}
return res;
}
}
思路
因为是只有出现两次和出现一次的数,所以使用了hashSet来求解。
还可以使用位运算来求解。直接用异或来计算就可以了。
代码
HashSet:
class Solution {
public int singleNumber(int[] nums) {
if(nums.length == 1) return nums[0];
HashSet<Integer> hashset = new HashSet<>();
for(int i = 0;i<nums.length;i++){
if(hashset.contains(nums[i])){
hashset.remove(nums[i]);
}else{
hashset.add(nums[i]);
}
}
return hashset.iterator().next();
}
}
位运算:
int res = 0;
for(int i =0;i<nums.length;i++){
res^=nums[i];
}
return res;
思路
就是将归并排序运用到链表上就可以了。
代码
/**
* 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 sortList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode midNode = mid(head);
ListNode node2 = midNode.next;
midNode.next = null;
return merge(sortList(head),sortList(node2));
}
public ListNode mid(ListNode head){
ListNode slow = head;
ListNode fast = head.next;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode merge(ListNode a,ListNode b){
ListNode dumy = new ListNode(0);
ListNode temp =dumy;
while(a != null && b != null){
if(a.val <= b.val){
temp.next = a;
a = a.next;
}else{
temp.next = b;
b = b.next;
}
temp = temp.next;
}
if(a == null)
temp.next = b;
if(b == null)
temp.next = a;
return dumy.next;
}
}
思路
记得每次遍历一个数组元素的时候,如果是负数则乘以之前最小的可能是最大,或者乘以最大的是最大的,或者当前元素就是最大的,不用使用dp数组来求解这道题。
代码
class Solution {
public int maxProduct(int[] nums) {
int max = nums[0];
int min = nums[0];
int res = nums[0];
for(int i = 1;i<nums.length;i++){
int temp = max;
max = Math.max(Math.max(max*nums[i],min*nums[i]),nums[i]);
min = Math.min(Math.min(temp*nums[i],min*nums[i]),nums[i]);
res = Math.max(res,max);
}
return res;
}
}
边栏推荐
- Touch and take you to implement an EventEmitter
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
- “找工作不要太在意工资”,这是我听过最大的谎言
- 【安全攻防】序列化与反序列,你了解多少?
- [microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
- [microservice openfeign] @feignclient detailed explanation
- Eig launched Grupo Cerro, a renewable energy platform in Chile
- Wechat official account infinite callback authorization system source code
- Kivy教程之 更改背景颜色(教程含源码)
猜你喜欢
深入解析结构化异常处理(SEH) - by Matt Pietrek
Senior developers tell you, how to write excellent code?
毕业设计项目
分布式CAP理论
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
多位科技公司创始人向Entrepreneur First提供高达1.58亿美元的C轮融资,协助其投资下一代全球创新者
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Virtual commodity account trading platform source code_ Support personal QR code collection
GUI 应用:socket 网络聊天室
Architecture training graduation design + summary
随机推荐
苹果CMS仿西瓜视频大气响应式视频模板源码
Graduation project
Kivy教程之 07 组件和属性绑定实现按钮button点击修改label组件(教程含源码)
RPC Technology
【愚公系列】2022年7月 Go教学课程 001-Go语言前提简介
Select function variable column name in dplyr of R language
MIN_RTO 对话
Unity Resource path
B. All Distinct
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Touch and take you to implement an EventEmitter
[Yugong series] go teaching course 001 in July 2022 - Introduction to go language premise
Redis:有序集合zset类型数据操作命令
Precautions for accompanying driving these 23 points should be paid attention to!
Architecture practice camp - graduation project of module 9 of phase 6
博朗与Virgil Abloh于2021年为纪念博朗品牌100周年而联合打造的“功能性艺术”将在博物馆展出Abloh作品期间首次亮相
JS realizes the effect of text scrolling marquee
最长递增子序列问题(你真的会了吗)
浅谈JVM的那些事
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)