leetcode刷题记录

这篇博客记录了作者在LeetCode上的刷题经历,涵盖了字符串、数组和二叉树遍历等主题。具体题目包括最长无重复字符子串、罗马数字到整数转换、最后一个单词的长度、同构字符串、两数之和、移除有序数组中的重复元素、图像旋转、帕斯卡三角形、两个数组的交集、最大水容器、字符串反转和二叉树的各种遍历问题。
摘要由CSDN通过智能技术生成

1 字符串

Leetcode 3. Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        const int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; ++i ){
            vector<int> seen(128);
            int j = i;
            while(j < n && !seen[s[j]])
                seen[s[j++]]++;
            ans = max(ans, j - i);
        }
        return ans;
    }
};

Leetcode 13  Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II

class Solution {
public:
    int romanToInt(string s) {
        map<char, int> m{
  {'I', 1}, {'V', 5},
                         {'X', 10}, {'L', 50},
                         {'C', 100}, {'D', 500},
                         {'M', 1000}};
        char p = 0;
        int ans = 0;
        for(char c : s){
            ans += m[c];
            if (p && m[c] > m[p])
                ans -= 2 * m[p];
            p = c;
        }
        return ans;
   
    }
};

58. Length of Last Word

Given a string s consisting of some words separated by some number of spaces, return 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值