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

算法通关村第5关【白银】| 哈希和栈经典算法题

1.两个栈实现队列 

思路:两个栈,一个输入栈,一个输出栈。

当需要输入的时候就往inStack中插入,需要输出就往outStack中输出,当输出栈是空就倒出输入栈的数据到输出栈中,这样就保证了后插入的数据从栈顶倒入了栈底,输出栈栈顶弹出的一定是原先输入栈栈底的数据,也就是先进来的,即先进先出。 

class MyQueue {Deque<Integer> inStack ;Deque<Integer> outStack;public MyQueue() {inStack = new LinkedList<>();outStack = new LinkedList<>();}public void push(int x) {inStack.push(x);}public int pop() {if(outStack.isEmpty()){while(!inStack.isEmpty()){outStack.push(inStack.pop());}return outStack.pop();}else{return outStack.pop();}}public int peek() {if(outStack.isEmpty()){while(!inStack.isEmpty()){outStack.push(inStack.pop());}return outStack.peek();}else{return outStack.peek();}}public boolean empty() {return inStack.isEmpty() && outStack.isEmpty();}
}

2.两个队列实现栈

思路:确保队列前端是后进数据,用两个队列实现后插入数据在前面效果

(从下方叠罗汉,每次插入,先放好一层,然后将原先所有数据抬起然后放到新的一层上面,这样达到后加入数据始终在前面)。

queue2必定为空,数据压入queue2,这样就确保队列前端是后进的数据

然后将queue1的数据灌入queue2,交换queue1和queue2,queue2仍然为空

需要弹出的时候就弹出queue1的数据就行,因为queue1始终保持后进数据在队列前端。

class MyStack {Deque<Integer> q1;Deque<Integer> q2;public MyStack() {q1 = new LinkedList<>();q2 = new LinkedList<>();}public void push(int x) {q2.offer(x);while(!q1.isEmpty()){q2.offer(q1.remove());}Deque<Integer> t = q1;q1 = q2;q2 = t;}public int pop() {return q1.remove();}public int top() {return q1.peek();}public boolean empty() {return q1.isEmpty();}
}

3.n数之和

两数之和

思路:

一、暴力两层循环 ,不可取

二、使用哈希表。每遍历过一个元素就记录下来,判断有没有包含target-nums[i]的值

class Solution {public int[] twoSum(int[] nums, int target) {Map<Integer,Integer> map = new HashMap<>();for(int i = 0;i<nums.length;i++){int tar = target - nums[i];if(map.containsKey(tar)){return new int[]{i,map.get(tar)};}else{map.put(nums[i],i);}}return null;}
}

三数之和

思路:

一、双层循环+两数之和。

排序之后,先确定nums[i]为三数之一,然后从剩下的数中找到两数之和为-nums[i]的数,三数之和就是0.

class Solution {public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();if (nums == null || nums.length < 3) {return result;}Arrays.sort(nums);for (int i = 0; i < nums.length - 2; i++) {if (i > 0 && nums[i] == nums[i - 1]) {continue; // Skip duplicates}int target = -nums[i];Map<Integer, Integer> map = new HashMap<>();for (int j = i + 1; j < nums.length; j++) {int complement = target - nums[j];if (map.containsKey(complement)) {result.add(Arrays.asList(nums[i], complement, nums[j]));while (j + 1 < nums.length && nums[j] == nums[j + 1]) {j++; // Skip duplicates}}map.put(nums[j], j);}}return result;}
}

二、排序+双指针

先从小到大排序,两层循环

外层循环用来确定一个三数之一,然后内层循环双指针确定另外两数

之和大于目标right--

之和小于目标left++

之和等于目标加入答案,同时为了避免重复答案,需要跳过相同的数字

外层循环需要跳过相同的数字避免重复答案,同时必须是nums[i]==nums[i-1]

例如:[-1,-1,0,1,2]

[-1,0,1],[-1,-1,2]都是答案,不能跳过第一个-1

if(i>0&&nums[i] == nums[i-1]){continue;}
class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> result = new ArrayList<>();if (nums == null || nums.length < 3) {return result;}Arrays.sort(nums);int i,l,r;for(i=0;i<nums.length;i++){if(nums[i]>0) break;if(i>0&&nums[i]==nums[i-1]){continue;}int tar = -nums[i];for(l = i+1,r = nums.length -1;l<r;){if(nums[l]+nums[r]>tar){r--;}else if(nums[l]+nums[r]<tar){l++;}else{List<Integer> list = new ArrayList<>();list.add(nums[i]);list.add(nums[l]);list.add(nums[r]);result.add(list);while (r > l && nums[r] == nums[r - 1]) r--;while (r > l && nums[l] == nums[l + 1]) l++;l++;r--;}}}return result;}
}

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

相关文章:

  • CrystalNet .Net VCL for Delphi Crack
  • 云计算在线实训系统建设方案
  • C++ 珠心算测验
  • prometheus+cadvisor监控docker容器
  • 13、Vue3 大事件管理系统
  • Redis三种特殊数据类型
  • python 模块BeautifulSoup 从HTML或XML文件中提取数据
  • VS Code插件汇总
  • QWidget
  • 【大数据】Linkis:打通上层应用与底层计算引擎的数据中间件
  • 权限提升-数据库提权-MSF-UDF提权
  • 基于XL32F003单片机的可控硅调光方案
  • 【ag-grid-vue】列定义(Updating Column Definitions)
  • mysql sql_mode数据验证检查
  • Prompt召唤 AI “生成”生产力,未来已来
  • 【0day】复现时空智友企业流程化管控系统SQL注入漏洞
  • python编程中fft的优缺点,以及如何使用cuda编程,cuda并行运算,信号处理(推荐)
  • 统计学补充概念-16-支持向量机 (SVM)
  • Python“牵手”天猫商品列表数据,关键词搜索天猫API接口数据,天猫API接口申请指南
  • SQL 错误 [22007]: ERROR: invalid input syntax for type date: ““
  • SpringBootWeb案例 Part 2
  • 4.14 HTTPS 中 TLS 和 TCP 能同时握手吗?
  • 游戏开发服务器选型的横向对比
  • 【android12-linux-5.1】【ST芯片】HAL移植后配置文件生成报错
  • 基于深度神经网络的分类--实现与方法说明
  • Java“牵手”天猫商品快递费用API接口数据,天猫API接口申请指南
  • 哲讯科技携手无锡华启动SCM定制化项目,共谋数字化转型之路
  • ModaHub魔搭社区:将图像数据添加至Milvus Cloud向量数据库中
  • svn下载
  • 为什么说es是近实时搜索