精华内容
下载资源
问答
  • 【leetcode学习笔记】leecode是什么

    万次阅读 2016-08-27 21:04:19
    在csdn上看到leecode是第二次了,之前一直不知道是什么?今天在学习linux系统如何将文本登录界面转换为图像登录界面的时候(出现了运行startx后,ubuntu密码正确也进不去系统的问题,进入了无限的循环登录中,没办法...

    在csdn上看到leecode是第二次了,之前一直不知道是什么?今天在学习linux系统如何将文本登录界面转换为图像登录界面的时候(出现了运行startx后,ubuntu密码正确也进不去系统的问题,进入了无限的循环登录中,没办法只有通过guest用户搜集解决方法,拍照参照测试,最终找到了解决方法:(运行startx后ubuntu密码正确进不去的问题。),在博主的博文目录里看到了leecode,搜集了这厮到底是什么?如下:

    leetcode 是一个美国的在线编程网站,上面主要收集了各大IT公司的笔试面试题,对于应届毕业生找工作是一个不可多得的好帮手。

    这个网站的的好处在于它会告诉你测试数据以及你的输出和正确的输出是什么,方便大家调试学习。目前,只支持C++和Java两种语言(现在支持了更多的语言)。

    另外它是支持在线编辑,还提供了一个在线运行环境,可以直接看到运行结果。

    另一个很好的地方在于提供了2个独立的代码窗口,分别编译运行。一个自己玩的开发窗口,一个提交代码的窗口,可以在第一个窗口里测试各种拿不准的功能点。

    官方网站:http://leetcode.com/

    自己看了一下确实是非常不错的练习和锻炼自己编程能力的网站,也是锻炼自己以后找工作的能力,自己看了下最简单的题,感觉解决问题的思路是懂得,但是编程的细节还是需要查阅相关的资料,这也是自己需要锻炼的基本功。自己找到了比较简单的一个题:

    自己的答案:

    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) 
        {
            vector<int> twonum;
            //把数输入到数组
            int num;
            while(cin>num)
            {
                nums.push_back(num);
            }
            //输入和
            cin>target;
            for(int i=0;i<nums.size();i++)
            for(int j=0;j<nums.size();j++)
            {
                if(nums[i]+nums[j]==target)
                twonum.push_back(i);
                twonum.push_back(j);
                
            }
            return twonum;
            
        }
    };

    由于网站说邮箱没有验证,不知道准确性 ,先打算自己在VS上测试一下效果。后面会贴出实验结果。

    关于vector数组的用法参考:http://bbs.csdn.net/topics/310022968

    另外补充的点:C++具有string类,以前自己因为记得没有,在此更正一下。

    leecode相关的资料:

    暂时刷完leetcode的小体会

    天码营网站-》经验-》leetcode(包含求解过程和注释)

    leetcode题解




    展开全文
  • 今天在LeeCode上刷到这样一道题: 「外观数列」一个整数序列,从数字 1 开始,序列中的每一项都对前一项的描述。前五项如下:      1 11 21 1211 111221 1 被读作 “one 1” (“一个一”) ...

    今天在LeeCode上刷到这样一道题:
    「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。前五项如下:
        

    1. 1
      
    2. 11
      
    3. 21
      
    4. 1211
      
    5. 111221
      

    1 被读作 “one 1” (“一个一”) , 即 11。
    11 被读作 “two 1s” (“两个一”), 即 21。
    21 被读作 “one 2”, “one 1” (“一个二” , “一个一”) , 即 1211。

    给定一个正整数 n(1 ≤ n ≤ 30),输出外观数列的第 n 项。

    注意:整数序列中的每一项将表示为一个字符串。
    示例1:

    输入: 1
    输出: "1"
    解释:这是一个基本样例。
    

    示例 2:

    输入: 4
    输出: "1211"
    解释:当 n = 3 时,序列是 "21",其中我们有 "2""1" 两组,"2" 可以读作 "12",也就是出现频次 = 1 而 值 = 2;类似 "1" 可以读作 "11"。所以答案是 "12""11" 组合在一起,也就是 "1211"

    以下大神的神级解答(用时0ms):

    class Solution {
        public String countAndSay(int n) {
            switch (n) {
                case 1:
                    return "1";
                case 2:
                    return "11";
                case 3:
                    return "21";
                case 4:
                    return "1211";
                case 5:
                    return "111221";
                case 6:
                    return "312211";
                case 7:
                    return "13112221";
                case 8:
                    return "1113213211";
                case 9:
                    return "31131211131221";
                case 10:
                    return "13211311123113112211";
                case 11:
                    return "11131221133112132113212221";
                case 12:
                    return "3113112221232112111312211312113211";
                case 13:
                    return "1321132132111213122112311311222113111221131221";
                case 14:
                    return "11131221131211131231121113112221121321132132211331222113112211";
                case 15:
                    return "311311222113111231131112132112311321322112111312211312111322212311322113212221";
                case 16:
                    return "132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211";
                case 17:
                    return "11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221";
                case 18:
                    return "31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211";
                case 19:
                    return "1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221";
                case 20:
                    return "11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211";
                case 21:
                    return "311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
                case 22:
                    return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113213221133122112231131122211211131221131112311332211211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
                case 23:
                    return "111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121113222123112221221321132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221";
                case 24:
                    return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211";
                case 25:
                    return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132132211231232112311321322112311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312111312212231131122211311123113322112111312211312111322111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113213221132213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121132211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
                case 26:
                    return "1113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123211211131211121311121321123113111231131122112213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122113221122112133221121113122113121113222123211211131211121311121321123113213221121113122113121113222113221113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
                case 27:
                    return "31131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322123211211131211121332211231131122211311122122111312211213211312111322211231131122211311123113322112111331121113112221121113122113111231133221121113122113121113222123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221123113112221131112311332111213122112311311123112111331121113122112132113311213211321222122111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311123113322113223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331221122311311222112111312211311123113322112132113213221133122211332111213112221133211322112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121122132112311321322112111312211312111322211213111213122112132113121113222112132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212321121113121112133221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213212312311211131122211213211331121321122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311222113111221221113122112132113121113222112132113213221133122211332111213322112132113213221132231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221";
                case 28:
                    return "13211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221232112111312211312113211223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321322113311213212322211322132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212311322123123112111321322123122113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132132211331221122311311222112111312211311123113322112111312211312111322212311322123123112112322211211131221131211132221132213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211";
                case 29:
                    return "11131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221231122212213211321322112311311222113311213212322211211131221131211132221232112111312111213322112131112131221121321131211132221121321132132212321121113121112133221121321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122111213122112311311222113111221131221221321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311222112111331121113112221121113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212321121113121112133221132211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112132132112211131221131211132221121321132132212321121113121112133221123113112221131112311332111213211322111213111213211231131211132211121311222113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131211131221223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112211213322112312321123113213221123113112221133112132123222112311311222113111231132231121113112221121321133112132112211213322112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212311222122132113213221123113112221133112132123222112311311222113111231133211121321132211121311121321122112133221123113112221131112311332211322111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
                case 30:
                    return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
                default:
                    return "0";
            }
        }
    }
    

    WTF?供大家一乐hhhhhhhhhhhhhhhhhhhhhhhhh.

    展开全文
  • leecode——螺旋矩阵

    2021-03-15 09:20:40
    文章目录前言一、题目是什么?二、解答1.我的解答2.官方总结 前言 提示:前两天leecode上面的题都是和hash映射有关,数据结构倒是讲了相关内容,但是孩子当时没有代码实践。。。 总的来说,hash就是一种基于地址...


    前言

    提示:前两天leecode上面的题都是和hash映射有关,数据结构倒是讲了相关内容,但是孩子当时没有代码实践。。。
    总的来说,hash就是一种基于地址直接查找的算法,突破了传统的查找算法,比如,顺序查找,二分查找,B树查找之类的乱七八糟的查找的时间极限。学习了一下代码,倒是没有自己打。


    提示:以下是本篇文章正文内容,本次代码思维量较少,还是考察代码功底,注意数组不要越界!

    一、题目是什么?

    给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

    示例 1:

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:[1,2,3,6,9,8,7,4,5]

    示例2:

    输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    输出:[1,2,3,4,8,12,11,10,9,5,6,7]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/spiral-matrix
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    二、解答

    1.我的解答

    大致思路是这样的,由于每次读一圈,规律都是相似的,所有考虑递归。本质上就是坐标(i,j)在边界上的循环。

    值得注意的是,当写递归主函数的时候,有一些特殊情况要判断,比如只有一行,只有一列,只有两行,只有两列等等,避免重复。

    本次代码用时,大概1:30分钟,中间debug主要是两个问题:
    1、递归函数传入参数的意义没有特别清楚,其实之前我还尝试过首坐标加行数之类的,但是写着写着自己就搞混了,导致了一些bug。

    2、数组越界问题,我记得C语言里面,对于数组越界是没有专门的防护机制的,但是这个C++好像有哦(good!)出现了runtime error 的问题,就是数组角标出现了-1之类的。

    代码如下:

    class Solution {
    
    
    public:
        vector<int> result;
        void youCanTry(vector<vector<int>>& matrix,int a,int b,int c,int d){
        //a,b,行首(尾)坐标,c,d,列首(尾)坐标
         if(b<a||d<c) return;//数组坐标越界检查,也是终止条件
         if(b==a&&d==c) {//只有一行一列
         result.push_back(matrix[a][c]);
         return;
         }
    
         for(int i=c;i<=d;i++){
             result.push_back(matrix[a][i]);
            // cout<<matrix[a][i]<<" ";
         }
         
         if(b==a) return;//只有一行
         
         for(int i=a+1;i<=b;i++){
            // cout<<" wrong";
             result.push_back(matrix[i][d]);
             //cout<<matrix[m-1][i]<<" ";
            //向下
         }
         if(d==c) return;//只有一列
         for(int i=d-1;i>=c;i--){
             result.push_back(matrix[b][i]);
         }
         //<-
         if(a==b-1) return;//只有两行
         for(int i=b-1;i>a;i--){
             result.push_back(matrix[i][c]);
         }
         youCanTry(matrix,a+1,b-1,c+1,d-1);
         }
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int m=matrix.size();//行
        int n=matrix[0].size();//列
        youCanTry(matrix,0,m-1,0,n-1);
        return result;
        }
    };
    

    性能:
    执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
    内存消耗:6.8 MB, 在所有 C++ 提交中击败了42.19%的用户
    毕竟用了递归嘛,内存肯定是比纯循环要大的。

    2.官方

    leetcode官方解答<-点击这里
    官方解答很详细了,我总结一些就OK了,官解有两个,一个和我想的差不多,就是循环遍历矩阵,不过官方更厉害一点,他直接就是一个循环,而且代码写的也很漂亮!
    另一个是有点意思的,模拟:
    他规定了四个方向向量(注意,这个顺序也是有讲究的,符合这个螺旋方向的优先级)
    还有一个visited数组,标志是否当前节点被访问过。
    值得细细琢磨。

    class Solution {
    private:
        static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            if (matrix.size() == 0 || matrix[0].size() == 0) {
                return {};
            }
            
            int rows = matrix.size(), columns = matrix[0].size();
            vector<vector<bool>> visited(rows, vector<bool>(columns));
            int total = rows * columns;
            vector<int> order(total);
    
            int row = 0, column = 0;
            int directionIndex = 0;
            for (int i = 0; i < total; i++) {
                order[i] = matrix[row][column];
                visited[row][column] = true;
                int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
                if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {//判断条件:越界,被访问过。
                    directionIndex = (directionIndex + 1) % 4;
                }
                //下一个方向向量一定就是了,如果不放心,可以把if改成while。
                //我试了一下,改成while超出了时间限制,还是if把,因为它之前设的方向向量的顺序,就默认了下一个就是对的。
                row += directions[directionIndex][0];
                column += directions[directionIndex][1];
            }
            return order;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

    总结

    好好学习,天天向上!!!多尝试,多实践!!! CSAPP我来了!!!
    展开全文
  • leecode 221 最大正方形

    2021-05-07 10:08:01
    题目描述 给定一个由0和1组成的2维矩阵,返回该矩阵中最大的由1组成的正方形的面积 示例1 ... 为什么是这三个的最小值? 直观理解,假如取到的最小值0,那么周围无法构成一个完整的正方形,只能由m

    题目描述
    给定一个由0和1组成的2维矩阵,返回该矩阵中最大的由1组成的正方形的面积
    示例1
    输入 [[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]]
    返回值 4

    题解
    f[i][j]:表示以坐标(i,j )为右下角的且只包含1的正方形的最大值。
    f[i][j] = min(f[i-1][j-1],min(f[i][j-1],f[i-1][j]))+1; 为什么是这三个的最小值?

    • 直观理解,假如取到的最小值是0,那么周围无法构成一个完整的正方形,只能由maxtrix[i][j]==‘1’ 这一块构成正方形。
    • 如果取到的最小值 x 大于1,那么f[i][j] 左边一格 上边一格 和 左上 一格取到的 f 值 都是小于等于 x 的,也就是左边 上边 和 左上边 再加上 本身的位置就可以组成一个正方形。所以需要在该位置处加1。
    class Solution {
    public:
        /**
         * 最大正方形
         * @param matrix char字符型vector<vector<>> 
         * @return int整型
         */
        int solve(vector<vector<char> >& matrix) {
            // write code here
            int res=0;
            int n=matrix.size(),m=matrix[0].size();
            vector<vector<int>> f(n+1,vector<int>(m+1));
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(matrix[i-1][j-1]=='1')
                    {
                        f[i][j]=min(f[i-1][j-1],min(f[i][j-1],f[i-1][j]))+1;
                        res=max(res,f[i][j]);
                    }
                }
            }
            return res*res;
        }
    };
    
    展开全文
  • Leecode reverse-integer

    2020-03-08 17:03:59
    如果整数的最后一位是0,那么输出应该是什么?比如10,100 你注意到翻转后的整数可能溢出吗?假设输入是32位整数,则将翻转10000000003就会溢出,你该怎么处理这样的样例?抛出异常?这样做很好,但是如果不允许抛出...
  • 看到这道题可以转换为,求解最长子序列,首先要求最长的子序列,我们看看限制条件是什么,是0的个数,如果在一个子序列中,0的个数恰好是可以翻转的k的个数,那这个子序列就一定是最长子序列的构成部分。 滑动窗口...
  • leecode刷水题

    2019-09-29 16:38:39
    大佬估计对水题没什么兴趣,所以就轮到我刷刷水题,提高一点自信心了:D 1021.有效括号字符串为空 ("")、"(" + A + “)” 或 A + B,其中 A 和 B 都有效的括号字符串,+ 代表字符串的连接。例如,"","()","(())()...
  • Leecode 896.单调数列

    2021-02-28 09:01:52
    896.单调数列前言一、题目是什么?二、分析与代码三、这里是总结 前言 思路还是很简单的,代码写的整洁一点就更好了! 提示:以下是本篇文章正文内容,下面代码可供参考 一、题目是什么? 如果数组是单调递增或...
  • leecode 86. 分隔链表

    2021-04-22 21:05:07
    链表结构一定要分清楚遍历到什么位置了,要得到整个链表的结果需要使用链表头结点,而不是每次遍历的节点。链表经常使用的技巧建立虚拟头结点,为了在写算法过程中便于处理头结点,比如头结点可能会被删除这类...
  • 此题主要要想明白什么是对称的二叉树。 使用递归的方式主要需要注意的对比的方式,分为两步: 1:比较左孩子的左孩子与右孩子的右孩子 2:比较右孩子的左孩子和左孩子的右孩子 /** * Definition for...
  • leecode--动态规划

    2020-12-15 13:32:20
    这个时候有一个非常非常重要的点,就是规定你这个数组元素的含义,例如你的 dp[i] 代表什么意思? 第二步骤: 找出数组元素之间的关系式,我觉得动态规划,还是有一点类似于我们高中学习时的归纳法的,当我们
  • Leecode674 - daily05

    2020-05-13 00:45:34
    人人避暑走如狂, 独有禅师不出房; 非禅房无热到, 为人心静身即凉。 心静自然凉—— 清凉一夏 ...674. 最长连续递增序列 ...给定一个未经排序的整数数组,找到最长且连续的的递增...(PS:可能有同学会有疑问,为什么c.
  • 什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和IList接口 灵活的设置数组的大小 如何使用ArrayList ...
  • 乍一看 (begin + end) / 2 与 begin + (end - begin) /2 没什么区别。 可是 前者可能溢出,后者却不会溢出。 所以尽量用begin + (end - begin) /2. 由此推想, 所有的加法,减法,乘法都可能造成溢出...
  • ** 有序链表转换二叉搜索树 LeeCode109 ** 算法思想: 考虑有序链表转化为二叉搜索树前,可以先考虑有序数组转化为二叉树。有序数组转化为二叉树的关键在于分治思想,对于...什么是快慢指针? 假设两个指针一个为slo...
  • Leecode934.最短的桥

    2020-11-25 21:03:10
    最短的桥 在给定的二维二进制数组 A ...这题的思路是什么呢? 给你两座岛,那么你最应该先区别开两座岛,这里岛都是用1来表示,那我我其中一座岛一定得跟另一座岛区分开来,这样对后面无论怎样都有好处,用深搜找出第一
  • 题意:给予一个字符串从中找出最长回文字串.这里利用了manacher算法,时间复杂度为O(n).测出来一个case通不过,... 算法的第一步就是在每个字符的左右都加上一个#,这样有什么效果呢。比如aba初始化之后为#a#b#a#,字符串
  • 一个很重要的数据结构,在计算机... 函数调用,比如递归为什么调用顺序和返回顺序相反的 在leecode里,有这样一道有意思的题,原题这样的: Given a string containing just the characters '(', ')'...
  • python中的list中有三个参数list[参数1:参数2:参数3] 参数1是起始的索引,默认为0;...函数后面跟着的箭头是函数返回值的类型建议符,用来说明该函数返回的值是什么类型。类型建议符仅仅是建议,并非如c++和j
  • 写完才知道自己学习都是似是而非啊,大家可以也在leecode上提交代码,纯手写,离开eclipse第一种方式:数据结构书上的...其实这里面有个思想迭代和递归的本质相同之处是什么?以后会写一篇我的思考。public class S...
  • 并查集详解+Leecode相关题集(一)

    千次阅读 2020-06-06 18:58:18
    关于什么是并查集以及并查集的详解,强烈推荐看这两篇博文,并查集详解(超级简单有趣~~就学会了)(这一篇讲的很通俗易懂,基本上看一遍就懂了)、LeetCode Union-Find(并查集) 专题(一)(这一篇讲的比较专业,...
  • 一般情况下Java不建议使用链表的 java中需要自定义链表的类来进行链表操作 * public class ListNode { * int val;//节点的值 * ListNode next;//下一个节点 ...Leecode.237.删除链表中的节点 /**...
  • 题目链接:LeeCode213打家劫舍Ⅲ 题目描述: 这题看了Leetcode大神liweiwei1419的题解,感觉自己还是太菜了,其实这题原理跟打家劫舍Ⅰ,Ⅱ一模一样的,但是可能由于我对树的理解有点浅,所以自己写了一个没跑过去...
  • 代码本身并不难,把问题抽象出来很重要...当n=5: 暂时看不出什么规律来。 2. 现在开始看 nums,根据上面结论,nums[0] = 1 我想让nums 尽量短 可以表示的n尽量大;规定length = len(nums) 为 nums 数组的长,max_n 为最
  • 我记得以前做题就经常碰到堆,但是也没有记录过什么,今天决定记录一下堆的应用! 1.找中位数 295. 数据流的中位数 中位数有序列表中间的数。如果列表长度偶数,中位数则中间两个数的平均值。 例如, [2,3,4] ...
  • 编写一个算法来判断一个数是不是“快乐数”。 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能无限...记什么呢?特点+做法...
  • 1 问题 给定一个二叉树,找出其最小深度。...什么是叶子节点,左右孩子都为空的节点才叶子节点! 所以,如果左子树为空,右子树不为空,说明最小深度 1 + 右子树的深度。 反之,右子树为空,左
  • ,但是实际使用的时候却不知道采用什么,比如我自己,就会使用全局变量来模拟线程控制 先分享博客的写作背景的题目: leecode1114按序打印 lecode1115交替打印 按序打印采用了三种种解决方式,都目前我学习到的...

空空如也

空空如也

1 2 3 4
收藏数 76
精华内容 30
关键字:

leecode是什么