【无标题】

学习目标:

  1. 算法每日练习:英雄每日直播题目
  2. lecode每日一题
  3. 或许随机一题

题目: 

题目链接难度
2296.设计一个文本编辑器困难
735.行星碰撞中等
                

ww 

解题思路

 1.

请你设计一个带光标的文本编辑器,它可以实现以下功能:

添加:在光标所在处添加文本。
删除:在光标所在处删除文本(模拟键盘的删除键)。
移动:将光标往左或者往右移动。
当删除文本时,只有光标左边的字符会被删除。光标会留在文本内,也就是说任意时候 0 <= cursor.position <= currentText.length 都成立。

请你实现 TextEditor 类:

TextEditor() 用空文本初始化对象。
void addText(string text) 将 text 添加到光标所在位置。添加完后光标在 text 的右边。
int deleteText(int k) 删除光标左边 k 个字符。返回实际删除的字符数目。
string cursorLeft(int k) 将光标向左移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的字符数目。
string cursorRight(int k) 将光标向右移动 k 次。返回移动后光标左边 min(10, len) 个字符,其中 len 是光标左边的字符数目。

class LinkNode:
    def __init__(self, val):
        self.val = val
        self.pre = None
        self.next = None
class TextEditor:

    def __init__(self):
        self . head = LinkNode('')
        self.curr = self.head

    def addText(self, text: str) -> None:
        NextNode = self.curr.next
        for item in text:
            addNode = LinkNode(item)
            addNode.next = NextNode
            if NextNode:
                NextNode.pre = addNode
            addNode.pre = self.curr
            self.curr.next = addNode
            self.curr = addNode

    def deleteText(self, k: int) -> int:
        res = 0
        while(self.curr!=self.head and k):
            k -=1
            self.curr.pre.next = self.curr.next
            if self.curr.next:
                self.curr.next.pre = self.curr.pre
            self.curr = self.curr.pre
            res +=1
        return res 


    def leftStr(self, k):
        res = ""
        record = self.curr
        while(record!=self.head and k):
            k -=1
            res = record.val +res
            record = record.pre
        return res

    def cursorLeft(self, k: int) -> str:
        while(self.curr!=self.head and k):
            k -=1
            self.curr = self.curr.pre
        return self.leftStr(10)

    def cursorRight(self, k: int) -> str:
        while(self.curr.next!=None and k):
            k -=1
            self.curr = self.curr.next
        return self.leftStr(10)

2

给定一个整数数组 asteroids,表示在同一行的行星。

对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。

找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/asteroid-collision
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        i = 0
        len(asteroids)-1
        while i<len(asteroids)-1:
            if asteroids[i]<0:
                i +=1
            elif asteroids[i]*asteroids[i+1] >0:
                i +=1
            elif asteroids[i]+asteroids[i+1]==0:
                asteroids[i], asteroids[i+1] = 0,0
                i +=1
            else:
                asteroids[i+1] = asteroids[i] if asteroids[i]>abs(asteroids[i+1]) else asteroids[i+1]
                asteroids[i] = 0
                if asteroids[i+1]<0:
                    flag = 0
                    for j in range(i,-1,-1):
                        if asteroids[j]!=0:
                            temp = asteroids[j]
                            asteroids[j] = asteroids[i]
                            asteroids[i] = temp
                            flag = 1
                            break
                    if flag==0:
                        i +=1
                else:
                    i += 1
            
        res = []
        for i in range(len(asteroids)):
            if asteroids[i]!=0:
                res.append(asteroids[i])
        return res

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值