Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory…
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就是最小值,若是没有,那么第一个值就是最小值. Time O(n). Method 2 Binary Search. 与Find Peak Element类似. while 的条件是 l < r 并且 nums[l] > nums[r], 不然 l 到 r 这一段就是sorted的. nums[mi…
题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解: 这道题与之前Search in Rotated Sorted Array…
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du…
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 题解:就是找数组中第一个“下凹”的地方,可以遍历,不过也可以用二分法,速度更快. 特别注意边界的处理…
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth…
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有…
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic…
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1.…
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 题解: class Solution { public: /** * @param nums…
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺序访问数组.按下标取值是对数组的常见操作. 相关LeetCode题: 905. Sort Array By Parity 题解 922. Sort Array By Parity II 题解 977. Squares of a Sorted Array 题解 1150. Check If a…
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用HashMap 计数,数值大于n/2(注意不是大于等于而是大于),就是返回值.Time O(n), Space O(n). Method 2: 用了sort,返回sort后array的中值即可.Time O(n*logn), Space O(1). Method 3: 维护个最常出现值,遇到相同count+…
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then…
原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should b…
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 题解:设置两个变量,一个是current,初始化为A[0…
原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Exam…
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority ele…
题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 题解:运用多数投票算法的思路来解:从头到尾遍历数…
堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shift_up操作,时间复杂度O(logn). 堆是优先级队列(Priority queue)的底层数据结构,较常使用优先级队列而非直接使用堆处理问题.利用堆的性质可以方便地获取极值,例如 LeetCode 题目 215. Kth Largest Element in an Array,时间复杂度O(nl…