0%

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

1
2
3
4
5
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
set1 = set(nums1)
set2 = set(nums2)
return [ list(set1 - set2), list(set2 - set1)]

https://leetcode.com/problems/sign-of-the-product-of-an-array/

There is a function signFunc(x) that returns:
1 if x is positive.
-1 if x is negative.
0 if x is equal to 0.
You are given an integer array nums. Let product be the product of all values in the array nums.
Return signFunc(product).

1
2
3
4
5
6
7
8
9
class Solution:
def arraySign(self, nums: List[int]) -> int:
sign = 1
for num in nums:
if num == 0:
return 0
if num < 0:
sign = -sign
return sign

https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/

You are given an array of unique integers salary where salary[i] is the salary of the ith employee.
Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.

1
2
3
4
class Solution:
def average(self, salary: List[int]) -> float:
salary.sort()
return sum(salary[1:-1]) / (len(salary) - 2)
1
2
3
4
def average(self, salary: List[int]) -> float:
salary.remove(min(salary))
salary.remove(max(salary))
return sum(salary) / len(salary)
1
2
def average(self, salary: List[int]) -> float:
return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2)
1
2
3
4
5
def average(self, salary: List[int]) -> float:
salary.sort()
salary.pop(0)
salary.pop()
return sum(salary) / len(salary)

換電腦後發現即使有用 time machine 做備份還是要重新部署
因為 push 至 github 的是已經編譯過後的檔案,所以沒有留原始檔的話,看起來應該沒救了(?)

Read more »

check.io 上的題目:判斷西洋棋中的兵有幾個是安全的
此系列文章主要是自己在解題時覺得有趣的題目,記錄下來讓自己以後好複習,有興趣的人可以參考看看

先來看看題目


總結來說:兵的行徑方向走斜對角的(左前、右前),若是兵的右下或左下有其他兵的話,他就是安全的

解題步驟

解題

1
2
3
4
5
6
def safe_pawns(pawns: set) -> int:
safe = 0
for p in pawns:
if chr(ord(p[0]) + 1) + str(int(p[1]) - 1) in pawns or chr(ord(p[0]) - 1) + str(int(p[1]) - 1) in pawns:
safe += 1
return safe

HackerRank 上的題目:找出成對的襪子並算出總共有幾雙
此系列文章主要是自己在解題時覺得有趣的題目,記錄下來讓自己以後好複習,有興趣的人可以參考看看

Read more »

實習開發網站,常遇到一些功能需求要上網查詢,整理成筆記以後再用到就不用又從頭開始找,也希望幫助到正在找學這些功能的人

Read more »