Documentation
¶
Index ¶
- func AddMinimum(word string) int
- func AlertNames(keyName []string, keyTime []string) []string
- func BinarySearch(nums []int, target int) int
- func CanJump(nums []int) bool
- func ContainsDuplicate(nums []int) bool
- func ContainsNearbyDuplicate(nums []int, k int) bool
- func EliminateMaximum(dist []int, speed []int) int
- func ExistInRotatedSorted(nums []int, target int) bool
- func FindMaxLength(nums []int) (maxL int)
- func FloodFill(image [][]int, sr int, sc int, newColor int) [][]int
- func FourSum(nums []int, target int) [][]int
- func GenerateParenthesis(n int) []string
- func GroupAnagrams(strs []string) [][]string
- func InsertAndMergeOverlapping(intervals [][]int, newInterval []int) [][]int
- func Intersection(nums1 []int, nums2 []int) []int
- func Jump(nums []int) int
- func LongestWPI(hours []int) int
- func MajorityElement[T any](s []T, eq func(a, b T) bool) T
- func MaxAreaOfIsland(grid [][]int) int
- func MaxProfit(prices []int) int
- func MaxProfit2(prices []int) int
- func MaximumNumberOfStringPairs(words []string) int
- func MergeOrderArray(nums1 []int, m int, nums2 []int, n int)
- func MergeOverlapping(intervals [][]int) [][]int
- func MergeSimilarItems(items1 [][]int, items2 [][]int) [][]int
- func MinimumRemoval(beans []int) int64
- func MinimumSize(nums []int, maxOperations int) int
- func MostFrequentEven(nums []int) int
- func MoveToEnd[T any](arr []T, match func(T) bool)
- func MoveZeroes(nums []int)
- func MovesToMakeZigzag(nums []int) int
- func NextPermutation(nums []int)
- func NumberOfPairs(nums []int) []int
- func PairSum(nums []int) int
- func PascalTriangle(numRows int) [][]int
- func Permute(nums []int) [][]int
- func RemoveDuplicates[T comparable](nums []T, appearNum int) int
- func RemoveDuplicates1[T comparable](nums []T) int
- func RemoveDuplicates2[T comparable](nums []T) int
- func RemoveSubfolders(folder []string) []string
- func RepairCars(ranks []int, cars int) int64
- func ReverseString(s []byte)
- func ReverseWord(s string) string
- func SearchInRotatedSorted(n int, f func(i int) int, cmp func(i, j int) int) int
- func SearchRange(nums []int, target int) []int
- func SearchRotated(nums []int, target int) int
- func SortColors(nums []int)
- func SortedSquares(nums []int) []int
- func Subset[T any](a []T) [][]T
- func SubsetsWithDup(nums []int) [][]int
- func TwoSumII(numbers []int, target int) []int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddMinimum ¶
func AlertNames ¶
func BinarySearch ¶
BinarySearch searches the index of element that value is equal target in nums. If there is no element equal to target, returns -1. Note: array should be sorted in asc.
func ContainsDuplicate ¶
ContainsDuplicate reports whether any value appears at least twice in the array
func ContainsNearbyDuplicate ¶
ContainsNearbyDuplicate returns true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k
func EliminateMaximum ¶
func ExistInRotatedSorted ¶
ExistInRotatedSorted returns true if target is in nums, or false if it is not in nums.
nums sorted in non-decreasing order (not necessarily with distinct values). and it 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].
func FindMaxLength ¶
FindMaxLength returns the maximum length of a contiguous subarray with an equal number of 0 and 1.
func FloodFill ¶
FloodFill performs a flood fill on the image starting from the pixel image[sr][sc] link: https://leetcode-cn.com/problems/flood-fill/
func FourSum ¶
FourSum returns an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that nums[a] + nums[b] + nums[c] + nums[d] == target, and a, b, c and d are distinct.
func GenerateParenthesis ¶
func GroupAnagrams ¶
func InsertAndMergeOverlapping ¶
InsertAndMergeOverlapping inserts newInterval into intervals, than merge overlapping interval.
func Intersection ¶
func LongestWPI ¶
func MajorityElement ¶
MajorityElement returns the majority element which appears more than ⌊n / 2⌋ times.
func MaxAreaOfIsland ¶
MaxAreaOfIsland returns the maximum area of an island in grid. If there is no island, return 0. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.)
func MaxProfit ¶
MaxProfit returns the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
func MaxProfit2 ¶
MaxProfit2 calculates the maximum profit that can be achieved by buying and selling stocks from the given prices array. It uses dynamic programming to compute the value. The function returns the maximum profit. origin: leetcode 122
func MaximumNumberOfStringPairs ¶
MaximumNumberOfStringPairs counts the number of pairs of strings in the given slice where swapping the first and second characters of a pair creates a duplicate pair. It returns the count of such pairs.
func MergeOrderArray ¶
MergeOrderArray merges nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
func MergeOverlapping ¶
MergeOverlapping merges all overlapping intervals, and returns an array of the non-overlapping intervals that cover all the intervals in the input.
func MergeSimilarItems ¶
func MinimumRemoval ¶
MinimumRemoval calculates the minimum number of removals required to obtain an array where each element is its index multiplied by the total sum of the array. It takes in an integer array `beans` and returns the minimum number of removals as an int64. The function first sorts the array in ascending order using the `sort.Ints` function. It calculates the total sum of all elements in the array using a loop and stores it in `total`. Then, it iterates over each element in the sorted array and calculates the number of removals required by subtracting the current element multiplied by the remaining number of elements from the total sum. The function keeps track of the minimum number of removals in the `removal` variable. Finally, it returns the value of `removal` as the result.
origin: leetcode problem 2171
func MinimumSize ¶
MinimumSize returns the minimum possible penalty after performing the operations. Penalty is the maximum number of balls in a bag.
func MostFrequentEven ¶
func MoveZeroes ¶
func MoveZeroes(nums []int)
MoveZeroes moves all 0's to the end of it while maintaining the relative order of the non-zero elements. link: https://leetcode-cn.com/problems/move-zeroes/
func MovesToMakeZigzag ¶
func NextPermutation ¶
func NextPermutation(nums []int)
func NumberOfPairs ¶
func PairSum ¶
PairSum groups these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. origin: leetcode 561
func PascalTriangle ¶
PascalTriangle returns the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it
func RemoveDuplicates ¶
func RemoveDuplicates[T comparable](nums []T, appearNum int) int
RemoveDuplicates removes the duplicates in-place, such that duplicates appeared at most appearNum and returns the new length. Given nums is sorted.
func RemoveDuplicates1 ¶
func RemoveDuplicates1[T comparable](nums []T) int
RemoveDuplicates1 removes the duplicates in-place, such that each element appears only once and returns the new length. Given nums is sorted.
func RemoveDuplicates2 ¶
func RemoveDuplicates2[T comparable](nums []T) int
RemoveDuplicates2 removes the duplicates in-place such that duplicates appeared at most twice and returns the new length slice. Given nums is sorted.
func RemoveSubfolders ¶
RemoveSubfolders returns the folders after removing all sub-folders in those folders.
func RepairCars ¶
func ReverseString ¶
func ReverseString(s []byte)
ReverseString reverses a string. link: https://leetcode-cn.com/problems/reverse-string/
func ReverseWord ¶
ReverseWord reverses every word in string. link: https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/
func SearchInRotatedSorted ¶
SearchInRotatedSorted returns index if f(index) == 0. or -1 if it is not in array. f(index) return 0, if array[i] equal to target. cmp compares array[i] array[j], if array[i] equal array[j] returns 0, if array[i] less than array[j] return -1, else return 1.
func SearchRange ¶
SearchRange returns starting and ending position of given target. Array was sorted in non-decreasing order.
func SearchRotated ¶
SearchRotated returns index of target. If target is not in array, it will return -1. Nums is possibly rotated at an unknown pivot index k (1 <= 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).
func SortColors ¶
func SortColors(nums []int)
func SortedSquares ¶
SortedSquares calculates the squares of slice element, and sort it.
func Subset ¶
func Subset[T any](a []T) [][]T
Subset generates all possible subsets of a given slice. It returns a slice of slices, where each element is a subset of the input slice. The order of the elements in the resulting subsets is not guaranteed. The function uses the binary counting method to generate the subsets. It iterates through all the possible binary numbers of length n, where n is the length of the input slice. For each binary number, the function checks which bits are set to 1, and includes the corresponding elements from the input slice in the subset. The function then appends the generated subset to the list of subsets. Finally, it returns the list of subsets.
For example, given the input slice [1, 2, 3], the function will generate the following subsets: [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]
The function has a type parameter T which represents the type of elements in the input slice. The function signature is `func Subset[T any](a []T) [][]T`.
Usage example: input := []int{1, 2, 3} subsets := Subset(input) // subsets = [][]int{{}, {1}, {2}, {1, 2}, {3}, {1, 3}, {2, 3}, {1, 2, 3}}
func SubsetsWithDup ¶
SubsetsWithDup returns all possible subsets (the power set). The result is not contain duplicate subsets.
Types ¶
This section is empty.