目前分類: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 解題
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 解題
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 解題
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 解題
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