• (反向思维) leetcode 836. Rectangle Overlap


    class Solution {
    public:
        bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
            
            //不重叠的情况:左;右;上;下 返回false
            //两个矩形的边或角重叠不算重叠
            if(rec1[2] <= rec2[0] || rec1[0] >= rec2[2] || rec1[3] <= rec2[1] || rec1[1] >= rec2[3])
                return false;
            else 
                return true;   
        }
    };

     

    class Solution {
    public:
        /**
         * @param word: a non-empty string
         * @param abbr: an abbreviation
         * @return: true if string matches with the given abbr or false
         */
        bool validWordAbbreviation(string &word, string &abbr) {
            // write your code here
            //把某一段替换为这一段的长度称为它的缩写
            //指针怎么指,见到数字怎么办,指针溢出如何处理
            int i = 0, j = 0;
            int n = word.size(), m = abbr.size();
            while(i<n && j<m){
                if(abbr[j] >= '0' && abbr[j] <= '9'){
                    if(abbr[j] == '0')
                        return false;   //前导0返回false
                    int val = 0;
                    while(j<m && abbr[j] >= '0' && abbr[j] <= '9'){
                        val = val*10 + (abbr[j] - '0');
                        j++;
                    }
                    i = i + val;  //i跳过数字的部分
                }
                else{
                    if(word[i] != abbr[j])
                        return false;
                    else{
                        i++;
                        j++;
                    }
                }
            }
            
            // "aa" "a2"
            if(i == n && j == m)
                return true;
            else return false;
        }
    };

    follow up :

     

     

     思路:

    1)直接模拟;

    2)  求出abbr 有重复就增加prefix, 继续求 abbr

    用hash来判断重复。

    class Solution {
    public:
        /**
         * @param dict: an array of n distinct non-empty strings
         * @return: an array of minimal possible abbreviations for every word
         */
        string getAbbr(string s, int p){  //p为前缀长度
            if(p >= s.size()-2)  //s的长度最小,需要大于等于 p+2(前缀的长度p + 数字的长度 1 + 最后一个字符的长度 1)
                return s;  //规则3
            string ans;
            //to_string : 将整型转换为string型,s.back()得到s的最后一个字符
            ans = s.substr(0, p) + to_string(s.size()-1-p) + s.back();
            return ans;
        }
        
        vector<string> wordsAbbreviation(vector<string> &dict) {
            // write your code here
            int len = dict.size();
            vector<string> ans(len);
            vector<int> prefix(len);
            unordered_map<string, int> count;
            
            for(int i=0; i<len; i++){
                prefix[i] = 1;  //前缀长度初始为1
                ans[i] = getAbbr(dict[i], 1); //得到每个字符串的缩写
                count[ans[i]] ++;
            }
            
            while(true){
                bool unique = true;
                for(int i=0; i<len; i++){
                    if(count[ans[i]] > 1){
                        prefix[i] ++;
                        ans[i] = getAbbr(dict[i], prefix[i]);
                        count[ans[i]] ++;
                        unique = false;
                    }  
                } //while循环直到unique为true退出
                if(unique)
                    break;
            }
            return ans;
        }
    };
  • 相关阅读:
    Learning Spark中文版--第三章--RDD编程(1)
    关于learning Spark中文版翻译
    dom4j 常用操作
    Invalid bound statement (not found)
    touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
    docker 运行tomcat 并部署 java web项目
    docker build no such file or directory
    Docker 笔记
    java 自动拆箱 自动装箱
    Ubuntu18 中文乱码 问题 解决
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11439114.html
Copyright © 2020-2023  润新知