leetcode题解日练--2016.7.1

来源:互联网 发布:淘宝卖家5天不发货 编辑:程序博客网 时间:2024/05/14 11:52

编程日记,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。标红题为之后还需要再看的题目。

今日题目:1、反转整数;2、最小栈;3、第一个坏版本;4、EXCEL表列标题。

7. Reverse Integer | Difficulty: Easy

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
题意:转换整型数
思路:
1、将整型转换为字符串再操作,最后换成long long型的之后判断是否溢出。

代码:
C++

class Solution {public:    int reverse(int x) {        //特殊情况:1、一位数字        if(x>-10 && x<10)   return x;        long long result;        string str_x = to_string(x);        string res;        if(str_x[0]=='-')   res += str_x[0];        for(int i=str_x.length()-1;i>=0;i--)        {            if(str_x[i]=='-')   continue;            res += str_x[i];        }        istringstream is(res);        is>>result;        if(result>2147483647 || result<-2147483648)     return 0;        return result;    }};

结果:16ms
2、用整型去解

class Solution {public:    int reverse(int x) {        long long res=0;        while(x)        {            res =res*10+x%10;            x /=10;        }        if(res>INT_MAX || res<INT_MIN)  return 0;        return res;    }};

结果:12ms

155. Min Stack | Difficulty: Easy

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
题意:设计一个增加了返回最小元素值的栈。
思路:
1、最初的想法是用一个双向的队列,push到尾部,pop、top尾部元素,然后得到最小值的时候用一次循环得到。但是如果栈太深就报错。
代码:
C++

class MinStack {    private:            deque<int> Q;public:    void push(int x) {        Q.push_back(x);    }    void pop() {        Q.pop_back();    }    int top() {        return Q.back();    }    int getMin() {        int min = INT_MAX;        for(int i=0;i<Q.size();i++)        {            if(Q[i]<min)    min = Q[i];        }        return min;    }};

结果:Time Limit Exceeded

2、正确的做法应该是用两个栈来存储。第一个栈就是正常的栈,push、pop、top都是用这个栈来处理,第二个栈用来存储小的元素,当这个栈为空的时候,push进第一个栈的同时push进第二个栈,当不为空的时候只有新来的元素小于栈顶才加入。pop的时候如果第一个栈的栈顶元素和第二个栈一样,就同时pop,否则无需操作第二个栈。top与第二个栈无关,getMin直接返回第二个栈顶元素。
C++

class MinStack {private:    stack<int> s1;    stack<int> s2;public:    void push(int x) {        s1.push(x);        if(s2.empty()||x<=s2.top())  s2.push(x);    }    void pop() {        if(s1.top()==s2.top())  s2.pop();        s1.pop();    }    int top() {        return s1.top();    }    int getMin() {        return s2.top();    }};

结果:60ms

278. First Bad Version | Difficulty: Easy

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
题意:1到n这n个版本,有一个错误导致后面的全部都错了,尽可能少的调用API找到第一个错误的版本。
思路:
1、二分查找,需要注意防止溢出。防止方法是将终点的选取用mid = start+(end-start)/2而不是mid = (start+end)/2
C++

// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public:    int firstBadVersion(int n) {       int res = 0;       int start = 1,end = n;       int mid;       while(end-start>1)       {           mid = start+(end-start)/2;           if(isBadVersion(mid))                {                res = mid;                end = mid-1;            }           else                {               start = mid+1;           }       }       if(isBadVersion(start))  res = start;       else if(isBadVersion(end)) res = end;       return res;    }};

结果:0ms
第二次刷代码

// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public:    int firstBadVersion(int n) {        int l = 1,r = n;        while(l<r)        {            int mid = l+(r-l)/2;            if(!isBadVersion(mid))   l = mid+1;            else                {                r = mid;            }        }        return l;    }};

结果:0ms

168. Excel Sheet Column Title | Difficulty: Easy

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 

题意:给一个整数,转换成excel表格的列表示方法。
思路:
其实就是10进制转换为26进制,逐位计算就好,有个需要注意的地方就是’A’-‘Z’是1-26而不是0-25,所以每次取余之前需要-1。
代码:
C++

class Solution {public:    string convertToTitle(int n) {        string res = "";        char tmp = 'a';        while(n)        {            tmp = (n-1)%26-0+'A';            n = (n-1)/26;            res.insert(0,1,tmp);        }        return res;    }};

结果:0ms

0 0
原创粉丝点击