Environment: Mysql

Key technique: N/A

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email |
+---------+
| a@b.com |
+---------+

Note: All emails are in lowercase.

Solution:

# Write your MySQL query statement below
select Email
from Person
group by Email
having count(Email) > 1;

Submissions:


Environment: Python 3.8

Key technique: &, set , list

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Analysis:

  1. Use & and set to get Intersection.
  2. Convert it to a list.

Solution:

class Solution:
def intersection(self, nums1, nums2):
return list(set(nums1) & set(nums2))

Submissions:


Environment: Python 3.8

Key technique: binarytree, Node, collections, deque

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.

Example 1:

Input: root = [3,9,20,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].

Analysis:

  1. Use collections.deque()
  2. check node or not
  3. get the same level node
  4. search next level node
  5. calculate the current level average and add…

Environment: Python 3.8

Key technique: set, Counters

Given a string s, return true if s is a good string, or false otherwise.

A string s is good if all the characters that appear in s have the same number of occurrences (i.e., the same frequency).

Example 1:

Input: s = "abacbc"
Output: true
Explanation: The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s.

Analysis:

  1. Counter it as {‘a’:2, ‘b’:2, ‘c’:2}
  2. Check value is equal or not.

Solution:

from collections import Counter
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
ans=set(Counter(s).values())
return len(ans)==1

Submissions:


Environment: Python 3.8

Key technique: List node

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1:

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Analysis:

  1. Use below four steps to revise link.
  2. temp=head
  3. head=head.next
  4. temp.next=ans
  5. ans=temp

Environment: Python 3.8

Key technique: binarytree, Node

Given the root of a binary tree, return the inorder traversal of its nodes' values.

Example 1:

Input: root = [1,null,2,3]
Output: [1,3,2]

Analysis:

  1. Please see leetcode solution, it is very clear.

Solution:

from binarytree import Node
root = Node(1)
root.right = Node(2)
root.right.left=Node(3)
# Getting binary tree
print('Binary tree :', root)
#root = [1,null,2,3]
class Solution:
def inorderTraversal(self, root):
if root:
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
else:
return []

Submissions:


Environment: Python 3.8

Key technique: for, if, !=

A string is good if there are no repeated characters.

Given a string s​​​​​, return the number of good substrings of length three in s​​​​​​.

Note that if there are multiple occurrences of the same substring, every occurrence should be counted.

A substring is a contiguous sequence of characters in a string.

Example 1:

Input: s = "xyzzaz"
Output: 1
Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz".
The only good substring of length 3 is "xyz".

Analysis:

  1. if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] !=s[i+2], ans+=1.
  2. Return ans

Solution:

class Solution:
def countGoodSubstrings(self, s):
ans=0
for i in range(len(s)-2):
if (s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] !=s[i+2]):
ans+=1
return ans

Submissions:


Environment: Python 3.8

Key technique: bin, append, replace

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

Example 1:

Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10

Analysis:

  1. Use loop and n is loop end. Convert input number to binary such as 1 to ‘0b1’.
  2. Replace ‘ob’ with ‘’.
  3. Count all ‘1’ numbers.
  4. Add them to the list.

Solution:

class Solution:
def countBits(self, n):
ans=[]
for i in range(n+1):
temp=bin(i).replace('ob', '')
ans.append(temp.count('1'))

return ans

Submissions:

Reference:

https://leetcode.com/problems/counting-bits/discuss/1348058/python


Environment: Python 3.8

Key technique: ASCII

The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).

The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.

  • For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21.

You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through…


Environment: Python 3.8

Key technique: //, %

Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.

After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.

Example 1:

Input: n = 34, k = 6
Output: 9
Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Analysis:

  1. Calculate quotient and remainder between n and k.
  2. If update n > k, continue step1.
  3. If update n≤k, stop calculate.
  4. Summarize the current quotient and remainder.

Solution:

class Solution:
def sumBase(self, n: int, k: int) -> int:
ans=0
while (n>=k):
ans+=n%k
n//=k
return ans +n

Submissions:

Fatboy Slim

Interesting in any computer science.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store