Press "Enter" to skip to content

Posts published in “Easy”

花花酱 LeetCode 760. Find Anagram Mappings

题目大意:输出数组A中每个元素在数组B中的索引。

Given two lists Aand B, and B is an anagram of AB is an anagram of A means B is made by randomizing the order of the elements in A.

We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j.

These lists A and B may contain duplicates. If there are multiple answers, output any of them.

For example, given

We should return

as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of Aappears at B[4], and so on.

Solution:

C++

Time complexity: O(nlogn)

Space complexity: O(n)

C++ / No duplication

 

花花酱 LeetCode 476. Number Complement

题目大意:给你一个正整数,输出和它互补的数(翻转所有的bits)。

Problem:

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Example 2:

Idea:

Bit



Solution:

C++

 

花花酱 LeetCode 367. Valid Perfect Square

题目大意:判断一个数是否是平方数。不能使用开根号函数。

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Example 2:

Idea:

Binary search

Solution:

C++

Time complexity: O(log(num))

Space complexity: O(1)

 

花花酱 LeetCode 653. Two Sum IV – Input is a BST

题目大意:给你一棵二叉搜索树,返回树中是否存在两个节点的和等于给定的目标值。

Problem:

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:

Example 2:

Solution:

C++

 

花花酱 LeetCode 748. Largest Number At Least Twice of Others

题目大意:给你一个数组,问你其中最大的数是不是比剩下的数大2倍以上,返回这样的数的索引。如果不存在,返回-1.

Problem:

In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Example 1:

Example 2:

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

Solution:

C++