leetcode二期

标准

语言
地址
思路
注释写好

代码done

15

// c with stl(c++)
// 15
// https://leetcode-cn.com/problems/3sum/
// 先排序,然后找,符合的就加入答案
class Solution {
public:

    vector<vector<int>> threeSum(vector<int>& nums) {
    	vector<vector<int>> res;
    	int NUM = nums.size();
    	if(NUM < 3)
    		return res;

    	sort(nums.begin(), nums.end());
    	int i,j,k, need, a, before=-111111;

    	for(i=0; i <NUM -2; i++){
    		need = -nums[i];
            if(before == -need)
                continue;
            else
                before = -need;
    		j = i+1;
    		k = NUM-1;

    		while(j<k){
    			if(nums[j]+nums[k] > need){
    				a = nums[k];
    				while(nums[k] ==a && k>j)
    					k--;
    			}
    			else if(nums[j]+nums[k] < need){
    				a = nums[j];
    				while(nums[j] ==a && j<k)
    					j++;
    			}
    			else{
    				vector<int> temp{nums[i],nums[j],nums[k]};
    				res.push_back(temp);
                    a = nums[k];
    				while(nums[k] ==a && k>j)
    					k--;
                    a = nums[j];
    				while(nums[j] ==a && k>j)
    					j++;
    			}
    		}
    	}
    	return res;
    }
};

81

// c with stl(c++)
// 81
// https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/
// 先分为两段,分别找,找到返回

class Solution {
public:
    bool bisearch(vector<int>& nums,int low,int high, int aim){
        int mid;
        while(low <= high){
            mid = (low +high)/2;
            if(nums[mid]== aim)
                return true;
            else if(nums[mid] > aim)
                high = mid -1;
            else if(nums[mid] < aim){
                low = mid +1;
            }
        }
        return false;
    }
    bool search(vector<int>& nums, int target) {
        int start=0;
        int size = nums.size();
        while((start<size-1)    &&  (nums[start] <= nums[start+1]))
            start++;
        return bisearch(nums, 0,start, target)||bisearch(nums, start+1,size-1, target);
    }
};

35

// c with stl(c++)
// 35
// https://leetcode-cn.com/problems/search-insert-position/


class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int high = nums.size()-1;
        int low = 0,mid;
        while(high >= low){
            mid = (low +high)/2;
            if( nums[mid] ==target)
                return mid;
            else if( nums[mid] <target)
                low = mid+1;
            else
                high = mid-1;
        }
        return low;
    }
};

33

// c with stl(c++)
// 33
// https://leetcode-cn.com/problems/search-in-rotated-sorted-array/

class Solution {
public:
    int bisearch(vector<int>& nums, int low, int high, int target) {
        int mid;
        while(high >= low){
            mid = (low +high) /2;
            if( nums[mid] == target)
                return mid;
            else if( nums[mid] <target)
                low = mid+1;
            else
                high = mid-1;
        }
        return -1;
    }

    int inside(vector<int>& nums,int left, int right, int target){
        if(left>right)
            return -1;
        int mid = (left + right)/2;
        if(target == nums[mid])
            return mid;
        if((target > nums[mid]) && (nums[right] >=target))
            return bisearch(nums, mid+1, right, target);
        if( (target >= nums[left]) && (target < nums[mid]) )
            return bisearch(nums, left, mid -1, target);
        if( nums[mid] < nums[left])
            return inside( nums, left,mid-1, target);
        else
            return inside( nums, mid +1 , right, target);
    }
    
    int search(vector<int>& nums, int target) {
        return inside(nums, 0, nums.size()-1, target);
    }
};

153

// c with stl(c++)
// 153
// https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/

class Solution {
public:
    int inside(vector<int>& nums, int left, int right){
        if(nums[left] <=nums[right])
            return nums[left];
        int mid = (left +right)/2;
        if( nums[left] <= nums[mid]){
            return inside(nums, mid+1, right);
        }else{
            return inside(nums, left, mid);
        }
    }

    
    int findMin(vector<int>& nums) {
        return inside(nums, 0, nums.size() -1);
    }
};

70

// c with stl(c++)
// 70
// https://leetcode-cn.com/problems/climbing-stairs/
// 用一个数组存1到n阶的方法,然后动态规划?也可以用递归,但是递归比较慢比较耗内存
class Solution {
public:
    int climbStairs(int n) {
        int dp[n+1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i =2; i <=n; i++){
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
};

154

// c with stl(c++)
// 154
// https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/
// 二分,递归

class Solution {
public:

	int inside(vector<int>& nums, int left, int right){
        // cout<< "IN";
        // cout<< '(';
        // cout<< left ;
        // cout<< '-';
        // cout<< right ;
        // cout<< ')';
		int val_l = nums[left] ;
		int val_r = nums[right] ;
		if( left+1 >= right)
			return (val_l > val_r)? val_r: val_l ;
		int mid = (left +right)/2 ;
		int val_m = nums[mid] ;

		if(val_l < val_r)
			return val_l;

		if(val_l < val_m)
			return inside( nums, mid, right);

		if(val_m < val_r)
			return inside( nums, left, mid);

		// cout << "flat";

    	// int i = left ;
    	// while((i< right-1) && (nums[i] <= nums[i+1])){
    	// 	i++;
    	// }
     //    // cout << i;
     //    if( i == right ){
     //        i = left-1;
     //    }
    	// return nums[i+1];

    	int min_l = inside(nums, left, mid);
    	int min_r = inside(nums, mid, right);
    	return (min_r < min_l)? min_r: min_l;
	}


    int findMin(vector<int>& nums) {
    	return inside(nums, 0, nums.size()-1);
    }
};

263

// c with stl(c++)
// 263
// https://leetcode-cn.com/problems/ugly-number/
// 分别去掉 n 中的质因数2,3,5,然后判断


class Solution {
public:
    bool isUgly(int n) {
        if((n == -2147483648)||(n==0))
            return false;

        while(n%2 ==0)
            n /=2;

        while(n%3 ==0)
            n /=3;

        while(n%5 ==0)
            n /=5;

        if(n==1)
            return true;

        return false;
    }
};

56

// c with stl(c++)
// 56
// https://leetcode-cn.com/problems/merge-intervals/
// 先排序,然后合并


class Solution {
public:
    bool judge(vector<int>& i1, vector<int>& i2){
        if(i1[1] >= i2[0])
            return true;
        return false;
    }


    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        int size = intervals.size();

        sort(intervals.begin(), intervals.end());
        // for(int i = 0; i <size; i++){
        //     cout<< intervals[i][0];
        //     cout<< ',';
        //     cout<< intervals[i][1] << endl;
        // }

        vector< vector<int>> res;
        for(int i = 0; i< size; ){
            int j = i+1;
            int max = intervals[i][1];
            while((j < size) && ( intervals[j][0] <= max)){
                (max<intervals[j][1])?(max = intervals[j][1]):1;
                j++;
            }
            res.push_back({intervals[i][0], max});
            i = j;
        }
       
        return res;
    }
};

264

// c with stl(c++)
// 264
// https://leetcode-cn.com/problems/ugly-number-ii/
// 3指针


class Solution {
public:
    int nthUglyNumber(int n) {
        int dp[n+1];
        if(n <=3)
            return n;
        dp[1] = 1;
        int i=1, j =1, k =1;
        int count =2;

        while(count<= n ){
            int min= dp[i]*2;
            if(dp[j]*3 <= min)
                min =dp[j]*3;
            if(dp[k]*5 <= min)
                min =dp[k]*5;
            dp[count] = min;
            while(dp[i]*2 <= min)
                i++;
            while(dp[j]*3 <= min)
                j++;
            while(dp[k]*5 <= min)
                k++;
            count++;
        }
        return dp[n];
    }
};

539

// c with stl(c++)
// 539
// https://leetcode-cn.com/problems/minimum-time-difference/
// 先排序,然后合并
#include <algorithm>

class Solution {
public:
    int getnum(string what){
        string min(what,0,2);
        string sec(what,3,2);
        // cout<< min<<'#'<<sec;
        return stoi(min)*60 +stoi(sec);
    }
    int findMinDifference(vector<string>& timePoints) {
        int size = timePoints.size();
        if(size >1440 )
            return 0;
        vector<int> res;
        int temp;
        for(int i = 0; i<size; i++){
            temp = getnum( timePoints.back());
            timePoints.pop_back();
            res.push_back(temp);
        }
        sort(res.begin(), res.end());
        int last = res[size-1];

        for(int i = 1; i< size; i++){
            res[size - i] = res[size - i] -res[size - i-1];
        }
        res[0] = res[0]+ 1440 -last;
        sort(res.begin(), res.end());
        return res[0];
    }
};

now

没done

日志

本文写于2021年3月11日,后续会不断更新,目前是用c++(带stl的c)来做题。

4.8 写了5道,主要是二分法的题目

4.9 写了两题

4.10 写了一题

4.11 write 2

posted @ 2021-03-11 10:36  RougeBW  阅读(50)  评论(0编辑  收藏  举报