Find the number I in the sorted array
Topic describes
Count the number of occurrences of a number in a sorted array.Copy the code
The sample
Example 1: input nums = [5,7,7,8,8,10], target = 8 output: 2 example 2: input nums = [5,7,7,8,8,10], target = 6 output: 0Copy the code
The problem solving
from collections import Counter
class Solution:
def search(self, nums: List[int], target: int) -> int:
for k, v in dict(Counter(nums)).items():
if k == target:
return v
return 0
Copy the code
Count the number of occurrences of elements, so again, Counter.
Turn Counter(nums) statistics into dict and iterate over the keys and values in the dict. When key==target, print value.
Powers of two, two
Topic describes
Given an integer n, determine whether the integer is a power of 2. If so, return true; Otherwise, return false. If there is an integer x such that n == 2x, n is considered to be a power of 2.Copy the code
The sample
Example 1: Input: n = 1 Output: true Description: 20 = 1 Example 2: Input: n = 16 Output: true Description: 24 = 16 Example 3: Input: n = 3 Output: false Example 4: Input: n = 4 Output: true Example 5: Input: n = 5 Output: false Warning: -2^31 <= n <= 2^ 31-1Copy the code
The problem solving
class Solution:
BIG = 2**30
def isPowerOfTwo(self, n: int) -> bool:
return n > 0 and Solution.BIG % n == 0
Copy the code
A number n is a power of 2 if and only if n is a positive integer. -2^31 <= n <= 2^31-1 = 2^31-1 = n <= 2^31-1 = n <= 2^31-1 = 2^30
Valid letter heterotopic words
Topic describes
Given two strings s and t, write a function to determine whether t is an alphabetic allotopic of S.Copy the code
The sample
Example 1: Enter s = "anagram", t = "nagaram" Output: true Example 2: Enter S = "rat", t = "car" Output: falseCopy the code
The problem solving
from collections import Counter
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return dict(Counter(s)) == dict(Counter(t))
Copy the code
If two strings have the same number of occurrences of each character, then they are valid alphabetic anagrams.
You can use Counter to count the frequency of occurrences of characters in each input parameter, convert it into a dict, and then determine if the two dictionaries are equal.
Merge two ordered arrays
Topic describes
Merge nums2 into nums1 to make nums1 an ordered array. Initialize nums1 and nums2 to m and n, respectively. You can assume that nums1 has a space size equal to m + n so that it has enough space to hold elements from Nums2.Copy the code
The sample
Example 1: Input nums1 = [1,2,3,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Nums1 = [1], m = 1, nums2 = [], n = 0Copy the code
The problem solving
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
nums1[m:] = nums2
return nums1.sort()
Copy the code
Just concatenate and sort.
Notice that there are two requirements for the length of the list, m and n. So nums1 concatenates nums2 from the MTH, then sort using sort().
5. Adjust the array order so that the odd number precedes the even number
Topic describes
Take an array of integers and implement a function to adjust the order of the numbers in the array so that all odd numbers are in the first half of the array and all even numbers are in the second half.Copy the code
The sample
Example: input: nums = [1,2,3,4] output: [1,3,2,4] note: [3,1,2,4] is also one of the correct answers.Copy the code
The problem solving
class Solution:
def exchange(self, nums: List[int]) -> List[int]:
list_odd = []
list_even = []
for i in nums:
if i % 2 == 0:
list_even.append(i)
else:
list_odd.append(i)
return list_odd + list_even
Copy the code
In the easiest way to understand. Iterate through the input list, then judge the elements and add them to the corresponding two arrays.
Finally, add the even number list_even to the odd number list_odd.