zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 32

    581. Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

    You need to find the shortest such subarray and output its length.

    Input: [2, 6, 4, 8, 10, 9, 15]
    Output: 5
    Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

    排好序,然后对比一下就行了

     1 class Solution {
     2 public:
     3     int findUnsortedSubarray(vector<int>& nums) {
     4         int n=nums.size();
     5         vector<int> nums2;
     6         nums2.insert(nums2.end(),nums.begin(),nums.end());
     7         sort(nums2.begin(),nums2.end());
     8         int st=0,ed=n-1;
     9         while(st!=n&&nums2[st]==nums[st])  st++;
    10         while(ed!=-1&&nums2[ed]==nums[ed])  ed--;
    11         if(st==n)   return 0;
    12         return ed-st+1;
    13     }
    14     
    15 };

    582. Kill Process

    删除一个树上的节点,求出所有子节点编号

    真的很生疏了,树都不知道怎么遍历了

    由于可能编号开的很大,因此用map作下映射,不说数据范围真坑,一直TLE(leetcode有TLE?),最后改了下数组大小AC了

    Input: 
    pid =  [1, 3, 10, 5]
    ppid = [3, 0, 5, 3]
    kill = 5
    Output: [5,10]
    Explanation: 
               3
             /   
            1     5
                 /
                10
    Kill 5 will also kill 10.
     1 class Solution {
     2 public:
     3 vector<int> ans;
     4         map<int,int> mp;
     5         map<int,int> fmp;
     6         int tot;
     7 vector<int> g[100000];
     8     void dfs(int i){
     9         ans.push_back(fmp[i]);
    10         for(int j=0;j<g[i].size();j++){
    11             dfs(g[i][j]);
    12         }
    13     }
    14     vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
    15         int n=pid.size();
    16         int i,st;
    17         tot=1;
    18         for(i=0;i<n;i++){
    19             if(!mp[ppid[i]]){
    20                 mp[ppid[i]]=tot;
    21                 fmp[tot]=ppid[i];
    22                 tot++;
    23             }
    24             if(!mp[pid[i]]){
    25                 mp[pid[i]]=tot;
    26                 fmp[tot]=pid[i];
    27                 tot++;
    28             }
    29             int u=mp[ppid[i]],v=mp[pid[i]];
    30             g[u].push_back(v);
    31         }
    32         dfs(mp[kill]);
    33         return ans;
    34         
    35     }
    36 };

    583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

    Input: "sea", "eat"
    Output: 2
    Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

    求下最长公共序列,注意是序列不是子串,因为对于abcab,abab这样的情况,公共子串就无法解决,求完之后减一下就可以了

     1 class Solution {
     2 public:
     3 int Max(int a,int b)  
     4 {  
     5     return (a>b)?a:b;  
     6 }  
     7 int creatDp(string s1,string s2,int *dp)  
     8 {  
     9     int len1 = s1.length();  
    10     int len2 = s2.length();  
    11     //int *dp = new int[len1*len2];  
    12     //先求出第一行  
    13     for(int j = 0;j<len2;j++)  
    14     {  
    15         if(s1[0] == s2[j]){  
    16             *(dp+0*len2+j) = 1;  
    17             for(;j<len2;j++)  
    18                 *(dp+0*len2+j) = 1;  
    19             break;  
    20         }  
    21         else  
    22                 *(dp+0*len2+j) = 0;  
    23     }  
    24     //然后求第一列  
    25     for(int i = 0;i<len1;i++)  
    26     {  
    27         if(s1[i] == s2[0]){  
    28             *(dp+i*len2+0) = 1;  
    29             for(;i<len1;i++)  
    30                 *(dp+i*len2+0) = 1;  
    31             break;  
    32         }  
    33         else  
    34             *(dp+i*len2+0) = 0;  
    35     }  
    36     //求其他的数据  
    37     for(int i =1;i<len1;i++)  
    38     {  
    39         for(int j =1;j<len2;j++)  
    40         {  
    41             if(s1[i] == s2[j])  
    42                 *(dp+i*len2+j) = *(dp+(i-1)*len2+(j-1))+1;  
    43             else  
    44                 *(dp+i*len2+j) = Max(*(dp+(i-1)*len2+j),*(dp+i*len2+j-1));  
    45         }  
    46     }  
    47    return dp[len1*len2-1];  
    48 }  
    49     int minDistance(string word1, string word2) {
    50         int len1 = word1.length();  
    51         int len2 = word2.length();  
    52         int *dp = new int[len1*len2];  
    53         int w=creatDp(word1, word2,dp);
    54         return len1-w+len2-w;
    55     }
    56 };

    587. Erect the Fence

    裸凸包问题,找了几个模板都没套进去,重载操作符没法用,23333,leetcode真坑

  • 相关阅读:
    MFC工作者线程
    【转】水煮TCPMP
    TCPMP的ARM编译器问题
    奇怪的链接警告-ole32.lib
    Stack overflow错误的一个原因
    旋转wince的桌面的函数
    ASNI to Unicode 转换与系统语言的问题
    WinCE 驱动开发问题精华集锦
    【转】OAL之系统时钟
    【转】蓝牙技术及其系统原理
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/6851911.html
Copyright © 2011-2022 走看看