算法 day2

【leetcode】連續整數求和

class Solution {
public:
    int sum1(int k)
    {
        return k*(k+1)/2;
    }
    int consecutiveNumbersSum(int N) {
        int ccount = 0;
        int k=1;
        while(sum1(k) <= N)
        {
            if((N-sum1(k))%k == 0)
            {
                ccount++;
            }
            k++;
        }
        return ccount;
    }
};

【劍指 Offer 】53 - II. 0~n-1中缺失的數字

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        if(nums[0]!=0)
        return 0;

        for(int i=0;i<nums.size()-1;i++)
        {
            if(nums[i]+1!=nums[i+1])
            {
                return nums[i]+1;
            }
        }
        return nums[nums.size()-1]+1;
    }
};

【leetcode】判斷是否有環

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while(slow!=NULL && fast!=NULL)
        {
            slow = slow->next;
            fast = fast->next;
            if(fast == NULL)
            break;
            fast = fast->next;
            if(slow == fast)
                return true;
        }
        return false;
    }
};

【leetcode】最接近的三數之和

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        int left,right;
        int len = nums.size();
        //[a,b)
        int a1;
        int min_delta = INT_MAX;
        for(int i=0;i<len-2;i++)
        {
            left = i+1;
            right = len-1;
            int sum;
            int ans = target - nums[i];
            while(left<right)
            {
                sum = nums[left] + nums[right];
                if(abs(ans - sum)<abs(min_delta))
                {
                    min_delta = ans - sum;
                    a1 = sum + nums[i];
                }

                if(sum < ans)
                {
                    left++;
                }
                else if(sum > ans)
                {
                    right--;
                }
                else if(sum == ans)
                {
                    return target;
                }
            }
        }
        return a1;
        
    }
};

【leetcode】盛最多水的容器

class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxv = 0;
        int left,right;
        left = 0;
        right = height.size()-1;
        while(left<right)
        {
            int nowv = (right-left)*min(height[right],height[left]);
            if(nowv>maxv) maxv = nowv;

            if(height[right]>height[left])
            {
                left++;
            }
            else right--;
        }
        return maxv;
    }
};

【leetcode】刪除鏈表的倒數第N個節點

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* pre;
        ListNode* right = head;
        ListNode* left = head;
        while(n--)
        {right = right->next;}

        if(right == NULL)
        {
            return head->next;
        }

        while(right!=NULL)
        {
            if(right->next == NULL)
                pre = left;
            left = left->next;
            right = right->next;
        }
        pre->next = left->next;
        return head;
    }
};

【leetcode】迴文串

class Solution {
public:
    bool isPalindrome(int x) {
        char str[10000];
        sprintf(str,"%d",x);
        int left ,right;
        left = 0;
        right = strlen(str)-1;
        while(left<right)
        {
            if(str[left] == str[right])
            {
                left++;
                right--;
            }
            else return false;
        }
        return true;

        
    }
};

【leetcode】最長不重複字符串

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        map<char,int> hashmap;
        int left = 0;
        int right = 0;
        int ccount = 0;
        int max = 0;
        while(left<s.length())
        {
            while((hashmap.find(s[right])==hashmap.end()||hashmap[s[right]]==0)&&right<s.length())
            {
                hashmap[s[right]] = 1;
                right++;
                printf("%d\n",right);
            }
            ccount = right-left;
            if(max<ccount)max = ccount;

            hashmap[s[left]]--;
            left++;

            printf("%c%d %c%d \n",s[left],left,s[right],right);
        }
        return max;
    }
};

【leetcode】括號匹配

最簡單的棧簡單題

class Solution {
public:
    bool isValid(string s) {
        char stack[10000];
        int p=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i] == '('||s[i] == '['||s[i] == '{')
            {
                stack[p++] = s[i];
            }
            else if(p==0)
            {
                return false;
            }
            else if(s[i] == ')')
            {
                if(stack[p-1]!='(')
                {
                    return false;
                }
                else 
                {
                    p--;
                }
            }            
            else if(s[i] == ']')
            {
                if(stack[p-1]!='[')
                {
                    return false;
                }
                else 
                {
                    p--;
                }
            }            
            else if(s[i] == '}')
            {
                if(stack[p-1]!='{')
                {
                    return false;
                }
                else 
                {
                    p--;
                }
            }
        }
        if(p!=0)return false;
        return true;
    }
};

要注意判斷當有輸入時,棧是不是爲空

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章