大家好,我是方圆
无它,唯手熟尔

题号

  • 数组操作
    • 54. 螺旋矩阵
    • 73. 矩阵置零
    • 78. 子集
    • 384. 打乱数组
    • 581. 最短无序连续子数组
    • 945. 使数组唯一的最小增量
  • 栈相关
    • 20. 有效的括号
    • 32. 最长有效括号
    • 155. 最小栈
    • 232. 用栈实现队列
    • 316. 去除重复字母
  • 堆相关
    • 215. 数组中的第K个最大值
    • 347. 前K个高频元素

数组操作

54. 螺旋矩阵

class Solution {public List<Integer> spiralOrder(int[][] matrix) {List<Integer> res = new ArrayList<>();if(matrix == null || matrix.length == 0) return res;int left = 0,right = matrix[0].length - 1;int up = 0,down = matrix.length - 1;while(left <= right && up <= down) {for(int i = left; i <= right && up <= down; i++) {res.add(matrix[up][i]);}up++;for(int i = up; i <= down && left <= right; i++) {res.add(matrix[i][right]);}right--;for(int i = right; i >= left && up <= down; i--) {res.add(matrix[down][i]);}down--;for(int i = down; i >= up && left <= right; i--) {res.add(matrix[i][left]);}left++;}return res;}
}

73. 矩阵置零

class Solution {public void setZeroes(int[][] matrix) {int row = matrix.length, col = matrix[0].length;boolean rowFlag = false, colFlag = false;for(int i = 0; i < col; i++) {if(matrix[0][i] == 0) {rowFlag = true;break;}}for(int j = 0; j < row; j++) {if(matrix[j][0] == 0) {colFlag = true;break;}}for(int i = 1; i < row; i++) {for(int j = 1; j < col; j++) {if(matrix[i][j] == 0) {matrix[0][j] = 0;matrix[i][0] = 0;}}}for(int i = 1; i < row; i++) {for(int j = 1; j < col; j++) {if(matrix[0][j] == 0 || matrix[i][0] == 0) matrix[i][j] = 0;}}if(rowFlag) {for(int i = 0; i < col; i++) {matrix[0][i] = 0;}}if(colFlag) {for(int i = 0; i < row; i++) {matrix[i][0] = 0;}}}
}

78. 子集

class Solution {public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> res = new ArrayList<>();res.add(new ArrayList<>());for(int i = 0; i < nums.length; i++) {int size = res.size();for(int j = 0; j < size; j++) {ArrayList<Integer> temp = new ArrayList<>(res.get(j));temp.add(nums[i]);res.add(temp);}}return res;}
}

384. 打乱数组

class Solution {private int[] array;private int[] original;private Random random = new Random();public Solution(int[] nums) {array = nums;original = nums.clone();}/** Resets the array to its original configuration and return it. */public int[] reset() {return original;}/** Returns a random shuffling of the array. */public int[] shuffle() {List<Integer> copy = new ArrayList<>();for(int i = 0; i < array.length; i++) {copy.add(array[i]);}for(int i = 0; i < array.length; i++) {int index = random.nextInt(copy.size());array[i] = copy.remove(index);}return array;}
}

581. 最短无序连续子数组

class Solution {public int findUnsortedSubarray(int[] nums) {int len = nums.length;//这个right = -1 实在是有点儿东西int left = 0, right = -1;int max = nums[0], min = nums[len - 1];for(int i = 0; i < len; i++) {if(nums[i] >= max) max = nums[i];else right = i;if(nums[len - i - 1] <= min) min = nums[len - 1 - i];else left = len - i - 1;}return right - left + 1;}
}

945. 使数组唯一的最小增量

class Solution {public int minIncrementForUnique(int[] A) {Arrays.sort(A);int res = 0;for(int i = 0; i < A.length - 1; i++) {if(A[i + 1] <= A[i]) {int temp = A[i + 1];A[i + 1] = A[i] + 1;res += A[i + 1] - temp;}}return res;}
}

栈相关

20. 有效的括号

class Solution {public boolean isValid(String s) {if(s.length() == 0) return true;if(s.length() % 2 == 1) return false;Stack<Character> stack = new Stack<>();for(char c : s.toCharArray()){if(c == '(') stack.push(')');else if(c == '[') stack.push(']');else if(c == '{') stack.push('}');else if(stack.empty() || stack.pop() != c)return false;}return stack.empty();}
}

32. 最长有效括号

class Solution {public int longestValidParentheses(String s) {int res = 0;Stack<Integer> stack = new Stack<>();stack.push(-1);for(int i = 0; i < s.length(); i++) {if(s.charAt(i) == '(') {stack.push(i);}else {stack.pop();if(stack.empty()) {stack.push(i);}else {res = Math.max(res,i - stack.peek());}}}return res;}
}

155. 最小栈

class MinStack {private Stack<Integer> stack;private Stack<Integer> minStack;/** initialize your data structure here. */public MinStack() {stack = new Stack<>();minStack = new Stack<>();}public void push(int x) {stack.push(x);if(minStack.empty() || x <= minStack.peek())minStack.push(x);}public void pop() { int x = stack.pop();if(x == minStack.peek())minStack.pop();}public int top() {return stack.peek();}public int getMin() {return minStack.peek();}
}

232. 用栈实现队列

class MyQueue {Stack<Integer> stackIn;Stack<Integer> stackOut;/** Initialize your data structure here. */public MyQueue() {stackIn = new Stack<>();stackOut = new Stack<>();}/** Push element x to the back of queue. */public void push(int x) {stackIn.push(x);}/** Removes the element from in front of queue and returns that element. */public int pop() {if(stackOut.empty()){while(!stackIn.empty())stackOut.push(stackIn.pop());}return stackOut.pop();}/** Get the front element. */public int peek() {if(stackOut.empty()){while(!stackIn.empty())stackOut.push(stackIn.pop());}return stackOut.peek();}/** Returns whether the queue is empty. */public boolean empty() {return stackOut.empty() && stackIn.empty();}
}

316. 去除重复字母

class Solution {public String removeDuplicateLetters(String s) {Stack<Character> stack = new Stack<>();char[] chars = s.toCharArray();for(int i = 0; i < chars.length; i++) {if(stack.contains(chars[i])) continue;while(!stack.empty() && stack.peek() > chars[i] && s.indexOf(stack.peek(),i) != -1)stack.pop();stack.push(chars[i]);}StringBuilder res = new StringBuilder();while(!stack.empty()) res.append(stack.pop());return res.reverse().toString();}
}

堆相关

215. 数组中的第K个最大值

class Solution {public int findKthLargest(int[] nums, int k) {PriorityQueue<Integer> heap = new PriorityQueue<>();for(int i = 0; i < nums.length; i++) {heap.offer(nums[i]);if(heap.size() > k) heap.poll();}return heap.poll();}
}

347. 前K个高频元素

class Solution {public int[] topKFrequent(int[] nums, int k) {HashMap<Integer,Integer> map = new HashMap<>();for(int n : nums) map.put(n,map.getOrDefault(n,0) + 1);PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> map.get(a) - map.get(b));for(int i : map.keySet()) {heap.offer(i);if(heap.size() > k) heap.poll();}int[] res = new int[k];for(int i = 0; i < k; i++) res[i] = heap.poll();return res;}
}

加油儿!

LeetCode,无它,唯手熟尔(三)相关推荐

  1. 樊登读书搞定读后感_读书笔记/读后感《读懂一本书,樊登读书法》——无他唯手熟尔...

    当我们在生活及工作中遇到问题的时候,通常会请教同事或者上网搜索解决方法,但是我们忽略了另外一个解决问题的途径,那就是通过阅读书籍积累知识.为什么大家并不习惯于阅读书籍呢,那是因为大家认为眼前遇到问题最 ...

  2. 237. 删除链表中的节点【我亦无他唯手熟尔】

    237. 删除链表中的节点 请编写一个函数,用于 删除单链表中某个特定节点 .在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 . 题目数据保证需要删除的节点 不 ...

  3. 786. 第 K 个最小的素数分数【我亦无他唯手熟尔】

    786. 第 K 个最小的素数分数 786. 第 K 个最小的素数分数 题解 786. 第 K 个最小的素数分数 难度 困难 给你一个按递增顺序排序的数组 arr 和一个整数 k .数组 arr 由 ...

  4. 136. 只出现一次的数字【我亦无他唯手熟尔】

    136. 只出现一次的数字 136. 只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素.说明:你的算法应该具有线性时间复杂度. 你 ...

  5. 1929. 数组串联【我亦无他唯手熟尔】

    1929. 数组串联 题目 题解 题目 给你一个长度为 n 的整数数组 nums .请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < ...

  6. 438. 找到字符串中所有字母异位词【我亦无他唯手熟尔】

    438. 找到字符串中所有字母异位词 438. 找到字符串中所有字母异位词 题解 官方 438. 找到字符串中所有字母异位词 难度 中等 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 ...

  7. 卖油翁 欧阳修 无他,但手熟尔

    陈康肃公尧咨善射,当世无双,公亦以此自矜.尝射于家圃,有卖油翁释担而立,睨之,久而不去.见其发矢十中八九,但微颔之. 康肃问曰:"汝亦知射乎?吾射不亦精乎?"翁曰:"无他 ...

  8. 算法唯手熟尔(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/3/5 PAT A1119_C 2020/3/6 PAT A1123_C PAT A1115_C PAT A1114_C leetcode 206. 反转链表_C leetcode ...

  9. 算法唯手熟尔4月(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/4/1 leetcode 914. 卡牌分组_Go leetcode 1111. 有效括号的嵌套深度_Go 2020/4/2 leetcode 289. 生命游戏_Go 2020/ ...

  10. 编程思想:我亦无他,唯手熟尔

    目录 一.编程思想三大定理 二.编程思想四大特性 三.做到高内聚,低耦合 (一)内聚 (二)耦合 一.编程思想三大定理 (1)思路分析,化繁为简:将需求拆成多个步骤实现,先完成基本功能和大致框架,在往 ...

最新文章

  1. 技术图文:如何在leetcode上进行算法刻意练习?
  2. python中使用socket编程实现图片或者其他文件的传输
  3. 《强化学习周刊》第10期:强化学习应用之计算机视觉
  4. Java获取运行环境信息
  5. webstorm代码行数统计_【Rust每周一库】Tokei 统计代码行数等信息的实用工具
  6. shell设计精髓_交互设计精髓
  7. 如何轻松地将可访问LAN的Pod部署到Kubernetes集群上
  8. 资深架构师十几年的架构干货经验总结分享!
  9. 【转】一致性hash算法与server列表维护
  10. 请实现一个函数,将一个字符串中的每个空格替换成...
  11. win10用账户登录计算机,图文详解让你的win10系统实现微软账户自动登录-系统操作与应用 -亦是美网络...
  12. aspnetpager 详解
  13. 视频会议的进化方向是什么?
  14. SQL AND OR 运算符的用法
  15. The PostScript backend does not support transparency; partially transparent artists will be rendered
  16. 墨尔本python培训班_墨的解释|墨的意思|汉典“墨”字的基本解释
  17. 【kali技巧】查看宿主机windows的ip地址
  18. python:初识自动化测试 playwright 库
  19. 腾讯派息式减持京东,“伯克希尔”的投资策略变的逻辑是?
  20. 易语言让我东山再起 邓学彬(优秀文章)

热门文章

  1. 【在线教育直播】直播很卡怎么办?
  2. php zip压缩包下载
  3. 推荐系统序列化建模总结
  4. 22牛客多校5 - Don‘t Starve(DP,依靠边更新端点)
  5. osm数据导入mysql_导入OSM数据至PostgreSQL数据库
  6. 一WAN多拨(一号多拨)实验
  7. jQuery读书笔记(一)
  8. 特技替身拜拜,迪士尼机器超人要上天了!
  9. ttf,eot,woff,svg,字体格式介绍及使用方法
  10. 数据治理(十一):数据安全管理Ranger初步认识