Leetcode刷题-编程基础

2235

class Solution:
    def sum(self, num1: int, num2: int) -> int:
        return num1 + num2

2469

class Solution:
    def convertTemperature(self, celsius: float) -> List[float]:
        return [celsius+273.15,celsius*1.80 + 32.00]

2413

class Solution:
    def smallestEvenMultiple(self, n: int) -> int:
        if n%2 == 0:
            return n
        else:
            return 2*n

2236

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def checkTree(self, root: Optional[TreeNode]) -> bool:
        left = root.left.val
        right = root.right.val
        if left + right == root.val:
            return True
        else:
            return False

1486

class Solution:
    def xorOperation(self, n: int, start: int) -> int:
        res = start
        for i in range(1,n):
            res = res ^ (start + i*2)
        return res

1512

class Solution:
    def numIdenticalPairs(self, nums: List[int]) -> int:
        res = 0
        for i , x in enumerate(nums):
            for j , y in enumerate(nums):
                if i < j and x == y:
                    res += 1
        return res

1534

class Solution:
    def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
        n = len(arr)
        res = 0
        for i in range(n):
            for j in range(n):
                for k in range(n):
                    if not i < j < k:
                        continue
                    if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
                        res += 1
        return res

584

# Write your MySQL query statement below
SELECT name FROM customer WHERE referee_id <> 2 OR referee_id IS NULL;

在MySQL中,需要对NULL进行特殊判断,上题中如果只判断是否等于2,会忽略掉id为NULL的行。(MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN)当我们拿一个非NULL值和一个NULL值,或者两个NULL值来比较时,会返回UNKNOWN。

1757

# Write your MySQL query statement below
Select product_id
from Products
where low_fats = 'Y' and recyclable = 'Y';

709

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

258

class Solution:
    def addDigits(self, num: int) -> int:
        if num == 0:
            return num
        return (num - 1)%9 + 1
        

1281

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        flag = 1
        num = int(n)
        s = 0
        a = 1
        while num > 0:
            s += int(num%10)
            a *= int(num%10)
            if num%10 == 0:
                flag = 0
            num //= 10
        return a - s
        
        

231

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n == 1:
            return True
        if n%2 == 1 or n == 0:
            return False
        while math.fabs(n) > 0:
            if n%2 == 1 and n != 1:
                return False
            n /= 2
        return True
        

326

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        m = 1
        while m < n:
            m *= 3
        if m == n:
            return True
        else:
            return False
        
        

263

class Solution:
    def isUgly(self, n: int) -> bool:
        if n <= 0:
            return False
        while n > 1:
            if n % 2 == 0:
                n //= 2
            elif n % 3 == 0:
                n //= 3
            elif n % 5 == 0:
                n //= 5
            else:
                return False
        return True        

1470

class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        res = []
        i = 0
        j = n
        while i < n and j < 2*n:
            res.append(nums[i])
            res.append(nums[j])
            i += 1
            j += 1
        return res

867

class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        res = [[0 for _ in range(len(matrix))] for _ in range(len(matrix[0]))]
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                res[j][i] = matrix[i][j]
        return res
        

1422

class Solution:
    def maxScore(self, s: str) -> int:
        mid = 1
        l = len(s)
        res = 0
        while mid < l:
            ans = 0
            for i in range(mid):
                if s[i] == '0':
                    ans += 1
            for j in range(mid,l):
                if s[j] == '1':
                    ans += 1
            mid += 1
            res = max(res,ans)
        return res
        

2586

class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        res = 0
        for i in range(left,right + 1):
            if words[i].startswith('a') or words[i].startswith('e') or words[i].startswith('i') or words[i].startswith('o') or words[i].startswith('u'):
                if words[i].endswith('a') or words[i].endswith('e') or words[i].endswith('i') or words[i].endswith('o') or words[i].endswith('u'):
                    res += 1
        return res
        

852

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left = 0
        right = len(arr) - 2
        while left + 1 < right:
            mid = (left + right) // 2
            if arr[mid + 1] < arr[mid]:
                right = mid
            else:
                left = mid
        return right


        

### LeetCode 基础语法入门教程 LeetCode 是程序员提升算法能力的重要平台之一,掌握其基础语法对于高效解至关重要。以下是关于 C++ 和 Java 的基础语法要点以及如何应用这些知识来解决 LeetCode 上的问。 #### 1. 数据类型与变量 C++ 提供了多种基本数据类型,包括但不限于 `int`、`long` 和 `double` 等[^2]。在编写程序时,应根据具体需求选择合适的数据类型以优化内存使用和计算效率。例如,在处理大规模数值运算时,推荐优先考虑浮点数或长整型以避免溢出问。 #### 2. 控制流语句 控制流是编程的核心部分,它决定了代码执行路径的选择逻辑。常用的条件分支结构如 `if...else` 或者更复杂的多路判断工具——`switch case` 可帮助开发者根据不同输入情况采取相应操作。此外还有循环机制(for/while),它们允许重复执行某段特定指令直到满足终止条件为止。 #### 3. 容器类简介及其应用场景分析 为了更好地管理和存储大量动态变化的信息单元组群对象集合体概念模型抽象表示形式即我们常说的各种标准模板库(STL)组件实例化后的实体形态表现出来的东西叫做容器(Container),其中最常用的一些包括: - **Vector**: 动态数组,支持随机访问并能在尾部快速增删元素。 - **Set & Unordered_Set**: 分别代表有序集合并具备查找功能的哈希表版本;前者按升序排列后者则不关心顺序只关注唯一性检验速度更快些时候会用到find()方法来进行成员存在性的检测工作流程简化很多哦~ - **Map & Multimap**: 键值映射关系管理利器,能够轻松实现一对一或多对一关联查询任务目标达成效果显著提高工作效率的同时也减少了错误发生的可能性几率大大降低啦!另外还有一种叫Unorderd_Map变种形式同样适用于某些特殊场合条件下呢😊 #### 4. 特殊用途的数据结构介绍 - Deque (双端队列) Deque是一种可以在两端都进行插入删除操作非常灵活方便的一种线性序列结构形式表达方式呈现出来的样子感觉特别棒👍🏻通过下面这个例子我们可以看到它是怎么被创建出来的:`Deque<Integer> deque = new LinkedList<>();` 这样我们就得到了一个基于链接列表实现原理构建而成的新对象实例可供后续进一步开发拓展之用了呀😄[^3] #### 5. 关于栈(Stacks)的知识补充说明 最后值得一提的是有关Stack方面的内容知识点分享给大家知道一下吧~原来啊,在Standard Template Library里面啊,我们的老朋友Stack其实背后隐藏着秘密武器呢🧐那就是它可以由三种不同的底层支撑技术方案任选其中之一作为实际运行环境下的物理载体介质哟😎分别是向量(Vector),双向队列(Deque)或者是简单的单链表(List)...怎么样是不是很神奇呢😉[^4] ```java // 示例:Java 中 Stack 的简单使用 import java.util.Stack; public class Main { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); // 添加元素 stack.push(10); stack.push(20); System.out.println("Top element is: " + stack.peek()); // 输出顶部元素 // 删除顶部元素 stack.pop(); System.out.println("After popping, top element is: " + stack.peek()); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YoloMari

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值