当前位置:网站首页>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;
}
}边栏推荐
- Pytest basic self-study series (I)
- 一位毕业生的自我分享
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- 西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
- Kivy教程之 更改背景颜色(教程含源码)
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- FT2000+下LPC中断绑核使用说明
- 1. Mx6u-alpha development board (LED drive experiment in C language version)
- Talking about what a high-quality little red book copy needs to have
- Wobo Union ended its strategic evaluation and decided to retain Bozi's business with excellent performance
猜你喜欢

北漂程序员,月薪20K,一年攒15W,正常吗?

浅谈JVM的那些事

The five pictures tell you: why is there such a big gap between people in the workplace?

leetcode:1314. 矩阵区域和【二维前缀和模板】

R语言dplyr中的Select函数变量列名

Leetcode skimming: binary tree 04 (sequence traversal of binary tree)

Leetcode skimming: binary tree 09 (minimum depth of binary tree)

Boutique website navigation theme whole station source code WordPress template adaptive mobile terminal

RPC Technology

Keysight n9320b RF spectrum analyzer solves tire pressure monitoring scheme
随机推荐
什么是上下文?
Select function variable column name in dplyr of R language
B. All Distinct
Unity资源路径
B. All Distinct
RHCSA 08 - automount配置
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
Leetcode brush questions: binary tree 05 (flip binary tree)
MIN_ RTO dialog
"Don't care too much about salary when looking for a job", this is the biggest lie I've ever heard
Modstartblog modern personal blog system v5.2.0 source code download
浅谈JVM的那些事
Operation of ES6
C language bidirectional linked list first edition
The five pictures tell you: why is there such a big gap between people in the workplace?
Wechat official account infinite callback authorization system source code
“找工作不要太在意工资”,这是我听过最大的谎言
The "functional art" jointly created by Bolang and Virgil abloh in 2021 to commemorate the 100th anniversary of Bolang brand will debut during the exhibition of abloh's works in the museum
RPC技术
I.MX6U-ALPHA开发板(C语言版本LED驱动实验)