665. Non-decreasing Array - LeetCode】的更多相关文章

原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin(vector<int>& nums) { if (nums.empty()) ; , nums.size()-);//好神奇,第二个参数无论减或者不减1,都不影响该题的结果 ) ; return nums[res]; } int find(vector<int>& nu…
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能为空 时间复杂度为O(logn) 解法 解法一:最普通的二分搜索,先找到一个target,然后向两边拓展. class Solution { public: int binarySearch(vector<int>& nums, int target) { int left = 0,rig…
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度:[1,2,2,3,1]这个数组,去重后的元素为[1,2,3],每个元素在原数组中重复的次数分别是221,数组的度就是元素最大重复次数,这个数组中元素最大重复次数就是2 连续子数组:和字符串中的了串概念类似,数组元素顺序保持不变,取其中一部分,该题就是求在度相同的情况下最小连续子数组的长度 思路:数…
Question 665. Non-decreasing Array Solution 题目大意: 思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求. Java实现: public boolean checkPossibility(int[] nums) { if (nums == null || nums.length < 3) return true; int count = 0; // 判断前2个 if (nums[1] < nums[…
examination questions Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always…
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are ma…
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has th…
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant s…
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the arr…
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the ta…
leetcode - 33. Search in Rotated Sorted Array - Medium descrition 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.…
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary search 的变形.去在rotated array里找最小值. Idea:分析binary search 三种cases: 1. nums[mid] > nums[r]: remove the left part: l = mid+1          e.g. 2 3 4 5 1 2 2. nu…
题目: 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. 解题思路: 首先假设一个sorted没有rotated的数组[1,2,3],假设我们通过一个…
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Summary: It's easy, b…
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. For example,Given input array A = […
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. 题目大意:给定一个有序数组,是经过旋转过的,不知道在哪儿旋转的,求数组最小元素. 解题思路:直接遍历…
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro…
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],…
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix direct…
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat…
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from…
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible…
算法描述: 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. For example,Given input array…
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B a…
题目: 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…
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题解: 首先我们先明确什么是median,即中位数. 引用Wikipedia对中位数的定义: 计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列…
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.…
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array. The returned array must be in sorted order. Expected time complexity: O(n) Example: nums = [-, -, ,…
Question 896. Monotonic Array Solution 题目大意: 类似于数学中的减函数,增函数和物理中的加速度为正或为负 思路: 先比较前两个是大于0还是小于0,如果等于0就比较第2,3两个,依次类推,得到这个是递增数组还递减数组后再遍历接下来的数就好办了 Java实现: public boolean isMonotonic(int[] A) { if (A.length == 1) return true; int compFlag = 0; int i = 1; wh…
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rotate(int[] nums, int k) { int[] numsCopy = Arrays.copyOf(nums, nums.length); for (int i=0; i<nums.length; i++) { nums[(i+k)%nums.length] = numsCopy[i];…