zoukankan      html  css  js  c++  java
  • 力扣 剑指 Offer 58

    1、字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

    示例 1:

    输入: s = "abcdefg", k = 2
    输出: "cdefgab"
    示例 2:

    输入: s = "lrloseumgh", k = 6
    输出: "umghlrlose"
     

    限制:

    1 <= k < s.length <= 10000

    class Solution(object):
        def reverseLeftWords(self, s, n):
            """
            :type s: str
            :type n: int
            :rtype: str
            """
            return s[n:]+s[:n]

    2、给你两个整数,n 和 start 。

    数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。

    请返回 nums 中所有元素按位异或(XOR)后得到的结果。

    示例 1:

    输入:n = 5, start = 0
    输出:8
    解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。
    "^" 为按位异或 XOR 运算符。
    示例 2:

    输入:n = 4, start = 3
    输出:8
    解释:数组 nums 为 [3, 5, 7, 9],其中 (3 ^ 5 ^ 7 ^ 9) = 8.
    示例 3:

    输入:n = 1, start = 7
    输出:7
    示例 4:

    输入:n = 10, start = 5
    输出:2
     

    提示:

    1 <= n <= 1000
    0 <= start <= 1000
    n == nums.length

    class Solution(object):
        def xorOperation(self, n, start):
            """
            :type n: int
            :type start: int
            :rtype: int
            """
            num=[]
            for i in range(n):
                num.append(start + 2*i)
            r=0
            for i in range(n):
                r^=num[i]
            return r

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/xor-operation-in-an-array

    晚生不才,请多指教!
  • 相关阅读:
    vc6.0执行程序正确而debug版和release版运行错误
    visio调整画布大小和旋转画布
    Safecracker(暴力)
    gets()
    MATLAB——axis
    截尾平均数及利用SPSS求截尾平均数
    MATLAB求实数绝对值——abs
    MATLAB描绘饼图——pie
    error: expected unqualifiedid before 'int'
    MATLAB描绘极坐标图像——polar
  • 原文地址:https://www.cnblogs.com/lkc-test/p/14752825.html
Copyright © 2011-2022 走看看