【LeetCode】81. Search in Rotated Sorted Array II 解题记录
问题描述
There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].
Given the array nums after the rotation and an integer target, re ...
【LeetCode】69. Sqrt(x) 解题记录
问题描述
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
测试样例
12Input: x = 4Output: 2
123Input: x = 8Output: 2Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
说明
10 <= x <= 2^31 - 1
解题
思路
对 [0, x] 区间使用二分法求解。
为了防止除以 0,把 x = 0 的情况单独考虑,然后对区间 [1, a] 进行二分查找。
补充:
二分法
时间复杂度 O(lg(n))
还有一种更 ...
【LeetCode】524. Longest Word in Dictionary through Deleting 解题记录
问题描述
Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
测试样例
12Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]Output: "apple"
12Input: s = " ...
【LeetCode】680. Valid Palindrome II 解题记录
问题描述
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
测试样例
12Input: s = "aba"Output: true
123Input: s = "abca"Output: trueExplanation: You could delete the character 'c'.
12Input: s = "abc"Output: false
说明
121 <= s.length <= 10^5s consists of lowercase English letters.
解题
思路
采用双指针来求解。两个指针分别从两端向中间遍历,遇到不相同的字符时,分别剔除左侧和右侧的一个元素,判断其余子串是否回文。
补充:
双指针
substring() 函数是左闭右开区间
时间复杂度 O(n)
代码
12345678910 ...
【LeetCode】633. Sum of Square Numbers 解题记录
问题描述
Given a non-negative integer c, decide whether there’re two integers a and b such that a2 + b2 = c.
测试样例
123Input: c = 5Output: trueExplanation: 1 * 1 + 2 * 2 = 5
12Input: c = 3Output: false
12Input: c = 4Output: true
12Input: c = 2Output: true
12Input: c = 1Output: true
说明
10 <= c <= 2^31 - 1
解题
思路
采用双指针来求解。左侧从 0 开始,右侧最大为 sqrt(c) :
两数平方和等于给定值,那么它们就是我们要的结果。
两数平方和小于给定值,把左边的指针右移一位,使得当前的平方和增加一点。
两数平方和大于给定值,把右边的指针左移一位,使得当前的平方和减少一点。
补充:
双指针
double 转 int 方法:(new Double(varible)).int ...
【LeetCode】76. Minimum Window Substring 解题记录
问题描述
Given two strings s and t of lengths m and n respectively, return the minimum window in s which will contain all the characters in t. If there is no such window in s that covers all characters in t, return the empty string “”.
Note that If there is such a window, it is guaranteed that there will always be only one unique minimum window in s.
测试样例
12Input: s = "ADOBECODEBANC", t = "ABC"Output: "BANC"
12Input: s = "a", t = "a"Output: "a ...
【LeetCode】142. Linked List Cycle II 解题记录
问题描述
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Notice that you should not modify the linked list.
测试样例
123Input: head = [3,2,0,-4], pos = 1Output: tail connects to ...
【LeetCode】88. Merge Sorted Array 解题记录
问题描述
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2.
测试样例
12Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3Output: [1,2,2,3,5,6]
12Input: nums1 = [1], m = 1, nums2 = [], n = 0Output: [1]
说明
12345nums1.length == m + nnums2.length == n0 < ...
【LeetCode】167. Two Sum II - Input array is sorted 解题记录
问题描述
Given an array of integers numbers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length.
You may assume that each input would have exactly one solution and you may not use the same element twice.
测试样例
123Input: numbers = [2,7,11,15], target = 9Output: [1,2]Explanation: The sum of 2 and ...
【LeetCode】665. Non-decreasing Array 解题记录
问题描述
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
测试样例
123input: nums = [4,2,3]output: trueexplanation: you could modify the first 4 to 1 to get a non-decreasing array.
123Input: nums = [4,2,1]Output: falseExplanation: You can't get a non-decreasing array by modify at most one elem ...