目前分類:leetcode (14)
- Sep 07 Mon 2020 10:00
[Leetcode]1221. Split a String in Balanced Strings python3 解題
- Sep 06 Sun 2020 10:00
[Leetcode]1313. Decompress Run-Length Encoded List python3 解題
題目:
We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list. Return the decompressed list.
- Sep 06 Sun 2020 10:00
[Leetcode]1486. XOR Operation in an Array python3 解題
題目: Given an integer n and an integer start. Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length. Return the bitwise XOR of all elements of nums. 解題: 題目要求將nums所有元素與0進行XOR,原本想說要轉二進再做XOR,沒想到直接XOR就可以了... Python Code:
class Solution: def xorOperation(self, n: int, start: int) -> int: ans = 0 for i in range(n): ans = ans^(start + i * 2) return ans
- Sep 06 Sun 2020 10:00
[Leetcode]1389. Create Target Array in the Given Order python3 解題
題目: Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty. From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array. Repeat the previous step until there are no elements to read in nums and index. Return the target array. It is guaranteed that the insertion operations will be valid. 解題: 將nums,index兩個list使用zip打包然後循序插入即可。 Python Code:
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: ans=[] for a,b in zip(nums,index): ans.insert(b,a) return ans
- Sep 02 Wed 2020 10:00
[Leetcode]1365. How Many Numbers Are Smaller Than the Current Number python3 解題
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
Return the answer in an array.
- Sep 02 Wed 2020 10:00
[Leetcode]1281. Subtract the Product and Sum of Digits of an Integer python3 解題
Given an integer number n, return the difference between the product of its digits and the sum of its digits. 解題: 大於九除十,餘數與p相乘,與s相加 小於九不遞迴 可是使用較多變數似乎不是好的code python code:
class Solution: def subtractProductAndSum(self, n: int) -> int: p,s=1,0 while n>9: p*=n%10 s+=n%10 n=n//10 p*=n s+=n return p-s
- Sep 01 Tue 2020 10:00
[Leetcode]1512. Number of Good Pairs python3 解題
Given an array of integers nums.
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
- Sep 01 Tue 2020 10:00
[Leetcode]1108. Defanging an IP Address python3 解題
Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]". 解題方法:用python 很直覺的就想到用replace替代.為[.],不知道這題有什麼比較高端的方法
class Solution: def defangIPaddr(self, address: str) -> str: return address.replace(".","[.]")
- Sep 01 Tue 2020 10:00
[Leetcode]771. Jewels and Stones python3 解題
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".
- Sep 01 Tue 2020 10:00
[Leetcode]1342. Number of Steps to Reduce a Number to Zero python3 解題
Given a non-negative integer num
, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
解題想法:
- Sep 01 Tue 2020 10:00
[Leetcode]1470. Shuffle the Array python3 解題
Given a string s and an integer array indices of the same length.
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
- May 28 Thu 2020 10:00
[Leetcode]1431. Kids With the Greatest Number of Candies in python3
def kidsWithCandies(candies, extraCandies): list=[] for item in candies: if int(item)+extraCandies>=int(max(candies)): list.append("true") else: list.append("false") return list
上面的不知道為什麼一直報錯,list裡面的元素都是true.. 但是在自己的電腦上跑看起來沒問題啊
- Mar 19 Thu 2020 10:00
[python]leetcode-Reverse Integer
class Solution: def reverse(self, x: int) -> int: x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1]) return x if x < 2147483648 and x >= -2147483648 else 0
- Oct 14 Mon 2019 10:00
Leetcode709. To Lower Case