LeetCode,无它,唯手熟尔(一)
大家好,我是
方圆
无它,唯手熟尔
目录
- Hash相关
- 1. 两数之和
- 387. 字符串中的第一个唯一字符
- 链表操作
- 2. 两数相加
- 19. 删除链表的倒数第N个节点
- 61. 旋转链表
- 138. 复制带随机指针的链表
- 206. 翻转链表
- 双指针遍历(滑动窗口)
- 3. 无重复字符的最长子串
- 11. 盛最多水的容器
- 15. 三数之和
- 16. 最接近的三数之和
- 26. 删除排序数组中的重复项
- 42. 接雨水
- 121. 买卖股票的最佳时机
- 209. 长度最小的子数组
Hash相关
1. 两数之和
class Solution {public int[] twoSum(int[] nums, int target) {int[] res = new int[2];HashMap<Integer,Integer> map = new HashMap<>();for(int i = 0;i < nums.length;i++) {int temp = target - nums[i];if(map.containsKey(temp)) {res[0] = map.get(temp);res[1] = i;return res;}map.put(nums[i],i);}return res;}
}
387. 字符串中的第一个唯一字符
class Solution {public int firstUniqChar(String s) {char[] chars = s.toCharArray();HashMap<Character,Boolean> map = new HashMap<>();for(int i = 0;i < chars.length;i++) {map.put(chars[i],!map.containsKey(chars[i]));}for(int j = 0;j < chars.length;j++) {if(map.get(chars[j])) return j;}return -1;}
}
链表操作
2. 两数相加
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode pre = new ListNode(0);ListNode cur = pre;int carry = 0;while(l1 != null || l2 != null) {int x = l1 == null ? 0 : l1.val;int y = l2 == null ? 0 : l2.val;int sum = x + y + carry;carry = sum / 10;sum %= 10;cur.next = new ListNode(sum);cur = cur.next;if(l1 != null) l1 = l1.next;if(l2 != null) l2 = l2.next;}if(carry == 1) cur.next = new ListNode(carry);return pre.next;}
}
19. 删除链表的倒数第N个节点
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode pre = new ListNode(0);pre.next = head;ListNode cur = pre;ListNode temp = pre;while(n != 0) {temp = temp.next;n--;}while(temp.next != null) {temp = temp.next;cur = cur.next;}cur.next = cur.next.next;return pre.next;}
}
61. 旋转链表
//计算step这里总出错
class Solution {public ListNode rotateRight(ListNode head, int k) {if(head == null || head.next == null) return head;ListNode oldTail = head;int length = 1;while(oldTail.next != null) {length++;oldTail = oldTail.next;}oldTail.next = head;ListNode newTail = head;int step = length - k % length - 1;for(int i = 0;i < step;i++) {newTail = newTail.next;}ListNode res = newTail.next;newTail.next = null;return res;}
}
138. 复制带随机指针的链表
class Solution {HashMap<Node,Node> map = new HashMap<>();public Node copyRandomList(Node head) {if(head == null) return null;if(map.containsKey(head)) return map.get(head);Node node = new Node(head.val);map.put(head,node);node.next = copyRandomList(head.next);node.random = copyRandomList(head.random);return node;}
}
206. 翻转链表
class Solution {public ListNode reverseList(ListNode head) {ListNode pre = null;ListNode cur = head;ListNode temp = null;while(cur != null) {temp = cur.next;cur.next = pre;pre = cur;cur = temp;}return pre;}
}
双指针遍历(滑动窗口)
3. 无重复字符的最长子串
class Solution {public int lengthOfLongestSubstring(String s) {int res = 0;int left = 0,right = 0;char[] strs = s.toCharArray();int len = strs.length;HashMap<Character,Integer> map = new HashMap<>();if(len == 0) return res;while(right < len) {if(map.containsKey(strs[right]))left = Math.max(left,map.get(strs[right]) + 1);map.put(strs[right],right);res = Math.max(res,right - left + 1);right++;}return res;}
}
11. 盛最多水的容器
class Solution {public int maxArea(int[] height) {int res = 0;int left = 0,right = height.length - 1;while(left < right) {res = Math.max(res,(right - left) * Math.min(height[left],height[right]));if(height[left] < height[right]) left++;else right--;}return res;}
}
15. 三数之和
class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> res = new ArrayList<>();if(nums == null || nums.length < 3) return res;Arrays.sort(nums);for(int i = 0;i < nums.length;i++) {if(nums[i] > 0) break;if(i > 0 && nums[i] == nums[i - 1]) continue;int left = i + 1;int right = nums.length - 1;while(left < right) {int sum = nums[i] + nums[left] + nums[right];if(sum == 0) {res.add(Arrays.asList(nums[i],nums[left],nums[right]));while(left < right && nums[left] == nums[left + 1]) left++;while(left < right && nums[right] == nums[right - 1]) right--;left++;right--;} else if(sum > 0) right--;else left++;}}return res;}
}
16. 最接近的三数之和
class Solution {public int threeSumClosest(int[] nums, int target) {Arrays.sort(nums);int res = nums[0] + nums[1] + nums[2];for(int i = 0;i < nums.length;i++) {int left = i + 1;int right = nums.length - 1;while(left < right) {int temp = nums[i] + nums[left] + nums[right];if(Math.abs(temp - target) < Math.abs(res - target)) {res = temp;}if(temp > target) right--;else if(temp < target) left++;else return res;}}return res;}
}
26. 删除排序数组中的重复项
class Solution {public int removeDuplicates(int[] nums) {if(nums == null || nums.length == 0) return 0;int left = 0,right = 1;while(right < nums.length) {if(nums[left] != nums[right]) {nums[left + 1] = nums[right];left++;}right++;}return left + 1;}
}
42. 接雨水
class Solution {public int trap(int[] height) {int length = height.length;int[] left = new int[length];int[] right = new int[length];int leftMax = 0,rightMax = 0;int res = 0;for(int i = 0;i < length;i++) {if(height[i] > leftMax) leftMax = height[i];left[i] = leftMax;if(height[length - 1 - i] > rightMax) rightMax = height[length - 1 - i];right[length - 1 - i] = rightMax;}for(int j = 0;j < length;j++) {if(height[j] < left[j] && height[j] < right[j])res += Math.min(left[j],right[j]) - height[j];} return res;}
}
121. 买卖股票的最佳时机
class Solution {public int maxProfit(int[] prices) {int minPrice = Integer.MAX_VALUE;int res = 0;for(int i = 0;i < prices.length;i++) {if(prices[i] < minPrice) minPrice = prices[i];else res = Math.max(res,prices[i] - minPrice);}return res;}
}
209. 长度最小的子数组
class Solution {public int minSubArrayLen(int s, int[] nums) {if(nums == null || nums.length == 0) return 0;int left = 0,right = 0,sum = 0;int res = Integer.MAX_VALUE;while(right < nums.length) {sum += nums[right];while(sum >= s) {res = Math.min(res,right - left + 1);sum -= nums[left];left++;}right++;}return res == Integer.MAX_VALUE ? 0 : res;}
}
加油儿!
LeetCode,无它,唯手熟尔(一)相关推荐
- 樊登读书搞定读后感_读书笔记/读后感《读懂一本书,樊登读书法》——无他唯手熟尔...
当我们在生活及工作中遇到问题的时候,通常会请教同事或者上网搜索解决方法,但是我们忽略了另外一个解决问题的途径,那就是通过阅读书籍积累知识.为什么大家并不习惯于阅读书籍呢,那是因为大家认为眼前遇到问题最 ...
- 237. 删除链表中的节点【我亦无他唯手熟尔】
237. 删除链表中的节点 请编写一个函数,用于 删除单链表中某个特定节点 .在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 . 题目数据保证需要删除的节点 不 ...
- 1929. 数组串联【我亦无他唯手熟尔】
1929. 数组串联 题目 题解 题目 给你一个长度为 n 的整数数组 nums .请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < ...
- 438. 找到字符串中所有字母异位词【我亦无他唯手熟尔】
438. 找到字符串中所有字母异位词 438. 找到字符串中所有字母异位词 题解 官方 438. 找到字符串中所有字母异位词 难度 中等 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 ...
- 786. 第 K 个最小的素数分数【我亦无他唯手熟尔】
786. 第 K 个最小的素数分数 786. 第 K 个最小的素数分数 题解 786. 第 K 个最小的素数分数 难度 困难 给你一个按递增顺序排序的数组 arr 和一个整数 k .数组 arr 由 ...
- 136. 只出现一次的数字【我亦无他唯手熟尔】
136. 只出现一次的数字 136. 只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素.说明:你的算法应该具有线性时间复杂度. 你 ...
- 卖油翁 欧阳修 无他,但手熟尔
陈康肃公尧咨善射,当世无双,公亦以此自矜.尝射于家圃,有卖油翁释担而立,睨之,久而不去.见其发矢十中八九,但微颔之. 康肃问曰:"汝亦知射乎?吾射不亦精乎?"翁曰:"无他 ...
- 算法唯手熟尔(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 ...
- 算法唯手熟尔4月(PAT剩余清单 or leetcode)---希望可以日更
文章目录 2020/4/1 leetcode 914. 卡牌分组_Go leetcode 1111. 有效括号的嵌套深度_Go 2020/4/2 leetcode 289. 生命游戏_Go 2020/ ...
- 编程思想:我亦无他,唯手熟尔
目录 一.编程思想三大定理 二.编程思想四大特性 三.做到高内聚,低耦合 (一)内聚 (二)耦合 一.编程思想三大定理 (1)思路分析,化繁为简:将需求拆成多个步骤实现,先完成基本功能和大致框架,在往 ...
最新文章
- 详解python实现FP-TREE进行关联规则挖掘(带有FP树显示功能)附源代码下载(3)
- 专栏 | 基于 Jupyter 的特征工程手册:特征选择(四)
- 你是我一首唱不完的歌
- MDOP 2011 R2 DaRT 7.0 创建包含诊断和恢复的图形化PE
- XOR 异或加密简介
- Service(一)
- Ubuntu赋予普通用户特定目录权限
- python等待时间1009python等待时间_Python之学习Selenium(四):固定的休眠时间、隐式等待和显示等待...
- 台式计算机为什么数字输入不了,计算机键盘无法输入数字,为什么?
- c++ 向量化_一种新的FIR滤波器系数量化方法
- python小数点后任意位_Python计算开方、立方、圆周率,精确到小数点后任意位的方法...
- 公网IP/内网IP:
- 用计算机归零,电脑计算器里的清零键是哪个?
- 微信聊天机器人搭建方式分享
- C语言深度学习之嵌套循环例题(金字塔模型)
- 两种图像拼接(无重叠相邻图有重叠相邻图)以及matlab实现(边线查找法)
- 鸿蒙系统能玩魔兽世界吗,《魔兽世界》7.0配置公布:仍不放弃XP
- Qt第四十一章:异型窗口
- 二类形容词(形容动词)地用法
- 闲置iPhone这么用
热门文章
- join and list删除 and set集合 and 深浅拷贝
- 故障恢复控制台应用指南
- 华三交换机配置ntp server
- The Apache Tomcat Native library which allows optimal performance in production environments wasn
- Android Botton 事件
- 输入的数字或者英文字符间距都特别大,怎么解决
- PhraseQuery slop
- 计算机软硬件的开发及应用,管理和控制计算机系统软硬件资源的软件是 计算机软硬件开发...
- STM32 单片机字符串生成二维码显示
- 金蝶云星空(Kingdee)的webapi 使用:修改生产领料单的实收数量,并且影响上下游单据(生产订单)