LeetCode 196,197,620,665,1185

196. Delete Duplicate Emails

Solution1:
Runtime: 677 ms, faster than 79.39% of MySQL online submissions for Delete Duplicate Emails.
Memory Usage: N/A

DELETE FROM Person
WHERE Id NOT IN
(
SELECT A.Id
FROM
(
SELECT MIN(Id) AS Id
FROM Person
GROUP BY Email
) A
);

Solution 2:
Runtime: 837 ms, faster than 44.21% of MySQL online submissions for Delete Duplicate Emails.
Memory Usage: N/A

DELETE p2
FROM Person p1 JOIN Person p2
WHERE p1.Email = p2.Email
AND p1.Id < p2.Id;

197. Rising Temperature

Runtime: 1717 ms, faster than 14.48% of MS SQL Server online submissions for Rising Temperature.
Memory Usage: N/A

SELECT w.Id 
FROM Weather as w left JOIN Weather as ww ON w.RecordDate = DATEADD(day,1,ww.RecordDate) 
WHERE w.Temperature > ww.Temperature

620. Not Boring Movies

Runtime: 124 ms, faster than 74.64% of MySQL online submissions for Not Boring Movies.
Memory Usage: N/A

# Write your MySQL query statement below
select * from cinema where id%2=1 and description != 'boring' order by rating desc;

665. Non-decreasing Array

Runtime: 1 ms, faster than 99.42% of Java online submissions for Non-decreasing Array.
Memory Usage: 46.6 MB, less than 9.52% of Java online submissions for Non-decreasing Array.

class Solution {
   public static boolean checkPossibility(int[] nums) {
       for(int i = 0; i < nums.length - 1; i++){
           //first occurance
           if(nums[i] > nums[i+1]){
               for(int j = i + 1; j < nums.length - 1; j++){
                   //second occurance
                   if (nums[j] > nums[j+1])
                       return false;
               }
               if(i == 0 || i + 2 > nums.length - 1){
                   return true;
               }
               if(nums[i] > nums[i + 2]){
                   if(nums[i - 1] > nums[i + 1]){
                       return false;
                   }
               }
               return true;
           }
       }
       return true;
   }
}

1185. Day of the Week

Runtime: 0 ms, faster than 100.00% of Java online submissions for Day of the Week.
Memory Usage: 33.7 MB, less than 100.00% of Java online submissions for Day of the Week.

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
        int[] daysOfMonth = new int[] {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 
        
        int base = 5 + day + daysOfMonth[month-1] + 
            (year-1971)*365 + 
            ((year-1)-1972)/4 + (year%4==0&&month>2? 1 : 0);

        return days[base%7]; 
    }
}


class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        String[] arrayWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return arrayWeek[dayOfWeek - 1];
    }
}

你可能感兴趣的:(Algorithm)