当前位置: 首页 > article >正文

408算法题leetcode--第六天

58. 最后一个单词的长度

  • 58. 最后一个单词的长度
  • 思路:反向遍历
  • 时间:O(n);空间:O(1)
class Solution {
public:
    int lengthOfLastWord(string s) {
        int id = s.size() - 1;
        while(s[id] == ' '){
            --id;
        }
        int ret = 0;
        while(id >= 0 && s[id] != ' '){
            ret++;
            id--;
        }
        return ret;
    }
};

1768. 交替合并字符串

  • 1768. 交替合并字符串
  • 思路:双指针,类似归并排序
  • 时间:O(max(m, n));空间:O(1)
class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        string ret;
        int p = 0, q = 0;
        int w1_size = word1.size(), w2_size = word2.size();
        while(p < w1_size && q < w2_size){
            ret += word1[p++];
            ret += word2[q++];
        }
        while(p < w1_size){
            ret += word1[p++];
        }
        while(q < w2_size){
            ret += word2[q++];
        }
        return ret;
    }
};

http://www.kler.cn/news/312099.html

相关文章:

  • 【项目案例】物联网比较好的10+练手项目推荐,附项目文档/源码/视频
  • Elasticsearch:一次生产集群 ES Watcher 失效的深度排查与分析 - 全过程剖析与解决方案
  • 【面试八股总结】GMP模型
  • IP-adapter masking
  • 高频旋转滑环的特点与应用分析
  • 前端大屏自适应方案
  • POI操作EXCEL增加下拉框
  • 《线性代数》常用公式定理总结
  • 第十一章 【后端】商品分类管理微服务(11.5)——增强响应
  • JavaScript网页设计案例(动态表单、实时搜索、交互式地图、无限滚动加载等)
  • 【qt】一个WPS项目了解qt界面设计的基本套路
  • 基于SpringBoot+Vue的私人牙科诊所管理系统
  • 从0书写一个softmax分类 李沐pytorch实战
  • 《深入了解 Linux 操作系统》
  • Scrapy爬虫框架 Pipeline 数据传输管道
  • K8S容器实例Pod安装curl-vim-telnet工具
  • 人工智能在鼻咽癌中的应用综述|文献精析·24-09-13
  • Python中使用Redis布隆过滤器
  • 苹果为什么不做折叠屏手机?
  • 2024蓝桥杯省B好题分析
  • vulnhub靶机:Holynix: v1
  • GO CronGin
  • 【Flask教程】 flask安装简明教程
  • Visual Studio配置opencv环境
  • Web Worker 简单使用
  • 2024永久激活版 Studio One 6 Pro for mac 音乐创作编辑软件 完美兼容
  • 基于STM32设计的路灯故障定位系统(微信小程序)(229)
  • flink自定义process,使用状态求历史总和(scala)
  • spring boot启动报错:so that it conforms to the canonical names requirements
  • 【系统架构设计师-2017年真题】案例分析-答案及详解