leetcode day1

LeetCode 557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Note: In the string, each word is separated by single space and there will not be any extra space in the string.

my answer:

class Solution(object):
    def reverseWords(self, s):
        new=s.split()
        l=""
        leng=len(new)
        for i in range(leng):
            length=len(new[i])
            n=reversed(new[i])
            for j in range(length):
                l+=n.next()
            l+=' '
        return l.strip()

Time Limit Exceeded 哭
查了一下别人的做法

 class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        return ' '.join(w[::-1] for w in s.split())

……路漫漫其修远兮

541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = “abcdefg”, k = 2
Output: “bacdfeg”

my answer:

class Solution(object):
    def reverseStr(self, s, k):
        new=""
        i=0
        length=len(s)
        while (i*k)<length:
            if length-i*k<k:
                new+=s[i*k:][::-1]
            elif length-i*k>=k and length-i*k<2*k:
                new += s[i*k:(i+1)*k][::-1]
                new += s[(i+1)*k:]
            else:
                new+=s[i*k:(i+1)*k][::-1]
                new+=s[(i+1)*k:(i+2)*k]
            i+=2
        return new

本以为会超时的
看了下别人的答案

class Solution(object):
    def reverseStr(self, s, k):
        l=list(s)
        for i in xrange(0,len(l),2*k):
            l[i:i+k]=reversed(l[i:i+k])
        return ''.join(l)

::>_<::

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,可以看出这是一段使用迭代器进行循环遍历的代码,并且遍历的对象是`vector<int>`类型的向量。迭代器`it`初始化为`c.begin()`,结束条件为`it < c.end()`。这段代码中省略了循环体,需要根据具体上下文来确定循环体的具体操作。 根据引用,我们可以了解到`vector`是一种动态数组,可以存储多个相同类型的元素。代码示例中用`assign`函数将另一个向量的一部分元素赋值给目标向量。具体来说,`a.assign(b.begin(), b.begin()+3)`意味着将向量`b`的前三个元素赋值给向量`a`。 根据引用,可以看出这是一段关于在VSCode中安装leetcode插件和配置的说明文档。文档中提到了如何安装插件、如何编译和构建代码等内容。 综上所述,LeetCode的`vector`是一种动态数组,可以存储多个相同类型的元素。可以通过迭代器对其进行循环遍历,也可以使用成员函数`assign`来赋值部分元素。在VSCode中,可以安装LeetCode插件并进行相关配置来进行编译和构建。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++LeetCode每日一题 Day3 常用容器vector的使用](https://blog.csdn.net/m0_75276704/article/details/129737396)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vscode安装leetcode-Leetcode:力码](https://download.csdn.net/download/weixin_38557935/20051243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值