array

package
v0.0.0-...-bced520 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMinimum

func AddMinimum(word string) int

func AlertNames

func AlertNames(keyName []string, keyTime []string) []string

func BinarySearch

func BinarySearch(nums []int, target int) int

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 CanJump

func CanJump(nums []int) bool

func ContainsDuplicate

func ContainsDuplicate(nums []int) bool

ContainsDuplicate reports whether any value appears at least twice in the array

func ContainsNearbyDuplicate

func ContainsNearbyDuplicate(nums []int, k int) bool

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 EliminateMaximum(dist []int, speed []int) int

func ExistInRotatedSorted

func ExistInRotatedSorted(nums []int, target int) bool

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

func FindMaxLength(nums []int) (maxL int)

FindMaxLength returns the maximum length of a contiguous subarray with an equal number of 0 and 1.

func FloodFill

func FloodFill(image [][]int, sr int, sc int, newColor int) [][]int

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

func FourSum(nums []int, target int) [][]int

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 GenerateParenthesis(n int) []string

func GroupAnagrams

func GroupAnagrams(strs []string) [][]string

func InsertAndMergeOverlapping

func InsertAndMergeOverlapping(intervals [][]int, newInterval []int) [][]int

InsertAndMergeOverlapping inserts newInterval into intervals, than merge overlapping interval.

func Intersection

func Intersection(nums1 []int, nums2 []int) []int

func Jump

func Jump(nums []int) int

func LongestWPI

func LongestWPI(hours []int) int

func MajorityElement

func MajorityElement[T any](s []T, eq func(a, b T) bool) T

MajorityElement returns the majority element which appears more than ⌊n / 2⌋ times.

func MaxAreaOfIsland

func MaxAreaOfIsland(grid [][]int) int

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.)

link:https://leetcode-cn.com/problems/max-area-of-island

func MaxProfit

func MaxProfit(prices []int) int

MaxProfit returns the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

func MaxProfit2

func MaxProfit2(prices []int) int

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

func MaximumNumberOfStringPairs(words []string) int

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

func MergeOrderArray(nums1 []int, m int, nums2 []int, n int)

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

func MergeOverlapping(intervals [][]int) [][]int

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 MergeSimilarItems(items1 [][]int, items2 [][]int) [][]int

func MinimumRemoval

func MinimumRemoval(beans []int) int64

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

func MinimumSize(nums []int, maxOperations int) int

MinimumSize returns the minimum possible penalty after performing the operations. Penalty is the maximum number of balls in a bag.

func MostFrequentEven

func MostFrequentEven(nums []int) int

func MoveToEnd

func MoveToEnd[T any](arr []T, match func(T) bool)

MoveToEnd moves match element to end.

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 MovesToMakeZigzag(nums []int) int

func NextPermutation

func NextPermutation(nums []int)

func NumberOfPairs

func NumberOfPairs(nums []int) []int

func PairSum

func PairSum(nums []int) int

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

func PascalTriangle(numRows int) [][]int

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 Permute

func Permute(nums []int) [][]int

Permute returns all the possible permutations of nums.

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

func RemoveSubfolders(folder []string) []string

RemoveSubfolders returns the folders after removing all sub-folders in those folders.

func RepairCars

func RepairCars(ranks []int, cars int) int64

func ReverseString

func ReverseString(s []byte)

ReverseString reverses a string. link: https://leetcode-cn.com/problems/reverse-string/

func ReverseWord

func ReverseWord(s string) string

ReverseWord reverses every word in string. link: https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/

func SearchInRotatedSorted

func SearchInRotatedSorted(n int, f func(i int) int, cmp func(i, j int) int) int

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

func SearchRange(nums []int, target int) []int

SearchRange returns starting and ending position of given target. Array was sorted in non-decreasing order.

func SearchRotated

func SearchRotated(nums []int, target int) int

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

func SortedSquares(nums []int) []int

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

func SubsetsWithDup(nums []int) [][]int

SubsetsWithDup returns all possible subsets (the power set). The result is not contain duplicate subsets.

func TwoSumII

func TwoSumII(numbers []int, target int) []int

TwoSumII returns the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. link:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL