目前分類:leetcode (14)

瀏覽方式: 標題列表 簡短摘要

題目:

Balanced strings are those who have equal quantity of 'L' and 'R' characters.


文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

題目:

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.

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

題目: 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
文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

題目: 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
文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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.

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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
文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

Given an array of integers nums.

A pair (i,j) is called good if nums[i] == nums[j] and i < j.

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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(".","[.]")
文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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".

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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.

解題想法:

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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.

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

    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.. 但是在自己的電腦上跑看起來沒問題啊

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

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

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

class Solution:
def toLowerCase(self, str: str) -> str:
return(str.lower())

搞定收工

文章標籤

低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()

Close

您尚未登入,將以訪客身份留言。亦可以上方服務帳號登入留言

請輸入暱稱 ( 最多顯示 6 個中文字元 )

請輸入標題 ( 最多顯示 9 個中文字元 )

請輸入內容 ( 最多 140 個中文字元 )

reload

請輸入左方認證碼:

看不懂,換張圖

請輸入驗證碼