首页 > 其他分享 >LeetCode刷题记录——day6

LeetCode刷题记录——day6

时间:2024-03-24 18:33:54浏览次数:16  
标签:150 return string day6 int length ans LeetCode 刷题

1、https://leetcode.cn/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150
直接从后往前遍历就好

class Solution {
public:
    int lengthOfLastWord(string s) {
        int length=0;
        int len=s.length();
        for(int i=len-1;i>=0;i--){
            if(s[i]!=' '){
                length++;
            }
            if(s[i]==' '&&length>0){
                break;
            }
        }
        return length;
    }
};

2、https://leetcode.cn/problems/longest-common-prefix/?envType=study-plan-v2&envId=top-interview-150
简单遍历就好

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        char falg_s;
        string ans;
        for(int i=0;i<strs[0].length();i++){
            falg_s=strs[0][i];
            for(int j=1;j<strs.size();j++){
                if(strs[j][i]!=falg_s||i>strs[j].length()){
                    return ans;
                }
            }
            ans=ans+falg_s;
        }
        return ans;
    }
};

标签:150,return,string,day6,int,length,ans,LeetCode,刷题
From: https://www.cnblogs.com/humanplug/p/18092783

相关文章

  • 【leetcode】【100268. 最长公共后缀查询】【Trie树】
    Howtoslovethisproblem?Ifwereversethestrings,theproblemchangestofindingthelongestcommonprefix.BuildaTrie,eachnodeisaletterandonlysavesthebestword’sindexineachnode,basedonthecriteria.code:classSolution{publ......
  • LeetCodeHot100 栈 155. 最小栈 394. 字符串解码
    155.最小栈https://leetcode.cn/problems/min-stack/description/?envType=study-plan-v2&envId=top-100-likedclassMinStack{Deque<Integer>deque;PriorityQueue<Integer>priorityQueue;publicMinStack(){de......
  • NSSCTF刷题日记复健day6
    昨天感觉左眼蒙上了一层黑雾,视野还收缩到只能看到中间一半,哈人,遂决定每日早睡早起。[NSSCTF2022SpringRecruit]ezgame游戏题,看js文件就可以了。[LitCTF2023]Followmeandhackme 如图,get加post就是了,彩蛋应该是当时这个比赛的某个flag组成题 彩蛋直接看的别的......
  • java数据结构与算法刷题-----LeetCode75. 颜色分类
    java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846文章目录1.双指针两次遍历2.三指针1.双指针两次遍历解题思路:时间复杂度O(......
  • java数据结构与算法刷题-----LeetCode451. 根据字符出现频率排序
    java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846文章目录1.hash统计出现次数后排序2.桶排序1.hash统计出现次数后排序解题思路:时间复杂度O(......
  • LeetCode 1778. Shortest Path in a Hidden Grid
    原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-hidden-grid/description/题目:Thisisan interactiveproblem.Thereisarobotinahiddengrid,andyouaretryingtogetitfromitsstartingcelltothetargetcellinthisgrid.Thegridiso......
  • LeetCode 834. Sum of Distances in Tree
    原题链接在这里:https://leetcode.com/problems/sum-of-distances-in-tree/description/题目:Thereisanundirectedconnectedtreewith n nodeslabeledfrom 0 to n-1 and n-1 edges.Youaregiventheinteger n andthearray edges where edges[i]=[a......
  • 和为 K 的子数组 - LeetCode 热题 10
    大家好!我是曾续缘......
  • 找到字符串中所有字母异位词 - LeetCode 热题 9
    大家好!我是曾续缘......
  • Hive 刷题——奖金瓜分问题
    题目描述在活动大促中,有玩游戏瓜分奖金环节。现有奖金池为3000元,代表奖金池中的初始额度。用户的分数信息如下:uid,score1001,451002,401003,351004,301005,25表中的数据代表每一个用户和其对应的得分,user_id和score都不会有重复值。瓜分奖金的规则如下:按照score从高到......