leetcode第二周解题总结(3,8,65)

来源:互联网 发布:idc数据公司 编辑:程序博客网 时间:2024/06/06 03:59

3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

题意解析:
寻找字符串中没有重复字符的最长子串,并且需要注意答案必须是子串。

解题思路:
刚看题的时候,想法比较混乱,决定先尝试一下暴力的方法,在字符串的每一位上都计算一次最长子串,然后返回最长的长度。

class Solution {public:    int lengthOfLongestSubstring(string s) {        set<int> sub;        size_t len = s.size();        if(len == 0) return 0;        if(len == 1) return 1;        int count = 1;        for (size_t i = 0; i < len; i ++) {            if (len - i <= count) break;            sub.insert(s[i]);            for (size_t j = i + 1; j < len; j ++) {                if (sub.find(s[j]) == sub.end()) {                    sub.insert(s[j]);                    count = count > sub.size()?count:sub.size();                } else {                    break;                }            }            sub.clear();        }        return count;    }};

这里写图片描述
当然这种方法的速度非常慢,而且有很多的冗余的计算,没有利用到上一次计算子串的结果。改成使用两个指针的方法,指向当前子串的头尾,当遇到重复字符的时候,将子串的头部向后移动到重复字符的后一位,每次都将尾指针移动一位,最后返回最长的子串长度。

class Solution {public:    int lengthOfLongestSubstring(string s) {        size_t len = s.size();        if(len == 0) return 0;        if(len == 1) return 1;        int count = 1;        int begin = 0, end = 0;        for (size_t i = 1; i < len; i++) {            if (len - begin <= count) break;            for (int j = begin; j <= end; j++) {                if (s[j] == s[i]) {                    begin = j + 1;                }             }            end = i;            int temp = end - begin + 1;            count = count > temp?count:temp;        }        return count;    }};

这里写图片描述

8. String to Integer (atoi)

Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

题意解析:
实现atoi函数,字符串转换整数

解题思路:
思路比较明确,但需要考虑好可能出现的各种情况,比如有正负号和没有符号,比较容易忽视掉溢出的问题。

class Solution {public:    int myAtoi(string str) {        size_t len = str.size();        if(len == 0) return 0;        int flag = 0;        int start = len;        for (size_t i = 0; i < len; i++) {            if (isdigit(str[i])) {                start = i;                break;            }            if (str[i] == ' ' && i < start) continue;            if (str[i] == '-') {                if (flag != 0 || i > start) break;                flag = -1;                start = i;            } else if (str[i] == '+') {                if (flag != 0 || i > start) break;                flag = 1;                start = i;            } else if (!isdigit(str[i])) {                break;            }         }        if(flag == 0) flag = 1;        double result = 0;        for (size_t x = start; x < len; x++) {            if(!isdigit(str[x])) break;            int a = str[x] - '0';            if (result > 214748364 || (result == 214748364 && a >= 8)) {                return (flag == -1)?(-2147483648):2147483647;            }            result = result*10 + a;        }        return flag*result;    }};

65. Valid Number

Validate if a given string is numeric.
Some examples:
“0” => true
” 0.1 ” => true
“abc” => false
“1 a” => false
“2e10” => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

题意解析:
判断字符串是否为合法数字

解题思路:
第一道hard难度的题目,但是题目描述却特别的简单,可以想到里面有很多坑。类比状态机,最好画个图来帮助理清思路。

class Solution {public:    bool isNumber(string s) {    size_t len = s.size();    int sign = 0, point = 0, ex = 0;    size_t i = 0;    while(i < len) {        if(sign == 0 && s[i]== ' ') {            i++;        }else if(sign == 1 && s[i] == ' ') {            for (size_t k = i + 1; k < len; k ++) {                if(s[k] != ' ') return false;            }            return true;        } else if(isdigit(s[i])) {            sign = 1;            i++;            if(i < len && !isdigit(s[i])) {                if(point == 0 && ex == 0 && s[i] == '.') {                    i++;                    point = 1;                    if(i < len) {                        if(ex == 0 && s[i] == 'e') {                            if(i+1 < len && (isdigit(s[i+1]) || s[i+1] == '+' || s[i+1] == '-')) {                                i++;                                ex = 1;                            } else {                                return false;                            }                        }                    }                } else if(ex == 0 && s[i] == 'e') {                    if(i+1 < len && (isdigit(s[i+1]) || s[i+1] == '+' || s[i+1] == '-')) {                        i++;                        ex = 1;                    } else {                        return false;                    }                }            }        } else if (point ==0 && ex == 0 && s[i] == '.') {            point = 1;            if(i+1 < len && isdigit(s[i+1])) {                i++;                sign = 1;            } else {                return false;            }        } else if (s[i] == '+' || s[i] == '-') {            if (sign == 0 && i+1 < len && (isdigit(s[i+1]) || s[i+1]=='.')) {                i++;                sign = 1;            } else if(sign == 1 && ex == 1 && (i+1 < len) && isdigit(s[i+1])) {                i++;            } else {                return false;            }        } else {            return false;        }    }    if(sign == 1) return true;    return false;}};

这题我的代码写的相当不clean,WA了好几次,自己再看都有点看不懂了,还是来看看官方答案的java代码:

public boolean isNumber(String s) {   int i = 0, n = s.length();   while (i < n && Character.isWhitespace(s.charAt(i))) i++;   if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;   boolean isNumeric = false;   while (i < n && Character.isDigit(s.charAt(i))) {i++;      isNumeric = true;   }   if (i < n && s.charAt(i) == '.') {      i++;      while (i < n && Character.isDigit(s.charAt(i))) {         i++;         isNumeric = true;      }}if (isNumeric && i < n && s.charAt(i) == 'e') {   i++;   isNumeric = false;   if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;   while (i < n && Character.isDigit(s.charAt(i))) {i++;      isNumeric = true;   }}while (i < n && Character.isWhitespace(s.charAt(i))) i++;   return isNumeric && i == n;}

本周做了这三道题下来,感觉leetcode还是慢点刷,多总结多思考,下周应该能做完字符串数组这个知识点的题目,再进行整体性的总结,并且补上应该学习的内容。

0 0
原创粉丝点击