【LeetCode151-160】逆转string,最大连续乘积,实现附加功能stack,找到两单链表的交点(精巧)

来源:互联网 发布:inux高性能服务器编程 编辑:程序博客网 时间:2024/06/11 23:35


151.逆转string


Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

class Solution {public:void reverseWords(string &s) {if (s.size() == 0)return;stack<string>all;int begin = 0;while (begin<s.size() && s[begin] == ' ') { ++begin; }for (int i = begin + 1; i<s.size() + 1; ++i) {if (i == s.size()) {all.push(s.substr(begin, i - begin));}else if (s[i] == ' ') {all.push(s.substr(begin, i - begin)); while (++i<s.size() && s[i] == ' ') {}begin = i;}}string result = "";while (all.size()) {result += (all.top() + " ");all.pop();if (!all.size())result = result.substr(0, result.size() - 1);}s = result;return;}};


152.找出数列里乘积最大的

//在三星酒店里写的这条代码(@中兴和泰酒店,来参加中兴活动,晚上顺手写条代码

考虑两种就好

1.有0

2.有负的

构造两个dp  (其实不用存储,因为每个只与上一个有关


class Solution {public:    int maxProduct(vector<int>& nums) {        int n=nums.size();        int result=nums[0];        int begin=0;        maxProduct(nums,begin,n,result);        return result;    }    int maxProduct(vector<int>& nums,int & begin,int n,int &result){        int temp=1,temp2=1;        bool goon=true;        for(;begin<n;++begin){            if(nums[begin]==0){                result=max(result,0);                return maxProduct(nums,++begin,n,result);            }            temp*=nums[begin];            result=max(result,temp);            if(!goon){                temp2*=nums[begin];                result=max(result,temp2);            }            if(temp<0&&goon){                goon=false;                temp2=1;            }        }        return 0;    }};


153.找出有序序列里最小的(被切成了两半)


Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array.


二分法

class Solution {public:    int findMin(vector<int>& nums) {        int result=min(nums[0],nums.back());        int right=nums.size()-1;        int left=0;        int mid=(left+right)/2;        while(left<mid&&mid<right){            if(nums[left]<nums[mid])left=mid;            else if(nums[left]>nums[mid])right=mid;            mid=(left+right)/2;        }        //result=min(result,nums[left]);        result=min(result,nums[right]);        result=min(result,nums[mid]);        return result;    }};




154.上一题包含一样数字的情况

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.



粗暴遍历一遍其实速度已经很快了……

class Solution {public:    int findMin(vector<int>& nums) {        int result=nums[0];        for(int i=1;i<nums.size();++i){            result=min(result,nums[i]);        }        return result;    }};

二分法(参考别人的非常漂亮的解法)

class Solution {public:int findMin(vector<int> &num) {    int start = 0;    int end = num.size()-1;    int mid;    while(start<end){        if(num[start]<num[end])        break;        mid = start+(end-start)/2;        if(num[mid]>num[end]){            start = mid+1;        }        else if(num[mid]==num[end]){            start++;            end--;        }        else        end= mid;    }    return num[start]; }};


155.实现stack并且能返回最小值 O(1)时间【Easy,精巧】

思路比较精巧,两个Stack存储

class MinStack {public:    /** initialize your data structure here. */    MinStack() {            }        void push(int x) {        if(small.empty()||x<=small.top())small.push(x);        all.push(x);    }        void pop() {        if(small.top()==all.top())small.pop();        all.pop();    }        int top() {       return all.top();     }        int getMin() {        return small.top();    }private:    stack<int>all;    stack<int>small;};



160.找到两个单链表相交的地方【巧妙】

Write a program to find the node at which the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2                   ↘                     c1 → c2 → c3                   ↗            B:     b1 → b2 → b3

begin to intersect at node c1.


Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.


第一反应,新建一个unordered_set来存储…(然而不满足O(1)存储的要求)

class Solution {public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {unordered_set<ListNode *>all;while (headA) {all.insert(headA);headA = headA->next;}while (headB) {if (all.find(headB) != all.end())return headB;headB = headB->next;}return nullptr;}};

然后参考了Solution里已经Discuss里的思路,吓死了的解决方案,五行!!!

class Solution {public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {    ListNode *cur1 = headA, *cur2 = headB;    while(cur1 != cur2){        cur1 = cur1?cur1->next:headB;        cur2 = cur2?cur2->next:headA;    }    return cur1;}};

如果A的线路长度为n,B的长度为m,  A遍历完后接到B开始,B遍历完后接着A开始。。。相遇的地方就是最终的结果(可能为空)//例如不相交,都是走m+n步到空的地方相等…




156-159锁着了,要花钱…尴尬……





原创粉丝点击