最近面美团,结果栽到了算法上,好不甘心,需要把各个大厂近半年的全部刷N遍,保证在O(1)的时间内作出来,希望接下来的一两周不再害怕常规的算法题
文章目录
-
- [[2980. 检查按位或是否存在尾随零](https://leetcode.cn/problems/check-if-bitwise-or-has-trailing-zeros/)](#2980. 检查按位或是否存在尾随零)
- [[35. 搜索插入位置](https://leetcode.cn/problems/search-insert-position/)](#35. 搜索插入位置)
- [[48. 旋转图像](https://leetcode.cn/problems/rotate-image/)](#48. 旋转图像)
- [[42. 接雨水](https://leetcode.cn/problems/trapping-rain-water/)](#42. 接雨水)
- [[70. 爬楼梯](https://leetcode.cn/problems/climbing-stairs/)](#70. 爬楼梯)
- [[82. 删除排序链表中的重复元素 II](https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/)](#82. 删除排序链表中的重复元素 II)
- [[206. 反转链表](https://leetcode.cn/problems/reverse-linked-list/)](#206. 反转链表)
- [[958. 二叉树的完全性检验](https://leetcode.cn/problems/check-completeness-of-a-binary-tree/)](#958. 二叉树的完全性检验)
- [[LCR 152. 验证二叉搜索树的后序遍历序列](https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/)](#LCR 152. 验证二叉搜索树的后序遍历序列)
- [[LCR 016. 无重复字符的最长子串](https://leetcode.cn/problems/wtcaE1/)](#LCR 016. 无重复字符的最长子串)
- [[LCR 026. 重排链表](https://leetcode.cn/problems/LGjMqU/)](#LCR 026. 重排链表)
- [[LCR 024. 反转链表](https://leetcode.cn/problems/UHnkqh/)](#LCR 024. 反转链表)
- [3. 无重复字符的最长子串](#3. 无重复字符的最长子串)
- [[5. 最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/)](#5. 最长回文子串)
2980. 检查按位或是否存在尾随零
java
class Solution {
public boolean hasTrailingZeros(int[] nums) {
Set<String> binarySet = new HashSet<>();
for(int num: nums){
String curr = Integer.toBinaryString(num);
for(String binary: binarySet){
if(Integer.toBinaryString(num | Integer.parseInt(binary,2)).endsWith("0")){
return true;
}
}
binarySet.add(curr);
}
return false;
}
}
35. 搜索插入位置
java
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length -1;
while(left <= right){
int mid = left+ (right-left)/2;
if(nums[mid] == target){
return mid;
}else if(nums[mid] > target){
right = mid -1;
}else {
left = mid +1;
}
}
return left;
}
}
48. 旋转图像
java
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// 对角线
for(int i=0; i<n; i++){
for(int j=i;j <n; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// 对每一行反转
for(int i=0; i<n; i++){
for(int j=0;j<n/2;j++){
int temp = matrix[i][j];
// 0,1,2,3
matrix[i][j] = matrix[i][n-j-1];
matrix[i][n-j-1] = temp;
}
}
}
}
42. 接雨水
java
class Solution {
public int trap(int[] height) {
//双指针
int left = 0;
int right = height.length-1;
int leftMax = 0;
int rightMax = 0;
int res = 0;
while(left < right){
leftMax = Math.max(height[left],leftMax);
rightMax = Math.max(height[right],rightMax);
// 走下坡路才需要
if(height[left] < height[right]){
res += leftMax-height[left];
left++;
}else{
res += rightMax-height[right];
right--;
}
}
return res;
}
}
70. 爬楼梯
java
class Solution {
public int climbStairs(int n) {
// check
if(n <= 1){
return 1;
}
int[] dp = new int[n+1];
dp[1] = 1;
dp[0] = 1;
for(int i=2;i<=n;i++){
dp[i] = dp[i-1]+dp[i-2];
}
return dp[n];
}
}
82. 删除排序链表中的重复元素 II
java
/**
* 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 deleteDuplicates(ListNode head) {
// check
if(head == null){
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy;
ListNode curr = head;
while(curr != null){
while(curr.next != null && curr.val == curr.next.val){
curr = curr.next;
}
if(prev.next == curr){
prev = prev.next;
}else{
// 重复
prev.next = curr.next;
}
curr = curr.next;
}
return dummy.next;
}
}
206. 反转链表
java
/**
* 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 reverseList(ListNode head) {
// check
if(head == null || head.next == null){
return head;
}
ListNode prev = null;
ListNode curr = head;
while(curr != null){
// 1->2
ListNode tempNext = curr.next;
curr.next = prev;
prev = curr;
curr = tempNext;
}
return prev;
}
}
958. 二叉树的完全性检验
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isCompleteTree(TreeNode root) {
// check
if(root == null){
return true;
}
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
boolean isEnd = false;
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(isEnd && node!= null){
return false;
}
if(node == null){
isEnd = true;
continue;
}
queue.offer(node.left);
queue.offer(node.right);
}
return true;
}
}
LCR 152. 验证二叉搜索树的后序遍历序列
java
class Solution {
public boolean verifyTreeOrder(int[] postorder) {
// 左右中
return recur(postorder,0,postorder.length-1);
}
public boolean recur(int[] postorder, int start, int end){
// terminal
if(start >= end){
return true;
}
int root = postorder[end];
int mid = start;
// left right的分界点
while(mid < end && postorder[mid] < root){
mid++;
}
// 检查 right 是否都 > root
for(int i=mid; i<end;i++){
if(postorder[i] < root){
return false;
}
}
return recur(postorder,start,mid-1) && recur(postorder,mid,end-1);
}
}
LCR 016. 无重复字符的最长子串
java
class Solution {
public int lengthOfLongestSubstring(String s) {
//滑动窗口
// hash 字符:索引
HashMap<Character,Integer> map = new HashMap<>();
int maxLength = 0;
int left = 0;
for(int right = 0;right<s.length();right++){
char currentChar = s.charAt(right);
//如果已存在,更新left
if(map.containsKey(currentChar)){
left = Math.max(map.get(currentChar)+1,left);
}
// 更新hash表和最大长度
map.put(currentChar,right);
maxLength = Math.max(maxLength,right-left+1);
}
return maxLength;
}
}
LCR 026. 重排链表
java
/**
* 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 void reorderList(ListNode head) {
if(head == null || head.next == null){
return;
}
Stack<ListNode> stack = new Stack<>();
ListNode curr = head;
while(curr != null){
stack.push(curr);
curr = curr.next;
}
int n = stack.size();
curr = head;
for(int i=0;i<n/2;i++){
ListNode temp = curr.next;
ListNode top = stack.pop();
curr.next = top;
top.next = temp;
curr = curr.next.next
}
curr.next = null;
}
}
LCR 024. 反转链表
java
/**
* 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 reverseList(ListNode head) {
// check
if(head == null || head.next == null){
return head;
}
ListNode prev = null;
ListNode curr = head;
while(curr != null){
// 1->2->3
ListNode tempNext = curr.next;
curr.next = prev;
prev = curr;
curr = tempNext;
}
// 最终 prev
return prev;
}
}
3. 无重复字符的最长子串
java
class Solution {
public int lengthOfLongestSubstring(String s) {
// check
if(s==null || s.length()==0){
return 0;
}
// 滑动窗口
HashSet<Character> set = new HashSet<>();
int maxLength = 0;
int left = 0;
int right =0;
while(right < s.length()){
while(set.contains(s.charAt(right))){
set.remove(s.charAt(left));
left++;
}
set.add(s.charAt(right));
maxLength = Math.max(maxLength,right-left+1);
right++;
}
return maxLength;
}
}
5. 最长回文子串
中心扩展法的思想
回文字符串有一个对称的中心。我们可以从每个字符以及每两个字符之间作为中心开始,向两边扩展,看看能扩展多长的回文。
举个例子
假设我们有一个字符串"babad"。
我们从第一个字符'b'开始:
- 以第一个'b'作为中心,我们得到的回文是"b"。
- 以第一个'b'和第二个'a'之间作为中心,我们得到的回文长度是0(因为'b'和'a'不相同)。
然后我们以第二个字符'a'开始:
- 以第二个'a'作为中心,向两边扩展,得到的回文是"aba"。
- 以第二个'a'和第三个'b'之间作为中心,我们得到的回文长度是0(因为'a'和'b'不相同)。
我们继续这样做,直到遍历完所有字符。
代码
java
class Solution {
public int lengthOfLongestSubstring(String s) {
// check
if(s==null || s.length()==0){
return 0;
}
// 滑动窗口
HashSet<Character> set = new HashSet<>();
int maxLength = 0;
int left = 0;
int right =0;
while(right < s.length()){
while(set.contains(s.charAt(right))){
set.remove(s.charAt(left));
left++;
}
set.add(s.charAt(right));
maxLength = Math.max(maxLength,right-left+1);
right++;
}
return maxLength;
}
}