提交 64c90759 编写于 作者: 每日一练社区's avatar 每日一练社区

test

上级 c21f8dc1
{
"question_id": 702203,
"question_title": "检查一个3位数是否是水仙花数",
"question_content": "检查一个3位数是否是水仙花数。\n输入:一个数字,比如 371,输出:x是水仙花数,\n如果不是,则输出:x不是水仙花数。\n注:x为输入的数字",
"difficulty": "简单",
"answer_id": 592515,
"answer_content": "\n\n```\n #include <iostream> \nusing namespace std;\nint main()\n{\n\tint a, b, c, y, n = 0;\n\tcout << \"请输入三位数字:\" << endl;\n\tcin >> n;\n\n\ta = n % 1000 / 100; //求第一位数 \n\tb = n % 100 / 10; //求第二位数\n\tc = n % 10 / 1; //求第三位数 \n\ty = a*a*a + b*b*b + c*c*c;\n\tif (y == n) cout << n << \"是水仙花数\" << endl;\n\telse cout << n << \"不是水仙花数\" << endl;\n\t\n\n\tsystem(\"pause\");\n\treturn 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <iostream> \nusing namespace std;\nint main()\n{\n\tint a, b, c, y, n = 0;\n\tcout << \"请输入三位数字:\" << endl;\n\tcin >> n;\n\ta = n % 1000 / 100; \n\tb = n % 100 / 10;\t\n\tc = n % 10 / 1;\t \n\ty = a*a*a + b*b*b + c*c*c;\n\tif (y == n) cout << n << \"是水仙花数\" << endl;\n\telse cout << n << \"不是水仙花数\" << endl;\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469841",
"status": 1,
"keywords": "数学,算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/0.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7437073,
"question_title": "编写递归函数和非递归函数",
"question_content": "<p>编写一个递归函数和一个非递归函数&#xff0c;分别实现求1&#43;2&#43;3&#43;...&#43;n</p>",
"difficulty": "简单",
"answer_id": 53409711,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n//递归方法\nint sum(int n)\n{\n\tif(n &#61;&#61; 1)\n\t\treturn 1;\n\telse\n\t\treturn n &#43; sum(n-1);\n}\n\n//非递归方法\nint sum_2(int n)\n{\n\tint ss &#61; 0;\n\tfor (int i &#61; 1; i &lt;&#61;n;i&#43;&#43;)\n\t{\n\t\tss &#43;&#61; i; \n\t}\n\treturn ss;\n}\n\nint main()\n{\n\tint n;\n\tprintf(&#34;请输入n&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;n);\n\t\n\tint s1 &#61; sum(n);\n\tint s2 &#61; sum_2(n);\n\tprintf(&#34;递归计算&#61;%d;循环计算&#61;%d\\n&#34;,s1,s2);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint sum(int n)\n{\n\tif(n == 1)\n\t\treturn 1;\n\telse\n\t\treturn n + sum(n-1);\n}\nint sum_2(int n)\n{\n\tint ss = 0;\n\tfor (int i = 1; i <=n;i++)\n\t{\n\t\tss += i; \n\t}\n\treturn ss;\n}\nint main()\n{\n\tint n;\n\tprintf(\"请输入n:\");\n\tscanf(\"%d\",&n);\n\tif(n == 0)\n\t\t{\n\t\t\tprintf(\"请输入正整数n\");\n\t\t\treturn -1;\n\t\t}\n\tint s1 = sum(n);\n\tint s2 = sum_2(n);\n\tprintf(\"递归计算=%d;循环计算=%d\\n\",s1,s2);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470242",
"status": 1,
"keywords": "递归,算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/1.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1071974,
"question_title": "输入两个小写英语字母后, 输出两个字母的差",
"question_content": "两个小写字母的差用整数输出。\n但是,输入小写以外的文字就结束程序。\n比较两个字母的部分用diff这个名字的函数书写。执行结果\n请输入两个小写:a b\n字母a和字母b的区别是1。\n请输入两个小写:f b\n字母f和字母b的差异是4。\n请输入两个小写:0 a\n退出程序。",
"difficulty": "简单",
"answer_id": 1274118,
"answer_content": "\n```\n#include <iostream>\nusing namespace std;\nint diff(char ch1, char ch2)\n{\nif (ch1 > ch2) return ch1 - ch2; else return ch2 - ch1;\n}\nint main()\n{\nchar a, b;\nwhile (1)\n{\ncout << \"请输入两个小写:\";\ncin >> a >> b;\nif (a >= 'z' || a <= 'a' || b >= 'z' || b <= 'a') break;\ncout << \"文字\" << a << \"和文字\" << b << \"的差异是\" << diff(a, b) << \"\\n\";\n}\nreturn 0;\n}\n```\n# 问题解决的话,请点下采纳",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint diff(char ch1, char ch2)\n{\n\tif (ch1 > ch2) return ch1 - ch2; else return ch2 - ch1;\n}\nint main()\n{\n\tchar a, b;\n\twhile (1)\n\t{\n\t\tcout << \"请输入两个小写:\";\n\t\tcin >> a >> b;\n\t\tif (a > 'z' || a < 'a' || b > 'z' || b < 'a') break;\n\t\tcout << \"文字\" << a << \"和文字\" << b << \"的差异是\" << diff(a, b) << \"\\n\";\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470248",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/10.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7446447,
"question_title": "判断是否能组成三角形",
"question_content": "<p>根据输入的三角形的三边判断是否能组成三角形&#xff0c;若可以则输出它的周长和三角的类型</p>",
"difficulty": "简单",
"answer_id": 53425759,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;stdlib.h&gt;\n#include&lt;stdio.h&gt;\nint main ( void )\n{\n\tint num1,num2,num3;\n\tprintf(&#34;请输入第一条边&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;num1);\n\tprintf(&#34;请输入第二条边&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;num2);\n\tprintf(&#34;请输入第三条边&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;num3);\n\tif(num1&#43;num2&gt;num3&amp;&amp;num2&#43;num3&gt;num1&amp;&amp;num1&#43;num3&gt;num2)\n\t{\n\t\tif (num1*num1&#43;num2*num2&#61;&#61;num3*num3||num2*num2&#43;num3*num3&#61;&#61;num1*num1||num1*num1&#43;num3*num3&#61;&#61;num2*num2){\n\t\t\tprintf ( &#34;%d、%d和%d可以组成直角三角形。\\n &#34; ,num1,num2,num3);\n\t\t\tprintf (&#34;三角形周长:%d\\n&#34;,num1&#43;num2&#43;num3);\n\t\t}\n\t\t\n\telse if (num1*num1&#43;num2*num2&lt;num3*num3||num2*num2&#43;num3*num3&lt;num1*num1||num1*num1&#43;num3*num3&lt;num2*num2)\n\t\t\t{\n\t\t\t\tprintf (&#34;%d、%d和%d可以组成锐角三角形。\\n&#34; ,num1,num2,num3);\n\t\t\t\tprintf (&#34;三角形周长:%d\\n&#34;,num1&#43;num2&#43;num3);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tprintf (&#34;%d、%d和%d可以组成钝角三角形\\n&#34; ,num1,num2,num3);\n\t\t\t\tprintf (&#34;三角形周长:%d\\n&#34;,num1&#43;num2&#43;num3);\n\t\t\t}\n\t}\n\telse\n\t\tprintf(&#34;%d, %d和%d不能组成三角形。\\n&#34;,num1,num2,num3);\n\tsystem(&#34;PAUSE&#34;);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include<stdlib.h>\n#include<stdio.h>\nint main(void)\n{\n\tint num1,num2,num3;\n\tprintf(\"请输入第一条边:\");\n\tscanf(\"%d\",&num1);\n\tprintf(\"请输入第二条边:\");\n\tscanf(\"%d\",&num2);\n\tprintf(\"请输入第三条边:\");\n\tscanf(\"%d\",&num3);\n\tif(num1+num2>num3&&num2+num3>num1&&num1+num3>num2)\n\t{\n\t\tif (num1*num1+num2*num2==num3*num3||num2*num2+num3*num3==num1*num1||num1*num1+num3*num3==num2*num2){\n\t\t\tprintf ( \"%d、%d和%d可以组成直角三角形。\\n \" ,num1,num2,num3);\n\t\t\tprintf (\"三角形周长:%d\\n\",num1+num2+num3);\n\t\t}\n\telse if (num1*num1+num2*num2<num3*num3||num2*num2+num3*num3<num1*num1||num1*num1+num3*num3<num2*num2)\n\t\t\t{\n\t\t\t\tprintf (\"%d、%d和%d可以组成锐角三角形。\\n\" ,num1,num2,num3);\n\t\t\t\tprintf (\"三角形周长:%d\\n\",num1+num2+num3);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tprintf (\"%d、%d和%d可以组成钝角三角形\\n\" ,num1,num2,num3);\n\t\t\t\tprintf (\"三角形周长:%d\\n\",num1+num2+num3);\n\t\t\t}\n\t}\n\telse\n\t\tprintf(\"%d, %d和%d不能组成三角形。\\n\",num1,num2,num3);\n\tsystem(\"PAUSE\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469958",
"status": 1,
"keywords": "数学,三角形",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/100.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7448172,
"question_title": "统计各类字符个数",
"question_content": "<pre><p>分别统计一个字符串中出现小写字母、大写字母、数字和空格的个数。\n输入格式:\n在一行中输入长度不超过40的字符串。\n输出格式:\n第一行中输出“小写字母&#61;x“ 第二行中输出“大写字母&#61;y“ 第三行中输出“数字&#61;z” 第四行中输出“空格&#61;m” 所有结果均原样输出&#xff0c;没有列宽控制。</p></pre>\n<p>输入样例:</p>\n<code>sd2h b57 sA</code>\n<p>输出样例:</p>\n<pre>\n<code>小写字母&#61;5\n大写字母&#61;1\n数字&#61;3\n空格&#61;2</code></pre>",
"difficulty": "简单",
"answer_id": 53428190,
"answer_content": "<p>对每个字符做判断就可以了。</p>\n\n<pre>\n<code>#include&lt;stdio.h&gt;\nint main()\n{\nint a&#61;0,b&#61;0,c&#61;0,d&#61;0,e&#61;0;\nchar *p,str[80];\np&#61;str;\ngets(str);\nwhile(*p)\nif(*p&gt;&#61;&#39;A&#39; &amp;&amp; *p &lt;&#61;&#39;Z&#39;)\n{a&#43;&#43;;p&#43;&#43;;}\nelse if(*p&gt;&#61;&#39;a&#39; &amp;&amp; *p &lt;&#61;&#39;z&#39;)\n{b&#43;&#43;;p&#43;&#43;;}\nelse if(*p&#61;&#61;&#39; &#39;)\n{c&#43;&#43;;p&#43;&#43;;}\nelse if(*p&gt;&#61;&#39;0&#39; &amp;&amp; *p &lt;&#61;&#39;9&#39;)\n{d&#43;&#43;;p&#43;&#43;;}\nelse\n{e&#43;&#43;;p&#43;&#43;;}\nprintf(&#34;%d %d %d %d %d\\n&#34;,a,b,c,d,e);\nreturn 0;\n}</code></pre>\n\n<p><strong>如有帮助往采纳。点击我回答右上角【采纳】按钮。</strong></p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a=0,b=0,c=0,d=0,e=0;\n\tchar *p,str[80];\n\tp=str;\n\tgets(str);\n\twhile(*p)\n\tif(*p>='A' && *p <='Z')\n\t\t{a++;p++;}\n\telse if(*p>='a' && *p <='z')\n\t\t{b++;p++;}\n\telse if(*p==' ')\n\t\t{c++;p++;}\n\telse if(*p>='0' && *p <='9')\n\t\t{d++;p++;}\n\telse\n\t\t{e++;p++;}\n\tprintf(\"%d %d %d %d %d\\n\",a,b,c,d,e);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470174",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/101.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7433939,
"question_title": "员工工资统计",
"question_content": "<p>一公司有10名员工&#xff0c;每位员工的信息包括&#xff1b;员工编号&#xff0c;姓名&#xff0c;工资&#xff1b;要求编写函数实现:\n1.输入10名员工的信息。\n2.输出平均工资及所有工资高于平均工资的职工的姓名。\n呜呜呜是老师布置的作业&#xff0c;要用c语言编写&#xff0c;可是俺啥也不会&#xff0c;sos&#xff0c;希望能有人帮忙_(:3 ⌒&#xff9e;)_</p>",
"difficulty": "简单",
"answer_id": 53404281,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\nstruct Worker{\n int Number;\n char Name[50] ;\n int Age;\n float Salary;\n}s[10], *p&#61;s;\nint main(int argc, char *argv[]) {\n float sum&#61;0.0;\n for(int i&#61;0;i&lt;3;i&#43;&#43;){\n struct Worker work;\n printf(&#34;请输入第%d个员工信息&#xff1a;\\n&#34;,i&#43;1);\n printf(&#34;工号&#xff1a;&#34;);\n scanf(&#34;%d&#34;,&amp;work.Number); \n printf(&#34;姓名&#xff1a;&#34;);\n scanf(&#34;%s&#34;,&amp;work.Name); \n printf(&#34;年龄&#xff1a;&#34;);\n scanf(&#34;%d&#34;,&amp;work.Age); \n printf(&#34;工资&#xff1a;&#34;);\n scanf(&#34;%f&#34;,&amp;work.Salary); \n sum&#43;&#61;work.Salary;\n printf(&#34;输入完成&#xff01;\\n&#34;);\n p[i]&#61;work;\n }\n printf(&#34;平均工资为&#xff1a;%0.3f\\n&#34;,sum/10);\n printf(&#34;高于平均工资的姓名如下&#xff1a;\\n&#34;);\n for(int j&#61;0;j&lt;10;j&#43;&#43;){\n\t\tif (p[j].Salary&gt;sum/10) {\n\t\t\tprintf(&#34;%s\\n&#34;,p[j].Name); \n\t\t}\n\t }\n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct Worker{\n\t int Number;\n\t char Name[50] ;\n\t int Age;\n\t float Salary;\n}s[10], *p=s;\nint main(int argc, char *argv[]) {\n\tfloat sum=0.0;\n\tfor(int i=0;i<3;i++){\n\t\tstruct Worker work;\n\t\tprintf(\"请输入第%d个员工信息:\\n\",i+1);\n\t\tprintf(\"工号:\");\n\t\tscanf(\"%d\",&work.Number); \n\t\tprintf(\"姓名:\");\n\t\tscanf(\"%s\",&work.Name); \n\t\tprintf(\"年龄:\");\n\t\tscanf(\"%d\",&work.Age); \n\t\tprintf(\"工资:\");\n\t\tscanf(\"%f\",&work.Salary); \n\t\tsum+=work.Salary;\n\t\tprintf(\"输入完成!\\n\");\n\t\tp[i]=work;\n\t}\n\t printf(\"平均工资为:%0.3f\\n\",sum/10);\n\t printf(\"高于平均工资的姓名如下:\\n\");\n\t for(int j=0;j<10;j++){\n\t\tif (p[j].Salary>sum/10) {\n\t\t\tprintf(\"%s\\n\",p[j].Name); \n\t\t}\n\t }\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469858",
"status": 0,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/102.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7462901,
"question_title": "存了多少零钱",
"question_content": "<p style=\"margin-left:.0001pt; margin-right:0\">小林和小树兄弟俩相约存零钱。眼看到年底了&#xff0c;兄弟俩决定算算一共存了多少钱&#xff0c;请帮他们算出来。</p><p style=\"margin-left:.0001pt; margin-right:0\">输入&#xff1a;</p><p style=\"margin-left:.0001pt; margin-right:0\">两行&#xff0c;第一行三个整数分别对应元、角、分&#xff0c;表示小林存的零钱数&#xff1b;</p><p style=\"margin-left:.0001pt; margin-right:0\">第二行三个整数分别对应元、角、分&#xff0c;表示小树存的零钱数。</p><p style=\"margin-left:.0001pt; margin-right:0\">输出&#xff1a;</p><p style=\"margin-left:.0001pt; margin-right:0\">两人存的钱数&#xff08;单位&#xff1a;元&#xff0c;保留2位小数)</p><p style=\"margin-left:.0001pt; margin-right:0\">样例输入&#xff1a;</p><p style=\"margin-left:.0001pt; margin-right:0\">30 5 5</p><p style=\"margin-left:.0001pt; margin-right:0\">45 7 4</p><p style=\"margin-left:.0001pt; margin-right:0\">样例输出&#xff1a;</p><p style=\"margin-left:.0001pt; margin-right:0\">76.29</p>",
"difficulty": "简单",
"answer_id": 53448638,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n\nint main()\n{\n\tint y1,j1,f1,y2,j2,f2;\n\tint y,j,f;\n\tint shiftj &#61; 0;\n\tint shifty &#61; 0;\n\tfloat ss ;\n\tscanf(&#34;%d %d %d&#34;,&amp;y1,&amp;j1,&amp;f1);\n\tscanf(&#34;%d %d %d&#34;,&amp;y2,&amp;j2,&amp;f2);\n\n\t\n\tf &#61; f1 &#43; f2;\n\tif(f&gt;&#61;10)\n\t{\n\t\tf &#61; f-10;\n\t\tshiftj &#61; 1;\n\t}\n\tj &#61; j1 &#43; j2 &#43; shiftj;\n\tif(j &gt;&#61; 10)\n\t{\n\t\tj &#61; j - 10;\n\t\tshifty &#61; 1;\n\t}\n\ty &#61; y1 &#43; y2 &#43; shifty;\n\t\n\tss &#61; y &#43; j/10.0 &#43; f/100.0;\n\tprintf(&#34;%.2f\\n&#34;,ss);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint y1,j1,f1,y2,j2,f2;\n\tint y,j,f;\n\tint shiftj = 0;\n\tint shifty = 0;\n\tfloat ss ;\n\tscanf(\"%d %d %d\",&y1,&j1,&f1);\n\tscanf(\"%d %d %d\",&y2,&j2,&f2);\n\tf = f1 + f2;\n\tif(f>=10)\n\t{\n\t\tf = f-10;\n\t\tshiftj = 1;\n\t}\n\tj = j1 + j2 + shiftj;\n\tif(j >= 10)\n\t{\n\t\tj = j - 10;\n\t\tshifty = 1;\n\t}\n\ty = y1 + y2 + shifty;\n\tss = y + j/10.0 + f/100.0;\n\tprintf(\"%.2f\\n\",ss);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470175",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/103.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7450404,
"question_title": "将字符串2小写字母复制到字符串1",
"question_content": "<p>编写程序,输入字符串s2,将其中所有小写字母复制到字符串数组strl中。例如<br />\naal1bb22cc33de4AA55BB”,生成的strl为&#34;aabbccde&#34;。</p>",
"difficulty": "简单",
"answer_id": 53431030,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\n\n\nint main()\n{\n int sum&#61;0,t&#61;0,i;\n char s[50],s1[50];\n scanf(&#34;%s&#34;,s);\n for(i&#61;0;s[i]!&#61;&#39;\\0&#39;;i&#43;&#43;)\n {\n if(s[i]&gt;&#61;&#39;a&#39;&amp;&amp;s[i]&lt;&#61;&#39;z&#39;){\n s1[t&#43;&#43;]&#61;s[i];\n }\n }\n s1[t]&#61;&#39;\\0&#39;;\n printf(&#34;%s&#34;,s1);\n}\n</code></pre>\n\n<p><img alt=\"\" height=\"263\" src=\"https://img-ask.csdnimg.cn/upload/1623808202693.png\" width=\"505\" /> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint sum=0,t=0,i;\n\tchar s[50],s1[50];\n\tscanf(\"%s\",s);\n\tfor(i=0;s[i]!='\\0';i++)\n\t{\n\t\tif(s[i]>='a'&&s[i]<='z'){\n\t\t\ts1[t++]=s[i];\n\t\t}\n\t}\n\ts1[t]='\\0';\n\tprintf(\"%s\",s1);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469859",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/104.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1058389,
"question_title": "秒数转换",
"question_content": "输入一个秒数,转换成HH:MM:SS的格式输出。\n输入样例 \n365\n输出样例 \n00:06:05",
"difficulty": "简单",
"answer_id": 1255698,
"answer_content": "\n```\n#include<cstdio>\nint n;\nvoid print(int x){\n\tif(x==0)printf(\"00\");\n\telse if(x<10)printf(\"0%d\",x);\n\telse printf(\"%d\",x);\n\treturn;\n}\nint main(){\n\tscanf(\"%d\",&n);\n\tint s,f,m;\n\ts=n/3600;\n\tf=n/60%60;\n\tm=n%60;\n\tprint(s);\n\tprintf(\":\");\n\tprint(f);\n\tprintf(\":\");\n\tprint(m);\n\treturn 0;\n}\n```\n如果还没有学过函数的话,可以这么做\n\n```\n#include<cstdio>\nint n;\nint main(){\n\tscanf(\"%d\",&n);\n\tprintf(\"%d%d:%d%d:%d%d\",n/3600/10,n/3600%10,n/60%60/10,n/60%10,n%60/10,n%10);\n\treturn 0;\n}\n```",
"tag_name": "c++",
"cpp": "#include<cstdio>\nint n;\nvoid print(int x){\n\tif(x==0)printf(\"00\");\n\telse if(x<10)printf(\"0%d\",x);\n\telse printf(\"%d\",x);\n\treturn;\n}\nint main(){\n\tscanf(\"%d\",&n);\n\tint s,f,m;\n\ts=n/3600;\n\tf=n/60%60;\n\tm=n%60;\n\tprint(s);\n\tprintf(\":\");\n\tprint(f);\n\tprintf(\":\");\n\tprint(m);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470176",
"status": 1,
"keywords": "时间转换",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/105.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1056814,
"question_title": "寻找孪生素数",
"question_content": "数学家希尔伯特在1900年国际数学家大会的报告上提出一个“孪生素数猜想”,即: 存在无穷多个素数p,使得p + 2是素数。p和p+2这一对差为2的素数,被称为“孪生素数”。\n看起来,这个猜想是成立的,我们总能找到很多对孪生素数,例如:3和5,5和7,11和13…… 这一猜想至今还未被证明。\n现在,对于给定的整数n, 请寻找大于n的最小的一对孪生素数p和q(q=p+2)。",
"difficulty": "困难",
"answer_id": 1253864,
"answer_content": "你的sushu判断不对,比如说121,1,这些都不是素数,但是你都返回真了。\n```\n#include <cmath>\n#include <iostream>\n\nusing namespace std;\nint sushu(int x)\n{\nif (x <= 1) return 0;\nint i,j=1;\nfor(i=2;i<=sqrt(x);i++)\n{\nif(x%i==0){j=0;break;}\n}\nreturn j;\n}\nint main()\n{\nint x;\ncin>>x;\nint i=x+1;\nfor(;;i++)\n{\nif (sushu(i)&&sushu(i+2))\nbreak;\n}\ncout<<i<<' '<<i+2;\nreturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include <cmath>\n#include <iostream>\nusing namespace std;\nint sushu(int x)\n{\n\tif (x <= 1) return 0;\n\tint i,j=1;\n\tfor(i=2;i<=sqrt(x);i++)\n\t{\n\t\tif(x%i==0){j=0;break;}\n\t}\n\treturn j;\n}\nint main()\n{\n\tint x;\n\tcin>>x;\n\tint i=x+1;\n\tfor(;;i++)\n\t{\n\t\tif (sushu(i)&&sushu(i+2))\n\t\t\tbreak;\n\t}\n\tcout<<i<<' '<<i+2;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469959",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/106.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1001511,
"question_title": "采用插入排序,按照字符顺序从小到大进行排序",
"question_content": "【问题描述】\n编写一个程序,从键盘接收一个字符串(长度不超过20),采用插入排序,按照字符顺序从小到大进行排序,最后输出排序后的字符串。\n【输入形式】\n输入一行字符串,长度不超过20。\n【输出形式】\n输出排序后的字符串。\n【样例输入】\nH2e3L*Lo,Wor#Ld.\n【样例输出】\n#*,.23HLLLWdeoor",
"difficulty": "简单",
"answer_id": 1179762,
"answer_content": "# 问题解决的话,请点下采纳,以及之前的问题 https://ask.csdn.net/questions/1001509 也点下采纳,谢谢\n\n之前看错了下面是插入排序\n\n```\n#include <stdio.h>\n#include <string.h>\nint main()\n{\n char a[21];\n scanf(\"%s\",a);\n int t,j=0,i=0;\n int n = strlen(a);\n for(j=1;j < n; j++){\n for(i=0;i<j;i++) {\n if (a[i] > a[j]) break;\n }\n int t=a[j];\n int t1;\n for(;i<=j;i++){\n\t\t\tt1=a[i];\n\t\t\ta[i]=t;\n\t\t\tt=t1;\n }\n }\n printf(\"%s\",a);\n return 0;\n}\n\n```\nhttps://ideone.com/QL7g22 在线调试通过\n\n\n下面是选择排序\n```\n#include <stdio.h>\n#include <string.h>\nint main()\n{\n\tchar a[21];\n\tscanf(\"%s\",a);\n\tint t,j=0,i=0;\n\tint n = strlen(a);\n\tfor(j=0;j < n - 1; j++){\n\t\tint min = j;\n\t\tfor(i=j+1;i < n; i++) {\n\t\t\tif (a[i] < a[min]) min=i;\n\t\t}\n\t\tif (min != j) {\n\t\t\tt=a[min];\n\t\t\ta[min]=a[j];\n\t\t\ta[j]=t;\n\t\t}\n\t}\n\tprintf(\"%s\",a);\n\treturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <string.h>\nint main()\n{\n\tchar a[21];\n\tscanf(\"%s\",a);\n\tint t,j=0,i=0;\n\tint n = strlen(a);\n\tfor(j=1;j < n; j++){\n\t\tfor(i=0;i<j;i++) {\n\t\t\tif (a[i] > a[j]) break;\n\t\t}\n\t\tint t=a[j];\n\t\tint t1;\n\t\tfor(;i<=j;i++){\n\t\t\tt1=a[i];\n\t\t\ta[i]=t;\n\t\t\tt=t1;\n\t\t}\n\t}\n\tprintf(\"%s\",a);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470272",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/107.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 720288,
"question_title": "找x",
"question_content": "题目描述\n输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。\n输入\n测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。\n输出\n对于每组输入,请输出结果。\n样例输入\n4\n1 2 3 4\n3\n样例输出\n2\n",
"difficulty": "困难",
"answer_id": 714710,
"answer_content": "折半查找是不行的,折半查找有一个前提那就是:数组的数必须有序,不然效率低的可怜。下面我写的,比较简单,什么也没做处理,你要的功能基本实现,根据这个改下吧\n\n#include <iostream>\n\nusing namespace std;\n\nint main() {\n\tint n = 0;\n\tcin >> n;\n\n\tint* ptr = new(nothrow) int[n];\n\tfor (auto i = 0; i < n; i++) {\n\t\tcin >> ptr[i];\n\t}\n\n\tint x = 0;\n\tcin >> x;\n\n\tauto j = 0;\n\tauto status = 0;\n\tfor (; j < n; ++j) {\n\t\tif (ptr[j] == x) {\n\t\t\tstatus = 1;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (status == 0) {\n\t\tj = -1;\n\t}\n\tcout << j << endl;\n\t\n\tdelete[] ptr;\n\n\tcin.get();\n\tcin.get();\n\n\treturn 0;\n}\n```\n\n```",
"tag_name": "c语言",
"cpp": "#include <iostream>\nusing namespace std;\nint main() {\n\tint n = 0;\n\tcin >> n;\n\tint* ptr = new(nothrow) int[n];\n\tfor (auto i = 0; i < n; i++) {\n\t\tcin >> ptr[i];\n\t}\n\tint x = 0;\n\tcin >> x;\n\tauto j = 0;\n\tauto status = 0;\n\tfor (; j < n; ++j) {\n\t\tif (ptr[j] == x) {\n\t\t\tstatus = 1;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (status == 0) {\n\t\tj = -1;\n\t}\n\tcout << j << endl;\n\tdelete[] ptr;\n\tcin.get();\n\tcin.get();\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470177",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,最大子数组问题",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/108.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7411209,
"question_title": "阶乘和数",
"question_content": "一个正整数如果等于组成它的各位数字的阶乘之和,该整数称为阶乘和数。\n例如,145=1!+4!+5!,则145是一个三位阶乘和数。\n请输出所有阶乘数(不会超过十万)",
"difficulty": "困难",
"answer_id": 53361950,
"answer_content": "<p>循环太多&#xff0c;系统卡死</p>\n\n<p>#include &lt;stdio.h&gt;</p>\n\n<p>int b[10] &#61; {0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};</p>\n\n<p>int main(){<!-- --><br />\n    int x, sum &#61; 0, a;<br />\n    for(int j &#61; 1; j &lt;&#61; 100000; j&#43;&#43;){<!-- --><br />\n        sum &#61; 0; <br />\n        a &#61; j;<br />\n        while(j !&#61; 0){<!-- --><br />\n            x &#61; j % 10;<br />\n            j /&#61; 10;<br />\n            sum &#43;&#61; b[x];<br />\n        }<br />\n        j &#61; a;<br />\n        if(sum &#61;&#61; a){<!-- --><br />\n            printf(&#34;%d &#34;, j);<br />\n        }<br />\n    }<br />\n    return 0;<br />\n} </p>",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint b[10] = {0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};\nint main(){\n    int x, sum = 0, a;\n    for(int j = 1; j <= 100000; j++){\n        sum = 0; \n        a = j;\n        while(j != 0){\n            x = j % 10;\n            j /= 10;\n            sum += b[x];\n        }\n        j = a;\n        if(sum == a){\n            printf(\"%d \", j);\n        }\n    }\n    return 0;\n} ",
"topic_link": "https://bbs.csdn.net/topics/600469860",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/109.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 6722211,
"question_title": "怎么求尾数与常数之和?",
"question_content": "<p>数列的前3项都为1&#xff0c;从第4项开始&#xff0c;每项都是其前3项的和&#xff1a;1, 1, 1, 3, 5, 9, 17, … 请你编程求出数列第N项的<strong>4位尾数</strong>与90000之和。输入一个正整数N&#xff0c;输出所求的和。</p>",
"difficulty": "简单",
"answer_id": 47620887,
"answer_content": "<p>#include&lt;iostream&gt;</p>\n\n<p>#include&lt;cstring&gt;</p>\n\n<p>#include&lt;cstdio&gt;</p>\n\n<p>#include&lt;cstdlib&gt;</p>\n\n<p>#include&lt;cmath&gt;</p>\n\n<p>#include&lt;iomanip&gt;</p>\n\n<p>#include&lt;algorithm&gt;</p>\n\n<p>#include&lt;set&gt;</p>\n\n<p>using namespace std;</p>\n\n<p>#define NUM 10000//符号常量</p>\n\n<p>long long dp[1000000];</p>\n\n<p>int main(){<!-- --></p>\n\n<p>    //freopen(&#34;E:\\\\VSCode_C&#43;&#43;\\\\test.txt&#34;,&#34;r&#34;,stdin);</p>\n\n<p>    int n;</p>\n\n<p>    cin&gt;&gt;n;</p>\n\n<p>    dp[1]&#61;1;</p>\n\n<p>    dp[2]&#61;1;</p>\n\n<p>    dp[3]&#61;1;</p>\n\n<p>    for(int i&#61;4;i&lt;&#61;n;i&#43;&#43;){<!-- --></p>\n\n<p>        dp[i]&#61;dp[i-1]&#43;dp[i-2]&#43;dp[i-3];</p>\n\n<p>    }</p>\n\n<p>    cout&lt;&lt;dp[n]&lt;&lt;endl;</p>\n\n<p>    long long x&#61;dp[n]%10000&#43;9000;</p>\n\n<p>    cout&lt;&lt;x;</p>\n\n<p>    system(&#34;pause&#34;);</p>\n\n<p>    return 0;</p>\n\n<p>}</p>",
"tag_name": "c语言",
"cpp": "#include <iostream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <iomanip>\n#include <algorithm>\n#include <set>\nusing namespace std;\n#define NUM 10000\nlong long dp[1000000];\nint main(){\n    int n;\n    cin>>n;\n    dp[1]=1;\n    dp[2]=1;\n    dp[3]=1;\n    for(int i=4;i<=n;i++){\n        dp[i]=dp[i-1]+dp[i-2]+dp[i-3];\n    }\n    cout<<dp[n]<<endl;\n    long long x=dp[n]%NUM+90000;\n    cout<<x;\n    system(\"pause\");\n    return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470143",
"status": 1,
"keywords": "数列",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/11.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7424992,
"question_title": "蓝桥杯Candy Store",
"question_content": "<p>问题描述\n经营一家糖果店是非常困难的&#xff0c;你需要优化各种各样的东西。最近你在销售一种非常时髦的糖果&#xff0c;叫做Whizboppers。这种糖果变质非常迅速&#xff0c;所以&#xff1a;<br />\n  ·你必须每天早上从供应商买来新的Whizboppers。<br />\n  ·你必须用当天早上从供应商买来的盒子装着糖果出售。<br />\n  你可以从你的供应商处买来装有任意整数克糖果的盒子。<br />\n  每天有至多k位顾客到你的店里来。从第1个人开始&#xff0c;每个人会选择花费整数分的钱来买Whizboppers&#xff0c;钱数在1分到C分之间&#xff08;包含1分和C分&#xff09;。你打算以1分钱每克的价格出售&#xff1b;所以如果一个人想要花4分钱&#xff0c;你会给他恰好4克糖果。你可以给他1个4克的盒子&#xff0c;也可能是1个2克的盒子和2个1克的盒子。<br />\n  你最少需要买几个盒子才能保证&#xff0c;不管每个人想花多少钱买糖&#xff0c;你总是可以给他们对应质量的糖果&#xff1f;\n<br />  注意&#xff1a;当一个人选择自己想买多少糖果后&#xff0c;你知道之前的人已经买了多少糖&#xff0c;但不能预知之后的人打算买多少糖。<br />\n  举个例子&#xff0c;如果每天至多有2位顾客到你的店里&#xff0c;每个人至多花2分钱(k&#61;2,C&#61;2)&#xff0c;你可以从你的供应商买4个1克的盒子。但是你可以做的更好&#xff1a;只要买2个1克的盒子和1个2克的盒子&#xff0c;就可以满足你的顾客。如下所示&#xff1a;<br />\n第一个人给第一个人的盒子第二个人给第二个人的盒子2分1 个 2克2分<br />\n1分2 个 1克<br />\n1 个 1克1分1 个 1克2分<br />\n1分1 个 2克<br />\n1 个 1克<br />  不论第一个人怎么买&#xff0c;你都可以给他对应质量的盒子&#xff0c;同时保证第二个人也能拿到正确质量的糖果。所以对于k&#61;2,C&#61;2&#xff0c;你用3个盒子就可以满足任意的顾客需求。\n输入格式\n  第一行一个整数T&#xff0c;表示询问数量。\n  接下来T行&#xff0c;每行包含两个整数k和C&#xff0c;分别表示最大人数和每个人花费的最多钱数。\n输出格式\n  对于每一个询问&#xff0c;输出一行包含&#34;Case #x: y&#34;&#xff0c;x是询问编号&#xff08;从1开始标号&#xff09;&#xff0c;y是你每天最少需要的盒子数量。</p>",
"difficulty": "困难",
"answer_id": 53386266,
"answer_content": "<p>帮你梳理一下思路&#xff0c;这个问题有两个关键点&#xff1a;</p>\n\n<p>1、需要的盒子的容量必须大于等于顾客的最大购买量。</p>\n\n<p>2、不论哪种情况&#xff0c;永远从2个1克的盒子开始计算。</p>\n\n<p><strong>例如&#xff0c;样例2 2&#xff0c;</strong>即每天至多有2位顾客到你的店里&#xff0c;每个人至多花2分钱&#xff0c;此时&#xff1a;</p>\n\n<p>1&#xff09;最大购买数量为&#xff1a;2*2&#61;4g</p>\n\n<p>2&#xff09;先给定两个1克的盒子&#xff1a;1g*2&#xff0c;容量&#xff1a;2g</p>\n\n<p>如果每人购买2g&#xff0c;则总共需要可装4g的盒子&#xff0c;此时的问题是&#xff1a;如何确定下一个盒子&#xff1f;</p>\n\n<p>当然&#xff0c;题目已经告诉我们了还需要1个2g的盒子&#xff0c;那么这个1个如何确定的呢&#xff1f;</p>\n\n<p>确定方法&#xff1a;(4-2)/2&#61;1。</p>\n\n<p>至此&#xff0c;容量为4g&#xff0c;满足最大购买数量&#xff0c;需要3个盒子&#xff0c;规格为&#xff1a;1g*2,2g*1。</p>\n\n<p>当然也有除不尽的情况&#xff0c;方法是向上取整。</p>\n\n<p><strong>例如&#xff0c;样例&#xff1a;1 5&#xff0c;</strong>即每天至多有1位顾客到店&#xff0c;至多花费5分钱&#xff0c;此时&#xff1a;</p>\n\n<p>1&#xff09;最大购买数量为&#xff1a;1*5&#61;5g</p>\n\n<p>2&#xff09;先给定两个1克的盒子&#xff1a;1g*2&#xff0c;容量&#xff1a;2g</p>\n\n<p>如果这个顾客购买3g&#xff0c;此时的问题是&#xff1a;如何确定下一个盒子&#xff1f;</p>\n\n<p>确定方法&#xff1a;(3-2)/3&#61;0.333&#xff0c;向上取整为1&#xff0c;所以还需要1个3g的盒子。</p>\n\n<p>至此&#xff0c;容量为5g&#xff0c;满足最大购买数量&#xff0c;需要3个盒子&#xff0c;规格为&#xff1a;1g*2,3g*1。</p>\n\n<p><strong>再比如&#xff0c;样例2 50&#xff0c;</strong>即每天至多有2位顾客到店&#xff0c;至多花费50分钱&#xff0c;此时&#xff1a;</p>\n\n<p>1&#xff09;最大购买数量为&#xff1a;2*50&#61;100g</p>\n\n<p>2&#xff09;先给定两个1克的盒子&#xff1a;1g*2&#xff0c;容量&#xff1a;2g</p>\n\n<p>如果每人购买2g&#xff0c;则还需要1个2g的盒子&#xff08;前面已给出&#xff09;&#xff0c;此时容量为4g</p>\n\n<p>如果每人购买3g&#xff0c;则总共需要可装6g的盒子&#xff0c;此时的问题依然是&#xff1a;如何确定下一个盒子&#xff1f;</p>\n\n<p>确定方法&#xff1a;(6-4)/3&#61;0.67&#xff0c;向上取整为1&#xff0c;所以还需要1个3g的盒子&#xff0c;此时容量为7g</p>\n\n<p>如果每人购买4g&#xff0c;则需要可装8g的盒子&#xff0c;此时的问题依然是&#xff1a;如何确定下一个盒子&#xff1f;</p>\n\n<p>确定方法&#xff1a;(8-7)/4&#61;0.25&#xff0c;向上取整为1&#xff0c;所以还需要1个4g的盒子&#xff0c;此时容量为11g</p>\n\n<p>如果每人购买6g&#xff0c;则需要可装12g的盒子&#xff0c;此时的问题依然是&#xff1a;如何确定下一个盒子&#xff1f;</p>\n\n<p>确定方法&#xff1a;(12-11)/6&#61;1/6&#xff0c;向上取整为1&#xff0c;所以还需要1个6g的盒子&#xff0c;此时容量为17g</p>\n\n<p>为什么是&#xff1a;如果每人购买6g&#xff1f;因为每次“如果”&#xff0c;都要使需要的容量超过了前一次的容量。</p>\n\n<p>......</p>\n\n<p>以此类推&#xff0c;直到容量大于等于100g&#xff0c;最终发现需要的盒子数量是11个&#xff0c;规格为&#xff1a;1g*2,2g*1,3g*1,4g*1,6g*1,......</p>\n\n<p>这就是这个问题的基本思路。</p>\n\n<p><strong>代码可以参考&#xff1a;</strong></p>\n\n<pre>\n<code class=\"language-cpp\">#include&lt;iostream&gt;\n#include&lt;cmath&gt;\nusing namespace std;\n\nint main()\n{\n\tint n, k, count &#61; 1;\n\tlong long c;\n\tcin &gt;&gt; n;\n\twhile (n--)\n\t{\n\t\tcin &gt;&gt; k &gt;&gt; c;\n\t\tint num &#61; k; \n\t\tlong long sum &#61; k;\n\t\tfor (long long i &#61; 2; i &lt;&#61; c; i &#61; sum / k &#43; 1)\n\t\t{\n\t\t\tint t &#61; num;\n\t\t\tnum &#43;&#61; ceil((k * i - sum) * 1.0 / i);\n\t\t\tsum &#43;&#61; i * (num - t);\n\t\t}\n\t\tcout &lt;&lt; &#34;Case #&#34; &lt;&lt; count&#43;&#43; &lt;&lt;&#34;: &#34; &lt;&lt; num &lt;&lt; endl;\n\t}\n\treturn 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<iostream>\n#include<cmath>\nusing namespace std;\nint main()\n{\n\tint n, k, count = 1;\n\tlong long c;\n\tcin >> n;\n\twhile (n--)\n\t{\n\t\tcin >> k >> c;\n\t\tint num = k; \n\t\tlong long sum = k;\n\t\tfor (long long i = 2; i <= c; i = sum / k + 1)\n\t\t{\n\t\t\tint t = num;\n\t\t\tnum += ceil((k * i - sum) * 1.0 / i);\n\t\t\tsum += i * (num - t);\n\t\t}\n\t\tcout << \"Case #\" << count++ <<\": \" << num << endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470178",
"status": 1,
"keywords": "算法中阶,贪心算法,活动选择问题,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/110.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 237159,
"question_title": "按字典顺序排列问题",
"question_content": "输入若干英文单词,将每个单词的首字母转换成大写字母,其他字母为小写,并按字典顺序排列",
"difficulty": "中等",
"answer_id": 216930,
"answer_content": "\n\n```\n #include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint cmp(const void * a, const void * b)\n{\n\treturn strcmp(*(char **)a, *(char **)b);\n}\n\nint main(int argc, char* argv[])\n{\n\tint n = 0;\n\tint i;\n\tprintf(\"how many words?\\n\");\n\tscanf(\"%d\", &n);\n\tchar ** s = new char *[n];\n\tfor (i = 0; i < n; i++)\n\t{\n\t\ts[i] = new char[100];\n\t\tscanf(\"%s\", s[i]);\n\t\tchar * t = s[i];\n\t\twhile (*t != '\\0')\n\t\t{\n\t\t\tif (t == s[i] && (*t >= 'a' && *t <= 'z')) *t = *t - 'a' + 'A';\n\t\t\tif (t > s[i] && (*t >= 'A' && *t <= 'Z')) *t = *t - 'A' + 'a';\n\t\t\tt++;\n\t\t}\n\t}\n\tqsort(s, n, sizeof(char *), cmp);\n\tfor (i = 0; i < n; i++)\n\t{\n\t\tprintf(\"%s\\n\", s[i]);\n\t}\n\treturn 0;\n}\n```\n\n\n\n```\n how many words?\n5\nwORd\nHellO\nyEllow\nshe\nAPPLE\nApple\nHello\nShe\nWord\nYellow\nPress any key to continue\n```\n\n",
"tag_name": "c语言",
"cpp": " #include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nint cmp(const void * a, const void * b)\n{\n\treturn strcmp(*(char **)a, *(char **)b);\n}\nint main(int argc, char* argv[])\n{\n\tint n = 0;\n\tint i;\n\tprintf(\"how many words?\\n\");\n\tscanf(\"%d\", &n);\n\tchar ** s = new char *[n];\n\tfor (i = 0; i < n; i++)\n\t{\n\t\ts[i] = new char[100];\n\t\tscanf(\"%s\", s[i]);\n\t\tchar * t = s[i];\n\t\twhile (*t != '\\0')\n\t\t{\n\t\t\tif (t == s[i] && (*t >= 'a' && *t <= 'z')) *t = *t - 'a' + 'A';\n\t\t\tif (t > s[i] && (*t >= 'A' && *t <= 'Z')) *t = *t - 'A' + 'a';\n\t\t\tt++;\n\t\t}\n\t}\n\tqsort(s, n, sizeof(char *), cmp);\n\tfor (i = 0; i < n; i++)\n\t{\n\t\tprintf(\"%s\\n\", s[i]);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470273",
"status": 1,
"keywords": "算法,排序",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/111.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7459909,
"question_title": "完数",
"question_content": "<pre>一个数如果恰好等于它的所有因子之和&#xff0c;这个数就称为“完数”。统计自然数 1 — 100 间完数的个数。</pre>",
"difficulty": "中等",
"answer_id": 53444950,
"answer_content": "<p>供参考&#xff1a;</p>\n\n<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n\nint perfect()\n{\n int i,x,sum,cnt&#61;0;\n for(i&#61;1;i&lt;&#61;100;i&#43;&#43;)\n {\n sum&#61;0;\n for(x&#61;1;x&lt;i;x&#43;&#43;)\n {\n if(i%x&#61;&#61;0)sum&#43;&#61;x;\n }\n if(i&#61;&#61;sum)\n {\n cnt&#43;&#43;;\n printf(&#34;%d &#34;,i);\n }\n }\n return cnt;\n}\n\nint main()\n{\n printf(&#34;\\ncount&#61;%d\\n&#34;,perfect());\n \n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint perfect()\n{\n\tint i,x,sum,cnt=0;\n\tfor(i=1;i<=100;i++)\n\t{\n\t\tsum=0;\n\t\tfor(x=1;x<i;x++)\n\t\t{\n\t\t\tif(i%x==0)sum+=x;\n\t\t}\n\t\tif(i==sum)\n\t\t{\n\t\t\tcnt++;\n\t\t\tprintf(\"%d \",i);\n\t\t}\n\t}\n\treturn cnt;\n}\nint main()\n{\n\tprintf(\"\\ncount=%d\\n\",perfect());\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469960",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,证明主定理,对b的幂证明主定理",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/112.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7451531,
"question_title": "写一个函数,对任意一维数组进行降序排序",
"question_content": "<p>在主函数中随机生成一有n个元素的一维数组(元素的取值范围载10-90之间),调用排序函数对该数组进行排序,并输出排序结果。</p>",
"difficulty": "困难",
"answer_id": 53432910,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n#include &lt;random&gt;\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;time.h&gt;\n\nusing namespace std;\n\nvoid InsertSort(int a[], int n)\n{\n\tfor (int i &#61; 1; i&lt;n; i&#43;&#43;) {\n\t\tif (a[i] &lt; a[i - 1]) { //若第i个元素大于i-1元素&#xff0c;直接插入。小于的话&#xff0c;移动有序表后插入 \n\t\t\tint j &#61; i - 1;\n\t\t\tint x &#61; a[i]; //复制为哨兵&#xff0c;即存储待排序元素 \n\t\t\ta[i] &#61; a[i - 1]; //先后移一个元素 \n\t\t\twhile (x &lt; a[j]) { //查找在有序表的插入位置 \n\t\t\t\ta[j &#43; 1] &#61; a[j];\n\t\t\t\tj--; //元素后移 \n\t\t\t}\n\t\t\ta[j &#43; 1] &#61; x; //插入到正确位置 \n\t\t}\n\t}\n}\n\nint main()\n{\n\tcout &lt;&lt; &#34;请输入产生多个随机数&#xff1a;&#34;;\n\tint ranNum[100],num &#61; 0;\n\tsrand((unsigned)time(NULL));//time()用系统时间初始化种。为rand()生成不同的随机种子\n\tcin &gt;&gt; num;\n\tcout &lt;&lt; &#34;随机数组&#xff1a;&#34;;\n\tfor (int i &#61; 0; i &lt; num; i&#43;&#43;)\n\t{\n\t\tranNum[i] &#61; 10 &#43; (rand() % 80);\n\t\tcout &lt;&lt; &#34; &#34; &lt;&lt; ranNum[i];\n\t}\n\tcout &lt;&lt; endl;\n\tInsertSort(ranNum, num);\n\tcout &lt;&lt; &#34;排序后整数序列&#xff1a;&#34;;\n\tfor (int j &#61; num-1; j &gt;&#61; 0; j--) {\n\t\tcout &lt;&lt; ranNum[j] &lt;&lt; &#34; &#34;;\n\t}\n\tcout &lt;&lt; endl;\n\tsystem(&#34;pause&#34;);\n\treturn 0;\n}</code></pre>\n\n<p><img alt=\"\" height=\"195\" src=\"https://img-ask.csdnimg.cn/upload/1623911523832.png\" width=\"1149\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <random>\n#include <iostream>\n#include <string>\n#include <time.h>\nusing namespace std;\nvoid InsertSort(int a[], int n)\n{\n\tfor (int i = 1; i<n; i++) {\n\t\tif (a[i] < a[i - 1]) {\t\t\t \n\t\t\tint j = i - 1;\n\t\t\tint x = a[i];\t\t\n\t\t\ta[i] = a[i - 1];\t\t \n\t\t\twhile (x < a[j]) { \n\t\t\t\ta[j + 1] = a[j];\n\t\t\t\tj--;\t\t \n\t\t\t}\n\t\t\ta[j + 1] = x;\t \n\t\t}\n\t}\n}\nint main()\n{\n\tcout << \"请输入产生多个随机数:\";\n\tint ranNum[100],num = 0;\n\tsrand((unsigned)time(NULL));\n\tcin >> num;\n\tcout << \"随机数组:\";\n\tfor (int i = 0; i < num; i++)\n\t{\n\t\tranNum[i] = 10 + (rand() % 80);\n\t\tcout << \" \" << ranNum[i];\n\t}\n\tcout << endl;\n\tInsertSort(ranNum, num);\n\tcout << \"排序后整数序列:\";\n\tfor (int j = num-1; j >= 0; j--) {\n\t\tcout << ranNum[j] << \" \";\n\t}\n\tcout << endl;\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469961",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/113.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7407528,
"question_title": "实现五则运算",
"question_content": "<pre><p>设计一个可以完成任意五则运算&#xff08;加法/减法/乘法/除法/取余&#xff09;的程序。除法按照计算机中<strong>整型相除</strong>来计算。\n输入格式\n多行输入&#xff0c;每输入一行数据对应输出一行。\n每行输入格式为 a # b&#xff0c;其中 &#xff03;∈{&#43;,−,∗,/,%}\na,b均为自然数\n输出格式\n每行输出对应的计算结果&#xff1b;\n当运算为除法/取余的时候&#xff0c;如果除数为 0 &#xff0c;输出 <code>WA</code> 。</pre>\n<p>输入样例</p>\n<pre>\n<code>2&#43;2\n4*5\n6/7\n4%3\n4%0</code></pre>\n<p>输出样例</p>\n<pre>\n<code>4\n20\n0\n1\nWA</code></pre>",
"difficulty": "简单",
"answer_id": 53357554,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n\nint main()\n{\n\tint a;\n\tint b;\n\tchar operation;\n\tint num;\n\n\twhile (scanf(&#34;%d&#34;, &amp;a) !&#61; EOF)\n\t{\n\t\tscanf(&#34;%c&#34;, &amp;operation);\n\t\tscanf(&#34;%d&#34;, &amp;b);\n\n\t\tif (operation &#61;&#61; &#39;&#43;&#39;)\n\t\t{\n\t\t\tnum &#61; a &#43; b;\n\t\t\tprintf(&#34;%d\\n&#34;, num);\n\t\t}\n\t\telse if (operation &#61;&#61; &#39;-&#39;) {\n\t\t\tnum &#61; a - b;\n\t\t\tprintf(&#34;%d\\n&#34;, num);\n\t\t}\n\t\telse if (operation &#61;&#61; &#39;*&#39;) {\n\t\t\tnum &#61; a * b;\n\t\t\tprintf(&#34;%d\\n&#34;, num);\n\t\t}\n\t\telse if (operation &#61;&#61; &#39;/&#39; &amp;&amp; b !&#61; 0) {\n\t\t\tnum &#61; a / b;\n\t\t\tprintf(&#34;%d\\n&#34;, num);\n\t\t}\n\t\telse {\n\t\t\tprintf(&#34;%s\\n&#34;, &#34;WA&#34;);\n\t\t}\n\t}\n\treturn 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a;\n\tint b;\n\tchar operation;\n\tint num;\n\twhile (scanf(\"%d\", &a) != EOF)\n\t{\n\t\tscanf(\"%c\", &operation);\n\t\tscanf(\"%d\", &b);\n\t\tif (operation == '+')\n\t\t{\n\t\t\tnum = a + b;\n\t\t\tprintf(\"%d\\n\", num);\n\t\t}\n\t\telse if (operation == '-') {\n\t\t\tnum = a - b;\n\t\t\tprintf(\"%d\\n\", num);\n\t\t}\n\t\telse if (operation == '*') {\n\t\t\tnum = a * b;\n\t\t\tprintf(\"%d\\n\", num);\n\t\t}\n\t\telse if (operation == '/' && b != 0) {\n\t\t\tnum = a / b;\n\t\t\tprintf(\"%d\\n\", num);\n\t\t}\n\t\telse {\n\t\t\tprintf(\"%s\\n\", \"WA\");\n\t\t}\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470274",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编,确定任意一对线段是否相交",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/114.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7442914,
"question_title": "最小花费",
"question_content": "<p>题目描述</p><p style=\"margin-left:0; margin-right:0\">乐乐做好了一二三等奖奖品费用预算方案之后&#xff0c;决定到市场上具体咨询一下三种奖品的具体价格&#xff0c;发现各个商家的报价还是有差别的&#xff0c;乐乐决定哪家所有奖品总金额最少就在哪家购买&#xff0c;假设每家货源是充足的。乐乐咨询了n个店家&#xff0c;几乎跑遍了整个市场&#xff0c;才得到各个商家的最低报价清单。现在请你计算出乐乐会在哪家商家购买奖品以及费用是多少。\n输入</p><p style=\"margin-left:0; margin-right:0\">共 n&#43;1 行 </p><p style=\"margin-left:0; margin-right:0\">第一行&#xff0c;有四个整数 n,x,y,z&#xff0c;数与数之间用一个空格隔开&#xff0c;n 表示乐乐咨询的商家数&#xff0c;x,y,z 分别表示要购买一等奖奖品数量、二等奖奖品数量、三等奖奖品数量。 </p><p style=\"margin-left:0; margin-right:0\">接下来的 n 行&#xff0c;每行都有三个数&#xff0c;数与数之间用一个空格隔开&#xff0c;第 i&#43;1 行分别表示编号为 i 的商家对一、二、三等奖奖品的报价&#xff08;单价&#xff09; \n输出</p><p style=\"margin-left:0; margin-right:0\">共有两个整数&#xff0c;第一个数表示购买奖品的商家编号&#xff08;如果有多家总费用最少&#xff0c;输出编号最小的商家&#xff09;&#xff0c;第二个数是购买奖品的总费用。 \n样例输入</p><pre class=\"has\" style=\"margin-left:0; margin-right:0\">\n<code class=\"language-content\">3 1 2 3\n70 50 30\n60 40 20\n80 55 33\n</code></pre><p>样例输出</p><pre class=\"has\" style=\"margin-left:0; margin-right:0\">\n<code class=\"language-content\">2 200</code></pre><p>提示</p><p style=\"margin-left:0; margin-right:0\">【样例解释】</p><p style=\"margin-left:-0.5pt; margin-right:0\">乐乐咨询了 3 个商家&#xff0c;打算购买一二三等奖奖品数量分别是 1 个、2 个、3 个&#xff0c;编号为 1 的商家一二三等奖奖品报价分别是 70、50、30 元&#xff0c;编号为 2 的商家报价分别是 60、40、20 元&#xff0c;编号为 3 的商家报价分别是 80、55、20 元&#xff0c;乐乐在编号为 2 的商家购买总费用最低&#xff0c;为200 元。</p><p style=\"margin-left:0; margin-right:0\">【数据范围】</p><p style=\"margin-left:23.75pt; margin-right:0\">40%数据&#xff1a;1≤n≤5000 </p><p style=\"margin-left:23.75pt; margin-right:0\">100%数据&#xff1a;1≤n≤100000&#xff0c;1≤x,y,z≤1000&#xff0c;奖品报价都是 1000 范围以内的正整数。</p>",
"difficulty": "困难",
"answer_id": 53419944,
"answer_content": "<pre>\n<code>#include &lt;iostream&gt;\n#include &lt;stdio.h&gt;\nusing namespace std;\n\nint main(){\n int n,x,y,z,i,min&#61;1000000,n1,n2,n3,temp,t&#61;0;\n cin&gt;&gt;n&gt;&gt;x&gt;&gt;y&gt;&gt;z;\n for(i&#61;0;i&lt;n;i&#43;&#43;){\n temp&#61;0;\n cin&gt;&gt;n1&gt;&gt;n2&gt;&gt;n3;\n temp&#43;&#61;n1*x&#43;n2*y&#43;n3*z;\n if(temp&lt;min){\n min&#61;temp;\n t&#61;i&#43;1;\n }\n }\n cout&lt;&lt;t&lt;&lt;&#34; &#34;&lt;&lt;min;\n return 0;\n}</code></pre>\n\n<p><img alt=\"\" height=\"212\" src=\"https://img-ask.csdnimg.cn/upload/1622894313928.png\" width=\"461\" /></p>\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <stdio.h>\nusing namespace std;\nint main(){\n\tint n,x,y,z,i,min=1000000,n1,n2,n3,temp,t=0;\n\tcin>>n>>x>>y>>z;\n\tfor(i=0;i<n;i++){\n\t\ttemp=0;\n\t\tcin>>n1>>n2>>n3;\n\t\ttemp+=n1*x+n2*y+n3*z;\n\t\tif(temp<min){\n\t\t\tmin=temp;\n\t\t\tt=i+1;\n\t\t}\n\t}\n\tcout<<t<<\" \"<<min;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469962",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/115.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7435486,
"question_title": "将一个字符串中第一个连续数字转换成整数",
"question_content": "<p>请编写一个fun函数&#xff0c;实现如下功能&#xff1a;将一个字符串中第一个连续数字转换成整数&#xff0c;作为函数值返回&#xff0c;否则返回0&#xff08;程序的输入输出要有提示&#xff09;\n比如&#xff1a;字符串中的内容为:&#34;abc123 def45gh&#34;&#xff0c;则函数的返回值为123。</p>",
"difficulty": "中等",
"answer_id": 53406098,
"answer_content": "<p>为什么不写注释&#xff1f;</p>\n\n<p>懒得写。。。 </p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nint fun(string str);\n\nint main(void) {\n\tstring str &#61; &#34;abc123 def45gh&#34;;\n\n\tcout &lt;&lt; fun(str);\n\n\n\treturn 0;\n}\n\n\nint fun(string str) {\n\tint index &#61; -1;\n\tint score &#61; 0;\n\n\tfor (int i &#61; 0; i &lt; str.length(); i&#43;&#43;) {\n\t\tif ((str[i] &gt;&#61; 48 &amp;&amp; str[i] &lt;&#61; 57) &amp;&amp; (i &#43; 1 &lt; str.length()) &amp;&amp; (str[i &#43; 1] &gt;&#61; 48 &amp;&amp; str[i &#43; 1] &lt;&#61; 57)) {\n\t\t\tindex &#61; i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (index &#61;&#61; -1) {\n\t\treturn score;\n\t}\n\n\tscore &#61; str[index] - &#39;0&#39;;\n\tfor (int i &#61; index &#43; 1; i &lt; str.length(); i&#43;&#43;) {\n\t\tif (str[i] &gt;&#61; 48 &amp;&amp; str[i] &lt;&#61; 57) {\n\t\t\tscore *&#61; 10;\n\t\t\tscore &#43;&#61; str[i] - &#39;0&#39;;\n\t\t\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn score;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint fun(string str);\nint main(void) {\n\tstring str = \"abc123 def45gh\";\n\tcout << fun(str);\n\treturn 0;\n}\nint fun(string str) {\n\tint index = -1;\n\tint score = 0;\n\tfor (int i = 0; i < str.length(); i++) {\n\t\tif ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57)) {\n\t\t\tindex = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (index == -1) {\n\t\treturn score;\n\t}\n\tscore = str[index] - '0';\n\tfor (int i = index + 1; i < str.length(); i++) {\n\t\tif (str[i] >= 48 && str[i] <= 57) {\n\t\t\tscore *= 10;\n\t\t\tscore += str[i] - '0';\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn score;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469963",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/116.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 222425,
"question_title": "字符图形输出",
"question_content": "编程实现把输入任意整数n后,可打印出n行三角字符阵列图形。例如,输入整数5时,程序运行结果如下:\nENTER A NUMBER:5<回车>\nA C F J O\nB E I N\nD H M\nG L\nK",
"difficulty": "中等",
"answer_id": 178405,
"answer_content": "\n\n```\n #include <iostream>\nusing namespace std;\n\nchar a[100][100];\n\nint main()\n{\n char c = 'A';\n int n = 5;\n for (int i = 0; i < n; i++)\n {\n for (int j = i; j >= 0; j--)\n {\n a[j][i - j] = c++;\n }\n }\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n - i; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n}\n```\n\n",
"tag_name": "c++",
"cpp": " #include <iostream>\nusing namespace std;\nchar a[100][100];\nint main()\n{\n\tchar c = 'A';\n\tint n = 5;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tfor (int j = i; j >= 0; j--)\n\t\t{\n\t\t\ta[j][i - j] = c++;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tfor (int j = 0; j < n - i; j++)\n\t\t{\n\t\t\tcout << a[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469861",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/117.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1078333,
"question_title": "从用户处输入5人2个整数排列a、b,并检查对应的排列要素是否相同。",
"question_content": "<p>使用array_equal(int a[],int b[],int size)\na[0]和b[0],a[1]和b[1],…,a[size-1]和b[size-1]检查是否相同\n如果整体要素相同,则返还1,否则返还0。\n利用反复循环检查排列的各要素是否相等。如果有一个不同的地方,就可以马上返还0。![图片说明](https://img-ask.csdn.net/upload/202005/28/1590646351_294989.png)\n翻译是:\n输入第一次排列:1 2 3 4 5\n输入第二次排列:1 2 2 2 2 \n不一致返还0\n第二个一致就返还1</p>",
"difficulty": "中等",
"answer_id": 1282702,
"answer_content": "\n```\n#include<stdio.h>\n\nint main()\n{\n\tint a[5], b[5], i;\n\tprintf(\"输入5个数:\");\n\tfor(i = 0; i < 5; i++)\n\t\tscanf(\"%d\", &a[i]);\n\tfor(i = 0; i < 5; i++)\n\t\tscanf(\"%d\", &b[i]);\n\tfor(i = 0; i < 5; i++)\n\t\tif(a[i] != b[i])\n\t\t\tbreak;\n\tif(i==4)\n\t\tprintf(\"1\");\n\telse\n\t\tprintf(\"0\");\n\treturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a[5], b[5], i;\n\tprintf(\"输入5个数:\");\n\tfor(i = 0; i < 5; i++)\n\t\tscanf(\"%d\", &a[i]);\n\tfor(i = 0; i < 5; i++)\n\t\tscanf(\"%d\", &b[i]);\n\tfor(i = 0; i < 5; i++)\n\t\tif(a[i] != b[i])\n\t\t\tbreak;\n\tif(i==4)\n\t\tprintf(\"1\");\n\telse\n\t\tprintf(\"0\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469862",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/118.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 3368121,
"question_title": "计算一下函数的值",
"question_content": "<p>计算s&#61;1&#43;(1&#43;2)&#43;(1&#43;2&#43;3)&#43;……&#43;(1&#43;2&#43;3&#43;……&#43;n)的值</p>\n\n<p>当某一项的值超过50时&#xff0c;结束循环&#xff0c;并将当时的s的值进行输出&#xff0c;将当时刚好超过50的那一项的值和项数一并输出;</p>\n\n<p>如果最终项的值没有超过50&#xff0c;则按循环计划计算完所有的累加和&#xff0c;也将最终项的值和项数一并输出。  </p>",
"difficulty": "简单",
"answer_id": 22392963,
"answer_content": "<p>#include&lt;stdio.h&gt;<br />\n#include&lt;math.h&gt;<br />\n#include&lt;string.h&gt;<br />\n#include&lt;stdlib.h&gt;<br />\nint main(int argc,char * argv[])<br />\n{<!-- --><br />\n        int summer,summer2,num;//声明和与项数变量<br />\n        summer&#61;0,summer2&#61;0;//初始化和变量<br />\n        num&#61;atoi(argv[1]);//将命令行第二个参数转化为整数项数<br />\n        for(int i&#61;1;i&lt;&#61;num;i&#43;&#43;)//进行循环求和<br />\n        {<!-- --><br />\n                summer&#43;&#61;i;<br />\n                summer2&#43;&#61;summer;<br />\n                if(summer&gt;50)//当某一项的值大于50时<br />\n                {<!-- --><br />\n                        printf(&#34;计算总和是%d\\n超过50的项是第%d项\\n&#34;,summer2,i);<br />\n                        printf(&#34;该项的值是%d\\n&#34;,summer);<br />\n                        return 0;<br />\n                }</p>\n\n<p>        }<br />\n        printf(&#34;计算总和是%d\\n,最后一项是%d\\n没有任何一项的值超过50\\n&#34;,summer2,num);//当任一一项的值均不大于50时<br />\n        return 0;<br />\n       </p>\n\n<p>}</p>\n\n<p><br />\n代码编译运行正确无误&#xff0c;使用格式为&#xff1a;×××.exe    number。***.exe指本地编译成的程序&#xff0c;number是要循环计算的项数。二者之间有一个空格。如果我对题目的理解有误&#xff0c;请私信我或者评论处给我留言。</p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<math.h>\n#include<string.h>\n#include<stdlib.h>\nint main(int argc,char * argv[])\n{\n        int summer,summer2,num;\n        summer=0,summer2=0;\n        num=atoi(argv[1]);\n        for(int i=1;i<=num;i++)\n        {\n                summer+=i;\n                summer2+=summer;\n                if(summer>50)\n                {\n                        printf(\"计算总和是%d\\n超过50的项是第%d项\\n\",summer2,i);\n                        printf(\"该项的值是%d\\n\",summer);\n                        return 0;\n                }\n        }\n        printf(\"计算总和是%d\\n,最后一项是%d\\n没有任何一项的值超过50\\n\",summer2,num);\n        return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470275",
"status": 0,
"keywords": "算法高阶,计算几何学,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/119.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7452215,
"question_title": "由数值大小在闭区间的",
"question_content": "<p>创建一个大小为 100 的整型数组&#xff0c;数组元素由数值大小在闭区间[0,1000]内的\n随机数组成。在控制台窗口中输出数组内数值为 7 的倍数&#xff0c;且除以 3 余数为 2 的\n元素。若数组中不存在符合规则的元素&#xff0c;则在控制台中给出相应提示</p>",
"difficulty": "简单",
"answer_id": 53434106,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\nint main()\n{\n\tint a[100];\n\tint i,nmb &#61; 0;\n\tfor(i &#61; 0;i&lt;100;i&#43;&#43;)\n\t\ta[i] &#61; rand()%1000;\n\tfor (i &#61; 0;i&lt;100;i&#43;&#43;)\n\t{\n\t\tif( (a[i]%7 &#61;&#61; 0) &amp;&amp; (a[i]%3 &#61;&#61;2) )\n\t\t{\n\t\t\tprintf(&#34;%5d&#34;,a[i]);\n\t\t\tnmb&#43;&#43;;\n\t\t}\n\t}\n\tprintf(&#34;\\n&#34;);\n\tif(nmb &#61;&#61; 0)\n\t\tprintf(&#34;没有符合条件的数\\n&#34;);\n\t\n\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint main()\n{\n\tint a[100];\n\tint i,nmb = 0;\n\tfor(i = 0;i<100;i++)\n\t\ta[i] = rand()%1001;\n\tfor (i = 0;i<100;i++)\n\t{\n\t\tif( (a[i]%7 == 0) && (a[i]%3 ==2) )\n\t\t{\n\t\t\tprintf(\"%5d\",a[i]);\n\t\t\tnmb++;\n\t\t}\n\t}\n\tprintf(\"\\n\");\n\tif(nmb == 0)\n\t\tprintf(\"没有符合条件的数\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469937",
"status": 1,
"keywords": "区间树,算法中阶,数据结构,数据结构的扩张",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/12.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 3082820,
"question_title": "逆序存放数组中的数据,并输出指定元素",
"question_content": "<pre><p>将给定的n个整数存入数组中&#xff0c;将数组中的这n个数逆序存放&#xff0c;再按要求输出指定的数组元素。\n输入格式:\n在第一行中给出一个正整数n&#xff08;1≤n≤10&#xff09;。第二行输入n个整数&#xff0c;用空格分开。第三行输入一个非负整数m&#xff08;m&lt;n&#xff09;。\n输出格式:\n在一行中输出逆序存放后下标为m的数组元素。行末无空格。</pre>\n输入样例:</p>\n<pre>\n<code>6\n10 8 1 2 3 4\n2</code></pre>\n<p>输出样例:</p>\n<pre>\n<code>2</code></pre>",
"difficulty": "中等",
"answer_id": 19384015,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main()\n{\n int n,m;\n scanf(&#34;%d&#34;,&amp;n);\n if(n&lt;1||n&gt;10){\n printf(&#34;1≤n≤10&#34;);\n return 0;\n }\n int *a &#61; (int*)malloc(sizeof(int)*n);\n for(int i&#61;n-1; i&gt;&#61;0; i-- ){\n scanf(&#34;%d&#34;,&amp;a[i]);\n }\n scanf(&#34;%d&#34;,&amp;m);\n if(m&lt;0||m&gt;&#61;n){\n printf(&#34;0≤m&lt;n&#34;);\n return 0;\n }\n printf(&#34;%d&#34;,a[m]); \n return 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint main()\n{\n\tint n,m;\n\tscanf(\"%d\",&n);\n\tif(n<1||n>10){\n\t printf(\"1≤n≤10\");\n\t return 0;\n\t}\n\tint *a = (int*)malloc(sizeof(int)*n);\n\tfor(int i=n-1; i>=0; i-- ){\n\t\tscanf(\"%d\",&a[i]);\n\t}\n\tscanf(\"%d\",&m);\n\tif(m<0||m>=n){\n\t printf(\"0≤m<n\");\n\t return 0;\n\t}\n\tprintf(\"%d\",a[m]); \n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469863",
"status": 1,
"keywords": "算法高阶,数论算法,元素的幂,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/120.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7429364,
"question_title": "计算英文单词的能量值",
"question_content": "<p>【问题描述】规定26个英文字符的能量值分别是1-26&#xff0c;键盘输入一个英文单词&#xff0c;输出它的能量值。字符不区分大小写。\n【输入形式】在提示语后面输入一个单词\n【输出形式】输出energy&#61;计算值\n【样例输入1】Input a word:Integrity\n【样例输出1】energy&#61;127\n【样例输入2】Input a word:Upset\n【样例输出2】energy&#61;81\n【样例说明】下划线上是输入输出的数据&#xff0c;其余是程序的提示信息</p>",
"difficulty": "简单",
"answer_id": 53396246,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n#include &lt;string&gt;\nusing namespace std;\n#pragma warning(disable:4996)\nint main()\n{\n\tint i;\n\tint sum &#61; 0;\n\tchar arrs[1000];\n\tprintf(&#34;Input a word:&#34;);\n\twhile (scanf(&#34;%s&#34;, &amp;arrs) !&#61; EOF) {\n\t\tsum &#61; 0;\n\t\tfor (i &#61; 0; i &lt; strlen(arrs); i&#43;&#43;) {\n\t\t\t/// &lt;summary&gt;\n\t\t\t/// a-z 的ascii码高于 A-Z\n\t\t\t/// &lt;/summary&gt;\n\t\t\t/// &lt;returns&gt;&lt;/returns&gt;\n\t\t\tif (arrs[i] &gt; &#39;a&#39;) {\n\t\t\t\tsum &#43;&#61; arrs[i] - &#39;a&#39; &#43; 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tsum &#43;&#61; arrs[i] - &#39;A&#39; &#43; 1;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tprintf(&#34;energy&#61;%d\\n&#34;, sum);\n\t\tprintf(&#34;Input a word:&#34;);\n\t\t\n\t}\n\t\n\treturn 0;\n}</code></pre>\n\n<p> <img alt=\"\" height=\"560\" src=\"https://img-ask.csdnimg.cn/upload/1620992982648.png\" width=\"802\" /></p>\n\n<p>程序运行结果&#xff1a;</p>\n\n<pre>\n<code class=\"language-cpp\">Input a word:Integrity\nenergy&#61;127\nInput a word:Upset\nenergy&#61;81\nInput a word:</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <cstring>\nusing namespace std;\n#pragma warning(disable:4996)\nint main()\n{\n\tint i;\n\tint sum = 0;\n\tchar arrs[1000];\n\tprintf(\"Input a word:\");\n\twhile (scanf(\"%s\", &arrs) != EOF) {\n\t\tsum = 0;\n\t\tfor (i = 0; i < strlen(arrs); i++) {\n\t\t\tif (arrs[i] >= 'a') {\n\t\t\t\tsum += arrs[i] - 'a' + 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tsum += arrs[i] - 'A' + 1;\n\t\t\t}\n\t\t}\n\t\tprintf(\"energy=%d\\n\", sum);\n\t\tprintf(\"Input a word:\");\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469864",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/121.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1049165,
"question_title": "数组元素循环右移问题",
"question_content": "题目:一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?\n输入格式:\n每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。\n输出格式:\n在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。\n输入样例:\n\t6 2\n\t1 2 3 4 5 6\n输出样例:\n\t5 6 1 2 3 4\n",
"difficulty": "简单",
"answer_id": 1246646,
"answer_content": "6 7\n1 2 3 4 5 6\n这个输出应该是\n6 1 2 3 4 5\n你的是\n1 2 3 4 5 6\n\n这样写\n\n```\n#include<stdio.h>\nint main()\n{\n int n,m,a[1000];\n scanf(\"%d %d\",&n,&m);\n m = m % n;\n int count=m;\n while(m<n)\n {\n scanf(\"%d\",&a[m]);\n m++;\n }\n for(int i=0;i<count;i++)\n scanf(\"%d\",&a[i]);\n int first=1;\n for(int i=0;i<n;i++)\n {\n if(!first)printf(\" \");\n printf(\"%d\",a[i]);\n first=0;\n }\n\n}\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint n,m,a[1000];\n\tscanf(\"%d %d\",&n,&m);\n\tm = m % n;\n\tint count=m;\n\twhile(m<n)\n\t{\n\t\tscanf(\"%d\",&a[m]);\n\t\tm++;\n\t}\n\tfor(int i=0;i<count;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tint first=1;\n\tfor(int i=0;i<n;i++)\n\t{\n\t\tif(!first)printf(\" \");\n\t\tprintf(\"%d\",a[i]);\n\t\tfirst=0;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469865",
"status": 1,
"keywords": "算法中阶,动态规划,最长公共子序列,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/122.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7431734,
"question_title": "总分数最大",
"question_content": "<p>每位同学都有自己的一个幸运数&#xff0c;乐乐所在班级共有n位同学&#xff0c;因此有编号为1至n的n块标牌&#xff0c;标牌的编号与自己的幸运数相乘&#xff0c;就是这位同学的分数。你的工作就是帮乐乐寻找一种方案&#xff0c;使得班级总分数最大。\n输入\n第一行只有一个整数n\n第二行共有n个不超过10000的正整数&#xff0c;中间有一个空格隔开。\n输出\n只有一行且只有一个整数&#xff0c;乐乐班级的总分数。</p>",
"difficulty": "简单",
"answer_id": 53400440,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\nint main()\n{\n int a[10005];\n long long t&#61;0;\n\tint n,i,j,x;\n\tscanf(&#34;%d&#34;,&amp;n);\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;)\n\t scanf(&#34;%d&#34;,&amp;a[i]);\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;){\n for(j&#61;0;j&lt;n;j&#43;&#43;){\n if(a[i]&gt;a[j]){\n x&#61;a[i];\n a[i]&#61;a[j];\n a[j]&#61;x;\n }\n }\n\t}\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tt&#43;&#61;a[i]*(i&#43;1);\n\t}\n\tprintf(&#34;%lld&#34;,t);\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a[10005];\n\tlong long t=0;\n\tint n,i,j,x;\n\tscanf(\"%d\",&n);\n\tfor(i=0;i<n;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tfor(i=0;i<n;i++){\n\t\tfor(j=0;j<n;j++){\n\t\t\tif(a[i]>a[j]){\n\t\t\t\tx=a[i];\n\t\t\t\ta[i]=a[j];\n\t\t\t\ta[j]=x;\n\t\t\t}\n\t\t}\n\t}\n\tfor(i=0;i<n;i++)\n\t{\n\t\tt+=a[i]*(i+1);\n\t}\n\tprintf(\"%lld\",t);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470276",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编,寻找最近点对",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/123.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7457714,
"question_title": "判断字符串是否连续",
"question_content": "编写程序实现判断一个字符串中的英文字母是由连续的英文字母组成的\n \n \n 比如“ABC”或“DCAB”都是连续的",
"difficulty": "中等",
"answer_id": 53442184,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;iostream&gt;\n#include&lt;algorithm&gt;\nusing namespace std;\nint main()\n{\n char ans[4]&#61;{&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;};\n sort(ans,ans&#43;4); /* 这个sort可以不用&#xff0c;因为{1&#xff0c;2&#xff0c;3&#xff0c;4}已经排好序*/\n do /*注意这步&#xff0c;如果是while循环&#xff0c;则需要提前输出*/\n {\n for(int i&#61;0;i&lt;4;&#43;&#43;i)\n cout&lt;&lt;ans[i]&lt;&lt;&#34; &#34;;\n cout&lt;&lt;endl;\n }while(next_permutation(ans,ans&#43;4));\n return 0;\n}</code></pre>\n\n<p> 去判断吧。</p>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nint main()\n{\n\tchar ans[4]={'A','B','C','D'};\n\tsort(ans,ans+4);\t\n\tdo\t\t\t\t\t\t\t \n\t{\n\t\tfor(int i=0;i<4;++i)\n\t\t\tcout<<ans[i]<<\" \";\n\t\tcout<<endl;\n\t}while(next_permutation(ans,ans+4));\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470179",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/124.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1071594,
"question_title": "从键盘输入任意一个大写英文字母,要求它在26个字母表中的位置和其后面的第四个字母",
"question_content": "例如:程序运行\n输入:B<回车>。\n输出:B在第2个位置,其后面第四个字母是F",
"difficulty": "简单",
"answer_id": 1273628,
"answer_content": "\n```\n#include <stdio.h>\n\nint main(){\n char c,c2;\n printf(\"输入:\");\n c = getchar();\n int m=0,n=0;\n if(c>='A'&& c<='z')\n {\n m = c - 'A' + 1;\n if(m < 23)\n {\n c2 = c + 4;\n n = m+4;\n }\n }\n if(n > 0)\n printf(\"%c在第%d个位置,其后面第四个字母是%c\\n\",c,m,c2);\n else\n printf(\"%c在第%d个位置,其后面没有第四个字母\\n\",c,m);\n return 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main(){\n\tchar c,c2;\n\tprintf(\"输入:\");\n\tc = getchar();\n\tint m=0,n=0;\n\tif(c>='A'&& c<='z')\n\t{\n\t\tm = c - 'A' + 1;\n\t\tif(m < 23)\n\t\t{\n\t\t\tc2 = c + 4;\n\t\t\tn = m+4;\n\t\t}\n\t}\n\tif(n > 0)\n\t\tprintf(\"%c在第%d个位置,其后面第四个字母是%c\\n\",c,m,c2);\n\telse\n\t\tprintf(\"%c在第%d个位置,其后面没有第四个字母\\n\",c,m);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470277",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/125.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7400042,
"question_title": "魔法少女锦姐姐",
"question_content": "锦姐姐作为魔法少女有很多魔法石,每个魔法石都有一个符文,符文是一个数字。\n \n 锦姐姐想知道她拥有某一种魔法石的数量。\n \n \n \n \n 输入\n \n \n 第一行输入一个数t,代表有t组数据\n \n 每组数据的第一行输入两个数n,q,代表n个魔法石,q次询问\n \n 接下来一行n个整数ai,表示每个魔法石的符文\n \n 接下来q行,每行一个数x,代表符文为x\n \n 数据范围:1<=t<=100,1<=n<=100000,1<=q<=1000,0<=ai<=100000,0<=x<=100000\n \n \n 输出\n \n \n 每组输出q行\n \n 每行输出符文为x的魔法石的个数\n \n \n 样例输入Copy\n \n \n1\n5 2\n1 1 2 2 3\n2\n3 \n \n 样例输出Copy\n \n \n2\n1 ",
"difficulty": "困难",
"answer_id": 53347760,
"answer_content": "<p>我帮你改良了一下&#xff0c;试试我这个代码</p>\n\n<pre>\n<code>#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\nint a[100001];\nint main()\n{\n int t,i,n,q,x,y;\n scanf(&#34;%d&#34;,&amp;t);\n while(t--)\n {\n\t\tmemset(a,0,sizeof(a));\n scanf(&#34;%d %d&#34;,&amp;n,&amp;q);\n for(i&#61;0;i&lt;n;i&#43;&#43;)\n\t\t{\n scanf(&#34;%d&#34;,&amp;y);\n\t\t\ta[y]&#43;&#43;;\n\t\t }\n while(q--)\n {\n scanf(&#34;%d&#34;,&amp;x);\n printf(&#34;%d\\n&#34;,a[x]);\n }\n }\n return 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<string.h>\nint a[100001];\nint main()\n{\n\tint t,i,n,q,x,y;\n\tscanf(\"%d\",&t);\n\twhile(t--)\n\t{\n\t\tmemset(a,0,sizeof(a));\n\t\tscanf(\"%d %d\",&n,&q);\n\t\tfor(i=0;i<n;i++)\n\t\t{\n\t\t\tscanf(\"%d\",&y);\n\t\t\ta[y]++;\n\t\t }\n\t\t\twhile(q--)\n\t\t\t{\n\t\t\t\tscanf(\"%d\",&x);\n\t\t\t\tprintf(\"%d\\n\",a[x]);\n\t\t\t}\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469964",
"status": 0,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/126.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7444359,
"question_title": "好数对",
"question_content": " 已知一个集合A,对A中任意两个不同的元素求和,若求得的和仍在A内,则称其为好数对。例如,集合A={1 2 3 4},1+2=3,1+3=4,则1,2和1,3 是两个好数对。编写程序求给定集合中好数对的个数。\n 注:集合中最多有1000个元素,元素最大不超过10000\n 程序运行示例1:\n 4↙\n 1 2 3 4↙\n 2\n 程序运行示例2:\n 7↙\n 2456 3251 654 890 100 754 1234↙\n 1\n 其中,“↙”表示输入",
"difficulty": "中等",
"answer_id": 53422539,
"answer_content": "<p>代码修改如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\nint main()\n{\n\tint n,i,j,t;\n\t\n\tscanf(&#34;%d&#34;,&amp;n);\n\tint* a &#61; (int*)malloc(n*sizeof(int));\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tscanf(&#34;%d&#34;,&amp;a[i]);\n\t}\n\tint cout&#61;0;\n\tfor( i&#61;0;i&lt;n;i&#43;&#43;)\n\t{ \n\t\tfor( j&#61;i&#43;1;j&lt;n;j&#43;&#43;)\n\t\t{\n\t\t\tfor(t &#61; 0; t &lt;n;t&#43;&#43;)\n\t\t\t\tif(a[i]&#43;a[j]&#61;&#61;a[t])\n\t\t\t\t\tcout&#43;&#43;;\n\t\t}\n\t}\n\tprintf(&#34;%d&#34;,cout);\n\tfree(a);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint main()\n{\n\tint n,i,j,t;\n\tscanf(\"%d\",&n);\n\tint* a = (int*)malloc(n*sizeof(int));\n\tfor(i=0;i<n;i++)\n\t{\n\t\tscanf(\"%d\",&a[i]);\n\t}\n\tint cout=0;\n\tfor( i=0;i<n;i++)\n\t{ \n\t\tfor( j=i+1;j<n;j++)\n\t\t{\n\t\t\tfor(t = 0; t <n;t++)\n\t\t\t\tif(a[i]+a[j]==a[t])\n\t\t\t\t\tcout++;\n\t\t}\n\t}\n\tprintf(\"%d\",cout);\n\tfree(a);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470180",
"status": 1,
"keywords": "算法高阶,高级数据结构,不相交集合森林,用于不相交集合的数据结构",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/127.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7400029,
"question_title": "卖树苗",
"question_content": "<pre><p>题目描述\n植树节又到了,商家A和商家B为了卖出更多的树苗。<br />\n商家A有了新的决定&#xff1a;<br />\n购买树苗数量小于等于10棵&#xff0c;所有树苗按正常价格10元一棵收费&#xff1b;<br />\n购买树苗数量大于10且小于等于20棵&#xff0c;超出10棵以上的树苗按8.2元一棵收费&#xff0c;其余树苗按正常价格收费&#xff1b;<br />\n购买树苗数量大于20棵&#xff0c;超出20棵以上的树苗按7.5元一棵收费&#xff0c;10至20棵部分树苗按8.2元一棵收费&#xff0c;其余树苗按正常价格收费。<br />\n商家B决定&#xff1a;<br />\n所有树苗12元一棵&#xff0c;但是均打七折卖出。</pre><pre>图图要代表班级去买树苗&#xff0c;要求输入图图需要购买的树苗棵数&#xff0c;输出在哪家商家购买更加划算及其所要花费的钱数。\n输入要求\n1 行,一个整数&#xff0c;表示图图需要购买的树苗数量。\n输出要求\n1 行,如果商家A的树苗比较划算&#xff0c;输出&#xff1a;A&#xff0c;否则输出&#xff1a;B&#xff0c;同时输出图图购买树苗最优惠的钱数&#xff08;文字和数字间用空格隔开&#xff09;。</pre>\n样例输入 </p>\n<pre>\n30</pre>\n<p>样例输出 </p>\n<pre>\nB 252</pre>",
"difficulty": "中等",
"answer_id": 53347698,
"answer_content": "<p>修改数据类型int为double。在计算过程中多是浮点计算&#xff0c;所以如果使用int系统会对计算结果进行取整&#xff0c;多次执行会有误差</p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\nint main() {\n double n, A, B; //数据类型用double&#xff0c;保证计算精度\n cin &gt;&gt; n;\n B &#61; 12 * 0.7 * n;\n if (n &lt;&#61; 10) {\n A &#61; 10 * n;\n }\n else if (n &gt; 10 &amp;&amp; n &lt;&#61; 20) {\n A &#61; 10 * 10 &#43; (n - 10) * 8.2;\n }\n else {\n A &#61; 10 * 8.2 &#43; (n - 20) * 7.5 &#43; 10 * 10;\n }\n if (A &lt; B) {\n cout &lt;&lt; &#34;A &#34; &lt;&lt; A;\n }\n else {\n cout &lt;&lt; &#34;B &#34; &lt;&lt; B;\n }\n return 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint main() {\n\tdouble n, A, B;\t\n\tcin >> n;\n\tB = 12 * 0.7 * n;\n\tif (n <= 10) {\n\t\tA = 10 * n;\n\t}\n\telse if (n > 10 && n <= 20) {\n\t\tA = 10 * 10 + (n - 10) * 8.2;\n\t}\n\telse {\n\t\tA = 10 * 8.2 + (n - 20) * 7.5 + 10 * 10;\n\t}\n\tif (A < B) {\n\t\tcout << \"A \" << A;\n\t}\n\telse {\n\t\tcout << \"B \" << B;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469866",
"status": 1,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/128.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7446099,
"question_title": "找出string中只出现过一次的字符",
"question_content": "找出string中只出现过一次的字符\n例如\"abcdef abcd\"中需要得到ef",
"difficulty": "简单",
"answer_id": 53425698,
"answer_content": "<p>#include &lt;iostream&gt;<br />\n#include &lt;string&gt;</p>\n\n<p>using namespace std;</p>\n\n<p>int main()<br />\n{<!-- --><br />\n    string temp &#61; &#34;&#34;;<br />\n    cout &lt;&lt; &#34;请输入字符串&#xff1a;&#34;;<br />\n    cin &gt;&gt; temp;<br />\n    string str &#61; &#34;&#34;;        //存放只出现一次的字符<br />\n    string str1 &#61; &#34;&#34;;        //存放重复的字符<br />\n    for (int i &#61; 0; i &lt; temp.length(); i&#43;&#43;)<br />\n    {<!-- --><br />\n        string tempSub &#61; temp.substr(i, 1);    <br />\n        int b &#61; temp.rfind(tempSub);                //从后向前查找字符出现的位置<br />\n        if (i &#61;&#61; b &amp;&amp; str1.find(tempSub) &#61;&#61; -1)        //如果b和遍历查找的位置一致&#xff0c;且str1 里没有该字符&#xff0c;说明只出现一次<br />\n            str &#43;&#61;temp.substr(i, 1);<br />\n        else if (str1.find(tempSub) &#61;&#61; -1)<br />\n            str1 &#43;&#61; temp.substr(i, 1);;<br />\n    }<br />\n    cout &lt;&lt; &#34;只出现一次的字符&#xff1a;&#34; &lt;&lt; str &lt;&lt; endl;<br />\n    cout &lt;&lt; &#34;重复出现的字符的字符&#xff1a;&#34; &lt;&lt; str1 &lt;&lt; endl;<br />\n    system(&#34;pause&#34;);<br />\n    return 0;<br />\n}</p>\n\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623295486577.png\" /></p>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint main()\n{\n    string temp = \"\";\n    cout << \"请输入字符串:\";\n    cin >> temp;\n    string str = \"\";        \n    string str1 = \"\";        \n    for (int i = 0; i < temp.length(); i++)\n    {\n        string tempSub = temp.substr(i, 1);    \n        int b = temp.rfind(tempSub);                \n        if (i == b && str1.find(tempSub) == -1)        \n            str +=temp.substr(i, 1);\n        else if (str1.find(tempSub) == -1)\n            str1 += temp.substr(i, 1);\n    }\n    cout << \"只出现一次的字符:\" << str << endl;\n    cout << \"重复出现的字符的字符:\" << str1 << endl;\n    system(\"pause\");\n    return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470278",
"status": 1,
"keywords": "算法,字符串",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/129.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1090014,
"question_title": "有序表的折半查找",
"question_content": "问题描述:\n用有序表表示静态查找表时,通常检索函数可以用折半查找来实现。\n折半查找的查找过程是:首先确定待查记录所在的范围,然后逐步缩小范围直到找到或者确定找不到相应的记录为止。而每次需要缩小的范围均为上一次的一半,这样的查找过程可以被称为折半查找。 \n第二行包含n个用空格隔开的正整数,表示n个有序的整数。输入保证这n个整数是从小到大递增的。\n第三行包含k个用空格隔开的正整数,表示k次查询的目标。\n输出:\n只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出其相应的位置,否则输出-1。\n请在每个整数后输出一个空格,并请注意行尾输出换行。",
"difficulty": "困难",
"answer_id": 1296683,
"answer_content": "\n```\n#include <stdio.h>\n\nint binary( int *a, int key, int n )\n{\n int left = 0, right = n - 1, mid = 0;\n mid = ( left + right ) / 2;\n while( left < right && a[mid] != key )\n {\n if( a[mid] < key )\n left = mid + 1;\n else if( a[mid] > key )\n right = mid - 1;\n mid = ( left + right ) / 2;\n }\n if( a[mid] == key ) return mid;\n return -1;\n}\n\nint main (void)\n{\n int Base_a[20] = {1,3,5,8,9,40,120,123,125,150,199,200,1250,1255,1900,2000,2001,3000,3950,5000};\n\tint Search_a[5] = {12,199,9,2001,3500};\n\t\n\tint result = 0x00;\n\n\tfor(int i = 0;i < sizeof(Search_a)/sizeof(Search_a[0]);i++)\n\t{\n\t\tresult = binary(Base_a,Search_a[i],sizeof(Base_a)/sizeof(Base_a[0]));\n\t\tprintf(\"[%d %d] \",Search_a[i],result);\n\t}\n\tprintf(\"\\n\");\n\t\n\treturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint binary( int *a, int key, int n )\n{\n\tint left = 0, right = n - 1, mid = 0;\n\tmid = ( left + right ) / 2;\n\twhile( left < right && a[mid] != key )\n\t{\n\t\tif( a[mid] < key )\n\t\tleft = mid + 1;\n\t\telse if( a[mid] > key )\n\t\tright = mid - 1;\n\t\tmid = ( left + right ) / 2;\n\t}\n\tif( a[mid] == key ) return mid;\n\treturn -1;\n}\nint main (void)\n{\n\tint Base_a[20] = {1,3,5,8,9,40,120,123,125,150,199,200,1250,1255,1900,2000,2001,3000,3950,5000};\n\tint Search_a[5] = {12,199,9,2001,3500};\n\tint result = 0x00;\n\tfor(int i = 0;i < sizeof(Search_a)/sizeof(Search_a[0]);i++)\n\t{\n\t\tresult = binary(Base_a,Search_a[i],sizeof(Base_a)/sizeof(Base_a[0]));\n\t\tprintf(\"[%d %d] \",Search_a[i],result);\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470249",
"status": 1,
"keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/13.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7428827,
"question_title": "报数游戏",
"question_content": "<p><strong>题目描述</strong><br />报数游戏\n首先&#xff0c;会给他们一人一个编号&#xff0c;并且每个人的编号都不相同。接下来的每一回合&#xff0c;会给一个数&#xff0c;编号超过它的最小编号的人要报出自己的编号。如果没有人的编号比给出的数要大&#xff0c;那么编号最大的人要报出自己的编号。每个人可以重复报号。\n会按照一个列表顺次报出每个回合的数&#xff0c;朋友们想知道每回合报出的编号应该是多少。\n<strong>输入</strong><br />输入数据共 3 行。\n第一行有两个整数 n,m(1≤n≤100,000,1≤m≤100,000)&#xff0c;分别表示参与游戏的朋友的个数&#xff0c;和游戏的回合数。 \n第二行 n个整数 ai(1≤ai≤100,000,000)&#xff0c;表示朋友们每个人的编号。对于 0≤i&lt;j&lt;n&#xff0c;都有 ai&lt;aj&#xff0c;即他们的编号递增排列。 \n第三行 m 个整数 qi(1≤qi≤100,000,000)&#xff0c;表示每回合给的数字。\n<strong>输出</strong><br />\n输出共一行 m 个整数&#xff0c;表示每回合报出的编号&#xff0c;每两个整数之间一个空格&#xff0c;最后一个数后面没有空格。</p>",
"difficulty": "中等",
"answer_id": 53395178,
"answer_content": "<pre>\n<code>#include&lt;iostream&gt;\n#include&lt;cstring&gt;\n#include&lt;vector&gt;\n#include&lt;algorithm&gt;\nusing namespace std;\nint ai[100010], qi[100010];\nint main()\n{\n\tint a, q;\n\twhile (cin &gt;&gt; a &gt;&gt; q)\n\t{\n\t\tfor (int i &#61; 0; i &lt; a; i&#43;&#43;)cin &gt;&gt; ai[i];\n\t\tfor (int i &#61; 0; i &lt; q; i&#43;&#43;)cin &gt;&gt; qi[i];\n\t\tfor (int i &#61; 0; i &lt; q; i&#43;&#43;) {\n\t\t\tint left &#61; 0, right &#61; a - 1, mid;\n\t\t\twhile (left &lt; right) {\n\t\t\t\tmid &#61; (left &#43; right) &gt;&gt; 1;\n\t\t\t\tif (ai[mid] &lt;&#61; qi[i])left &#61; mid &#43; 1;\n\t\t\t\telse right &#61; mid;\n\t\t\t}\n\t\t\tif (left - 1 &lt; 0 || ai[left] &lt; qi[i])left&#43;&#43;;//qi[i]小于最小值或大于最大值的情况\n\t\t\ti ? cout &lt;&lt; &#34; &#34; &lt;&lt; ai[left - 1] : cout &lt;&lt; ai[left - 1];\t\t\t\n\t\t}\n\t\tcout &lt;&lt; endl;\n\t}\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<iostream>\n#include<cstring>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nint ai[100010], qi[100010];\nint main()\n{\n\tint a, q;\n\twhile (cin >> a >> q)\n\t{\n\t\tfor (int i = 0; i < a; i++)cin >> ai[i];\n\t\tfor (int i = 0; i < q; i++)cin >> qi[i];\n\t\tfor (int i = 0; i < q; i++) {\n\t\t\tint left = 0, right = a - 1, mid;\n\t\t\twhile (left < right) {\n\t\t\t\tmid = (left + right) >> 1;\n\t\t\t\tif (ai[mid] <= qi[i])left = mid + 1;\n\t\t\t\telse right = mid;\n\t\t\t}\n\t\t\tif (left - 1 < 0 || ai[left] < qi[i])left++;\n\t\t\ti ? cout << \" \" << ai[left - 1] : cout << ai[left - 1];\t\t\t\n\t\t}\n\t\tcout << endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469965",
"status": 1,
"keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/130.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1003780,
"question_title": "计算位于矩阵边缘的元素之和",
"question_content": "描述\n输入一个整数矩阵,计算位于矩阵边缘的元素之和。所谓矩阵边缘的元素,就是第一行和最后一行的元素以及第一列和最后一列的元素。\n输入\n第一行分别为矩阵的行数m和列数n(m < 100,n < 100),两者之间以一个空格分开。\n接下来输入的m行数据中,每行包含n个整数,整数之间以一个空格分开。\n输出\n输出对应矩阵的边缘元素和\n样例输入\n3 3\n3 4 1\n3 7 1\n2 0 1\n样例输出\n15",
"difficulty": "简单",
"answer_id": 1184126,
"answer_content": "1 1\n1\n2Press any key to continue . . ..\n你这个用例不行,应该是1\n\n\n```\n#include<stdio.h>\nint main()\n{\n int a[256][256];\n int n,m,i,j,sum=0;\n scanf(\"%d %d\",&n,&m);\n for(i=1;i<=n;i++)\n {\n for(j=1;j<=m;j++)\n {\n scanf(\"%d\",&a[i][j]);\n\t\t\tif (i == 1 || i == n || j == 1 || j == m) sum += a[i][j];\n }\n }\n printf(\"%d\",sum);\n return 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a[256][256];\n\tint n,m,i,j,sum=0;\n\tscanf(\"%d %d\",&n,&m);\n\tfor(i=1;i<=n;i++)\n\t{\n\t\tfor(j=1;j<=m;j++)\n\t\t{\n\t\t\tscanf(\"%d\",&a[i][j]);\n\t\t\tif (i == 1 || i == n || j == 1 || j == m) sum += a[i][j];\n\t\t}\n\t}\n\tprintf(\"%d\",sum);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470181",
"status": 1,
"keywords": "算法高阶,矩阵运算,矩阵求逆,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/131.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 220434,
"question_title": "找出字符串中出现最多的字母",
"question_content": "Description\n钟Sir是一个迷信的(superstitious)家伙。他相信每一个字符串(string)里都有一个幸运字符。我们可以通过以下方法找到这个字符。例如,在字符串abbccc中,c 出现的次数最多,所以这个幸运字符就是 c 啦!(>_<)  (钟Sir的想法好简单啊…)\nInput\n第一行是测试数据的组数n,接下来的每组测试数据占一行,每行数据不超过1000个字符且非空。\n字符串里只含小写字母。\nOutput\n每组数据对应输出一行,包括出现次数最多的字符和该字符出现的次数,中间是一个空格。如果有多个字符出现的次数相同且最多,那么输出ASCII码最小的那一个字符。\nSample Input\n2\nabbccc\nadfadffasdf\nSample Output\nc 3\nf 4",
"difficulty": "简单",
"answer_id": 173122,
"answer_content": "\n\n```\n #include <string.h>\n#include\"stdio.h\"\nint main(void)\n{\n\tint n,i, z[26], max, xia;\n\tchar c[1050], ch;\n\n\tscanf(\"%d\",&n);\n\twhile(n>0)\n\t{\n\t\tn--;\n\t\tscanf(\"%s\", c);\n\n\t\tfor(i=0; i<26; i++)\n\t\t\tz[i]=0;\n\t\txia=strlen(c);\n\n\t\tfor(i=0; i<xia; i++)\n\t\t\tz[c[i]-'a']++;\n\n\t\tmax=z[0]; xia=0;\n\t\tfor(i=1; i<=25; i++)\n\t\tif(z[i]>max){ \n\t\t\tmax=z[i];\n\t\t\txia=i;\n\t\t}\n\n\t\tch='a'+xia;\n\t\tprintf(\"%c %d\\n\", ch, max);\n\t}\n\n\treturn 0;\n}\n```\n\n",
"tag_name": "c语言",
"cpp": " #include <string.h>\n#include\"stdio.h\"\nint main(void)\n{\n\tint n,i, z[26], max, xia;\n\tchar c[1050], ch;\n\tscanf(\"%d\",&n);\n\twhile(n>0)\n\t{\n\t\tn--;\n\t\tscanf(\"%s\", c);\n\t\tfor(i=0; i<26; i++)\n\t\t\tz[i]=0;\n\t\txia=strlen(c);\n\t\tfor(i=0; i<xia; i++)\n\t\t\tz[c[i]-'a']++;\n\t\tmax=z[0]; xia=0;\n\t\tfor(i=1; i<=25; i++)\n\t\tif(z[i]>max){ \n\t\t\tmax=z[i];\n\t\t\txia=i;\n\t\t}\n\t\tch='a'+xia;\n\t\tprintf(\"%c %d\\n\", ch, max);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469966",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/132.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1093013,
"question_title": "偶数 or 奇数",
"question_content": "偶数 or 奇数\n时间限制: 1 Sec 内存限制: 128 MB\n题目描述\n编程班老师搞了一个有 N (1 <= N <= 100) 个正整数 I (1 <= I <= 10^60) 的表,叫 同学们去统计每个数里面数字(0,1,2,3,4,5,6,7,8,9)(注 0 为偶数)的奇偶数 字个数。写一个程序读入 N 个整数,统计每个整数的数字奇偶个数。\n输入\n第 1 行: 一个单独的整数: N \n第 2 到第 N+1 行: 每行一个长长(小于等于 60 位)的整数,需要统计数字奇偶个数。\n输出\n 1..N 行: 第 j 行根据第 j 个整数输出奇数个数与偶数个数。\n样例输入\n2\n1024\n5931\n样例输出\n1 3\n4 0",
"difficulty": "中等",
"answer_id": 1301954,
"answer_content": "代码本身没有什么问题\n关键是10^60\n因此long long可能也不够,应该用字符串。\n\n\n```\n#include<iostream>\n#include<string>\nusing namespace std;\nstring a[105];\nint b[105],c[105];\nint qiujishu(string x){\n int jishu=0;\n for(int i = 0; x.c_str()[i]; i++){\n if ((x.c_str()[i] - '0') % 2 == 1) jishu++;\n }\n return jishu;\n}\nint qiuoushu(string x){\n int oushu=0;\n for(int i = 0; x.c_str()[i]; i++){\n if ((x.c_str()[i] - '0') % 2 == 0) oushu++;\n }\n return oushu;\n}\nint main()\n{\n int n;\n cin>>n;\n for(int i=1;i<=n;i++){\n cin>>a[i];\n b[i]=qiujishu(a[i]);\n c[i]=qiuoushu(a[i]);\n }\n for(int i=1;i<=n;i++){\n cout<<b[i]<<\" \"<<c[i]<<endl;\n }\n return 0;\n}\n\n```",
"tag_name": "c++",
"cpp": "#include<iostream>\n#include<string>\nusing namespace std;\nstring a[105];\nint b[105],c[105];\nint qiujishu(string x){\n\tint jishu=0;\n\tfor(int i = 0; x.c_str()[i]; i++){\n\t\tif ((x.c_str()[i] - '0') % 2 == 1) jishu++;\n\t}\n\treturn jishu;\n}\nint qiuoushu(string x){\n\tint oushu=0;\n\tfor(int i = 0; x.c_str()[i]; i++){\n\t\tif ((x.c_str()[i] - '0') % 2 == 0) oushu++;\n\t}\n\treturn oushu;\n}\nint main()\n{\n\tint n;\n\tcin>>n;\n\tfor(int i=1;i<=n;i++){\n\t\tcin>>a[i];\n\t\tb[i]=qiujishu(a[i]);\n\t\tc[i]=qiuoushu(a[i]);\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tcout<<b[i]<<\" \"<<c[i]<<endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469967",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量,期望为线性时间的选择算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/133.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1057457,
"question_title": "输入一个正整数n(代表图形的行数),输出如样例形式的图形。",
"question_content": "输入:7\n输出:\n D D\n CD DC\n BCD DCB\n ABCDDCBA\n BCD DCB\n CD DC\n D D",
"difficulty": "简单",
"answer_id": 1254605,
"answer_content": "\n```\n#include<stdio.h>\n#include<vector>\n#include<string>\n#include<iostream>\n#include<algorithm>\nusing namespace std;\nint main() {\n\tint n;\n\tcin>>n;\n\tvector<string> a(n,\"\"),b(n,\"\");\n\tint m=(n+1)/2;\n\tint p=0;\n\tfor(int i=m-1;i>=0;i--){\n\t\tfor(int j=0;j<=i;j++)\n\t\t\ta[i].push_back('A'+j+p);\n\t\tb[i]=a[i];\n\t\treverse(b[i].begin(),b[i].end());\n\t\tfor(int j=i+1;j<m;j++){\n\t\t\ta[i]+=\" \";\n\t\t\tb[i]+=\" \";\n\t\t}\n\t\tp++;\n\t}\n\tp=0;\n\tfor(int i=n-1;i>=m;i--){\n\t\ta[i]=a[p];\n\t\tb[i]=b[p++];\n\t}\n\tfor(int i=0;i<n;i++)\n\t\tcout<<a[i]<<b[i]<<endl;\n\treturn 0;\n}\n```",
"tag_name": "c++",
"cpp": "#include<stdio.h>\n#include<vector>\n#include<string>\n#include<iostream>\n#include<algorithm>\nusing namespace std;\nint main() {\n\tint n;\n\tcin>>n;\n\tvector<string> a(n,\"\"),b(n,\"\");\n\tint m=(n+1)/2;\n\tint p=0;\n\tfor(int i=m-1;i>=0;i--){\n\t\tfor(int j=0;j<=i;j++)\n\t\t\ta[i].push_back('A'+j+p);\n\t\tb[i]=a[i];\n\t\treverse(b[i].begin(),b[i].end());\n\t\tfor(int j=i+1;j<m;j++){\n\t\t\ta[i]+=\" \";\n\t\t\tb[i]+=\" \";\n\t\t}\n\t\tp++;\n\t}\n\tp=0;\n\tfor(int i=n-1;i>=m;i--){\n\t\ta[i]=a[p];\n\t\tb[i]=b[p++];\n\t}\n\tfor(int i=0;i<n;i++)\n\t\tcout<<a[i]<<b[i]<<endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470182",
"status": 1,
"keywords": "算法,图形输出",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/134.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1095942,
"question_title": "利用字母组成图形",
"question_content": "利用字母可以组成一些美丽的图形,下面给出了一个例子:\nABCDEFG\nBABCDEF\nCBABCDE\nDCBABCD\nEDCBABC\n这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。\n输入一行,包含两个整数n和m,分别表示你要输出的图形的行数的列数。",
"difficulty": "中等",
"answer_id": 1307791,
"answer_content": "\n```\n#include<stdio.h>\n#include<math.h>\nint main()\n{\n int m,n;\n scanf(\"%d%d\",&n,&m);\n int i,j;\n for(i=0;i<n;i++)\n {\n for(j=0;j<m;j++)\n {\n printf(\"%c\",65+abs(i-j)); \n }\n printf(\"\\n\");\n } \n return 0;\n}\n\n\n```\nhttps://blog.csdn.net/qq_45281807/article/details/104167624",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<math.h>\nint main()\n{\n\tint m,n;\n\tscanf(\"%d%d\",&n,&m);\n\tint i,j;\n\tfor(i=0;i<n;i++)\n\t{\n\t\t for(j=0;j<m;j++)\n\t\t {\n\t\t\t printf(\"%c\",65+abs(i-j)); \n\t\t }\n\t\t printf(\"\\n\");\n\t} \n return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470183",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/135.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7430426,
"question_title": "计算阶乘的和",
"question_content": "<p>计算1!-2!&#43;3!-4!&#43;5!-6!&#43;7!-8!&#43;9!-10!,并输出计算结果</p>",
"difficulty": "简单",
"answer_id": 53397520,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &#34;stdio.h&#34;\n\ndouble fun(int n)//用double提高精度和计数位数。\n\n{\n\n double sum&#61;1.0;\n\n int i;\n\n for(i&#61;1;i&lt;&#61;n;i&#43;&#43;)\n\n sum*&#61;i;\n\n return sum;\n\n}\n\n \n\nint main()\n\n{\n\n int i,mark&#61;1;\n\n double sum&#61;0,item&#61;0;\n\n for(i&#61;1;i&lt;&#61;10;i&#43;&#43;)\n\n {\n\n item&#61;mark*fun(i);\n\n sum&#43;&#61;item;\n\n mark&#61;-mark;\n\n }\n\n printf(&#34;1!-2!&#43;3!-4!&#43;5!-6!&#43;7!-8!&#43;9!-10!&#61;%.0lf\\n&#34;,sum);\n\n\n\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include \"stdio.h\"\ndouble fun(int n)\n{\n\tdouble sum=1.0;\n\tint i;\n\tfor(i=1;i<=n;i++)\n\t\tsum*=i;\n\treturn sum;\n}\nint main()\n{\n\tint i,mark=1;\n\tdouble sum=0,item=0;\n\tfor(i=1;i<=10;i++)\n\t{\n\t\titem=mark*fun(i);\n\t\tsum+=item;\n\t\tmark=-mark;\n\t}\n\tprintf(\"1!-2!+3!-4!+5!-6!+7!-8!+9!-10!=%.0lf\\n\",sum);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469867",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/136.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7443725,
"question_title": "输出最长的递增数字字符串",
"question_content": "<p>如何在一亿位整数组成的字符串中找到最长的递增数字字符串&#xff1f;</p>",
"difficulty": "中等",
"answer_id": 53421026,
"answer_content": "<p>1亿个字符串占用的缓存超出了编译器的默认缓存&#xff0c;所以我代码中定义了一个宏来表示字符串的最大长度&#xff0c;如果你需要扩展到1亿&#xff0c;需要修改编译器的默认缓存大小&#xff0c;然后把宏定义的值调大即可。代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#define MAX_SIZE 100000\nint main()\n{\n\tchar buf[MAX_SIZE] &#61; {0};\n\tint i &#61; 0,len &#61; 0,index &#61; 0;\n\tchar maxbuf[12] &#61; {0}; //最大的可能是0-9&#xff0c;最多10个字符&#xff0c;12个空间足够用\n\tchar maxbuf2[12] &#61; {0};\n\tint maxlen &#61; 0;\n\tgets(buf); //实际字符串由外部输入&#xff0c;或者从文件读取&#xff0c;这个根据实际情况修改吧\n\tlen &#61; strlen(buf); //获取实际长度\n\n\tmaxbuf2[0] &#61; buf[0];\n\ti &#61; 1;\n\tindex &#61; 1;\n\twhile(i &lt; len)\n\t{\n\t\tif (buf[i] &gt; buf[i-1])\n\t\t{\n\t\t\tmaxbuf2[index] &#61; buf[i];\n\t\t\tindex&#43;&#43;;\n\t\t}else\n\t\t{\n\t\t\tif (index &gt; maxlen)\n\t\t\t{\n\t\t\t\tmaxlen &#61; index;\n\t\t\t\tstrcpy(maxbuf,maxbuf2);\n\t\t\t\tmaxbuf[index] &#61; &#39;\\0&#39;;\n\t\t\t\t\n\t\t\t\tmaxbuf2[0] &#61; buf[i];\n\t\t\t\tindex &#61; 1;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tmaxbuf2[0] &#61; buf[i];\n\t\t\t\tindex &#61; 1;\n\t\t\t}\n\t\t}\n\t\ti&#43;&#43;;\n\t}\n\t//处理最后一个串\n\tif (index &gt; maxlen)\n\t{\n\t\tmaxlen &#61; index;\n\t\tstrcpy(maxbuf,maxbuf2);\n\t\tmaxbuf[index] &#61; &#39;\\0&#39;;\n\t}\n\tprintf(&#34;最大串长度:%d,字符串:%s\\n&#34;,maxlen,maxbuf);\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <string.h>\n#define MAX_SIZE 100000\nint main()\n{\n\tchar buf[MAX_SIZE] = {0};\n\tint i = 0,len = 0,index = 0;\n\tchar maxbuf[12] = {0}; \n\tchar maxbuf2[12] = {0};\n\tint maxlen = 0;\n\tgets(buf); \n\tlen = strlen(buf); \n\tmaxbuf2[0] = buf[0];\n\ti = 1;\n\tindex = 1;\n\twhile(i < len)\n\t{\n\t\tif (buf[i] > buf[i-1])\n\t\t{\n\t\t\tmaxbuf2[index] = buf[i];\n\t\t\tindex++;\n\t\t}else\n\t\t{\n\t\t\tif (index > maxlen)\n\t\t\t{\n\t\t\t\tmaxlen = index;\n\t\t\t\tstrcpy(maxbuf,maxbuf2);\n\t\t\t\tmaxbuf[index] = '\\0';\n\t\t\t\tmaxbuf2[0] = buf[i];\n\t\t\t\tindex = 1;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tmaxbuf2[0] = buf[i];\n\t\t\t\tindex = 1;\n\t\t\t}\n\t\t}\n\t\ti++;\n\t}\n\tif (index > maxlen)\n\t{\n\t\tmaxlen = index;\n\t\tstrcpy(maxbuf,maxbuf2);\n\t\tmaxbuf[index] = '\\0';\n\t}\n\tprintf(\"最大串长度:%d,字符串:%s\\n\",maxlen,maxbuf);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469968",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/137.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 985858,
"question_title": "移动数组中的元素",
"question_content": "题目描述\n将一维数组中的元素循环左移 k 个位置\n输入描述\n第 1 行是一维数组元素的个数 n (数组大小)\n第 2 行是一个整数 k , 表示移动的位置\n下面 n 行为数组的元素个数\n输出描述\n输出 n 行,表示移动后的数字",
"difficulty": "简单",
"answer_id": 1149184,
"answer_content": "\n```\n#include<stdio.h>\n#define N 10000\nint main()\n{\n int k,a[N],b[N],n,t,w,i;\n scanf(\"%d\",&n);\n scanf(\"%d\",&k);\n for(i=0;i<n;i++)\n\t\tscanf(\"%d\",&a[i]);\n for (i = 0; i < k % n; i++)\n\t\tb[i] = a[i];\n\tfor (i = 0; i < n; i++)\n\t{\n\t\tif (i < n - k % n)\n\t\t\ta[i] = a[i + k % n];\n\t\telse\n\t\t\ta[i] = b[i - n + k % n];\n\t}\n\tfor(i=0;i<n;i++)\n\t\tprintf(\"%d\\n\",a[i]);\n\treturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#define N 10000\nint main()\n{\n\tint k,a[N],b[N],n,t,w,i;\n\tscanf(\"%d\",&n);\n\tscanf(\"%d\",&k);\n\tfor(i=0;i<n;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tfor (i = 0; i < k % n; i++)\n\t\tb[i] = a[i];\n\tfor (i = 0; i < n; i++)\n\t{\n\t\tif (i < n - k % n)\n\t\t\ta[i] = a[i + k % n];\n\t\telse\n\t\t\ta[i] = b[i - n + k % n];\n\t}\n\tfor(i=0;i<n;i++)\n\t\tprintf(\"%d\\n\",a[i]);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469868",
"status": 1,
"keywords": "算法高阶,数论算法,元素的幂,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/138.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7408274,
"question_title": "约瑟夫问题",
"question_content": "\n<p>有n人&#xff08;编号分别为1到n号&#xff09;围成一圈&#xff0c;从第s人开始报数&#xff0c;报到第m的人出列&#xff0c;然后从出列的下一人重新开始报数&#xff0c;报到第m的人又出列&#xff0c;……&#xff0c;如此重复直到n-1全部出列&#xff0c;只剩最后一个人为止。求剩下的最后一人是谁&#xff1f;\n输入\n一行&#xff0c;三个整数n&#xff0c;m&#xff0c;s&#xff0c;&#xff08;0&lt;&#61;n&#xff0c;m&#xff0c;s&lt;&#61;1000&#xff09;意义如上。\n输出\n一行&#xff0c;一个正整数&#xff0c;表示最后剩下的人的编号。\n输入样例</p>\n<pre>\n<code class=\"language-html\">8 3 1</code></pre>\n<p>输出样例</p>\n<pre>\n<code class=\"language-html\">7</code>\n</pre>\n<p>要求从第s个数开始&#xff0c;数m个出列&#xff0c;第s个不出列</p>",
"difficulty": "困难",
"answer_id": 53358508,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\n \nvoid JosePhus(int n, int m, int start) {\n    int i, *arr &#61; new int[n]; // 动态分配数组\n    int count &#61; 1;; // 保存当前已站出来的人数\n    for(i &#61; 0;i &lt; n; i&#43;&#43;) // 初始化&#xff0c;把各位置号存入数组中\n        arr[i] &#61; i &#43; 1;\n    int sum &#61; n;\n    //输出参考    printf(&#34;出列顺序为&#xff1a;&#34;);\n    while(count &lt; n) { // 出列顺序\n        start--; // 因为数组下标从0开始&#xff0c;所以要减1\n        int index &#61; (start&#43;m-1) % sum;//记录第count次出列的下标 \n    //输出参考  std::cout &lt;&lt; arr[index] &lt;&lt; &#34; &#34;; // 输出当前要站出来的人的位置号\n        for(i &#61; index; i &lt; sum-1; i&#43;&#43;)//出列相当于人数少了一个&#xff0c;循环上限为sum-1\n            arr[i] &#61; arr[i&#43;1]; // 把位置号前移\n        start &#61; index &#43; 1;//第count&#43;1次开始位置为上次出列下标位置&#43;1\n        sum--;//总人数减一 \n        count&#43;&#43;;//出列人数&#43;1 \n    }\n    std::cout&lt;&lt; arr[0] &lt;&lt;&#34;\\n&#34;;//最后剩下的第一个元素为最后留下的人\n}\n \nint main(int argc, const char * argv[]) {\n    int n, m, start; // n:人数  m:数到多少出列  start:开始位置\n \n    std::cout &lt;&lt; &#34;请输入n,m,start:\\n&#34;;\n    while(std::cin &gt;&gt; n &gt;&gt; m &gt;&gt; start) {\n        JosePhus(n, m, start); // 调用解决约瑟夫问题的函数\n        std::cout &lt;&lt; &#34;请输入n,m,start:\\n&#34;;\n    }\n    return 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c++",
"cpp": "#include <iostream>\nvoid JosePhus(int n, int m, int start) {\n    int i, *arr = new int[n]; \n    int count = 1;; \n    for(i = 0;i < n; i++) \n        arr[i] = i + 1;\n    int sum = n;\n    while(count < n) { \n        start--; \n        int index = (start+m-1) % sum;\n        for(i = index; i < sum-1; i++)\n            arr[i] = arr[i+1]; \n        start = index + 1;\n        sum--;\n        count++;\n    }\n    std::cout<< arr[0] <<\"\\n\";\n}\nint main(int argc, const char * argv[]) {\n    int n, m, start; \n    std::cout << \"请输入n,m,start:\\n\";\n    while(std::cin >> n >> m >> start) {\n        JosePhus(n, m, start); \n        std::cout << \"请输入n,m,start:\\n\";\n    }\n    return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469969",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/139.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7435119,
"question_title": "指针排序问题",
"question_content": "<p>输入10个数&#xff0c;按绝对值从大到小排序输出。\n输入形式&#xff1a;输入10个float实数\n输出形式&#xff1a;保留小数点后两位有效数字;输出从大到小排列\n</p>",
"difficulty": "困难",
"answer_id": 53405662,
"answer_content": "<pre>\n<code class=\"hljs language-cpp\">#include &lt;math.h&gt;\n#include &lt;stdio.h&gt;\nvoid paixu(float *p,int n)\n{\n\tint i,j;\n\tfor (i &#61; 0; i&lt;n-1; i&#43;&#43;) \n\t{ //控制n-1趟冒泡\n\t\tfor (j &#61; 0; j&lt;n - 1 - i; j&#43;&#43;)\n\t\t{\n\t\t\tif (fabs(p[j])&lt;fabs(p[j &#43; 1])) \n\t\t\t{ //比较相邻的两个元素\n\t\t\t\tfloat tmp; //临时变量\n\t\t\t\ttmp &#61; p[j]; //交换\n\t\t\t\tp[j] &#61; p[j &#43; 1];\n\t\t\t\tp[j &#43; 1] &#61; tmp;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main()\n{\n\tfloat f[10];\n\tint i;\n\tfor(i&#61;0;i&lt;10;i&#43;&#43;)\n\t\tscanf(&#34;%f&#34;,&amp;f[i]);\n\tpaixu(f,10);\n\tfor(i&#61;0;i&lt;10;i&#43;&#43;)\n\t\tprintf(&#34;%.2f &#34;,f[i]);\n\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <math.h>\n#include <stdio.h>\nvoid paixu(float *p,int n)\n{\n\tint i,j;\n\tfor (i = 0; i<n-1; i++) \n\t{ \n\t\tfor (j = 0; j<n - 1 - i; j++)\n\t\t{\n\t\t\tif (fabs(p[j])<fabs(p[j + 1])) \n\t\t\t{ \n\t\t\t\tfloat tmp; \n\t\t\t\ttmp = p[j]; \n\t\t\t\tp[j] = p[j + 1];\n\t\t\t\tp[j + 1] = tmp;\n\t\t\t}\n\t\t}\n\t}\n}\nint main()\n{\n\tfloat f[10];\n\tint i;\n\tfor(i=0;i<10;i++)\n\t\tscanf(\"%f\",&f[i]);\n\tpaixu(f,10);\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%.2f \",f[i]);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470144",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/14.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 237827,
"question_title": "字符串排序",
"question_content": "编写程序,输入若干个字符串。\n要求:\n(1)按字符串长度的大小升序输出各个字符串。\n(2)按字符串中字符的ASCII码值大小升序输出各个字符串。",
"difficulty": "简单",
"answer_id": 218781,
"answer_content": "如果按照 1条件优先于2的话,我会这么写。。(你作业好多呀)\n\n```\n #include <string>\n#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\n\n//比较函数,用于排序\nbool compare(string a,string b) {\n //长度不一样的时候采用长度来排序\n if (a.length() != b.length()) {\n return a.length() < b.length();\n }\n //长度一样的时候采用ASCII值排序\n return a < b;\n}\n\nint main()\n{\n \n vector<string>list;\n \n string inputString;\n \n while (cin>>inputString) {\n \n //结束标志,测试方便,可以注释掉\n if (inputString == \"0\") {\n break;\n }\n //加入到vector\n list.push_back(inputString);\n }\n //排序,系统方法\n sort(list.begin(),list.end(),compare);\n //依次输出\n for (int i=0; i<list.size(); i++) {\n cout<<list[i]<<endl;\n }\n \n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <string>\n#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\nbool compare(string a,string b) {\n\tif (a.length() != b.length()) {\n\t return a.length() < b.length();\n\t}\n\treturn a < b;\n}\nint main()\n{\n\tvector<string>list;\n\tstring inputString;\n\twhile (cin>>inputString) {\n\t\tif (inputString == \"0\") {\n\t\t\tbreak;\n\t\t}\n\t\tlist.push_back(inputString);\n\t}\n\tsort(list.begin(),list.end(),compare);\n\tfor (int i=0; i<list.size(); i++) {\n\t\tcout<<list[i]<<endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470184",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/140.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 229922,
"question_title": "求解一元二次方程组根问题",
"question_content": "<pre>利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax<sup>2</sup> + bx + c =0 的根,其中a不等于0。\n输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax<sup>2</sup> + bx + c =0 的系数。输出一行,表示方程的解。\n若两个实根相等,则输出形式为:x1=x2=...。\n若两个实根不等,则输出形式为:x1=...;x2 = ...,其中x1若是两个虚根,则输出:x1=实部+虚部i; x2=实部-虚部i,其中x1,x2满足以下两个条件中的一个:\n1. x1的实部大于x2的实部\n2. x1的实部等于x2的实部且x1的虚部大于等于x2的虚部\n所有实数部分要求精确到小数点后5位,数字、符号之间没有空格。\n样例输入:1.0 2.0 8.0\n样例输出:x1=-1.00000+2.64575i;x2=-1.00000-2.64575i</pre>",
"difficulty": "困难",
"answer_id": 198570,
"answer_content": "\n\n```\n #include <iostream>\n#include<iomanip>\n#include <cmath>\n#include <complex>\nusing namespace std;\nstatic const double e = 1e-12;\nbool operator == (complex<double> c1, complex<double> c2) { return abs(c1-c2) < e;}\nint main()\n{\n complex<double> a,b,c;\n complex<double> x1,x2;\n cin >> a >> b >> c;\n x1 = (-b + sqrt(b*b-a*c*4.0))/(a*2.0);\n x2 = (-b - sqrt(b*b-a*c*4.0))/(a*2.0);\n cout << setiosflags(ios::fixed);\n cout.precision(6);\n if ( abs(x1.imag()) < e ) // real\n {\n if (x1 == x2) {\n cout << \"x1=x2=\" << x1.real();\n } else {\n cout << \"x1=\" << x1.real() <<\";x2=\" << x1.real();\n }\n }\n else {\n cout << \"x1=\" << x1.real()<<\"+\"<<x1.imag()<<\"i;\"\n <<\"x2=\" << x2.real()<<\"+\"<<x2.imag()<<\"i\";\n }\n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include<iomanip>\n#include <cmath>\n#include <complex>\nusing namespace std;\nstatic const double e = 1e-12;\nbool operator == (complex<double> c1, complex<double> c2) { return abs(c1-c2) < e;}\nint main()\n{\n\tcomplex<double> a,b,c;\n\tcomplex<double> x1,x2;\n\tcin >> a >> b >> c;\n\tx1 = (-b + sqrt(b*b-a*c*4.0))/(a*2.0);\n\tx2 = (-b - sqrt(b*b-a*c*4.0))/(a*2.0);\n\tcout << setiosflags(ios::fixed);\n\tcout.precision(6);\n\tif ( abs(x1.imag()) < e ) \n\t{\n\t\tif (x1 == x2) {\n\t\t\tcout << \"x1=x2=\" << x1.real();\n\t\t} else {\n\t\t\tcout << \"x1=\" << x1.real() <<\";x2=\" << x1.real();\n\t\t}\n\t}\n\telse {\n\t\tcout << \"x1=\" << x1.real()<<\"+\"<<x1.imag()<<\"i;\"\n\t\t\t\t<<\"x2=\" << x2.real()<<\"+\"<<x2.imag()<<\"i\";\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470279",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/141.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1061901,
"question_title": "移动字符串",
"question_content": "给定一个字符串长度为 nn 的字符串 s1 (10<n<100) , 求出将字符串循环向左移动 k 位的字符串 s2 (1<k<n) , 例如:字符串 abcdefghijk , 循环向左移动 3 位就变成 defghijkabc\n输入描述\n输入仅两行,第一行为左移的位数 k , 第二行为字符串 s1 .\n输出描述\n输出仅一行,为将字符串 s1 左移 k 位得到的字符串 s2 .\n样例输入\n3\nabcdefghijk\n样例输出\ndefghijkabc",
"difficulty": "中等",
"answer_id": 1259749,
"answer_content": "题目没有说要循环啊\n\n```\n#include<iostream>\nusing namespace std;\n#include <string.h>\nvoid reverse(char *a,int start, int end)\n{\n int i ,j,temp;\n for(i = start,j = end; i < j; i++,j--)\n {\n temp = a[i];\n a[i] = a[j];\n a[j] = temp;\n } \n}\nvoid turnleft(char *a,int i,int n)\n{\n int left = i % n;\n if(left == 0)\n return ;\n reverse(a,0,left-1);\n reverse(a,left,n-1);\n reverse(a,0,n-1);\n return ;\n}\nint main()\n{\n char a[1024];\n int i; \n\tcin>>i;\n\tcin>>a;\n int n = strlen(a);\n turnleft(a,i,n);\n\tcout<<a<<endl;\n}\n```",
"tag_name": "c++",
"cpp": "#include<iostream>\n#include <string.h>\nusing namespace std;\nvoid reverse(char *a,int start, int end)\n{\n\tint i ,j,temp;\n\tfor(i = start,j = end; i < j; i++,j--)\n\t{\n\t\ttemp = a[i];\n\t\ta[i] = a[j];\n\t\ta[j] = temp;\n\t} \n}\nvoid turnleft(char *a,int i,int n)\n{\n\tint left = i % n;\n\tif(left == 0)\n\t\treturn ;\n\treverse(a,0,left-1);\n\treverse(a,left,n-1);\n\treverse(a,0,n-1);\n\treturn ;\n}\nint main()\n{\n\tchar a[1024];\n\tint i;\t\n\tcin>>i;\n\tcin>>a;\n\tint n = strlen(a);\n\tturnleft(a,i,n);\n\tcout<<a<<endl;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470280",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/142.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 769920,
"question_title": "给出一个由O和X组成的串,长度为1~80,统计得分",
"question_content": "给出一个由O和X组成的串,长度为1~80,统计得分,每个O的得分为目前连续出现的O的个数,X的得分为0,例如OOXXO的得分为1+2+0+0+1.",
"difficulty": "简单",
"answer_id": 831163,
"answer_content": "//有一些小问题,改了一下\n#include\"stdlib.h\"\n#include\"string.h\"\n#include\"stdio.h\"\nint main()\n{\n\t\tchar s[85];\n int score = 0,x;\n scanf(\"%s\",&s);\n for (int i = 0;s[i];i++)\n {\n \t\n if (s[i] == 'X')\n\t\tscore += 0;\n if (s[i] == 'O')\n {\n int temp = 1;\n if (i == 0)score += 1;//这里要改为score+=1;否则当O处于第一个的时候会出统计错误问题\n else\n {\n \tx=i;//主要问题在于循环查找O的时候外部‘i'与内部i发生冲突\n while (x > 0)\n {\n if (s[x - 1] == 'O')temp++;\n else break; \n x--;\n }\n score += temp;\n }\n }\n }\n printf(\"%d\\n\", score);\n\t\tsystem(\"pause\");\n return 0;\n}",
"tag_name": "c语言",
"cpp": "#include\"stdlib.h\"\n#include\"string.h\"\n#include\"stdio.h\"\nint main()\n{\n\tchar s[85];\n\tint score = 0,x;\n\tscanf(\"%s\",&s);\n\tfor (int i = 0;s[i];i++)\n\t{\n\t\tif (s[i] == 'X')\n\t\tscore += 0;\n\t\tif (s[i] == 'O')\n\t\t{\n\t\t\tint temp = 1;\n\t\t\tif (i == 0)score += 1;\n\t\t\telse\n\t\t\t{\n\t\t\t\tx=i;\n\t\t\t\twhile (x > 0)\n\t\t\t\t{\n\t\t\t\t\tif (s[x - 1] == 'O')temp++;\n\t\t\t\t\telse break; \n\t\t\t\t\tx--;\n\t\t\t\t}\n\t\t\t\tscore += temp;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", score);\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469970",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/143.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1081343,
"question_title": "编写计算组合数的程序?",
"question_content": "1.编写计算组合数的程序。\n\n问题分析:\n\n(1)组合数的计算公式为:\n\n \n\n(2)此公式中用到3次阶乘的计算,所以可以编写一个求阶层函数fact(x)。\n\n(3)函数fact(x)的功能为计算整数x的阶层。\n\n要求:上传运行结果截图时,输入n的值为10,m的值为4。程序运行结果参考下图:![图片说明](https://img-ask.csdn.net/upload/202006/07/1591491052_938571.png)",
"difficulty": "简单",
"answer_id": 1287002,
"answer_content": "\n```\n#include <stdio.h>\nint fact(int n)\n{\nint r = 1;\nfor (int i = 1; i <= n; i++) r *= i;\nreturn r;\n}\nint main()\n{\nint m, n;\nscanf(\"%d%d\", &m, &n);\nint c = fact(n) / (fact(m) * fact(n - m));\nprintf(\"c=%d\", c);\nreturn 0;\n}\n```\n# 问题解决的话,请点下采纳",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint fact(int n)\n{\nint r = 1;\nfor (int i = 1; i <= n; i++) r *= i;\nreturn r;\n}\nint main()\n{\nint m, n;\nscanf(\"%d%d\", &m, &n);\nint c = fact(n) / (fact(m) * fact(n - m));\nprintf(\"c=%d\", c);\nreturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469869",
"status": 0,
"keywords": "算法中阶,摊还分析,聚合分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/144.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 227460,
"question_title": "实现从键盘输入一行字符串,将该字符串中所有的apple换成orange.如:I have an apple.Do you like apple? 变为I have an orange.Do you like orange?",
"question_content": "实现从键盘输入一行字符串,将该字符串中所有的apple换成orange.如:I have an apple.Do you like apple? 变为I have an orange.Do you like orange?",
"difficulty": "中等",
"answer_id": 191734,
"answer_content": "\n\n```\n #include<stdio.h>\n#include<stdlib.h>\nint main()\n{\n\tchar s[]=\"I have an apple Do you like apple\";\n\tchar s1[]=\"apple\";\n\tchar s2[]=\"orange\";\n\tint a[10],i,j,k,n=0,h,m=0;\n\tfor(i=0;s[i]!='\\0';i++)\n\t\tfor(j=i,k=0;s1[k]==s[j];k++,j++)\n\t\t\tif(s1[k+1]=='\\0')\n\t\t\t{\n\t\t\t\tm++;\n\t\t\t\ta[n++]=j-k;\n\t\t\t\tbreak;\n\t\t\t}\n\tfor(n=0;n<m;n++)\n\t\tfor(i=a[n],h=0;s[i]!='\\0'&&s2[h]!='\\0';)\n\t\t\ts[i++]=s2[h++];\n\tprintf(\"%s\\n\",s);\n\tsystem(\"pause\");\n\treturn 0;\n}\n```\n\n![图片说明](https://img-ask.csdn.net/upload/201512/19/1450524934_139844.jpg)\n",
"tag_name": "c语言",
"cpp": " #include<stdio.h>\n#include<stdlib.h>\nint main()\n{\n\tchar s[]=\"I have an apple Do you like apple\";\n\tchar s1[]=\"apple\";\n\tchar s2[]=\"orange\";\n\tint a[10],i,j,k,n=0,h,m=0;\n\tfor(i=0;s[i]!='\\0';i++)\n\t\tfor(j=i,k=0;s1[k]==s[j];k++,j++)\n\t\t\tif(s1[k+1]=='\\0')\n\t\t\t{\n\t\t\t\tm++;\n\t\t\t\ta[n++]=j-k;\n\t\t\t\tbreak;\n\t\t\t}\n\tfor(n=0;n<m;n++)\n\t\tfor(i=a[n],h=0;s[i]!='\\0'&&s2[h]!='\\0';)\n\t\t\ts[i++]=s2[h++];\n\tprintf(\"%s\\n\",s);\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470250",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/15.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 696086,
"question_title": "海港(port)",
"question_content": "海港(port)\n【问题描述】\n小谢是海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客。\n小谢对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只情况;对于第i艘到达的船,他记录了这艘船只到达的时间ti(单位:秒),船上的乘客数量Ki,以及每名乘客的国籍x(i,1),x(i,2),···,x(i,k)。\n小谢统计了n艘船的信息,希望你帮忙计算出以每一艘船到达时间为止的24小时(24小时=86400秒)内所有乘船到达的乘客来自多少个不同的国家。\n形式化的讲,你需要计算n条信息。对于输出的第i条信息,你需要统计满足:ti-86400<tp<=ti的船只p,在所有的x(p,j)中,总共有多少个不同的数。\n输入格式:第1行输入一个正整数n,表示小谢统计了n艘船的信息。\n接下来的n行,每行描述一艘船的信息:前两个整数ti和ki分别表示这艘船到达海港的时间和船上的乘客数量,接下来的ki个整数x(i,j)表示从小谢第一次上班开始计时,这艘船在第ti秒到达海港。\n保证1<=n<=105,ki>=1,∑ki<=3×105,1<=x(i,j)<=105,1<=ti-1<ti<=109。其中∑ki表示所有ki的和。输出格式\n输出n行,第i行输出一个整数表示第i艘船到达后的统计信息。\n【输入样例1】\n3\n1 4 4 1 2 2\n2 2 2 3\n10 1 3\n【输出样例1】\n3\n4\n4\n样例1说明:第一艘船在第一秒到达海港,最近24小时到达的船是第一艘船,共4个乘客,分别来自国家4,1,2,2,共来自3个不同的国家。\n第2艘船在第2秒到达海港,最近24小时到达的船是第1艘船和第2艘船,共有4+2=6个乘客,分别来自国家4,1,2,2,2,3,共来自4个不同的国家;\n第三艘船在第10秒到达海港,最近24小时到达的船是第1艘船、第2艘船和第3艘船,共有4+2+1=7个乘客,分别是来自国家4,1,2,2,2,3,3,共来自4个不同的国家。\n【输入样例2】\n4\n1 4 1 2 2 3\n3 2 2 3\n86401 2 3 4\n86402 1 5\n【输出样例2】\n3\n3\n3\n4\n样例2说明:第一艘船在第一秒到达海港,最近24小时到达的船是第1艘,共有4个乘客,分别是来自国家1,2,2,3,共来自3个不同的国家。\n第2艘船是第3秒到达海港,最近24小时到达的船是第一艘船和第2艘船,共有4+2=6个乘客,分别来自1,2,2,3,2,3,共来自3个不同的国家\n第3艘船是第86401秒到达海港,最近24小时到达的船是第2艘船和第3艘船,共有2+2=4个乘客,分别来自2.3,3,4,共来自3个不同的国家\n第4艘船是第86402秒到达海港,最近24小时到达的船是第2艘船、第3艘船和第4艘船,共有2+2+1=5个乘客,分别来自2,3,3,4,5,共来自4个不同的国家",
"difficulty": "困难",
"answer_id": 579334,
"answer_content": "\n\n```\n 主要思路是利用结构体和队列 记录每个人的国籍和时间 \n再用sum记录下从开始的时候海港一共来过多少个不同国籍的 \n每次找的时候 用old=q.front 如果这个人在一天以前来的 \n就把他赶走 如果他们国家就他一个 那么答案数sun减一 \n最后输出sum就行了\n\n#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<algorithm>\n#include<cmath>\n#include<queue>\nusing namespace std;\nint a[100100];\nint people[500100];\nstruct node{\n int country;\n int time;\n};\nqueue<node>q;\nint main(){\n int n,sum=0;\n scanf(\"%d\",&n);\n for(int i=1;i<=n;i++){\n int t,p;\n scanf(\"%d%d\",&t,&p);\n node temp;\n temp.time=t;\n for(int i=1;i<=p;i++){\n int cty;\n scanf(\"%d\",&cty);\n temp.country=cty;\n q.push(temp);\n if(!people[cty]) sum++;\n people[cty]++;\n }\n while(1){\n node old;\n old=q.front();\n if(temp.time-86400>=old.time)\n {\n int tc=old.country;\n people[tc]--;\n if(!people[tc]) sum--;\n q.pop();\n }\n else break; \n }\n cout<<sum<<endl;\n }\n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <queue>\nusing namespace std;\nint a[100100];\nint people[500100];\nstruct node{\n\tint country;\n\tint time;\n};\nqueue<node>q;\nint main(){\n\tint n,sum=0;\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++){\n\t\tint t,p;\n\t\tscanf(\"%d%d\",&t,&p);\n\t\tnode temp;\n\t\ttemp.time=t;\n\t\tfor(int i=1;i<=p;i++){\n\t\t\tint cty;\n\t\t\tscanf(\"%d\",&cty);\n\t\t\ttemp.country=cty;\n\t\t\tq.push(temp);\n\t\t\tif(!people[cty]) sum++;\n\t\t\tpeople[cty]++;\n\t\t}\n\t\twhile(1){\n\t\t\tnode old;\n\t\t\told=q.front();\n\t\t\tif(temp.time-86400>=old.time)\n\t\t\t{\n\t\t\t\tint tc=old.country;\n\t\t\t\tpeople[tc]--;\n\t\t\t\tif(!people[tc]) sum--;\n\t\t\t\tq.pop();\n\t\t\t}\n\t\t\telse break; \n\t\t}\n\t\tcout<<sum<<endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470251",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量,最坏情况为线性时间的选择算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/16.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7448176,
"question_title": "二维数组找最值",
"question_content": "<pre><p>从键盘输入m(2&lt;&#61;m&lt;&#61;6)行n(2&lt;&#61;n&lt;&#61;6)列整型数据&#xff0c;编程找出其中的最大值及其所在位置的行列下标值并输出。\n输入格式:\n在第一行输入数据的行数m和列数n的值&#xff0c;从第二行开始以二维数组的形式依次输入m行n列整型数据。\n输出格式:\n依次输出最大值及其所在位置的行列下标值&#xff0c;中间以逗号,分隔&#xff0c;最后换行。\n</pre>输入样例:</p>\n<pre>\n<code>3 4\n1 2 3 4\n8 9 7 6\n5 6 7 0</code></pre>\n<p>输出样例:</p>\n<pre>\n<code>9,1,1</code></pre>",
"difficulty": "中等",
"answer_id": 53428211,
"answer_content": "<p>代码如下&#xff1a;</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\nint main ()\n{\n\tint a[6][6];\n\tint m,n;\n\tint i,j;\n\tint max;\n\tint indexx&#61;0,indexy&#61;0;\n\tscanf(&#34;%d %d&#34;,&amp;m,&amp;n);\n\tfor (i &#61; 0;i&lt;m;i&#43;&#43;)\n\t{\n\t\tfor (j &#61; 0;j&lt;n;j&#43;&#43;)\n\t\t{\n\t\t\tscanf(&#34;%d&#34;,&amp;a[i][j]);\n\t\t\tif(i &#61;&#61; 0 &amp;&amp; j&#61;&#61;0)\n\t\t\t{\n\t\t\t\tmax &#61; a[i][j];\n\t\t\t\tindexx &#61; 0;\n\t\t\t\tindexy &#61; 0;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tif (a[i][j] &gt; max)\n\t\t\t\t{\n\t\t\t\t\tmax &#61; a[i][j];\n\t\t\t\t\tindexx &#61; i;\n\t\t\t\t\tindexy &#61; j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\tprintf(&#34;%d,%d,%d\\n&#34;,max,indexx,indexy);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main ()\n{\n\tint a[6][6];\n\tint m,n;\n\tint i,j;\n\tint max;\n\tint indexx=0,indexy=0;\n\tscanf(\"%d %d\",&m,&n);\n\tfor (i = 0;i<m;i++)\n\t{\n\t\tfor (j = 0;j<n;j++)\n\t\t{\n\t\t\tscanf(\"%d\",&a[i][j]);\n\t\t\tif(i == 0 && j==0)\n\t\t\t{\n\t\t\t\tmax = a[i][j];\n\t\t\t\tindexx = 0;\n\t\t\t\tindexy = 0;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tif (a[i][j] > max)\n\t\t\t\t{\n\t\t\t\t\tmax = a[i][j];\n\t\t\t\t\tindexx = i;\n\t\t\t\t\tindexy = j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d,%d,%d\\n\",max,indexx,indexy);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470252",
"status": 1,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/17.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1064299,
"question_title": "请问如何实现这个代码的多组输入输出?",
"question_content": "<p>给定 2 个正整数 a, b ,a 和 b 最多可能有 40 位,求出 a + b 的和。\n输入描述\n两个正整数 a, b,a 和 b 最多可能有 40 位。一行表示一个数。\n输出描述\na + b 的和。</p><p>样例输入</p><pre><code class=\"language-\" style=\"white-space: break-spaces;\">111111111111111111111111111111111111111\n222222222222222222222222222222222222222\n</code></pre><p>样例输出</p><pre><code class=\"language-\" style=\"white-space: break-spaces;\">333333333333333333333333333333333333333\n</code></pre>",
"difficulty": "简单",
"answer_id": 1262493,
"answer_content": "\n```\n#include<iostream>\n#include<cstring>\nusing namespace std;\nint main(){\n\twhile (1)\n\t{\n\t\tchar s1[200],s2[200];\n\t\tint a[200]={0},b[200]={0},l1,l2,c,k,i;\n\t\tgets(s1);\n\t\tl1=strlen(s1);\n\t\tif (l1 == 0) break;\n\t\tgets(s2);\n\t\tl2=strlen(s2);\n\t\tif(l1<l2) k=l2;\n\t\telse k=l1;c=k;\n\t\tfor(i=0;i<l1;k--,i++)\n\t\t\ta[k]=s1[l1-1-i]-'0';\n\t\tfor(k=c,i=0;i<l2;k--,i++)\n\t\t\tb[k]=s2[l2-1-i]-'0';\n\t\tfor(i=c;i>=0;i--){\n\t\t\ta[i]+=b[i];\n\t\t\tif(a[i]>=10){\n\t\t\t\ta[i]=10;\n\t\t\t\ta[i-1]++;\n\t\t\t}\n\t\t}\n\t\tif(a[0]!=0){\n\t\t\tfor(i=0;i<=c;i++)\n\t\t\t\tcout<<a[i];\n\t\t}else{\n\t\t\tfor(i=1;i<=c;i++)\n\t\t\t\tcout<<a[i];\n\t\t}\n\t\tcout << endl;\n\t}\n}\n```\n按回车退出。",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <cstring>\nusing namespace std;\nint main(){\n\twhile (1)\n\t{\n\t\tchar s1[200],s2[200];\n\t\tint a[200]={0},b[200]={0},l1,l2,c,k,i;\n\t\tgets(s1);\n\t\tl1=strlen(s1);\n\t\tif (l1 == 0) break;\n\t\tgets(s2);\n\t\tl2=strlen(s2);\n\t\tif(l1<l2) k=l2;\n\t\telse k=l1;c=k;\n\t\tfor(i=0;i<l1;k--,i++)\n\t\t\ta[k]=s1[l1-1-i]-'0';\n\t\tfor(k=c,i=0;i<l2;k--,i++)\n\t\t\tb[k]=s2[l2-1-i]-'0';\n\t\tfor(i=c;i>=0;i--){\n\t\t\ta[i]+=b[i];\n\t\t\tif(a[i]>=10){\n\t\t\t\ta[i]=10;\n\t\t\t\ta[i-1]++;\n\t\t\t}\n\t\t}\n\t\tif(a[0]!=0){\n\t\t\tfor(i=0;i<=c;i++)\n\t\t\t\tcout<<a[i];\n\t\t}else{\n\t\t\tfor(i=1;i<=c;i++)\n\t\t\t\tcout<<a[i];\n\t\t}\n\t\tcout << endl;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470253",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/18.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7457152,
"question_title": "1,3,3,5,5,5,7,7,7,7,9,9,9,9,9,.....",
"question_content": "<p><a href=\"http://114.67.86.56:5000/Problem_Show.asp?id&#61;1045\">简单数列5</a><br />\n<strong> </strong>已知一数列&#xff1a;1,3,3,5,5,5,7,7,7,7,9,9,9,9,9,11,11,11,11,11,11,11.....请根据规律求出第n项的值。例如&#xff1a;n&#61;4 项时&#xff0c;其值为5,n&#61;11时&#xff0c;其值为9.\n<strong>输入格式 </strong>一个自然数n,(0&lt;n&lt;1000)<br />\n<br />\n<strong>输出格式</strong> 一个整数&#xff0c;即第n项的值。<br />\n<strong>样例输入&#xff1a;4</strong><br />\n<strong>样例输出 &#xff1a;5</strong></p>\n",
"difficulty": "简单",
"answer_id": 53441264,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main(){\n int a,b;\n cin&gt;&gt;a;\n \n int n&#61;1,count&#61;1,num&#61;1;\n \n for(int i&#61;0;i&lt;a;i&#43;&#43;)\n {\n if(count &gt; n)\n {\n num&#43;&#61;2;\n n&#43;&#43;;\n count&#61;1;\n i--;\n }\n else\n {\n cout &lt;&lt; num &lt;&lt; &#34;,&#34;; //输出整个数列\n count&#43;&#43;;\n }\n }\n cout &lt;&lt; endl;\n \n \n cout &lt;&lt; num;\n \n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <iostream>\nusing namespace std;\nint main(){\n\tint a,b;\n\tcin>>a;\n\tint n=1,count=1,num=1;\n\tfor(int i=0;i<a;i++)\n\t{\n\t\tif(count > n)\n\t\t{\n\t\t\tnum+=2;\n\t\t\tn++;\n\t\t\tcount=1;\n\t\t\ti--;\n\t\t}\n\t\telse\n\t\t{\n\t\tcout << num << \",\";\t\n\t\tcount++;\n\t\t}\n\t}\n\tcout << endl;\n\tcout << num;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470254",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/19.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7398746,
"question_title": "可怕的流感 ",
"question_content": "<p style=\"margin-left:0pt; margin-right:0pt\">Ø 问题描述&#xff1a;已知一只家禽得了流感&#xff0c;流感的传播时间为 24 小时&#xff0c;在这 24 小时内最多能将流感传给其它 M 只家禽&#xff0c;农场主需要购买紧急药品&#xff0c;假设发现时已经过了 N 天&#xff0c;那么农场主需要至少买多少包药呢&#xff1f;&#xff08;一包药为一只家禽用量&#xff09;</p><p style=\"margin-left:0pt; margin-right:0pt\">Ø 输入&#xff1a;一行&#xff0c;两个整数&#xff0c;第一个表示流感传染家禽的数量 M&#xff0c;第二个表示发现时已过的天数。</p><p style=\"margin-left:0pt; margin-right:0pt\">Ø 输出&#xff1a;一行&#xff0c;一个整数&#xff0c;表示需要药的包数。</p><p style=\"margin-left:0pt; margin-right:0pt\">Ø 样例输入&#xff1a;</p><p style=\"margin-left:0pt; margin-right:0pt\">10 2</p><p style=\"margin-left:0pt; margin-right:0pt\">Ø 样例输出&#xff1a;</p><p style=\"margin-left:0pt; margin-right:0pt\">121</p>",
"difficulty": "困难",
"answer_id": 53345307,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;iostream&gt;\nusing namespace std;\nint total(int x); //定义函数\nint m; //m为感染数\nint main() {\n\tint x; //x为发现天数\n\tcin &gt;&gt; m &gt;&gt; x;\n\tint to &#61; total(x); //to为结果\n\tcout &lt;&lt; to;\n\treturn 0;\n}\nint total(int x) {\n\tif (x &gt; 0)\n\t{\n\t\treturn (m &#43; 1) * total(x - 1);\n\t}\n\telse return 1;\n}\n</code></pre>\n\n<p> <img alt=\"\" height=\"80\" src=\"https://img-ask.csdnimg.cn/upload/1612343504683.png\" width=\"363\" /></p>",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint total(int x);\t\t\t \nint m;\t\t\t\t\t\t \nint main() {\n\tint x;\t\t\t\t\t \n\tcin >> m >> x;\n\tint to = total(x);\t\t \n\tcout << to;\n\treturn 0;\n}\nint total(int x) {\n\tif (x > 0)\n\t{\n\t\treturn (m + 1) * total(x - 1);\n\t}\n\telse return 1;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469842",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序分析,期望运行时间,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/2.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7431172,
"question_title": "一个班有10个同学,通过键盘输入成绩,并打印输出,每行输出5个同学的成绩。并求出平均成绩,最高分、最低分并输出。",
"question_content": "<p>一个班有10个同学&#xff0c;通过键盘输入成绩&#xff0c;\n并打印输出&#xff0c;每行输出5个同学的成绩。并求出平均成绩&#xff0c;最高分、最低分并输出。算法分析&#xff1a;\n(1)定义一个数组用来存放10个成绩数据。\n(2)用循环结构实现成绩输入&#xff1b;\n(3)用循环结构实现成绩输出,并控制换行&#xff1b;\n(4)使用循环结构求平均成绩、最高分、最低分并输出。</p>",
"difficulty": "简单",
"answer_id": 53399158,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\n\nint main(){\n int x,i,max&#61;0,min&#61;0;\n double sum&#61;0,ave&#61;0;\n int a[10];\n for(i&#61;0;i&lt;10;i&#43;&#43;){\n scanf(&#34;%d&#34;,&amp;a[i]);\n sum&#43;&#61;a[i];\n if(max&lt;a[i])\n max&#61;a[i];\n if(min&gt;a[i])\n min&#61;a[i];\n }\n ave&#61;sum/10;\n for(i&#61;0;i&lt;5;i&#43;&#43;)\n printf(&#34;%d &#34;,a[i]);\n printf(&#34;\\n&#34;);\n for(i&#61;5;i&lt;10;i&#43;&#43;)\n printf(&#34;%d &#34;,a[i]);\n printf(&#34;平均成绩%f,最高分%d,最低分%d &#34;,ave,max,min);\n\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main(){\n\tint x,i,max=0,min=0;\n\tdouble sum=0,ave=0;\n\tint a[10];\n\tfor(i=0;i<10;i++){\n\t\tscanf(\"%d\",&a[i]);\n\t\tif(i==0)\n\t\t\tmin = a[i];\n\t\tsum+=a[i];\n\t\tif(max<a[i])\n\t\t\tmax=a[i];\n\t\tif(min>a[i])\n\t\t\tmin=a[i];\n\t}\n\tave=sum/10;\n\tfor(i=0;i<5;i++)\n\t\tprintf(\"%d \",a[i]);\n\t printf(\"\\n\");\n\tfor(i=5;i<10;i++)\n\t\tprintf(\"%d \",a[i]);\n\tprintf(\"平均成绩%f,最高分%d,最低分%d \",ave,max,min);\n}",
"topic_link": "https://bbs.csdn.net/topics/600470145",
"status": 1,
"keywords": "算法初阶,基础知识,算法基础,设计算法,分析分治算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/20.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7404098,
"question_title": "一个整数的序列,要求对其重新排序",
"question_content": "<p>一个整数的序列&#xff0c;要求对其重新排序。排序要求:\n1.奇数在前&#xff0c;偶数在后&#xff1b;\n2.奇数按从大到小排序&#xff1b;\n3.偶数按从小到大排序。<br />\n输入一行&#xff0c;包含整数个数n&#xff0c; n个整数值&#xff0c;彼此以一个空格分开。\n按照要求排序后输出一行&#xff0c;包含排序后的n 个整数。\n </p>",
"difficulty": "简单",
"answer_id": 53352887,
"answer_content": "<p>用一个数组也可以的&#xff1a;</p>\n\n<p>#include &lt;iostream&gt;<br />\n#include&lt;algorithm&gt;<br />\nusing namespace std;</p>\n\n<p>bool cmp(int a, int b){<!-- --><br />\n    int x &#61; a % 2;<br />\n    int y &#61; b % 2;<br />\n    if(x &#61;&#61; y)<br />\n        if(x &#61;&#61; 0)<br />\n            return a &lt; b;<br />\n        else<br />\n            return a &gt; b;<br />\n    else<br />\n        return x &gt; y;<br />\n}</p>\n\n<p><br />\nint main()<br />\n{<!-- --><br />\n    int n, i;<br />\n    cin &gt;&gt; n;<br />\n    int a[n];<br />\n    for(i &#61; 0; i &lt; n; i&#43;&#43;)<br />\n        cin &gt;&gt; a[i];<br />\n    sort(a, a&#43;n, cmp);<br />\n    for(i &#61; 0; i &lt; n; i&#43;&#43;)<br />\n        cout &lt;&lt; a[i] &lt;&lt; &#34; &#34;;<br />\n}</p>",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nbool cmp(int a, int b){\n    int x = a % 2;\n    int y = b % 2;\n    if(x == y)\n        if(x == 0)\n            return a < b;\n        else\n            return a > b;\n    else\n        return x > y;\n}\nint main()\n{\n    int n, i;\n    cin >> n;\n    int a[n];\n    for(i = 0; i < n; i++)\n        cin >> a[i];\n    sort(a, a+n, cmp);\n    for(i = 0; i < n; i++)\n        cout << a[i] << \" \";\n}",
"topic_link": "https://bbs.csdn.net/topics/600470146",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/21.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 768078,
"question_title": "如何根据一个值,查找一个对应关系表,获得对应值呢?",
"question_content": "<p>如何根据一个小范围值区间,查找一个对应关系表,获得一个对应值呢\n例如电压值为2000~2026(mv),![图片说明](https://img-ask.csdn.net/upload/201907/03/1562152764_309711.png)\n其对应功率值为-52(dB),如何编程,完成建表,查表,获得结果呢?</p>",
"difficulty": "困难",
"answer_id": 827931,
"answer_content": "\n```\n/*17点:一一对应版本*/\n#include <stdio.h>\n\n#define NG (0xFF)\n\n//data structure\ntypedef struct\n{\n\t\tint voltage;\n\t\tint power;\t\n}stVoltPowerTbl;\n\n//table\nconst stVoltPowerTbl myTable[] = \n{\n\t\t{-19,1083},\n\t\t{-18,1049},\n\t\t{-17,1025},\n\t\t{-16,996},\n\t\t//add your data \n};\n\nint Find(int voltage)\n{\n\t\tint Ret = NG;\n\t\tint i = 0;\n\t\t\n\t\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t\t{\n\t\t\t\tif(myTable[i].voltage == voltage)\n\t\t\t\t{\n\t\t\t\t\t\tRet = myTable[i].power;\n\t\t\t\t}\n\t\t}\n\t\treturn Ret;\n}\n\nint main()\n{\n\t\tint Ret = 0;\n\t\t\n\t\tRet = Find(-17);\n\t\t\n\t\tif(Ret != NG)\n\t\t{ \n\t\t\t\tprintf(\"power = %d \\n\",Ret);\n\t\t}\n\t\telse\n\t\t{ \n\t\t\t\tprintf(\"Error Voltage! \\n\");\n\t\t}\n\t\t\n\t\treturn 0;\n}\n```\n\n```\n/*21点:根据范围求对应值版本*/\n#include <stdio.h>\n\n#define NG (0xFF)\n\n//data structure\ntypedef struct\n{\n\t\tint power;\t\t\n\t\tint minVoltage;\n\t\tint maxVoltage;\n}stVoltPowerTbl;\n\n//table\nconst stVoltPowerTbl myTable[] = \n{\n\t\t{-55,2080,2094},\n\t\t{-54,2060,2075},\n\t\t{-52,2000,2026},\n\t\t//add your data \n};\n\n\nint Find(int voltage)\n{\n\t\tint Ret = NG;\n\t\tint i = 0;\n\t\t\n\t\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t\t{\n\t\t\t\tif((myTable[i].minVoltage < voltage)&&(myTable[i].maxVoltage > voltage))\n\t\t\t\t{\n\t\t\t\t\t\tRet = myTable[i].power;\n\t\t\t\t}\n\t\t}\n\t\treturn Ret;\n}\n\nint main()\n{\n\t\tint Ret = 0;\n\t\t\n\t\tRet = Find(2001);\n\t\t\n\t\tif(Ret != NG)\n\t\t{ \n\t\t\t\tprintf(\"power = %d \\n\",Ret);\n\t\t}\n\t\telse\n\t\t{ \n\t\t\t\tprintf(\"Error Voltage! \\n\");\n\t\t}\n\t\t\n\t\treturn 0;\n}\n```",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#define NG (0xFF)\ntypedef struct\n{\n\tint voltage;\n\tint power;\t\n}stVoltPowerTbl;\nconst stVoltPowerTbl myTable[] = \n{\n\t{-19,1083},\n\t{-18,1049},\n\t{-17,1025},\n\t{-16,996},\n};\nint Find(int voltage)\n{\n\tint Ret = NG;\n\tint i = 0;\n\tfor(i=0;i<((sizeof(myTable))/(sizeof(stVoltPowerTbl)));i++)\n\t{\n\t\tif(myTable[i].voltage == voltage)\n\t\t{\n\t\t\tRet = myTable[i].power;\n\t\t}\n\t}\n\treturn Ret;\n}\nint main()\n{\n\tint Ret = 0;\n\tRet = Find(-17);\n\tif(Ret != NG)\n\t{\t\n\t\tprintf(\"power = %d \\n\",Ret);\n\t}\n\telse\n\t{\t\n\t\tprintf(\"Error Voltage! \\n\");\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470255",
"status": 0,
"keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/22.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7409321,
"question_title": "如何实现凯撒密码对文本文件(.txt)的读取穷举暴力破解?",
"question_content": "<p>对文本文件中凯撒密码穷举破解 并把26次结果 输出为二十六个文本文件&#xff08;.txt&#xff09;</p>",
"difficulty": "中等",
"answer_id": 53360172,
"answer_content": "<p> 将输入数据放于&#34;read.txt&#34;文件中&#xff0c;编译运行程序即可。</p>\n\n<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n\nint main(void){\n\tFILE *fp&#61;NULL;\n\tchar c[200000],x;\n\tint i&#61;0,n&#61;0,j&#61;0;\n\t\n\tfp&#61;fopen(&#34;read.txt&#34;,&#34;r&#34;); //读取txt文件数据 \n\twhile((x&#61;fgetc(fp))!&#61;EOF){\n\t\tc[i]&#61;x;\n\t\ti&#43;&#43;;\n\t}\n\tn&#61;i;\n\tc[n]&#61;&#39;\\0&#39;;\n\tfclose(fp);\n\t\n\tchar f[20]&#61;&#34;write00.txt&#34;;\n\tfor(i&#61;1;i&lt;&#61;26;i&#43;&#43;){\n\t\tif(f[6]&#61;&#61;&#39;9&#39;){ //将输出文件从01至26排序 \n\t\t\tf[6]&#61;&#39;0&#39;;\n\t\t\tf[5]&#43;&#43;;\n\t\t}\n\t\telse\n\t\t\tf[6]&#43;&#43;;\n\t\n\t\tfor(j&#61;0;j&lt;n;j&#43;&#43;){ //进行暴力破解&#xff0c;一次将所有字母往后移动一位 \n\t\t\tif(c[j]&gt;&#61;&#39;a&#39;&amp;&amp;c[j]&lt;&#39;z&#39;)\n\t\t\t\tc[j]&#43;&#61;1;\n\t\t\telse if(c[j]&#61;&#61;&#39;z&#39;)\n\t\t\t\tc[j]&#61;&#39;a&#39;;\n\t\t\tif(c[j]&gt;&#61;&#39;A&#39;&amp;&amp;c[j]&lt;&#39;Z&#39;)\n\t\t\t\tc[j]&#43;&#61;1;\n\t\t\telse if(c[j]&#61;&#61;&#39;Z&#39;)\n\t\t\t\tc[j]&#61;&#39;A&#39;;\n\t\t}\n\t\t\n\t\tfp&#61;fopen(f,&#34;w&#34;);//输出文件 \n\t\tfputs(c,fp);\n\t\tfclose(fp);\n\t}\n\treturn 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main(void){\n\tFILE *fp=NULL;\n\tchar c[200000],x;\n\tint i=0,n=0,j=0;\n\tfp=fopen(\"read.txt\",\"r\"); \n\twhile((x=fgetc(fp))!=EOF){\n\t\tc[i]=x;\n\t\ti++;\n\t}\n\tn=i;\n\tc[n]='\\0';\n\tfclose(fp);\n\tchar f[20]=\"write00.txt\";\n\tfor(i=1;i<=26;i++){\n\t\tif(f[6]=='9'){ \n\t\t\tf[6]='0';\n\t\t\tf[5]++;\n\t\t}\n\t\telse\n\t\t\tf[6]++;\n\t\tfor(j=0;j<n;j++){ \n\t\t\tif(c[j]>='a'&&c[j]<'z')\n\t\t\t\tc[j]+=1;\n\t\t\telse if(c[j]=='z')\n\t\t\t\tc[j]='a';\n\t\t\tif(c[j]>='A'&&c[j]<'Z')\n\t\t\t\tc[j]+=1;\n\t\t\telse if(c[j]=='Z')\n\t\t\t\tc[j]='A';\n\t\t}\n\t\tfp=fopen(f,\"w\");\n\t\tfputs(c,fp);\n\t\tfclose(fp);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470256",
"status": 0,
"keywords": "密码破解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/23.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7449253,
"question_title": "数学题",
"question_content": "编程求1-200中能被2、3、5除余1的前10个整数。",
"difficulty": "简单",
"answer_id": 53429293,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\nint main()\n{\n int n&#61;0,i;\n for(i&#61;1; i&lt;&#61;200; i&#43;&#43;)\n {\n if((i%2&#61;&#61;1)&amp;&amp;(i%3&#61;&#61;1)&amp;&amp;(i%5&#61;&#61;1))\n {\n n&#43;&#43;;\n if(n&lt;&#61;10)\n printf(&#34;%d &#34;,i);\n if(n&#61;&#61;10)\n break;\n\n }\n }\n}\n</code></pre>\n\n<p><img alt=\"\" height=\"221\" src=\"https://img-ask.csdnimg.cn/upload/1623675026909.png\" width=\"691\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint n=0,i;\n\tfor(i=1; i<=200; i++)\n\t{\n\t\tif((i%2==1)&&(i%3==1)&&(i%5==1))\n\t\t{\n\t\t\tn++;\n\t\t\tif(n<=10)\n\t\t\t\tprintf(\"%d \",i);\n\t\t\tif(n==10)\n\t\t\t\tbreak;\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469938",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/24.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7460385,
"question_title": "最小公倍数",
"question_content": "<p>从键盘输入两个正整数&#xff0c;求出它们的最小公倍数并输出所求结果。</p>",
"difficulty": "简单",
"answer_id": 53445460,
"answer_content": "<p>#include&lt;stdio.h&gt;<br />\nint main() {<!-- --><br />\n    int m,n,temp,i;<br />\n    scanf(&#34;%d%d&#34;,&amp;m,&amp;n);<br />\n    if(m&lt;n) /*比较大小&#xff0c;使得m中存储大数&#xff0c;n中存储小数*/<br />\n    {<!-- --><br />\n        temp &#61; m;<br />\n        m &#61; n;<br />\n        n &#61; temp;<br />\n    }<br />\n    for(i&#61;m; i&gt;0; i&#43;&#43;)  /*从大数开始寻找满足条件的自然数*/<br />\n        if(i%m&#61;&#61;0 &amp;&amp; i%n&#61;&#61;0)<br />\n        {/*输出满足条件的自然数并结束循环*/<br />\n            printf(&#34;%d 和 %d 的最小公倍数是: %d\\n&#34;, m, n, i);<br />\n            break;<br />\n        }<br />\n    return 0;<br />\n}</p>\n\n<p>私发点红就更好啦</p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main() {\n    int m,n,temp,i;\n    scanf(\"%d%d\",&m,&n);\n    if(m<n) \n    {\n        temp = m;\n        m = n;\n        n = temp;\n    }\n    for(i=m; i>0; i++)  \n        if(i%m==0 && i%n==0)\n        {\n            printf(\"%d 和 %d 的最小公倍数是: %d\\n\", m, n, i);\n            break;\n        }\n    return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470147",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/25.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 702202,
"question_title": "结合两个字符串",
"question_content": "写一个结合两个字符串的方法,从第一个字符串中取出一个字符,然后从第二个字符串中取出一个字符,以此类推。一旦一个字符串没有字符,它就应该继续使用另一个字符串\n输入:两个字符串,如s1=\"day\"和s2=\"time\"\n输出:一个结果字符串,对于上面的输入情况,它将是“dtaiyme”。",
"difficulty": "困难",
"answer_id": 592511,
"answer_content": "#include<iostream>\n#include<string>\nusing namespace std;\nstring StrCon(const string& a, const string& b)\n{\n\tstring c;\n\tint n = a.size(), m = b.size();\n\tif (0 == n)\treturn a;\n\tif (0 == m) return b;\n\tint i, j;\n\tfor (i = 0, j = 0; i < n && j < m; ++i, ++j)\n\t{\n\t\tc += a[i];\n\t\tc += b[i];\n\t}\n\twhile (i < n)\n\t\tc += a[i++];\n\twhile (j < m)\n\t\tc += b[j++];\n\treturn c;\n}\nint main()\n{\n\tstring s = \"day\", t = \"time\";\n\tcout << StrCon(s, t) << endl;\n\tsystem(\"pause\");\n\treturn 0;\n}\n\n```\n \n```\n\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nstring StrCon(const string& a, const string& b)\n{\n\tstring c;\n\tint n = a.size(), m = b.size();\n\tif (0 == n)\treturn a;\n\tif (0 == m) return b;\n\tint i, j;\n\tfor (i = 0, j = 0; i < n && j < m; ++i, ++j)\n\t{\n\t\tc += a[i];\n\t\tc += b[i];\n\t}\n\twhile (i < n)\n\t\tc += a[i++];\n\twhile (j < m)\n\t\tc += b[j++];\n\treturn c;\n}\nint main()\n{\n\tstring s = \"day\", t = \"time\";\n\tcout << StrCon(s, t) << endl;\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469844",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/26.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7429148,
"question_title": "if else 和 switch 使用",
"question_content": "<p>输入学生成绩&#xff0c;\n若成绩在95分以上&#xff0c;输出“A”&#xff1b;\n若成绩在85~94分&#xff0c;输出“B”&#xff1b;\n若成绩在75~84分&#xff0c;输出“C”&#xff1b;\n若成绩在65~74分&#xff0c;输出“D”&#xff1b;\n若成绩在65分以下&#xff0c;输出“E”。&#xff08;分别用if else 和 switch 语句完成&#xff09;</p>",
"difficulty": "简单",
"answer_id": 53395657,
"answer_content": "<pre>\n<code>#include &#34;stdio.h&#34; \n/*\n输入学生成绩&#xff0c;\n若成绩在95分以上&#xff0c;输出“A”&#xff1b;\n若成绩在85~94分&#xff0c;输出“B”&#xff1b;\n若成绩在75~84分&#xff0c;输出“C”&#xff1b;\n若成绩在65~74分&#xff0c;输出“D”&#xff1b;\n若成绩在65分以下&#xff0c;输出“E”。&#xff08;分别用if else 和 switch 语句完成&#xff09;\n*/\nint main(){\n\tint score;\n\t\n\tscanf(&#34;%d&#34;,&amp;score);\n\tif(score&gt;&#61;95){\n\t\tprintf(&#34;A\\n&#34;);\t\n\t}else if(score&gt;&#61;85 &amp;&amp; score&lt;&#61;94){\n\t\tprintf(&#34;B\\n&#34;);\n\t}else if(score&gt;&#61;75 &amp;&amp; score&lt;&#61;84){\n\t\tprintf(&#34;C\\n&#34;);\n\t}else if(score&gt;&#61;65 &amp;&amp; score&lt;&#61;74){\n\t\tprintf(&#34;D\\n&#34;);\n\t}else{\n\t\tprintf(&#34;E\\n&#34;);\n\t}\n\t\n\tprintf(&#34;以下用switch语句实现相同功能\\n&#34;);\n\tscore &#61; score-5;\n\tscore &#61; score/10;\n\tswitch(score){\n\t\tcase 9:\n\t\t\tprintf(&#34;A\\n&#34;);\t\n\t\t\tbreak;\n\t\tcase 8:\n\t\t\tprintf(&#34;B&#34;);\t\n\t\t\tbreak;\n\t\tcase 7:\n\t\t\tprintf(&#34;C\\n&#34;);\t\n\t\t\tbreak;\n\t\tcase 6:\n\t\t\tprintf(&#34;D\\n&#34;);\t\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tprintf(&#34;E\\n&#34;);\n\t\t\tbreak;\n\t}\n\t\n\t\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include \"stdio.h\" \nint main(){\n\tint score;\n\tscanf(\"%d\",&score);\n\tif(score>=95){\n\t\tprintf(\"A\\n\");\t\n\t}else if(score>=85 && score<=94){\n\t\tprintf(\"B\\n\");\n\t}else if(score>=75 && score<=84){\n\t\tprintf(\"C\\n\");\n\t}else if(score>=65 && score<=74){\n\t\tprintf(\"D\\n\");\n\t}else{\n\t\tprintf(\"E\\n\");\n\t}\n\tprintf(\"以下用switch语句实现相同功能\\n\");\n\tscore = score-5;\n\tscore = score/10;\n\tswitch(score){\n\t\tcase 9:\n\t\t\tprintf(\"A\\n\");\t\n\t\t\tbreak;\n\t\tcase 8:\n\t\t\tprintf(\"B\");\t\n\t\t\tbreak;\n\t\tcase 7:\n\t\t\tprintf(\"C\\n\");\t\n\t\t\tbreak;\n\t\tcase 6:\n\t\t\tprintf(\"D\\n\");\t\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tprintf(\"E\\n\");\n\t\t\tbreak;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470148",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/27.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7439547,
"question_title": "链表问题",
"question_content": "<p>1&#xff0e; 有一个有序的数组。现输入一个数&#xff0c;要求按原来的规律将它插入数组中&#xff08;使用指针实现&#xff09;。</p>\n\n<p>【样例输入】</p>\n\n<p>7</p>\n\n<p>【样例输出】</p>\n\n<p>26 18 16 14 12 9 8 7 6 4 3</p>",
"difficulty": "中等",
"answer_id": 53414064,
"answer_content": "<p><img alt=\"\" height=\"154\" src=\"https://img-ask.csdnimg.cn/upload/1622471341874.png\" width=\"583\" /></p>\n\n<pre>\n<code class=\"language-cpp\">#include&lt;iostream&gt;\nusing namespace std;\n#define length 10\n\nint main(){\n\tint *m &#61; new int();\n\tcin&gt;&gt;*m;\n\tint a[length] &#61; {26,18,16,14,12,9,8,6,4,3};\n\tint *b &#61; a;\n\tint *c &#61; a;\n\tint i, j;\n\tfor(i &#61; 0; i &lt; length; i&#43;&#43;){\n\t\tif(*m &gt; *b){\n\t\t\tbreak;\n\t\t}\n\t\tb&#43;&#43;;\n\t}\n\tfor(j &#61; length; j &gt; i; j--){\n\t\t*(b-i&#43;j) &#61; *(b-i&#43;j-1);\n\t}\n\t*b &#61; *m;\n\tfor(j &#61; 0; j &lt; length&#43;1; j&#43;&#43;){\n\t\tcout&lt;&lt;*(c&#43;&#43;)&lt;&lt;&#39; &#39;;\n\t}\n\treturn 0;\n} </code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include<iostream>\nusing namespace std;\n#define length 10\nint main(){\n\tint *m = new int();\n\tcin>>*m;\n\tint a[length] = {26,18,16,14,12,9,8,6,4,3};\n\tint *b = a;\n\tint *c = a;\n\tint i, j;\n\tfor(i = 0; i < length; i++){\n\t\tif(*m > *b){\n\t\t\tbreak;\n\t\t}\n\t\tb++;\n\t}\n\tfor(j = length; j > i; j--){\n\t\t*(b-i+j) = *(b-i+j-1);\n\t}\n\t*b = *m;\n\tfor(j = 0; j < length+1; j++){\n\t\tcout<<*(c++)<<' ';\n\t}\n\treturn 0;\n} ",
"topic_link": "https://bbs.csdn.net/topics/600469939",
"status": 0,
"keywords": "链表,算法中阶,数据结构,基本数据结构",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/28.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1019861,
"question_title": "最优路线",
"question_content": "题目描述\n探险队要穿越泥潭,必须选择可踩踏的落脚点。可是泥潭面积很大,落脚点又实在少得可怜,一不小心就会深陷泥潭而无法脱身。侦查员费尽周折才从老乡手里弄到了一份地图,图中标出了落脚点的位置,而且令人震惊的是:泥潭只有一条穿越路线,且对于 n×m 的地图,路线长度为 n+m-1!请编程为探险队找出穿越路线。\n输入描述\n两个整数 n 和 m,表示泥潭的长和宽。下面 n 行 m 列表示地形(0 表示泥潭,1 表示落脚点)\n输出描述\n用坐标表示穿越路线,坐标之间用 > 分隔\n样例输入\n6 9\n1 1 1 0 0 0 0 0 0\n0 0 1 1 1 0 0 0 0\n0 0 0 0 1 0 0 0 0\n0 0 0 0 1 1 0 0 0\n0 0 0 0 0 1 1 1 1\n0 0 0 0 0 0 0 0 1\n样例输出\n(1,1)>(1,2)>(1,3)>(2,3)>(2,4)>(2,5)>(3,5)>(4,5)>(4,6)>(5,6)>(5,7)>(5,8)>(5,9)>(6,9)",
"difficulty": "困难",
"answer_id": 1211665,
"answer_content": "https://www.cnblogs.com/acmblog/p/9481082.html\n\n\n```\n#include <stdio.h>\n#include <string.h>\nconst int N=101;\nint map[N][N];\nint n,m;\nstruct point{\n int l,r;\n}node[N];//结构体数组用来存储路径\nint ans;\nvoid DFS(int x,int y)//这是深度优先搜索实现过程,本质就是一个递归的过程\n{\n if(x==n&&y==m)\n {\n for(int i=1;i<ans;i++)\n\t\t\tprintf(\"(%d,%d)\", node[i].l, node[i].r);\n\t\tprintf(\"(%d,%d)\\n\", x, y);\n }\n else{\n if(map[x][y]==1&&x<=n&&y<=m)\n {\n node[ans].l=x;\n node[ans].r=y;\n ans++;\n DFS(x+1,y);\n DFS(x,y+1);\n }\n }\n}\nint main()\n{\n while(scanf(\"%d%d\", &n, &m) != EOF)\n {\n ans=1;\n memset(map,0,sizeof(map));\n for(int i=1;i<=n;i++)\n for(int j=1;j<=m;j++)\n\t\t\t\tscanf(\"%d\", &map[i][j]);\n DFS(1,1); \n }\n return 0;\n}\n\n```\n\nhttps://blog.csdn.net/u011797040/article/details/88752788",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <string.h>\nconst int N=101;\nint map[N][N];\nint n,m;\nstruct point{\n\tint l,r;\n}node[N];\nint ans;\nvoid DFS(int x,int y)\n{\n\tif(x==n&&y==m)\n\t{\n\t\tfor(int i=1;i<ans;i++)\n\t\t\tprintf(\"(%d,%d)>\", node[i].l, node[i].r);\n\t\tprintf(\"(%d,%d)\\n\", x, y);\n\t}\n\telse{\n\t\tif(map[x][y]==1&&x<=n&&y<=m)\n\t\t{\n\t\t\tnode[ans].l=x;\n\t\t\tnode[ans].r=y;\n\t\t\tans++;\n\t\t\tDFS(x+1,y);\n\t\t\tDFS(x,y+1);\n\t\t}\n\t}\n}\nint main()\n{\n\twhile(scanf(\"%d%d\", &n, &m) != EOF)\n\t{\n\t\tans=1;\n\t\tmemset(map,0,sizeof(map));\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tfor(int j=1;j<=m;j++)\n\t\t\t\tscanf(\"%d\", &map[i][j]);\n\t\tDFS(1,1);\t\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470149",
"status": 1,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/29.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7455894,
"question_title": "任意输入10数,存入数组,找出显示最大值,并且标记所在位置。",
"question_content": "任意输入10数,存入数组,找出显示最大值,并且标记所在位置。",
"difficulty": "简单",
"answer_id": 53439230,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\nint main()\n{\n\tint a[10],i,max,maxindex;\n\tfor(i &#61;0;i&lt;10;i&#43;&#43;)\n\t\tscanf(&#34;%d&#34;,&amp;a[i]);\n\n\t//找最大的数\n\tmax &#61; a[0]; //先将最大的数设为0\n\tmaxindex &#61; 0;//最大的数的索引\n\tfor (i &#61;1;i&lt;10;i&#43;&#43;)\n\t{\n\t\tif(a[i] &gt; max)\n\t\t{\n\t\t\tmax &#61; a[i]; //替换最大值\n\t\t\tmaxindex &#61; i; //记录最大值索引\n\t\t}\n\t}\n\tprintf(&#34;最大值%d&#xff0c;索引:%d\\n&#34;,max,maxindex);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint a[10],i,max,maxindex;\n\tfor(i =0;i<10;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tmax = a[0]; \n\tmaxindex = 0;\n\tfor (i =1;i<10;i++)\n\t{\n\t\tif(a[i] > max)\n\t\t{\n\t\t\tmax = a[i];\t\n\t\t\tmaxindex = i; \n\t\t}\n\t}\n\tprintf(\"最大值%d,索引:%d\\n\",max,maxindex);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469936",
"status": 1,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/3.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7443359,
"question_title": "数组排序",
"question_content": "<p>给定n&#xff08;n是偶数&#xff0c;且n≤100&#xff09;个正整数&#xff0c;所有正整数均≤10000&#xff1b;从前往后依次遍历这个数组&#xff0c;每两个为一组进行处理&#xff0c;若一组中的任意一个元素能被3或者5整除&#xff0c;则交换这两个元素的位置&#xff1b;否则不动&#xff1b;全部处理完成后&#xff0c;逆序输出整个数组。\n例如给定序列&#xff1a; 99 35 83 38 &#xff0c; 处理完成后得到&#xff1a;38 83 99 35\n给定序列&#xff1a; 6 7 3 4 &#xff0c;处理完成后得到&#xff1a;3 4 6 7</p>",
"difficulty": "困难",
"answer_id": 53420448,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;math.h&gt;\n\nint main()\n{\n\nint i,j,n,a[10005],t;\nscanf(&#34;%d&#34;,&amp;n);\nfor(i&#61;0;i&lt;n;i&#43;&#43;){\n scanf(&#34;%d&#34;,&amp;a[i]);\n if(i%2&#61;&#61;1){\n if((a[i]%3&#61;&#61;0)||(a[i]%5&#61;&#61;0)||(a[i-1]%3&#61;&#61;0)||(a[i-1]%5&#61;&#61;0)){\n t&#61;a[i];\n a[i]&#61;a[i-1];\n a[i-1]&#61;t;\n }\n }\n}\nfor(i&#61;n-1;i&gt;&#61;0;i--){\n printf(&#34;%d &#34;,a[i]);\n}\n\n\n}</code></pre>\n\n<p><img alt=\"\" height=\"225\" src=\"https://img-ask.csdnimg.cn/upload/1622968886801.png\" width=\"476\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <math.h>\nint main()\n{\n\tint i,j,n,a[10005],t;\n\tscanf(\"%d\",&n);\n\tfor(i=0;i<n;i++){\n\t\tscanf(\"%d\",&a[i]);\n\t\tif(i%2==1){\n\t\t\tif((a[i]%3==0)||(a[i]%5==0)||(a[i-1]%3==0)||(a[i-1]%5==0)){\n\t\t\t\tt=a[i];\n\t\t\t\ta[i]=a[i-1];\n\t\t\t\ta[i-1]=t;\n\t\t\t}\n\t\t}\n\t}\n\tfor(i=n-1;i>=0;i--){\n\t\tprintf(\"%d \",a[i]);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470257",
"status": 1,
"keywords": "算法初阶,基础知识,特征序列,概率分析和随机算法,概率分析和指示器随机变量的进一步使用",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/30.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 702251,
"question_title": "字符拆分和 反转",
"question_content": "将IP\"1.1.168.192\"\n\t\t\t\t变换成 IP\"192.168.1.1\"\n\t\t\t\t只改\"\"内的\n\t\t\t\t返回string类型",
"difficulty": "中等",
"answer_id": 592646,
"answer_content": "\n\n```\n #include <vector>\n#include <string>\n#include <iostream>\n\nusing namespace std;\n\nvector<string> split(const string &s, const string &seperator){\n vector<string> result;\n typedef string::size_type string_size;\n string_size i = 0;\n \n while(i != s.size()){\n //找到字符串中首个不等于分隔符的字母;\n int flag = 0;\n while(i != s.size() && flag == 0){\n flag = 1;\n for(string_size x = 0; x < seperator.size(); ++x)\n if(s[i] == seperator[x]){\n ++i;\n flag = 0;\n break;\n }\n }\n \n //找到又一个分隔符,将两个分隔符之间的字符串取出;\n flag = 0;\n string_size j = i;\n while(j != s.size() && flag == 0){\n for(string_size x = 0; x < seperator.size(); ++x)\n if(s[j] == seperator[x]){\n flag = 1;\n break;\n }\n if(flag == 0) \n ++j;\n }\n if(i != j){\n result.push_back(s.substr(i, j-i));\n i = j;\n }\n }\n return result;\n}\n\nint main() {\n string s = \"1.1.168.192\";\n vector<string> v = split(s, \".\");\n string result = v[v.size() - 1];\n for(vector<string>::size_type i = v.size() - 2; i >= 0; i--)\n {\n result = result + \".\";\n\tresult = result + v[i];\n }\n cout << result;\n}\n```\n\n192.168.1.1",
"tag_name": "c++",
"cpp": " #include <vector>\n#include <string>\n#include <iostream>\nusing namespace std;\nvector<string> split(const string &s, const string &seperator){\n vector<string> result;\n typedef string::size_type string_size;\n string_size i = 0;\n while(i != s.size()){\n\tint flag = 0;\n\twhile(i != s.size() && flag == 0){\n\t flag = 1;\n\t for(string_size x = 0; x < seperator.size(); ++x)\n\t\tif(s[i] == seperator[x]){\n\t\t ++i;\n\t\t flag = 0;\n\t\t break;\n\t\t}\n\t}\n\tflag = 0;\n\tstring_size j = i;\n\twhile(j != s.size() && flag == 0){\n\t for(string_size x = 0; x < seperator.size(); ++x)\n\t\tif(s[j] == seperator[x]){\n\t\t flag = 1;\n\t\t break;\n\t\t}\n\t if(flag == 0) \n\t\t++j;\n\t}\n\tif(i != j){\n\t result.push_back(s.substr(i, j-i));\n\t i = j;\n\t}\n }\n return result;\n}\nint main() {\n string s = \"1.1.168.192\";\n vector<string> v = split(s, \".\");\n string result = v[v.size() - 1];\n for(vector<string>::size_type i = v.size() - 2; i >= 0; i--)\n {\n\tresult = result + \".\";\n\tresult = result + v[i];\n }\n cout << result;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469940",
"status": 0,
"keywords": "算法高阶,算法问题选编,多项式与快速傅里叶变换",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/31.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1052907,
"question_title": "约分",
"question_content": "编写程序,要求用户输入一个分数,然后将其约分为最简式。如:\n输入一个分数:8/12\n最简分式:2/3",
"difficulty": "中等",
"answer_id": 1250263,
"answer_content": "我的思路是求出分子分母的最大公因数,分子分母同时除以最大公因数便得到了最简的分子与分母,代码如下:\n\n```\n\n```\n\n\n```\n#include<stdio.h>\n#include<stdlib.h>\nint main()\n{\n int a,b,x,y,c;\n printf(\"输入一个分式:\");\n scanf(\"%d/%d\",&a,&b);\n if(a<b)\n {\n x=b;y=a;\n }\n else\n {\n x=a;y=b;\n }\n c=x%y;\n while(c)\n {\n x=y;\n y=c;\n c=x%y;\n }\n if(b/y!=1)\n printf(\"最简分式为:%d/%d\",a/y,b/y);\n else\n printf(\"最简分式为:%d\",a/y);\n return 0;\n}\n\n```\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<stdlib.h>\nint main()\n{\n\tint a,b,x,y,c;\n\tprintf(\"输入一个分式:\");\n\tscanf(\"%d/%d\",&a,&b);\n\tif(a<b)\n\t{\n\t\tx=b;y=a;\n\t}\n\telse\n\t{\n\t\tx=a;y=b;\n\t}\n\tc=x%y;\n\twhile(c)\n\t{\n\t\tx=y;\n\t\ty=c;\n\t\tc=x%y;\n\t}\n\tif(b/y!=1)\n\t\tprintf(\"最简分式为:%d/%d\",a/y,b/y);\n\telse\n\t\tprintf(\"最简分式为:%d\",a/y);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469845",
"status": 1,
"keywords": "约分,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/32.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1098271,
"question_title": "成绩问题",
"question_content": "八个同学的语文数学成绩\n每位同学的语文数学成绩比较,如果成绩都大于等于,则输出1;否则输出0.\n最后形成一个八行八列的矩阵",
"difficulty": "简单",
"answer_id": 1312686,
"answer_content": "\n```\n#include <stdio.h>\nint main()\n{\nint ch[8];\nint ma[8];\nfor (int i = 0; i < 8; i++)\nscanf(\"%d%d\", &ch[i], &ma[i]);\nfor (int i = 0; i< 8; i++)\n{\nfor (int j = 0; j < 8; j++)\nif (ma[i] >= ma[j] && ch[i] >= ch[j])\nprintf(\"1 \");\nelse\nprintf(\"0 \");\nprintf(\"\\n\");\n}\nreturn 0;\n}\n```\n# 问题解决的话,请点下采纳",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint ch[8];\n\tint ma[8];\n\tfor (int i = 0; i < 8; i++)\n\t\tscanf(\"%d%d\", &ch[i], &ma[i]);\n\tfor (int i = 0; i< 8; i++)\n\t{\n\t\tfor (int j = 0; j < 8; j++)\n\t\t\tif (ma[i] >= ma[j] && ch[i] >= ch[j])\n\t\t\t\tprintf(\"1 \");\n\t\t\telse\n\t\t\t\tprintf(\"0 \");\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469846",
"status": 1,
"keywords": "算法高阶,矩阵运算,矩阵求逆,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/33.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 364242,
"question_title": "回文串",
"question_content": "题目描述\n回文串是从左到右或者从右到左读起来都一样的字符串,试编程判别一个字符串是否为回文串。\n输入\n输入一个字符串。串长度<255.\n输出\n判别输入的字符串是否为回文串,是回文串输出\"Y\",否则输出\"N\"\n样例输入\nabcba\n样例输出\nY\n",
"difficulty": "简单",
"answer_id": 385629,
"answer_content": "\n\n```\n #include<iostream>\n#include<string.h>\nusing namespace std;\n\nint main(void)\n{\n char *p=\"abcba\";\n int n=strlen(p);\n bool flag=1;\n int i;\n for(i=0;i<n/2;i++)\n {\n cout<<p[i]<<\"\\t\"<<p[n-1-i]<<endl;\n if(p[i]!=p[n-1-i])\n {\n flag=0;\n cout<<\"no\"<<endl;break;\n }\n }\n if(flag==1)\n cout<<\"yes\"<<endl;\n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include<iostream>\n#include<string.h>\nusing namespace std;\nint main(void)\n{\n\tchar *p=\"abcba\";\n\tint n=strlen(p);\n\tbool flag=1;\n\tint i;\n\tfor(i=0;i<n/2;i++)\n\t{\n\t\tcout<<p[i]<<\"\\t\"<<p[n-1-i]<<endl;\n\t\tif(p[i]!=p[n-1-i])\n\t\t{\n\t\t\tflag=0;\n\t\t\tcout<<\"N\"<<endl;break;\n\t\t}\n\t}\n\tif(flag==1)\n\t\tcout<<\"Y\"<<endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469941",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/34.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1081363,
"question_title": "1.编写计算组合数的程序。 问题分析: (1)组合数的计算公式为",
"question_content": "1.编写计算组合数的程序。\n\n问题分析:\n\n(1)组合数的计算公式为:\n\n(2)此公式中用到3次阶乘的计算,所以可以编写一个求阶层函数fact(x)。\n\n(3)函数fact(x)的功能为计算整数x的阶层。\n\n要求:上传运行结果截图时,输入n的值为10,m的值为4。程序运行结果参考下图:![图片说明](https://img-ask.csdn.net/upload/202006/07/1591503765_180606.png)",
"difficulty": "简单",
"answer_id": 1287051,
"answer_content": "\n```\n#include <stdio.h>\nint fact(int n)\n{\nint r = 1;\nfor (int i = 1; i <= n; i++) r *= i;\nreturn r;\n}\nint main()\n{\nint m, n;\nscanf(\"%d%d\", &m, &n);\nint c = fact(n) / (fact(m) * fact(n - m));\nprintf(\"c=%d\", c);\nreturn 0;\n}\n```\n# 问题解决的话,请点下采纳",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint fact(int n)\n{\nint r = 1;\nfor (int i = 1; i <= n; i++) r *= i;\nreturn r;\n}\nint main()\n{\nint m, n;\nscanf(\"%d%d\", &m, &n);\nint c = fact(n) / (fact(m) * fact(n - m));\nprintf(\"c=%d\", c);\nreturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470150",
"status": 0,
"keywords": "算法中阶,摊还分析,聚合分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/35.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7446622,
"question_title": "计算所有4位正整数中同时能被13和20整除的数的和",
"question_content": "<p>计算所有4位正整数中同时能被13和20整除的数的和&#xff0c; 并同时做到如下显示:\n①显示这些数:\n②显示这些数的个数:\n③显示这些数的和。</p>",
"difficulty": "简单",
"answer_id": 53426095,
"answer_content": "<p>代码&#xff1a;</p>\n\n<pre>\n<code>#include &#34;stdio.h&#34;\n\nint main()\n{\n\tint i &#61; 1000;\n\tint count &#61; 0;\n\tint sum &#61; 0;\n\tprintf(&#34;所有4位正整数中同时能被13和20整除的数:\\n&#34;);\n\tfor(i &#61; 1000;i&lt;10000;i&#43;&#43;)\n\t{\n\t\tif(i % 13 &#61;&#61; 0 &amp;&amp; i % 20 &#61;&#61; 0)\n\t\t{\n\t\t\tcount&#43;&#43;;\n\t\t\tsum &#61; sum &#43; i;\n\t\t\tprintf(&#34;%d、&#34;,i);\n\t\t}\n\t}\n\tprintf(&#34;\\n这些数一共有%d个\\n&#34;,count);\n\tprintf(&#34;这些数的和是&#xff1a;%d\\n&#34;,sum);\n}</code></pre>\n\n<p>截图</p>\n\n<p><img alt=\"\" height=\"151\" src=\"https://img-ask.csdnimg.cn/upload/1623310306015.png\" width=\"1214\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include \"stdio.h\"\nint main()\n{\n\tint i = 1000;\n\tint count = 0;\n\tint sum = 0;\n\tprintf(\"所有4位正整数中同时能被13和20整除的数:\\n\");\n\tfor(i = 1000;i<10000;i++)\n\t{\n\t\tif(i % 13 == 0 && i % 20 == 0)\n\t\t{\n\t\t\tcount++;\n\t\t\tsum = sum + i;\n\t\t\tprintf(\"%d、\",i);\n\t\t}\n\t}\n\tprintf(\"\\n这些数一共有%d个\\n\",count);\n\tprintf(\"这些数的和是:%d\\n\",sum);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469942",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/36.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436555,
"question_title": "商品优惠计算器",
"question_content": "<p>商品优惠计算器\n使用if语句编程实现输入购货金额&#xff0c;输出实际付款金额。购货折扣率如下&#xff1a;\n购货金额≤500元 不打折\n500元&lt;购货金额≤1000元 9折\n1000元&lt;购货金额 8折</p>",
"difficulty": "简单",
"answer_id": 53408479,
"answer_content": "<p> </p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;\n#include &lt;stdbool.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\nint main()\n{\n\tfloat money&#61;0.0;\n\tfloat pay&#61;0.0;\n\t\n\tbool run &#61; true;\n\twhile(run)\n\t{\n\t\tprintf(&#34;\\n请输入购货金额:\\n&#34;);\n\t\tscanf(&#34;%f&#34;, &amp;money);\n\t\tif(money &gt;&#61;1000)\n\t\t{\n\t\t\tpay&#61;money*0.8;\n\t\t\tprintf(&#34;打八折&#xff0c;应付金额:%.2f\\n&#34;,pay);\n\t\t}\n\t\telse if((money &gt;500)&amp;&amp;(money &lt;&#61;1000))\n\t\t{\n\t\t\tpay&#61;money*0.9;\n\t\t\tprintf(&#34;打九折&#xff0c;应付金额:%.2f\\n&#34;,pay);\n\t\t}\n\t\telse if(money &lt;&#61;500)\n\t\t{\n\t\t\tprintf(&#34;不打折&#xff0c;应付金额:%.2f\\n&#34;,money);\n\t\t}\n\t}\n\t\n\treturn 0;\n\n}</code></pre>\n\n<p><img alt=\"\" height=\"185\" src=\"https://img-ask.csdnimg.cn/upload/1622076538281.png\" width=\"353\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <ctype.h>\n#include <stdbool.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\nint main()\n{\n\tfloat money=0.0;\n\tfloat pay=0.0;\n\tbool run = true;\n\twhile(run)\n\t{\n\t\tprintf(\"\\n请输入购货金额:\\n\");\n\t\tscanf(\"%f\", &money);\n\t\tif(money >1000)\n\t\t{\n\t\t\tpay=money*0.8;\n\t\t\tprintf(\"打八折,应付金额:%.2f\\n\",pay);\n\t\t}\n\t\telse if((money >500)&&(money <=1000))\n\t\t{\n\t\t\tpay=money*0.9;\n\t\t\tprintf(\"打九折,应付金额:%.2f\\n\",pay);\n\t\t}\n\t\telse if(money <=500)\n\t\t{\n\t\t\tprintf(\"不打折,应付金额:%.2f\\n\",money);\n\t\t}\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470151",
"status": 1,
"keywords": "数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/37.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7437679,
"question_title": "从一个字符串中找出最长的单词",
"question_content": "<p>#include&lt;stdio.h&gt;<br />\n#include&lt;string.h&gt;<br />\nint main()<br />\n{<!-- --><br />\n    //void max(char[], char[]);<br />\n    char a[100], b[100],c[100];<br />\n    int i, j&#61;  0, k &#61; 0, n,m;<br />\n    gets(a);<br />\n    n &#61; strlen(a);<br />\n    for (i &#61; 0;  i&lt;n; i&#43;&#43;)<br />\n    {<!-- --><br />\n        if ((a[i] &gt;&#61; &#39;A&#39;&amp;&amp;a[i] &lt;&#61; &#39;Z&#39;) || (a[i] &gt;&#61; &#39;a&#39;&amp;&amp;a[i] &lt;&#61; &#39;z&#39;))<br />\n        {<!-- --><br />\n            b[j&#43;&#43;] &#61; a[i];</p>\n\n<p>        }<br />\n    <br />\n        else if (j &gt; k)<br />\n        {<!-- --><br />\n            for (m &#61; 0; m &lt; j; m&#43;&#43;)<br />\n                <br />\n            {<!-- --><br />\n                c[m] &#61; b[m];<br />\n                <br />\n            }<br />\n            k &#61; m;<br />\n        <br />\n            j &#61; 0;</p>\n\n<p>        }<br />\n        else if (j &lt;&#61; k)<br />\n            j &#61; 0;</p>\n\n<p><br />\n    }<br />\n    c[k] &#61; &#39;\\0&#39;;<br />\n    printf(&#34;%s&#34;, c);</p>\n\n<p><br />\n}</p>\n\n<p> </p>",
"difficulty": "中等",
"answer_id": 53410843,
"answer_content": "<p>楼主的代码修改如下&#xff0c;供参考&#xff1a;</p>\n\n<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\nint main()\n{\n //void max(char[], char[]);\n char a[100], b[100],c[100];\n int i, j&#61;0, k&#61;0, n,m;\n gets(a);\n n &#61; strlen(a);\n for (i &#61; 0; i&lt;n&#43;1; i&#43;&#43;)//for (i &#61; 0; i&lt;n; i&#43;&#43;)\n {\n if ((a[i] &gt;&#61; &#39;A&#39;&amp;&amp;a[i] &lt;&#61; &#39;Z&#39;) || (a[i] &gt;&#61; &#39;a&#39;&amp;&amp;a[i] &lt;&#61; &#39;z&#39;))\n {\n b[j&#43;&#43;] &#61; a[i];\n }\n\n else if (j &gt; k)\n {\n for (m &#61; 0; m &lt; j; m&#43;&#43;)\n\n {\n c[m] &#61; b[m];\n\n }\n k &#61; j;//k &#61; m;\n j &#61; 0;\n\n }\n else if (j &lt;&#61; k)\n j &#61; 0;\n\n\n }\n c[k] &#61; &#39;\\0&#39;;\n printf(&#34;%s\\n&#34;, c);\n \n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<string.h>\nint main()\n{\n\tchar a[100], b[100],c[100];\n\tint i, j=0, k=0, n,m;\n\tgets(a);\n\tn = strlen(a);\n\tfor (i = 0; i<n+1; i++)\n\t{\n\t\tif ((a[i] >= 'A'&&a[i] <= 'Z') || (a[i] >= 'a'&&a[i] <= 'z'))\n\t\t{\n\t\t\tb[j++] = a[i];\n\t\t}\n\t\telse if (j > k)\n\t\t{\n\t\t\tfor (m = 0; m < j; m++)\n\t\t\t{\n\t\t\t\tc[m] = b[m];\n\t\t\t}\n\t\t\tk = j;\n\t\t\tj = 0;\n\t\t}\n\t\telse if (j <= k)\n\t\t\tj = 0;\n\t}\n\tc[k] = '\\0';\n\tprintf(\"%s\\n\", c);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469847",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/38.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 876769,
"question_title": "求前n个素数之和",
"question_content": "题目描述\n求前n个素数的和。 \n例如,前5个素数是2、3、5、7、11,它们的和是28。 \n输入\n一个整数n,1<=n<=1000。 \n输出\n前n个素数的和 \n样例输入\n5\n样例输出\n28\n提示\n第1000个素数是7919。\n",
"difficulty": "中等",
"answer_id": 1011802,
"answer_content": "你的a是数组,a++是非法的\n\n```\n#include <iostream>\nusing namespace std;\nint main()\n{\n int n,i,j,sum,a;\n cin>>n;\n\n a = 0;\n i = 2;\n sum=0;\n while(a<n){\n for(j=2;j<=i;j++)\n if(i%j == 0)\n break;\n if(j == i)\n {\n sum += i;\n ++a;\n }\n ++i;\n }\n cout<<sum;\n}\n```",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint main()\n{\n\tint n,i,j,sum,a;\n\tcin>>n;\n\ta = 0;\n\ti = 2;\n\tsum=0;\n\twhile(a<n){\n\t\tfor(j=2;j<=i;j++)\n\t\t\tif(i%j == 0)\n\t\t\t\tbreak;\n\t\tif(j == i)\n\t\t{\n\t\t\tsum += i;\n\t\t\t++a;\n\t\t}\n\t\t++i;\n\t}\n\tcout<<sum;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469943",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/39.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7459691,
"question_title": "冒泡法排序大小",
"question_content": "4286\n3185\n2895\n3550\n2745\n按从小到大排序",
"difficulty": "简单",
"answer_id": 53444682,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\n \n#define ARR_LEN 255 /*数组长度上限*/\n#define elemType int /*元素类型*/\n \n/* 冒泡排序 */\n/* 1. 从当前元素起&#xff0c;向后依次比较每一对相邻元素&#xff0c;若逆序则交换 */\n/* 2. 对所有元素均重复以上步骤&#xff0c;直至最后一个元素 */\n/* elemType arr[]: 排序目标数组; int len: 元素个数 */\nvoid bubbleSort (elemType arr[], int len) {\n elemType temp;\n int i, j;\n for (i&#61;0; i&lt;len-1; i&#43;&#43;) /* 外循环为排序趟数&#xff0c;len个数进行len-1趟 */\n for (j&#61;0; j&lt;len-1-i; j&#43;&#43;) { /* 内循环为每趟比较的次数&#xff0c;第i趟比较len-i次 */\n if (arr[j] &gt; arr[j&#43;1]) { /* 相邻元素比较&#xff0c;若逆序则交换&#xff08;升序为左大于右&#xff0c;降序反之&#xff09; */\n temp &#61; arr[j];\n arr[j] &#61; arr[j&#43;1];\n arr[j&#43;1] &#61; temp;\n }\n }\n}\n \nint main (void) {\n elemType arr[ARR_LEN] &#61; {4286,3185,2895,3550,2745};\n int len &#61; 5;\n int i;\n \n bubbleSort (arr, len);\n for (i&#61;0; i&lt;len; i&#43;&#43;)\n printf (&#34;%d\\t&#34;, arr[i]);\n putchar (&#39;\\n&#39;);\n \n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#define ARR_LEN 255 \n#define elemType int \nvoid bubbleSort (elemType arr[], int len) {\n\telemType temp;\n\tint i, j;\n\tfor (i=0; i<len-1; i++) \n\t\tfor (j=0; j<len-1-i; j++) { \n\t\t\tif (arr[j] > arr[j+1]) { \n\t\t\t\ttemp = arr[j];\n\t\t\t\tarr[j] = arr[j+1];\n\t\t\t\tarr[j+1] = temp;\n\t\t\t}\n\t\t}\n}\nint main (void) {\n\telemType arr[ARR_LEN] = {4286,3185,2895,3550,2745};\n\tint len = 5;\n\tint i;\n\tbubbleSort (arr, len);\n\tfor (i=0; i<len; i++)\n\t\tprintf (\"%d\\t\", arr[i]);\n\tputchar ('\\n');\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470243",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/4.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7453758,
"question_title": "编写程序:判断素数的个数",
"question_content": "<p>在一个数组A中存放100个数据&#xff0c;用子函数判断该数组中哪些是素数&#xff0c;并统计该素数的个数&#xff0c;在主函数中输出该素数的个数</p>",
"difficulty": "简单",
"answer_id": 53436453,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint isPrime(int n)\n{\n\tint i &#61; 2;\n\tif(n&lt;2) return 0;\n\tfor (i&#61;2;i&lt;n;i&#43;&#43;)\n\t{\n\t\tif(n%i &#61;&#61; 0)\n\t\t\treturn 0;\n\t}\n\treturn 1;\n}\n\nint CountPrime(int a[],int size)\n{\n\tint i &#61;0,count &#61; 0;\n\t//统计素数个数\n\tfor (i &#61; 0;i&lt;size;i&#43;&#43;)\n\t{\n\t\tif(isPrime(a[i]))\n\t\t{\n\t\t\tprintf(&#34;%d &#34;,a[i]);\n\t\t\tcount&#43;&#43;;\n\t\t\tif(count%10 &#61;&#61; 0) //每10个一行\n\t\t\t\tprintf(&#34;\\n&#34;);\n\t\t}\n\t}\n\tprintf(&#34;\\n&#34;);\n\treturn count;\n}\n\nint main()\n{\n\tint a[100],i,count &#61; 0;\n\tfor(i &#61; 0;i&lt;100;i&#43;&#43;)\n\t\ta[i] &#61; rand()%1000;//生成0-999以内的随机数\n\t\n\tprintf(&#34;素数的个数:%d\\n&#34;,CountPrime(a,100));\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint isPrime(int n)\n{\n\tint i = 2;\n\tif(n<2) return 0;\n\tfor (i=2;i<n;i++)\n\t{\n\t\tif(n%i == 0)\n\t\t\treturn 0;\n\t}\n\treturn 1;\n}\nint CountPrime(int a[],int size)\n{\n\tint i =0,count = 0;\n\tfor (i = 0;i<size;i++)\n\t{\n\t\tif(isPrime(a[i]))\n\t\t{\n\t\t\tprintf(\"%d \",a[i]);\n\t\t\tcount++;\n\t\t\tif(count%10 == 0) \n\t\t\t\tprintf(\"\\n\");\n\t\t}\n\t}\n\tprintf(\"\\n\");\n\treturn count;\n}\nint main()\n{\n\tint a[100],i,count = 0;\n\tfor(i = 0;i<100;i++)\n\t\ta[i] = rand()%1000;\n\tprintf(\"素数的个数:%d\\n\",CountPrime(a,100));\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469944",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,证明主定理,对b的幂证明主定理",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/40.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436615,
"question_title": "分支结构问题 ",
"question_content": "<p>实现从键盘输入一个字符时&#xff0c;如果该字符为小写字母&#xff0c;则转换为大写字母输出&#xff1b;如果该字符为大写字母&#xff0c;则转换为小写字母输出&#xff1b;如果为其他字符&#xff0c;则原样输出。</p>",
"difficulty": "简单",
"answer_id": 53408453,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\nint main()\n{\nchar x,y;\nprintf(&#34;请输入一个字符&#xff1a;&#34;);\nscanf(&#34;%c&#34;,&amp;x);\nif(x&gt;&#61;&#39;A&#39;&amp;&amp;x&lt;&#61;&#39;Z&#39;)\n{\ny&#61;x&#43;32;\nprintf(&#34;此字母是一个大写字母,转换后的小写字母是&#xff1a;%c&#34;,y);\n}\nelse if(x&gt;&#61;&#39;a&#39;&amp;&amp;x&lt;&#61;&#39;z&#39;)\n{\ny&#61;x-32;\nprintf(&#34;此字母是一个小写字母,转换后的大写字母是%c&#34;,y);\n}\nelse\n{\nprintf(&#34;%c&#34;,x);\n}\nreturn 0;\n}\n</code></pre>\n\n<p>记得点个采纳&#xff0c;谢谢 </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tchar x,y;\n\tprintf(\"请输入一个字符:\");\n\tscanf(\"%c\",&x);\n\tif(x>='A'&&x<='Z')\n\t{\n\t\ty=x+32;\n\t\tprintf(\"此字母是一个大写字母,转换后的小写字母是:%c\",y);\n\t}\n\telse if(x>='a'&&x<='z')\n\t{\n\t\ty=x-32;\n\t\tprintf(\"此字母是一个小写字母,转换后的大写字母是%c\",y);\n\t}\n\telse\n\t{\n\t\tprintf(\"%c\",x);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469945",
"status": 1,
"keywords": "算法高阶,递归结构,高级数据结构,van Emde Boas树,原型van Emde Boas结构",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/41.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 717268,
"question_title": "从键盘输入一组数据建立单链表,然后输出奇数位上的元素。",
"question_content": "输入\n第一行输入单链表长度n。\n第二行输入字符串。\n输出\n第一行:输出各奇数位元素\n样例输入\n7\nABCDEFG\n样例输出\nACEG",
"difficulty": "简单",
"answer_id": 710446,
"answer_content": "\n```\n#include \"stdio.h\"\n#include \"stdlib.h\"\n\ntypedef struct node\n{\n\tchar x;\n\tnode * next;\n} LList;\n\nint main()\n{\n\tLList * header = NULL;\n\tnode * p;\n\tint n;\n\tscanf(\"%d\", &n);\n\tfflush(stdin);\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (!header) \n\t\t{\n\t\t\theader = (node *)malloc(sizeof(node));\n\t\t\tp = header;\n\t\t\tscanf(\"%c\", &(header->x));\n\t\t\theader->next = NULL;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tp->next = (node *)malloc(sizeof(node));\n\t\t\tp = p->next;\n\t\t\tscanf(\"%c\", &(p->x));\n\t\t\tp->next = NULL;\n\t\t}\n\t}\n\tp = header;\n\tint i = 0;\n\twhile (p)\n\t{\n\t\tif (i++ % 2 == 0)\n\t\t\tprintf(\"%c\", p->x);\n\t\tp = p->next;\n\t}\n\treturn 0;\n}\n\n\n```\n\n![图片说明](https://img-ask.csdn.net/upload/201812/16/1544968420_383190.gif)\n",
"tag_name": "c语言",
"cpp": "#include \"stdio.h\"\n#include \"stdlib.h\"\ntypedef struct node\n{\n\tchar x;\n\tnode * next;\n} LList;\nint main()\n{\n\tLList * header = NULL;\n\tnode * p;\n\tint n;\n\tscanf(\"%d\", &n);\n\tfflush(stdin);\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (!header) \n\t\t{\n\t\t\theader = (node *)malloc(sizeof(node));\n\t\t\tp = header;\n\t\t\tscanf(\"%c\", &(header->x));\n\t\t\theader->next = NULL;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tp->next = (node *)malloc(sizeof(node));\n\t\t\tp = p->next;\n\t\t\tscanf(\"%c\", &(p->x));\n\t\t\tp->next = NULL;\n\t\t}\n\t}\n\tp = header;\n\tint i = 0;\n\twhile (p)\n\t{\n\t\tif (i++ % 2 == 0)\n\t\t\tprintf(\"%c\", p->x);\n\t\tp = p->next;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470152",
"status": 1,
"keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/42.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436247,
"question_title": "将其中每个单词的字母顺序翻转后打印输出到屏幕",
"question_content": "<p>初始化一个字符数组为&#34;The best or nothing&#34;&#xff0c;并将其中每个单词的字母顺序翻转后打印输出到屏幕。要求&#xff1a;\n1、字符数组的初始化在程序运行时由用户输入&#xff1b;\n2、字符数组的翻转和结果输出功能通过函数实现&#xff1b;\n3、字符数组不能定义成全局变量。</p>",
"difficulty": "简单",
"answer_id": 53408492,
"answer_content": "<p>你要的结果是什么样的&#xff1f;</p>\n\n<p>输入&#xff1a;The best or nothing</p>\n\n<p>输出&#xff1a;ehT tseb ro gnihton</p>\n\n<p>这样的吗&#xff1f;如果是这样的&#xff0c;代码如下&#xff1a;</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;string&gt;\nvoid trans(char* p,int len)\n{\n\tchar* s &#61; new char[len];\n\tmemcpy(s,p,len);\n\tfor (int i &#61; 0; i &lt; len; i&#43;&#43;)\n\t{\n\t\tp[i] &#61; s[len-1-i];\n\t}\n\tdelete[] s;\n\ts &#61; 0;\n}\nvoid transfun(char* p,int len)\n{\n\tint start &#61; 0;\n\tint i &#61; 0;\n\tint shift &#61; 0;\n\twhile(i &lt; len)\n\t{\n\t\tfor (i &#61; start; i &lt; len;i&#43;&#43;)\n\t\t{\n\t\t\tif(p[i] &#61;&#61; &#39; &#39;)\n\t\t\t\tbreak;\n\t\t}\n\t\t\n\t\ttrans(p&#43;shift,i-start);\n\t\tshift &#43;&#61; i-start&#43;1;\n\t\tstart &#61; i&#43;1;\n\t\ti &#43;&#61;1;\n\t}\n\t\t\n}\nvoid output(char* p)\n{\n\tprintf(&#34;%s\\n&#34;,p);\n}\nint main()\n{\n\tchar buf[1000] &#61; {0};\n\tprintf(&#34;请输入字符串&#xff1a;&#34;);\n\tgets(buf);\n\ttransfun(buf,strlen(buf));\n\toutput(buf);\n\t//getchar();\n\t//getchar();\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <string>\nvoid trans(char* p,int len)\n{\n\tchar* s = new char[len];\n\tmemcpy(s,p,len);\n\tfor (int i = 0; i < len; i++)\n\t{\n\t\tp[i] = s[len-1-i];\n\t}\n\tdelete[] s;\n\ts = 0;\n}\nvoid transfun(char* p,int len)\n{\n\tint start = 0;\n\tint i = 0;\n\tint shift = 0;\n\twhile(i < len)\n\t{\n\t\tfor (i = start; i < len;i++)\n\t\t{\n\t\t\tif(p[i] == ' ')\n\t\t\t\tbreak;\n\t\t}\n\t\ttrans(p+shift,i-start);\n\t\tshift += i-start+1;\n\t\tstart = i+1;\n\t\ti +=1;\n\t}\n}\nvoid output(char* p)\n{\n\tprintf(\"%s\\n\",p);\n}\nint main()\n{\n\tchar buf[1000] = {0};\n\tprintf(\"请输入字符串:\");\n\tgets(buf);\n\ttransfun(buf,strlen(buf));\n\toutput(buf);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470153",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/43.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1072218,
"question_title": "商家商品销售量统计",
"question_content": "<p>现在有一个网站上半年的商品销售量,请你写一段代码帮助店主统计前半年的总销量和平均销量。\n商品销售表\n1月份\t2月份\t3月份\t4月份\t5月份\t6月份\t总销量\t平均销量\n64\t53\t77\t59\t61\t42\t0\t0\n(1)函数中应定义一个包含8个元素的一维数据,用来存放6个月的商品销量和统计后的总销量及平均销量;\n(2)完成6个月销量的输入;\n(3)计算半年的总销量及平均销量;\n(4)按照程序运行效果图,输出商品销量表。\n![图片说明](https://img-ask.csdn.net/upload/202005/16/1589596638_852927.jpg)</p>",
"difficulty": "简单",
"answer_id": 1274957,
"answer_content": "表格上可能有点不同,你可以再改改\n```\n#include<stdio.h>\nint main()\n{\n\tint a[8],s=0;\n\tprintf(\"----------商家商品销售统计----------\\n\");\n\tprintf(\"\\n\");\n\tprintf(\"请输入商品6个月的销售量:\");\n\tfor (int i = 0; i < 6; i++)\n\t{\n\t\tscanf(\"%d\", &a[i]);\n\t\ts += a[i];\n\t}\n\tprintf(\"\\n\\t\\t\\t商品销量表\\t\\t\\t\\n\");\n\tprintf(\"——————————————————————————————————————————————\\n\");\n\tfor (int i = 1; i <= 8; i++)\n\t{\n\t\tif (i <= 6)\n\t\t\tprintf(\" %d月份 \", i);\n\t\telse if (i == 7)\n\t\t\tprintf(\" 总销售 \");\n\t\telse\n\t\t\tprintf(\" 平均销售 \\n\");\n\t}\n\tfor (int i = 0; i < 8; i++)\n\t{\n\t\tif (i < 6)\n\t\t\tprintf(\" %d |\", a[i]);\n\t\telse if (i == 6)\n\t\t{\n\t\t\ta[i] = s;\n\t\t\tprintf(\" %d |\", a[i]);\n\t\t}\n\t\telse\n\t\t{\n\t\t\ta[i] = s / 6;\n\t\t\tprintf(\" %d |\",a[i]);\n\t\t}\n\t}\n\treturn 0;\n}\n```\n![图片说明](https://img-ask.csdn.net/upload/202005/17/1589691710_74517.png)\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint a[8],s=0;\n\tprintf(\"----------商家商品销售统计----------\\n\");\n\tprintf(\"\\n\");\n\tprintf(\"请输入商品6个月的销售量:\");\n\tfor (int i = 0; i < 6; i++)\n\t{\n\t\tscanf(\"%d\", &a[i]);\n\t\ts += a[i];\n\t}\n\tprintf(\"\\n\\t\\t\\t商品销量表\\t\\t\\t\\n\");\n\tprintf(\"——————————————————————————————————————————————\\n\");\n\tfor (int i = 1; i <= 8; i++)\n\t{\n\t\tif (i <= 6)\n\t\t\tprintf(\"\t%d月份 \", i);\n\t\telse if (i == 7)\n\t\t\tprintf(\"\t总销售 \");\n\t\telse\n\t\t\tprintf(\"\t平均销售 \\n\");\n\t}\n\tfor (int i = 0; i < 8; i++)\n\t{\n\t\tif (i < 6)\n\t\t\tprintf(\"\t %d |\", a[i]);\n\t\telse if (i == 6)\n\t\t{\n\t\t\ta[i] = s;\n\t\t\tprintf(\"\t %d |\", a[i]);\n\t\t}\n\t\telse\n\t\t{\n\t\t\ta[i] = s / 6;\n\t\t\tprintf(\"\t %d |\",a[i]);\n\t\t}\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470258",
"status": 1,
"keywords": "动态表,表扩张,算法中阶,摊还分析,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/44.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7458697,
"question_title": "求出最大分数和学号",
"question_content": "<p>【题目描述】\n输入一个班级的学生人数&#xff08;学生人数不超过60人&#xff09;&#xff0c;再输入该班学生每个学生的学号和某门课的成绩&#xff0c;要求用函数编程实现输出最高分及其学号。\n【输入形式】\n第一行输入学生人数N&#xff0c;接下来的N行分别输入N个学生的学号和成绩&#xff0c;学号和成绩均为整数&#xff0c;学号范围为1~9999&#xff0c;成绩范围为0~100。\n【输出形式】\n一行&#xff0c;两个正整数&#xff0c;分别表示最高分和对应的学号。如果最高分有相同的&#xff0c;则输出最先出现的最高分和对应的学号。\n【样例输入1】\n3\n1101 85\n1102 95\n1103 90\n【样例输出1】\n95 1102</p>\n<pre>\n<code>#include&lt;iostream&gt;\n#include&lt;cstring&gt;\nusing namespace std;\nvoid findmax(int score[],int num[],int *pmax,int *pmaxnum);\nint main()\n{\n\tint n,i,max,maxnum;\n\tint score[100];\n\tchar num[100];\n\tcin&gt;&gt;n;//输入n \n\tfor(int i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tcin&gt;&gt;num[i]&gt;&gt;score[i];//分别输入学号分数 \n\t}\n\tfindmax(score,num,n);//调用指针函数 \n} \nvoid findmax(int *score,char *num,int n)//找出最大分数和学号 \n{\n\tint max&#61;*score;int maxnum&#61;*num;//初始化最大分数和学号 \n\tfor(int i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tif(score[i]&gt;*max) \n\t\t{\n\t\t\t*pmax&#61;score[i];\n\t\t\t*pmaxnum&#61;num[i];\n\t\t}\n\t\t\n\t}\n\tcout&lt;&lt;*max&lt;&lt;&#34; &#34;&lt;&lt;*maxnum&lt;&lt;endl;\n\treturn 0;\n}</code></pre>\n<p> </p>",
"difficulty": "中等",
"answer_id": 53443352,
"answer_content": "<p>代码修改如下&#xff1a;</p>\n\n<pre>\n<code>#include&lt;iostream&gt;\n#include&lt;cstring&gt;\nusing namespace std;\nvoid findmax(int score[],int num[],int n,int &amp;max,int &amp;maxnum);\nint main()\n{\n\tint n,max,maxnum;\n\tint score[100];\n\tint num[100];\n\tcin&gt;&gt;n;//输入n \n\tfor(int i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tcin&gt;&gt;num[i]&gt;&gt;score[i];//分别输入学号分数\n\t}\n\tfindmax(score,num,n,max,maxnum);//调用指针函数 \n} \nvoid findmax(int *score,int *num,int n,int &amp;max,int &amp;maxnum)//找出最大分数和学号 \n{\n\tmax&#61;*score;\n\tmaxnum&#61;*num;//初始化最大分数和学号 \n\tfor(int i&#61;0;i&lt;n;i&#43;&#43;)\n\t{\n\t\tif(score[i]&gt;max) \n\t\t{\n\t\t\tmax&#61;score[i];\n\t\t\tmaxnum&#61;num[i];\n\t\t}\n\t}\n\tcout&lt;&lt;max&lt;&lt;&#34; &#34;&lt;&lt;maxnum&lt;&lt;endl;\n\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include<iostream>\n#include<cstring>\nusing namespace std;\nvoid findmax(int score[],int num[],int n,int &max,int &maxnum);\nint main()\n{\n\tint n,max,maxnum;\n\tint score[100];\n\tint num[100];\n\tcin>>n;\n\tfor(int i=0;i<n;i++)\n\t{\n\t\tcin>>num[i]>>score[i];\n\t}\n\tfindmax(score,num,n,max,maxnum);\n} \nvoid findmax(int *score,int *num,int n,int &max,int &maxnum)\n{\n\tmax=*score;\n\tmaxnum=*num;\n\tfor(int i=0;i<n;i++)\n\t{\n\t\tif(score[i]>max) \n\t\t{\n\t\t\tmax=score[i];\n\t\t\tmaxnum=num[i];\n\t\t}\n\t}\n\tcout<<max<<\" \"<<maxnum<<endl;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470154",
"status": 0,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/45.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7445574,
"question_title": "整数分解",
"question_content": "<p>输入一个整数&#xff0c;将其按7进制位分解为各乘式的累加和&#xff08;下图为输出结果&#xff09;</p>\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623209867965.jpg\" />\n </p>",
"difficulty": "简单",
"answer_id": 53424293,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n#define X 7\n\nint main()\n{\n int i&#61;0;\n int mod,num&#61;720;\n\n while(num)\n {\n mod&#61;num%X;\n num/&#61;X;\n if(mod!&#61;0)\n printf(&#34;%d*7^%d%c&#34;,mod,i,(num&gt;0)?&#39;&#43;&#39;:&#39;\\n&#39;);\n i&#43;&#43;;\n }\n\n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#define X 7\nint main()\n{\n\tint i=0;\n\tint mod,num=720;\n\twhile(num)\n\t{\n\t\tmod=num%X;\n\t\tnum/=X;\n\t\tif(mod!=0)\n\t\t\tprintf(\"%d*7^%d%c\",mod,i,(num>0)?'+':'\\n');\n\t\ti++;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470155",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/46.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 348590,
"question_title": "矩阵问题",
"question_content": "题目描述\n编写以下函数:\n(1)在一个二维数组中形成以下形式的n阶矩阵:\n[1 1 1 1 1\n2 1 1 1 1\n3 2 1 1 1\n4 3 2 1 1\n5 4 3 2 1]\n(2)去掉靠边的元素,生成新的n-2阶矩阵;\n(3)求生成的n阶矩阵主对角线上的元素之和;\n(4)以方阵形式输出数组。\n在main函数中调用以上函数进行测试。\n输入\n输入生成矩阵的阶数(n>=2)\n输出\n以方阵形式输出生成的n阶矩阵、去掉靠边的元素生成的新的n-2阶矩阵、以及生成的n阶矩阵主对角线上的元素之和,最后一行要回车\n样例输入\n5\n样例输出\nGenerated matrix:\n1 1 1 1 1\n2 1 1 1 1\n3 2 1 1 1\n4 3 2 1 1\n5 4 3 2 1\ndel the elements on the side:\n1 1 1\n2 1 1\n3 2 1\nThe sum of the diagonal:5\n",
"difficulty": "中等",
"answer_id": 355486,
"answer_content": "#include <iostream>\n\n/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */\nusing namespace std; \nint main() {\nwhile(1){\n\n\tint a;\n\tcin>>a;//输入一个数 \n\tint array[a][a];//创建n阶数组 \n\tfor (int i=0;i<a;i++)//赋值 \n\t\tfor (int j=0;j<a;j++)\n\t\t{\n\t\t\tif(j<i)\n\t\t\t\tarray[i][j]=i+1-j;\n\t\t\telse \n\t\t\t\tarray[i][j]=1;\n\t\t}\n\tcout<<\"Generated matrix:\"<<endl; \n\tfor (int i=0;i<a;i++)//输出这个 n阶数组 \n\t{\t\n\t\tfor (int j=0;j<a;j++)\n\t\t{\n\t\t\tcout<<array[i][j];\n\t\t}\n\t\tcout<<endl;\n\t}\n\tcout<<\"del the elements on the side:\"<<endl; \n\tfor (int i=1;i<a-1;i++)//少一圈的n阶数组 \n\t{\t\n\t\tfor (int j=1;j<a-1;j++)\n\t\t{\n\t\t\tcout<<array[i][j];\n\t\t}\n\t\tcout<<endl;\n\t}\n\tint sum=0;//计算 \n\tint i,j;\n\tfor (i = a - 2 ,j = 1 ; i >= 1 ; i-- , j++)//对角 \n\t{\t\n\t\tsum+=array[i][j];//累加 \n\t}\n\tcout<<\"The sum of the diagonal:\"<<sum<<endl;\n}\n\treturn 0;\n}",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std; \nint main() {\n\twhile(1){\n\t\tint a;\n\t\tcin>>a;\n\t\tint array[a][a];\n\t\tfor (int i=0;i<a;i++)\n\t\t\tfor (int j=0;j<a;j++)\n\t\t\t{\n\t\t\t\tif(j<i)\n\t\t\t\t\tarray[i][j]=i+1-j;\n\t\t\t\telse \n\t\t\t\t\tarray[i][j]=1;\n\t\t\t}\n\t\tcout<<\"Generated matrix:\"<<endl; \n\t\tfor (int i=0;i<a;i++)\n\t\t{\t\n\t\t\tfor (int j=0;j<a;j++)\n\t\t\t{\n\t\t\t\tcout<<array[i][j];\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t\tcout<<\"del the elements on the side:\"<<endl; \n\t\tfor (int i=1;i<a-1;i++)\n\t\t{\t\n\t\t\tfor (int j=1;j<a-1;j++)\n\t\t\t{\n\t\t\t\tcout<<array[i][j];\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t\tint sum=0;\n\t\tint i,j;\n\t\tfor (i = a - 2 ,j = 1 ; i >= 1 ; i-- , j++)\n\t\t{\t\n\t\t\tsum+=array[i][j];\n\t\t}\n\t\tcout<<\"The sum of the diagonal:\"<<sum<<endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469848",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,证明主定理,对b的幂证明主定理",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/47.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 764162,
"question_title": "国名排序",
"question_content": "【字符数组】国名排序\nDescription:\n小李在准备明天的广交会,明天有来自世界各国的客房跟他们谈生意,小李要尽快的整理出名单给经理,你能帮他把客户来自的国家按英文字典次序排好吗?\n例如小李手上有来自加拿大,美国,中国的名单,排好的名单应是美国,加拿大,中国\nInput\n  第一行为一个n(n<=100)表示n个国家,第2行到第n+1行分别为n个国家的名字.\nOutput\n  输出共计n行,为n个国家按字典顺序的排列,每行为一个国家\nSample Input:\n\t3\n\tChina\n\tCanada\n\tAmerica\nSample Output:\n\tAmerica\n\tCanada\n\tChina",
"difficulty": "中等",
"answer_id": 822920,
"answer_content": "\n```\n#include <iostream>\n#include <string>\nusing namespace std;\nstring a[1000];\nint main()\n{\n int i,n;\n cin>>n;\n for(i=1; i<=n; i++)\n {\n cin>>a[i];\n }\n for(i=1;i<=n;i++)\n\t{\n\t\tfor(int j=i+1;j<=n;j++)\n\t\t{\n\t\t\tif(a[i]>a[j])\n\t\t\t swap(a[i],a[j]);\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++)\n\t cout<<a[i]<<endl;\n return 0;\n}\n```\n这个问题我以自己解决啦!应该来说还是很易懂的!其他不懂的小伙伴也可以参考一下啦!",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nstring a[1000];\nint main()\n{\n\tint i,n;\n\tcin>>n;\n\tfor(i=1; i<=n; i++)\n\t{\n\t\tcin>>a[i];\n\t}\n\tfor(i=1;i<=n;i++)\n\t{\n\t\tfor(int j=i+1;j<=n;j++)\n\t\t{\n\t\t\tif(a[i]>a[j])\n\t\t\t\tswap(a[i],a[j]);\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++)\n\t\tcout<<a[i]<<endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470156",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/48.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 243896,
"question_title": "计算第n项的值",
"question_content": "计算 s=(1+1)+(1+2)+(1+2+3)+````+(1+2+3+...+n) 第n项的值",
"difficulty": "简单",
"answer_id": 232835,
"answer_content": "错误的地方:\n1、首先include后面没有包含<iostream>\n2、sum 值没有初始化;\n3、sum是局部变量,for语句执行完成就释放了,应该定位为static变量\n4、for循环中应添加相等的情况,也就是注意边界。\n\n正常程序如下:\n#include <iostream>\nusing namespace std;\nint main()\n{\n\tint n; \n\tstatic int\tsum=1;\n\tcout << \"请输入N:\" << endl;\n\tcin >> n;\n\tfor (int i = 1; i <= n; i++)\n\t{\n\t\tfor (int j = 1; j <= i; j++)\n\t\t{\n\t\t\t\n\t\t\tsum = sum + j;\n\t\t\t\n\t\t}\n\t}\n\tcout << \"结果2为:\" << sum;\n\tsystem(\"pause\");\n\treturn 0;\n}\n",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint main()\n{\n\tint n; \n\tstatic int\tsum=1;\n\tcout << \"请输入N:\" << endl;\n\tcin >> n;\n\tfor (int i = 1; i <= n; i++)\n\t{\n\t\tfor (int j = 1; j <= i; j++)\n\t\t{\n\t\t\tsum = sum + j;\n\t\t}\n\t}\n\tcout << \"结果为:\" << sum;\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469946",
"status": 1,
"keywords": "数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/49.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7462282,
"question_title": "对数组中所有的数据按照输入先后顺序进行显示输出",
"question_content": "<p>定义整数数组x&#xff0c;用键盘向数组输入10个数&#xff0c;输入之后&#xff0c;对数组中所有的数据按照输入先后顺序进行显示输出&#xff0c;最后设计一种算法&#xff0c;对数组中的数据进行升序排序&#xff0c;并输出排序后的数</p>\n",
"difficulty": "简单",
"answer_id": 53447798,
"answer_content": "<p>代码如下&#xff1a;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n\nvoid Sortfun(double a[],int n)\n{\n\tint i,j;\n\tdouble tmp;\n\tfor (i&#61;0;i&lt;n-1;i&#43;&#43;)\n\t{\n\t\tfor (j&#61;0;j&lt;n-1-i;j&#43;&#43;)\n\t\t{\n\t\t\tif (a[j] &gt; a[j&#43;1])\n\t\t\t{\n\t\t\t\ttmp &#61; a[j];\n\t\t\t\ta[j] &#61; a[j&#43;1];\n\t\t\t\ta[j&#43;1]&#61; tmp;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main()\n{\n\tint i;\n\tdouble a[10];\n\tprintf(&#34;请输入10个数:&#34;);\n\tfor(i &#61;0;i&lt;10;i&#43;&#43;)\n\t\tscanf(&#34;%lf&#34;,&amp;a[i]);\n\n\tprintf(&#34;原顺序:\\n&#34;);\n\tfor(i&#61;0;i&lt;10;i&#43;&#43;)\n\t\tprintf(&#34;%g &#34;,a[i]);\n\tprintf(&#34;\\n&#34;);\n\tSortfun(a,10);\n\n\tprintf(&#34;排序后:\\n&#34;);\n\tfor(i&#61;0;i&lt;10;i&#43;&#43;)\n\t\tprintf(&#34;%g &#34;,a[i]);\n\tprintf(&#34;\\n&#34;);\n\treturn 0;\n\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nvoid Sortfun(double a[],int n)\n{\n\tint i,j;\n\tdouble tmp;\n\tfor (i=0;i<n-1;i++)\n\t{\n\t\tfor (j=0;j<n-1-i;j++)\n\t\t{\n\t\t\tif (a[j] > a[j+1])\n\t\t\t{\n\t\t\t\ttmp = a[j];\n\t\t\t\ta[j] = a[j+1];\n\t\t\t\ta[j+1]= tmp;\n\t\t\t}\n\t\t}\n\t}\n}\nint main()\n{\n\tint i;\n\tdouble a[10];\n\tprintf(\"请输入10个数:\");\n\tfor(i =0;i<10;i++)\n\t\tscanf(\"%lf\",&a[i]);\n\tprintf(\"原顺序:\\n\");\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%g \",a[i]);\n\tprintf(\"\\n\");\n\tSortfun(a,10);\n\tprintf(\"排序后:\\n\");\n\tfor(i=0;i<10;i++)\n\t\tprintf(\"%g \",a[i]);\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470244",
"status": 1,
"keywords": "算法初阶,基础知识,算法基础,设计算法,分析分治算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/5.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7426411,
"question_title": "字符串的处理",
"question_content": "<p>给定一个正整数N和一个由小写英文字母组成的长度N的字符串S。确定该字符串是否是某个字符串的两个副本的连接。也就是说&#xff0c;确定是否存在一个字符串T使S&#61;T&#43;T。如果S是某个字符串的两个副本的连接&#xff0c;则输出Yes;否则,直接输出No。\n例如&#xff0c;输入6&#xff08;回车&#xff09;abcabc&#xff0c;输出Yes&#xff08;回车&#xff09;Let T&#61;abc&#xff0c;and S&#61;T&#43;T。\n或者输入4&#xff08;回车&#xff09;abac&#xff0c;输出No&#xff08;回车&#xff09;结束。</p>",
"difficulty": "简单",
"answer_id": 53389947,
"answer_content": "<pre>\n<code class=\"language-objectivec\">#include &lt;stdio.h&gt;\nint main() {\n int len,i,judge&#61;0;\n scanf(&#34;%d&#34;, &amp;len);\n char a[len&#43;1];\n scanf(&#34;%s&#34;, a);\n if(len%2&#61;&#61;0) {\n for(i&#61;0;i&lt;(len/2);i&#43;&#43;)\n if(a[i]!&#61;a[(len/2)&#43;i]){\n judge&#61;1;\n break;\n }\n if(judge&#61;&#61;1)\n printf(&#34;No&#34;);\n else {\n printf(&#34;Yes&#34;);\n }\n } else\n printf(&#34;No&#34;);\n printf(&#34;\\n&#34;);\n return 0;\n}</code></pre>\n\n<p><img alt=\"\" height=\"735\" src=\"https://img-ask.csdnimg.cn/upload/1620392744663.png\" width=\"903\" /></p>",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main() {\n\tint len,i,judge=0;\n\tscanf(\"%d\", &len);\n\tchar a[len+1];\n\tscanf(\"%s\", a);\n\tif(len%2==0) {\n\t\tfor(i=0;i<(len/2);i++)\n\t\t\tif(a[i]!=a[(len/2)+i]){\n\t\t\t\tjudge=1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tif(judge==1)\n\t\t\tprintf(\"No\");\n\t\telse {\n\t\t\tprintf(\"Yes\");\n\t\t}\n\t} else\n\t\tprintf(\"No\");\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470157",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/50.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 704613,
"question_title": "排序",
"question_content": "试题描述\n 由键盘上输入n个整数,请将这些数从大到小排序,然后输出排序后的数列。\n输入\n 输入包含两行:\n 第一行是n(1 <= n <= 1000)。\n 第二行是n个整数,邻近两数之间用一个空格隔开。\n输出\n 输出排序后的n个整数,邻近两数之间用一个空格隔开。\n输入示例\n 5\n 8 2 5 1 2\n输出示例\n 8 5 2 2 1\n数据范围\n 输入和输出均为int范围的整数",
"difficulty": "简单",
"answer_id": 598037,
"answer_content": "又是你.\n\n```\n #include<iostream>\nusing namespace std;\nint main() {\n\tint n,tmp;\n\tcin >> n;\n\tint *a = new int[n];\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> a[i];\n\tfor (int i = 0; i < n - 1; i++) {\t\t//冒泡排序\n\t\tfor (int j = i + 1; j < n; j++) {\n\t\t\tif (a[i] < a[j]) {\n\t\t\t\ttmp = a[i];\n\t\t\t\ta[i] = a[j];\n\t\t\t\ta[j] = tmp;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tcout << a[i];\n\t\tif (i != n - 1)\n\t\t\tcout << \" \";\n\t}\n\treturn 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": " #include<iostream>\nusing namespace std;\nint main() {\n\tint n,tmp;\n\tcin >> n;\n\tint *a = new int[n];\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> a[i];\n\tfor (int i = 0; i < n - 1; i++) {\t\t\n\t\tfor (int j = i + 1; j < n; j++) {\n\t\t\tif (a[i] < a[j]) {\n\t\t\t\ttmp = a[i];\n\t\t\t\ta[i] = a[j];\n\t\t\t\ta[j] = tmp;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tcout << a[i];\n\t\tif (i != n - 1)\n\t\t\tcout << \" \";\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470259",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/51.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7445111,
"question_title": "求数列的第n项的值",
"question_content": "已知数列:2,4,4,4,6,6,6,6,6,8,8,8,8,8,8,8,...求第n项的值",
"difficulty": "简单",
"answer_id": 53424084,
"answer_content": "<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623205203176.png\" /></p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\n\nint main()\n{\n unsigned int N;\n std::cout &lt;&lt; &#34;Please enter the value of N: &#34;;\n std::cin &gt;&gt; N;\n if (N % 2 !&#61; 0)\n {\n std::cout &lt;&lt; &#34;Please enter an even number greater than zero!&#34; &lt;&lt; std::endl;\n return -1;\n }\n\n int oddCount &#61; 1;\n int printCount &#61; 0;\n for (int i &#61; 2; i &lt;&#61; N; i &#43;&#61; 2)\n {\n for (int j &#61; 0; j &lt; oddCount; j&#43;&#43;)\n {\n std::cout &lt;&lt; i &lt;&lt; &#34; &#34;;\n printCount&#43;&#43;;\n if (printCount &#61;&#61; N)\n {\n std::cout &lt;&lt; &#34;&lt;---这个就是第N &#61; &#34; &lt;&lt; N &lt;&lt; &#34;个数。&#34; &lt;&lt; std::endl;\n return 0;\n }\n }\n\n oddCount &#43;&#61; 2;\n }\n return 0;\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <iostream>\nint main()\n{\n\tunsigned int N;\n\tstd::cout << \"Please enter the value of N: \";\n\tstd::cin >> N;\n\tif (N % 2 != 0)\n\t{\n\t\tstd::cout << \"Please enter an even number greater than zero!\" << std::endl;\n\t\treturn -1;\n\t}\n\tint oddCount = 1;\n\tint printCount = 0;\n\tfor (int i = 2; i <= N; i += 2)\n\t{\n\t\tfor (int j = 0; j < oddCount; j++)\n\t\t{\n\t\t\tstd::cout << i << \" \";\n\t\t\tprintCount++;\n\t\t\tif (printCount == N)\n\t\t\t{\n\t\t\t\tstd::cout << \"<---这个就是第N = \" << N << \"个数。\" << std::endl;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t\toddCount += 2;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470260",
"status": 1,
"keywords": "递归,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/52.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1061094,
"question_title": "难倒数万人的小学数学题",
"question_content": "汉堡包在大街上大摇大摆的走着,看着手机上一道难倒数万人的小学数学题:\n1 + 1 = 0\n1 + 6 = 1\n6 + 6 = 2\n8 + 1 = 2\n8 + 6 = 3\n汉堡包看完之后发现上面这些加法的答案就是看1,6,8中圈圈的个数嘛!\n突然之间,所有大厦上的LED屏幕上的广告全部变成数字1,6,8三个数字的随机闪现。\n现给你一块n*m的LED屏幕,上面有且仅有一个数字(1,6,or 8),请你输出你看见的那个字母。\n输入格式:\n第一行输入两个整数n,m(2<= m, n <= 1000);\n接下来n行,每行由m个数字0和1组成,其中1表示数字1,6,8的组成部分。\n输出格式:\n输出一个整数,代表图形表示的数字。\n输入样例:\n7 7\n0 0 0 0 0 0 0\n0 0 1 1 1 0 0\n0 0 1 0 1 0 0\n0 0 1 1 1 0 0\n0 0 1 0 1 0 0\n0 0 1 1 1 0 0\n0 0 0 0 0 0 0\n输出样例:\n8",
"difficulty": "中等",
"answer_id": 1258889,
"answer_content": "\n```\n#include <cstring>\n#include <algorithm>\n#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <stack>\n#include <queue>\nusing namespace std;\nint main(){\n int i,j,k=1;\n int n,m;\n int num[1010]={0};\n int num_cmp=0;\n int flag=1;\n int led[1005][1005];\n cin >> n >> m;\n for(i=1;i<=n;i++){\n for(j=1;j<=m;j++){\n cin >> led[i][j];\n if(led[i][j]==1) num[k]++;\n }\n if(num[k]!=0)k++;\n }\n num_cmp=num[k-1];\n for(i=k-1;i>0;i--){\n if(num[i]<num_cmp) {\n flag++;\n num_cmp=num[i];\n }\n }\n if(flag==1) cout <<\"1\"<<endl;\n else if(flag==2) cout <<\"8\"<<endl;\n else cout <<\"6\"<<endl;\n return 0;\n}\n```\nhttps://www.cnblogs.com/lvhang/p/12589068.html",
"tag_name": "c++",
"cpp": "#include <cstring>\n#include <algorithm>\n#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <stack>\n#include <queue>\nusing namespace std;\nint main(){\n\tint i,j,k=1;\n\tint n,m;\n\tint num[1010]={0};\n\tint num_cmp=0;\n\tint flag=1;\n\tint led[1005][1005];\n\tcin >> n >> m;\n\tfor(i=1;i<=n;i++){\n\t\tfor(j=1;j<=m;j++){\n\t\t\tcin >> led[i][j];\n\t\t\tif(led[i][j]==1) num[k]++;\n\t\t}\n\t\tif(num[k]!=0)k++;\n\t}\n\tnum_cmp=num[k-1];\n\tfor(i=k-1;i>0;i--){\n\t\tif(num[i]<num_cmp) {\n\t\t\tflag++;\n\t\t\tnum_cmp=num[i];\n\t\t}\n\t}\n\tif(flag==1) cout <<\"1\"<<endl;\n\telse if(flag==2) cout <<\"8\"<<endl;\n\telse cout <<\"6\"<<endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470261",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/53.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 968207,
"question_title": "查找书籍",
"question_content": "给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。\n输入格式:\n输入第一行给出正整数n(<10),随后给出n本书的信息。每本书在一行中给出书名,即长度不超过30的字符串,随后一行中给出正实数价格。题目保证没有同样价格的书。\n输出格式:\n在一行中按照“价格, 书名”的格式先后输出价格最高和最低的书。价格保留2位小数。\n输入样例:\n3\nProgramming in C\n21.5\nProgramming in VB\n18.5\nProgramming in Delphi\n25.0\n输出样例:\n25.00, Programming in Delphi\n18.50, Programming in VB\n",
"difficulty": "简单",
"answer_id": 1146300,
"answer_content": "\n```\n#include<stdio.h>\nstruct book\n{\n float price;\n char a[30];\n};\nint main()\n{\n int n;\n scanf(\"%d\",&n);\n\n char a[30];\n\tint i,k,maxi=0,mini=0;\n\tbook b[10];\n for(i=0;i<n;i++)\n {\n\t\tfflush(stdin);\n\t\tfor(k=0;;k++)\n {\n b[i].a[k]=getchar();\n if(b[i].a[k]=='\\n')\n\t\t\t{\n\t\t\t\tb[i].a[k] = '\\0';\n\t\t\t\tbreak;\n\t\t\t}\n }\n scanf(\"%f\",&b[i].price);\n }\n for(i=1;i<n;i++)\n {\n if(b[i].price>b[maxi].price)\n {\n maxi=i;\n }\n\t\tif(b[i].price<b[mini].price)\n {\n mini=i;\n }\n }\n printf(\"%.2f,%s\\n\",b[maxi].price,b[maxi].a);\n printf(\"%.2f,%s\",b[mini].price,b[mini].a);\n return 0;\n}\n\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nstruct book\n{\n\tfloat price;\n\tchar a[30];\n};\nint main()\n{\n\tint n;\n\tscanf(\"%d\",&n);\n\tchar a[30];\n\tint i,k,maxi=0,mini=0;\n\tbook b[10];\n\tfor(i=0;i<n;i++)\n\t{\n\t\tfflush(stdin);\n\t\tfor(k=0;;k++)\n\t\t{\n\t\t\tb[i].a[k]=getchar();\n\t\t\tif(b[i].a[k]=='\\n')\n\t\t\t{\n\t\t\t\tb[i].a[k] = '\\0';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tscanf(\"%f\",&b[i].price);\n\t}\n\tfor(i=1;i<n;i++)\n\t{\n\t\tif(b[i].price>b[maxi].price)\n\t\t{\n\t\t\tmaxi=i;\n\t\t}\n\t\tif(b[i].price<b[mini].price)\n\t\t{\n\t\t\tmini=i;\n\t\t}\n\t}\n\tprintf(\"%.2f,%s\\n\",b[maxi].price,b[maxi].a);\n\tprintf(\"%.2f,%s\",b[mini].price,b[mini].a);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469849",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/54.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7437199,
"question_title": "选择排序法",
"question_content": "<p>使用选择排序法对10个整数进行由大到小排序。</p>",
"difficulty": "简单",
"answer_id": 53409792,
"answer_content": "<pre>\n<code>\n#include &lt;stdio.h&gt;\nint main() {\n\tint a[10];\n\tint i,j,temp&#61;0;\n\tint k,x&#61;0;\n\tprintf(&#34;输入10个数&#xff1a;\\n&#34;);\n\tfor(i&#61;0; i&lt;10; i&#43;&#43;)\n\t\tscanf(&#34;%d&#34;,&amp;a[i]);\n\tfor(i&#61;0; i&lt;9; i&#43;&#43;) {\n\t\tk &#61; i;\n\t\tfor(j&#61;i&#43;1; j&lt;10; j&#43;&#43;)\n\t\t\tif(a[j]&lt;a[i])\n\t\t\t\tk &#61; j;\n\t\ttemp&#61;a[i];\n\t\ta[i]&#61;a[k];\n\t\ta[k]&#61;temp;\n\t}\n\tprintf(&#34;排序后&#xff1a;\\n&#34;);\n\tfor(i&#61;0; i&lt;10; i&#43;&#43;)\n\t\tprintf(&#34;%d &#34;,a[i]);\n\tgetchar();\n\tgetchar();\n}</code></pre>\n\n<p>代码如上&#xff0c;万望采纳。</p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main() {\n\tint a[10];\n\tint i,j,temp=0;\n\tint k,x=0;\n\tprintf(\"输入10个数:\\n\");\n\tfor(i=0; i<10; i++)\n\t\tscanf(\"%d\",&a[i]);\n\tfor(i=0; i<9; i++) {\n\t\tk = i;\n\t\tfor(j=i+1; j<10; j++)\n\t\t\tif(a[j]<a[i])\n\t\t\t\tk = j;\n\t\ttemp=a[i];\n\t\ta[i]=a[k];\n\t\ta[k]=temp;\n\t}\n\tprintf(\"排序后:\\n\");\n\tfor(i=0; i<10; i++)\n\t\tprintf(\"%d \",a[i]);\n\tgetchar();\n\tgetchar();\n}",
"topic_link": "https://bbs.csdn.net/topics/600470158",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/55.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1083775,
"question_title": "六角填数",
"question_content": "<p>![图片说明](https://img-ask.csdn.net/upload/202006/14/1592104139_240178.png)\n六角填数\n题目描述\n如下图所示六角形中,有12个点,依次填入1~12的数字,使得每条直线上的数字之和都相同。其中,已经替你填好了点1,2,3的数字,请你计算其他位置所代表的数字是多少?\n输入\n输入仅一行,以空格隔开,分别表示已经填好的点1,2,3的数字。\n输出\n输出仅一行,以空格隔开,分别表示所有位置所代表的数字。\n样例输入:\n1 8 2\n样例输出:\n1 8 2 9 7 11 10 12 3 5 6 4</p>",
"difficulty": "困难",
"answer_id": 1290115,
"answer_content": "\n```\n#include <iostream>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\nusing namespace std;\n\n#define eps 10e-10\n#define N 15\n\nint a[N];\nbool vis[N];\n\nvoid dfs(int x){\n if(x == 1 || x == 2 || x == 3){\n dfs(x+1);\n return ;\n }\n if(x > 12){\n int t[6];\n t[0] = a[1] + a[3] + a[6] + a[8];\n t[1] = a[1] + a[4] + a[7] + a[11];\n t[2] = a[2] + a[3] + a[4] + a[5];\n t[3] = a[2] + a[6] + a[9] + a[12];\n t[4] = a[8] + a[9] + a[10] + a[11];\n t[5] = a[12] + a[10] + a[7] + a[5];\n\n for(int i = 1; i < 6; ++i){\n if(t[i] != t[i-1])return ;\n }\n\t\tfor (int i = 1; i <= 12; i++) cout << a[i] << \" \";\n return ;\n }\n\n for(int i = 1;i < 13; ++i){\n if(!vis[i]){\n vis[i] = 1;\n a[x] = i;\n dfs(x+1);\n vis[i] = 0;\n }\n }\n\n\n}\n\nint main(){\n memset(vis,0,sizeof(vis));\n cin >> a[1];\n\tvis[a[1]] = 1;\n cin >> a[2];\n\tvis[a[2]] = 1;\n cin >> a[3];\n\tvis[a[3]] = 1;\n dfs(1);\n return 0;\n}\n\n```\n![图片说明](https://img-ask.csdn.net/upload/202006/14/1592106523_783342.png)\n\n# lz你刚来,长点心吧,这里很多人看似在回答你问题,实际上都是投机取巧胡乱抄袭。\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\nusing namespace std;\n#define eps 10e-10\n#define N 15\nint a[N];\nbool vis[N];\nvoid dfs(int x){\n\tif(x == 1 || x == 2 || x == 3){\n\t\tdfs(x+1);\n\t\treturn ;\n\t}\n\tif(x > 12){\n\t\tint t[6];\n\t\tt[0] = a[1] + a[3] + a[6] + a[8];\n\t\tt[1] = a[1] + a[4] + a[7] + a[11];\n\t\tt[2] = a[2] + a[3] + a[4] + a[5];\n\t\tt[3] = a[2] + a[6] + a[9] + a[12];\n\t\tt[4] = a[8] + a[9] + a[10] + a[11];\n\t\tt[5] = a[12] + a[10] + a[7] + a[5];\n\t\tfor(int i = 1; i < 6; ++i){\n\t\t\tif(t[i] != t[i-1])return ;\n\t\t}\n\t\tfor (int i = 1; i <= 12; i++) cout << a[i] << \" \";\n\t\treturn ;\n\t}\n\tfor(int i = 1;i < 13; ++i){\n\t\tif(!vis[i]){\n\t\t\tvis[i] = 1;\n\t\t\ta[x] = i;\n\t\t\tdfs(x+1);\n\t\t\tvis[i] = 0;\n\t\t}\n\t}\n}\nint main(){\n\tmemset(vis,0,sizeof(vis));\n\tcin >> a[1];\n\tvis[a[1]] = 1;\n\tcin >> a[2];\n\tvis[a[2]] = 1;\n\tcin >> a[3];\n\tvis[a[3]] = 1;\n\tdfs(1);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470159",
"status": 1,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/56.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7461173,
"question_title": "n个素数的求和问题。",
"question_content": "<p>给定n&#xff08;n≤100&#xff09;个正整数&#xff0c;所有正整数均≤1000000&#xff1b;求其中所有素数的和。\n例如给定序列&#xff1a; 2 3 4 5 6&#xff0c;素数和为&#xff1a;10\n给定序列&#xff1a; 3 4 5 6 7&#xff0c; 素数和为&#xff1a;15\n给定序列&#xff1a; 12 19 23 35 68 71&#xff0c; 素数和为&#xff1a; 113\n输入格式:\n输入为两行。第一行是一个正整数n&#xff0c;表示有多少个数据。第二行是n个正整数组成的序列。\n输出格式:\n输出一个正整数&#xff0c;是上述序列中所有素数之和。</p>",
"difficulty": "简单",
"answer_id": 53446246,
"answer_content": "<pre>\n<code class=\"language-objectivec\">#include &lt;stdio.h&gt;\nint isprime( int n);\nint main()\n{\n int i,n,m,s&#61;0;\n scanf(&#34;%d&#34;, &amp;n);\n for( i&#61;0; i&lt;n; i&#43;&#43; ) {\n scanf(&#34;%d&#34;, &amp;m);\n if(isprime(m)){\n s&#43;&#61;m;\n }\n }\n printf(&#34;%d&#34;, s);\n return 0;\n}\nint isprime(int n)\n{\n int i;\n if(n&lt;&#61;1) return 0;\n for(i&#61;2;i*i&lt;&#61;n;i&#43;&#43;)\n if(n%i&#61;&#61;0) return 0;\n return 1;\n}\n</code></pre>\n\n<p><img alt=\"\" height=\"61\" src=\"https://img-ask.csdnimg.cn/upload/1624791691130.png\" width=\"148\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint isprime( int n);\nint main()\n{\n\tint i,n,m,s=0;\n\tscanf(\"%d\", &n);\n\tfor( i=0; i<n; i++ ) {\n\t\tscanf(\"%d\", &m);\n\t\tif(isprime(m)){\n\t\t\ts+=m;\n\t\t}\n\t}\n\tprintf(\"%d\", s);\n\treturn 0;\n}\nint isprime(int n)\n{\n\tint i;\n\tif(n<=1) return 0;\n\tfor(i=2;i*i<=n;i++)\n\t\tif(n%i==0) return 0;\n\treturn 1;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470160",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/57.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1067964,
"question_title": "找最大数和最小数",
"question_content": "编写程序,设置正整数n的值,其中n取值为小于等于10的正整数,再继续输入n个整数数值,从n个整数中找出最大数和最小数,并将它们输出。\n输入格式:\nn的数值(取值为1-10整数)\nn个具体整数值\n输出格式:\n最大值\n最小值\n输入样例1:\n4 \n8 3 4 1 \n输出样例1:\n8\n1\n输入样例2:\n5\n-1 6 3 2 0\n输出样例2:\n6\n-1",
"difficulty": "简单",
"answer_id": 1268432,
"answer_content": "\n```\n#include <iostream>\nusing namespace std;\nint main()\n{\nint n;\ncin >> n;\nint ma, mi, a;\ncin >> a;\nmi = ma = a;\nfor (int i = 1; i < n; i++)\n{\ncin >> a;\nif (ma < a) ma = a;\nif (mi > a) mi = a;\n}\ncout << ma << endl << mi << endl;\n}\n```\n\n# 问题解决的话,请点下采纳",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin >> n;\n\tint ma, mi, a;\n\tcin >> a;\n\tmi = ma = a;\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tcin >> a;\n\t\tif (ma < a) ma = a;\n\t\tif (mi > a) mi = a;\n\t}\n\tcout << ma << endl << mi << endl;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470161",
"status": 1,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/58.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 251110,
"question_title": "筛选10到1000的回文数",
"question_content": "筛选出10到1000的回文数",
"difficulty": "简单",
"answer_id": 249084,
"answer_content": "\n\n```\n #include <stdio.h>\nint main() \n{ \n int m,n,k; \n for(m=1;m<=1000;m++) \n { \n k=m; \n n=0; \n while(k>0) \n { \n n=n*10+(k%10); \n k=k/10; \n } \n if(m==n) printf(\"%d \", m); \n }\n return 0; \n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main() \n{ \n\tint m,n,k; \n\tfor(m=1;m<=1000;m++) \n\t{ \n\t\tk=m; \n\t\tn=0; \n\t\twhile(k>0) \n\t\t{ \n\t\t\tn=n*10+(k%10); \n\t\t\tk=k/10; \n\t\t} \n\t\tif(m==n) printf(\"%d \", m); \n\t}\n\treturn 0;\t \n}",
"topic_link": "https://bbs.csdn.net/topics/600470162",
"status": 1,
"keywords": "回文,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/59.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1089360,
"question_title": "输出1~10的平方与平方根",
"question_content": "![图片说明](https://img-ask.csdn.net/upload/202006/28/1593313257_732285.jpg)\n\n如图所示列出1~10,输出平方根和平方",
"difficulty": "简单",
"answer_id": 1295912,
"answer_content": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <math.h>\n#include <cstring>\n#include <map>\n#include <set>\nusing namespace std;\n\nint main()\n{\n\tdouble sum;\n\tdouble sq;\n\tfor(int i=1;i<=10;i++)\n\t{\n\t\tsum=i*i;\n\t\tsq=sqrt(i);\n\t\tprintf(\"%d %5.5f %12.2f\\n\",i,sq,sum);\n\t} \n\t\t \n}",
"tag_name": "c语言",
"cpp": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <math.h>\n#include <cstring>\n#include <map>\n#include <set>\nusing namespace std;\nint main()\n{\n\tdouble sum;\n\tdouble sq;\n\tfor(int i=1;i<=10;i++)\n\t{\n\t\tsum=i*i;\n\t\tsq=sqrt(i);\n\t\tprintf(\"%d %5.5f %12.2f\\n\",i,sq,sum);\n\t} \n}",
"topic_link": "https://bbs.csdn.net/topics/600469843",
"status": 0,
"keywords": "数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/6.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7448649,
"question_title": "个位数是6,且能被3整除的五位数共有多少个?",
"question_content": "<p>要求:\n1、必须包含循环结构、顺序结构、选择分支结构。\n2、必须包含数组\n3、必须包含一个以上函数\n4、可以包含方针</p>",
"difficulty": "简单",
"answer_id": 53428800,
"answer_content": "<pre>\n<code>#include &lt;iostream&gt;\n\nusing namespace std;\nvoid search1(){\n int i,t&#61;0;\n for(i&#61;10000;i&lt;&#61;99999;i&#43;&#43;){\n if(i%3&#61;&#61;0&amp;&amp;i%10&#61;&#61;6)\n t&#43;&#43;;\n }\n cout&lt;&lt;t;\n}\nint main()\n{\n search1();\n\n return 0;\n}\n</code></pre>\n\n<p><img alt=\"\" height=\"223\" src=\"https://img-ask.csdnimg.cn/upload/1623581634678.png\" width=\"592\" /> </p>\n",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nvoid search1(){\n\tint i,t=0;\n\tfor(i=10000;i<=99999;i++){\n\t\tif(i%3==0&&i%10==6)\n\t\t\tt++;\n\t}\n\tcout<<t;\n}\nint main()\n{\n\tsearch1();\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470262",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量,期望为线性时间的选择算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/60.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1064304,
"question_title": "数据合并",
"question_content": "题目描述\n将两个从小到大排列的一维数组 (维长分别为 m,n , 其中 m,n≤100) 仍按从小到大的排列顺序合并到一个新的一维数组中,输出新的数组.\n输入描述\n第 1行一个正整数 m , 表示第一个要合并的一维数组中的元素个数\n第 2 行一个正整数 n , 表示第二个要合并的一维数组中的元素个数\n第 3 行输入 m 个整数 (每个数用空格分开) , 表示第一个数组元素的值.\n第 4 行输入 n 个整数 (每个数用空格分开) , 表示第二个数组元素的值.\n输出描述\n一行,表示合并后的数据,共 m +n 个数\n样例输入\n3\n4\n1 3 5\n2 4 6 8\n样例输出\n1 2 3 4 5 6 8\n要多组输入输出",
"difficulty": "简单",
"answer_id": 1262527,
"answer_content": "\n```\n#include <iostream>\nusing namespace std;\n\nvoid merge(int * a1, int m, int * a2, int n)\n{\n\tint m1 = m - 1;\n\tint n1 = n - 1;\n\tfor (int i = m + n - 1; i >= 0; i--)\n\t{\n\t\tif (m1 < 0) a1[i] = a2[n1--];\n\t\telse if (n1 < 0) a1[i] = a1[m1--];\n\t\telse if (a1[m1] < a2[n1]) a1[i] = a2[n1--];\n\t\telse a1[i] = a1[m1--];\n\t}\n}\n\nint main()\n{\n\tint m;\n\tint n;\n\tcin >> m;\n\tcin >> n;\n\tint a1[201];\n\tint a2[101];\n\tfor (int i = 0; i < m; i++) cin >> a1[i];\n\tfor (int i = 0; i < n; i++) cin >> a2[i];\n\tmerge(a1, m, a2, n);\n\tfor (int i = 0; i < m + n; i++) cout << a1[i] << \" \";\n\treturn 0;\n}\n```",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nvoid merge(int * a1, int m, int * a2, int n)\n{\n\tint m1 = m - 1;\n\tint n1 = n - 1;\n\tfor (int i = m + n - 1; i >= 0; i--)\n\t{\n\t\tif (m1 < 0) a1[i] = a2[n1--];\n\t\telse if (n1 < 0) a1[i] = a1[m1--];\n\t\telse if (a1[m1] < a2[n1]) a1[i] = a2[n1--];\n\t\telse a1[i] = a1[m1--];\n\t}\n}\nint main()\n{\n\tint m;\n\tint n;\n\tcin >> m;\n\tcin >> n;\n\tint a1[201];\n\tint a2[101];\n\tfor (int i = 0; i < m; i++) cin >> a1[i];\n\tfor (int i = 0; i < n; i++) cin >> a2[i];\n\tmerge(a1, m, a2, n);\n\tfor (int i = 0; i < m + n; i++) cout << a1[i] << \" \";\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470263",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/61.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1053941,
"question_title": "猴子吃桃",
"question_content": "问题:猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入不同的天数n进行递推,即假设第n天的桃子数为1。同时还要增加对用户输入数据的合法性验证(如:不允许输入的天数是0和负数)\n",
"difficulty": "中等",
"answer_id": 1251130,
"answer_content": "\n```\n#include <stdio.h>\n\nint main()\n{\n\tint ret,day,i=1,sum=1;\n\twhile (1)\n\t{\n\t\tprintf(\"Input days:\\n\");\n\t\tret=scanf(\"%d\",&day);\n\t\tif ((ret!=1)||(day<=0))\n\t\t{\n\t\t\tfflush(stdin);\n\t\t\tcontinue;\n\t\t}\n\t\tbreak;\n\t}\n\tdo\n\t{\n\t\tsum=(sum+1)*2;\n\t\ti++;\n\t}while(i<day);\n\tprintf(\"sum=%d\\n\",sum);\n\treturn 0;\n}\n```\n\n# 问题解决的话,请点下`采纳`",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint ret,day,i=1,sum=1;\n\twhile (1)\n\t{\n\t\tprintf(\"Input days:\\n\");\n\t\tret=scanf(\"%d\",&day);\n\t\tif ((ret!=1)||(day<=0))\n\t\t{\n\t\t\tfflush(stdin);\n\t\t\tcontinue;\n\t\t}\n\t\tbreak;\n\t}\n\tdo\n\t{\n\t\tsum=(sum+1)*2;\n\t\ti++;\n\t}while(i<day);\n\tprintf(\"sum=%d\\n\",sum);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469947",
"status": 1,
"keywords": "算法高阶,NP完全性,算法问题选编,多项式时间的验证",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/62.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436897,
"question_title": "输入某人的身高(厘米)和体重(公斤),按下式确定此人的体重是否标准、过胖或过瘦",
"question_content": "<p>&#xff08;1&#xff09;标准体重&#61;&#xff08;身高-110&#xff09;公斤&#xff1b;\n&#xff08;2&#xff09;超过标准体重5公斤为过胖&#xff1b;\n&#xff08;3&#xff09;低于标准体重5公斤为过瘦。\n例如&#xff1a;输入身高和体重分别为160&#xff0c;60&#xff0c;输出为过胖\n输入身高和体重分别为160&#xff0c;50&#xff0c;输出为标准\n输入身高和体重分别是160&#xff0c;40&#xff0c;输出为过瘦</p>",
"difficulty": "简单",
"answer_id": 53409166,
"answer_content": "<p>代码如下&#xff1a;</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n\n int main() \n { \n\t int h,w;\n\t printf(&#34;请输入身高&#xff08;厘米&#xff09;和体重&#xff08;公斤&#xff09;&#34;);\n\t scanf(&#34;%d %d&#34;,&amp;h,&amp;w);\n\n\tint s &#61; h - 110;\n\tif( (w-s) &gt; 5)\n\t\tprintf(&#34;过胖\\n&#34;);\n\telse if( s - w &gt; 5)\n\t\tprintf(&#34;过瘦&#34;);\n\telse if(s - w &#61;&#61; 0)\n\t\tprintf(&#34;标准\\n&#34;);\n\n\n\t //getchar();\n\t //getchar();\n\t return 0; \n } </code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main() \n{ \n\tint h,w;\n\tprintf(\"请输入身高(厘米)和体重(公斤)\");\n\tscanf(\"%d %d\",&h,&w);\n\tint s = h - 110;\n\tif( (w-s) > 5)\n\t\tprintf(\"过胖\\n\");\n\telse if( s - w > 5)\n\t\tprintf(\"过瘦\");\n\telse\n\t\tprintf(\"标准\\n\");\n\treturn 0; \n} ",
"topic_link": "https://bbs.csdn.net/topics/600469850",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/63.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436200,
"question_title": "数组排序",
"question_content": "定义一个包含5个整型元素(1, 5, 3, 92, 6)的数组,将数组元素按照由 小到大的顺序输出",
"difficulty": "简单",
"answer_id": 53407715,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n\n#define N (int)5\n\nint main()\n{\n\tint a[N] &#61; {1,5,3,92,6};\n\tint tmp;\n\tfor (int i &#61; 0; i &lt; N-1; i&#43;&#43;)\n\t{\n\t\tfor (int j &#61; 0;j &lt; N-1-i;j&#43;&#43;)\n\t\t{\n\t\t\tif (a[j] &gt; a[j&#43;1])\n\t\t\t{\n\t\t\t\ttmp &#61; a[j];\n\t\t\t\ta[j] &#61; a[j&#43;1];\n\t\t\t\ta[j&#43;1] &#61; tmp;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i &#61; 0; i &lt; N; i&#43;&#43;)\n\t{\n\t\tprintf(&#34;%d &#34;,a[i]);\n\t}\n\t//getchar();\n\t//getchar();\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#define N (int)5\nint main()\n{\n\tint a[N] = {1,5,3,92,6};\n\tint tmp;\n\tfor (int i = 0; i < N-1; i++)\n\t{\n\t\tfor (int j = 0;j < N-1-i;j++)\n\t\t{\n\t\t\tif (a[j] > a[j+1])\n\t\t\t{\n\t\t\t\ttmp = a[j];\n\t\t\t\ta[j] = a[j+1];\n\t\t\t\ta[j+1] = tmp;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < N; i++)\n\t{\n\t\tprintf(\"%d \",a[i]);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470264",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/64.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 924952,
"question_title": "找出所有三位素数",
"question_content": "题目描述\n一个n位超级素数是指一个n位正整数,它的前1位,前2位,......,前n位均为素数,例如,733是个3位超级素数,因为7,73,733均为素数。输出全部的3位数超级素数。 \n输入\n输出\n全部的3位数超级素数,每行一个数",
"difficulty": "简单",
"answer_id": 1060112,
"answer_content": "\n```\n#include <stdio.h>\n\nint isprime(int x)\n{\n if (x == 0 || x == 1) return 0;\n for (int i = 2; i <= x / 2; i++)\n if (!(x % i)) return 0;\n return 1;\n}\n\nint isallprime(int x)\n{\n\tdo\n {\n\t\tif (!isprime(x)) return 0;\n\t\tx /= 10;\n }\n\twhile (x > 0);\n return 1;\n}\n\nint main()\n{\n for (int i = 111; i < 999; i++)\n if (isallprime(i)) printf(\"%d\\n\", i);\n}\n```\n233\n239\n293\n311\n313\n317\n373\n379\n593\n599\n719\n733\n739\n797",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint isprime(int x)\n{\n\tif (x == 0 || x == 1) return 0;\n\tfor (int i = 2; i <= x / 2; i++)\n\t\tif (!(x % i)) return 0;\n\treturn 1;\n}\nint isallprime(int x)\n{\n\tdo\n\t{\n\t\tif (!isprime(x)) return 0;\n\t\tx /= 10;\n\t}\n\twhile (x > 0);\n\treturn 1;\n}\nint main()\n{\n\tfor (int i = 111; i <= 1000; i++)\n\t\tif (isallprime(i)) printf(\"%d\\n\", i);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469948",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/65.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 978676,
"question_title": "计算字符串逆序数",
"question_content": "例如:字符串中的内容为:a1Ab1D2,1<A,A<b 1<D 则调用该函数后,返回码为:3。 ",
"difficulty": "简单",
"answer_id": 1138072,
"answer_content": "\n```\n#include <iostream>\n#include <string>\nusing namespace std;\n\nint solve(string s)\n{\n\tif (s.length() == 0) return 0;\n\tint n = 0;\n\tfor (int i = 1; i < s.length(); i++)\n\t\tif (s.c_str()[i] < s.c_str()[i - 1]) n++;\n\treturn n;\n}\n\nint main()\n{\n\tstring s = \"a1Ab1D2\";\n\tint n = solve(s);\n\tcout << n << endl;\n\treturn 0;\n}\n```\n\n# 问题解决请点下采纳",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint solve(string s)\n{\n\tif (s.length() == 0) return 0;\n\tint n = 0;\n\tfor (int i = 1; i < s.length(); i++)\n\t\tif (s.c_str()[i] < s.c_str()[i - 1]) n++;\n\treturn n;\n}\nint main()\n{\n\tstring s = \"a1Ab1D2\";\n\tint n = solve(s);\n\tcout << n << endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469851",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/66.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1058505,
"question_title": "报数",
"question_content": "有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。",
"difficulty": "简单",
"answer_id": 1255809,
"answer_content": "\n```\n#include<stdio.h>\nint main()\n\n int i,n,a[100],count,* p,out;\n\n printf(\"共有几个人围成一圈?(不超过100人)\\n\");\n\n scanf(\"%d\",&n);\n\n for(i=0;i<n;i++)      //从1到n给每个人编号\n  a[i]=i+1;\n\n i=0;                  //i为每次循环时的计数变量\n\n count=0;              //这是按123报数时的计数变量\n\n out=0;                //记录退出人数\n\n p=a;\n\n    while(out<n-1)          //循环直到只剩下一个人没退出为止\n {\n  if(*(p+i)!=0)     //遍历遇到没退出的人(序号没被标为0),123的报数+1\n   count++;\n  \n  if(count==3)      //当报数到3的时候,把退出的人的序号标为0,123的计数重新开始,退出人数+1\n  {\n   *(p+i)=0;    \n\n   count=0;     \n\n   out++;\n  }\n  \n  i++;              //计数变量+1,继续遍历\n  \n  if(i==n)          //遍历完一遍后,有一部分人退出,接着遍历第二遍;\n   i=0;\n }\n\n while(*p==0)          //找出唯一一个不为0的数,即最后剩下的人的序号\n  p++;\n\n printf(\"最后剩下的人是第%d号\\n\",*p);\n\n return 0;\n}\n\n```\nhttps://blog.csdn.net/a251844314/article/details/53302762\n\n# 问题解决的话,请点下采纳",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n\n int i,n,a[100],count,* p,out;\n printf(\"共有几个人围成一圈?(不超过100人)\\n\");\n scanf(\"%d\",&n);\n for(i=0;i<n;i++)      \n  a[i]=i+1;\n i=0;                  \n count=0;              \n out=0;                \n p=a;\n    while(out<n-1)          \n {\n  if(*(p+i)!=0)     \n   count++;\n  if(count==3)      \n  {\n   *(p+i)=0;    \n   count=0;     \n   out++;\n  }\n  i++;              \n  if(i==n)          \n   i=0;\n }\n while(*p==0)          \n  p++;\n printf(\"最后剩下的人是第%d号\\n\",*p);\n return 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469949",
"status": 0,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/67.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1062008,
"question_title": "请问一共可以有多少种取木棍的方案",
"question_content": "目前有一个长度为 n 的木棍,当做直角三角形的斜边。A,B,C要从许多整数长度的木棍中选出三根,分别长为 a, b, c。 现在,蒜头君和花椰妹的木棍组成一条直角边长度为 a + b,白菜君组成另外一条直角边 c,并且要求 a + b ≤ c。请问一共可以有多少种取木棍的方案。 提示:a = 3, b = 4 与 a = 4, b = 3 算作同一种方案。",
"difficulty": "中等",
"answer_id": 1259875,
"answer_content": "\n```\n#include <stdio.h>\n\nint main()\n{\n\tint n;\n\tint cnt = 0;\n\tscanf(\"%d\", &n);\n\tfor (int a = 1; a < n; a++)\n\t\tfor (int b = a; b < n - a; b++)\n\t\t\tfor (int c = 1; c < n; c++)\n\t\t\t{\n\t\t\t\tif ((a+b)*(a+b)+c*c==n*n)\n\t\t\t\t{\n\t\t\t\t\tprintf(\"a=%d b=%d c=%d\\n\", a, b, c);\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t}\n\tprintf(\"一共有%d种方案\", cnt);\n\treturn 0;\n}\n```\n5\na=1 b=2 c=4\na=1 b=3 c=3\na=2 b=2 c=3\n一共有3种方案Press any key to continue . . .\n\n\n10\na=1 b=5 c=8\na=1 b=7 c=6\na=2 b=4 c=8\na=2 b=6 c=6\na=3 b=3 c=8\na=3 b=5 c=6\na=4 b=4 c=6\n一共有7种方案Press any key to continue . . .\n\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint n;\n\tint cnt = 0;\n\tscanf(\"%d\", &n);\n\tfor (int a = 1; a < n; a++)\n\t\tfor (int b = a; b < n - a; b++)\n\t\t\tfor (int c = 1; c < n; c++)\n\t\t\t{\n\t\t\t\tif ((a+b)*(a+b)+c*c==n*n)\n\t\t\t\t{\n\t\t\t\t\tprintf(\"a=%d b=%d c=%d\\n\", a, b, c);\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t}\n\tprintf(\"一共有%d种方案\", cnt);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469852",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/68.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7449552,
"question_title": "按要求完成数据的输入输出",
"question_content": "<p>有6个学生&#xff0c;每个学生的数据包括学号、姓名、3门课的成绩、平均成绩&#xff0c;输入每个学生的信息&#xff0c;平均成绩需通过计算得出&#xff0c;输出学生的信息。要求&#xff1a;输入、输出不使用函数。</p>",
"difficulty": "简单",
"answer_id": 53429731,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\n#include&lt;stdlib.h&gt;\n int n;\nstruct person\n{\n char id [20];\n char name [20];\n int s1,s2,s3;\n int sum;\n float ave;\n} p[100];\n\n\n\nint main()\n{\n int i,j,t,m;\n scanf(&#34;%d&#34;,&amp;n);\n for(int i&#61;0; i&lt;n; i&#43;&#43;)\n {\n p[i].sum&#61;0;\n scanf(&#34;%s %s %d %d %d&#34;,p[i].id,p[i].name,&amp;p[i].s1,&amp;p[i].s2,&amp;p[i].s3);\n p[i].sum&#43;&#61;p[i].s1&#43;p[i].s2&#43;p[i].s3;\n p[i].ave&#61;p[i].sum/3.0;\n }\n for(i&#61;0;i&lt;n;i&#43;&#43;) {\n\n\t}\n for(i&#61;0; i&lt;n; i&#43;&#43;)\n {\n printf(&#34;%s %s %d %d %d %.2f\\n&#34;,p[i].id,p[i].name,p[i].s1,p[i].s2,p[i].s3,p[i].ave);\n}\n }\n/* 6\n101 张三 80 76 98\n 102 李四 96 56 78\n 103 王五 64 89 63\n 104 小二 62 54 86\n 105 小四 78 86 91\n 106 小七 78 66 81*/\n</code></pre>\n\n<p><img alt=\"\" height=\"434\" src=\"https://img-ask.csdnimg.cn/upload/1623726404648.png\" width=\"665\" /> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<string.h>\n#include<stdlib.h>\nint n;\nstruct person\n{\n\tchar id [20];\n\tchar name [20];\n\tint s1,s2,s3;\n\tint sum;\n\tfloat ave;\n} p[100];\nint main()\n{\n\tint i,j,t,m;\n\tscanf(\"%d\",&n);\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tp[i].sum=0;\n\t\tscanf(\"%s %s %d %d %d\",p[i].id,p[i].name,&p[i].s1,&p[i].s2,&p[i].s3);\n\t\tp[i].sum+=p[i].s1+p[i].s2+p[i].s3;\n\t\tp[i].ave=p[i].sum/3.0;\n\t}\n\tfor(i=0; i<n; i++)\n\t{\n\t\tprintf(\"%s %s %d %d %d %.2f\\n\",p[i].id,p[i].name,p[i].s1,p[i].s2,p[i].s3,p[i].ave);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469853",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/69.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7449094,
"question_title": "工龄问题求解,工龄 人数",
"question_content": "<p>给定公司N名员工的工龄&#xff0c;要求按工龄增序输出每个工龄段有多少员工。输入首先给出正整数N&#xff0c;即员工总人数&#xff1b;\n随后给出N个整数&#xff0c;即每个员工的工龄&#xff0c;范围在[0, 99]。其中&#xff0c;0-9为第1个工龄段&#xff0c;10-19为第2个工龄段&#xff0c;…&#xff0c;90-99为第10个工龄段。按工龄的递增顺序输出每个工龄的员工个数&#xff0c;格式为&#xff1a;“工龄:人数”。每项占一行。如果人数为0则不输出该项。</p>",
"difficulty": "简单",
"answer_id": 53429153,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\nint main()\n{\n int i,n,a[11]&#61; {0},x;\n scanf(&#34;%d&#34;,&amp;n);\n for(i&#61;0; i&lt;n; i&#43;&#43;)\n {\n scanf(&#34;%d&#34;,&amp;x);\n if(x&lt;&#61;9)\n a[1]&#43;&#43;;\n else if(x&gt;9&amp;&amp;x&lt;&#61;19)\n a[2]&#43;&#43;;\n else if(x&gt;19&amp;&amp;x&lt;&#61;29)\n a[3]&#43;&#43;;\n else if(x&gt;29&amp;&amp;x&lt;&#61;39)\n a[4]&#43;&#43;;\n else if(x&gt;39&amp;&amp;x&lt;&#61;49)\n a[5]&#43;&#43;;\n else if(x&gt;49&amp;&amp;x&lt;&#61;59)\n a[6]&#43;&#43;;\n else if(x&gt;59&amp;&amp;x&lt;&#61;69)\n a[7]&#43;&#43;;\n else if(x&gt;69&amp;&amp;x&lt;&#61;79)\n a[8]&#43;&#43;;\n else if(x&gt;79&amp;&amp;x&lt;&#61;89)\n a[9]&#43;&#43;;\n else\n a[10]&#43;&#43;;\n }\n for(i&#61;1;i&lt;&#61;10;i&#43;&#43;){\n if(a[i]&gt;0){\n printf(&#34;%d-%d:%d\\n&#34;,i*10-10,i*10-1,a[i]);\n }\n }\n}\n</code></pre>\n\n<p><img alt=\"\" height=\"269\" src=\"https://img-ask.csdnimg.cn/upload/1623660087533.png\" width=\"642\" /></p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint i,n,a[11]= {0},x;\n\tscanf(\"%d\",&n);\n\tfor(i=0; i<n; i++)\n\t{\n\t\tscanf(\"%d\",&x);\n\t\tif(x<=9)\n\t\t\ta[1]++;\n\t\telse if(x>9&&x<=19)\n\t\t\ta[2]++;\n\t\telse if(x>19&&x<=29)\n\t\t\ta[3]++;\n\t\telse if(x>29&&x<=39)\n\t\t\ta[4]++;\n\t\telse if(x>39&&x<=49)\n\t\t\ta[5]++;\n\t\telse if(x>49&&x<=59)\n\t\t\ta[6]++;\n\t\telse if(x>59&&x<=69)\n\t\t\ta[7]++;\n\t\telse if(x>69&&x<=79)\n\t\t\ta[8]++;\n\t\telse if(x>79&&x<=89)\n\t\t\ta[9]++;\n\t\telse\n\t\t\ta[10]++;\n\t}\n\tfor(i=1;i<=10;i++){\n\t\tif(a[i]>0){\n\t\t\tprintf(\"%d-%d:%d\\n\",i*10-10,i*10-1,a[i]);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470245",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/7.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7420132,
"question_title": "动态规划-硬币重量最轻问题",
"question_content": "<p>设有n种不同面值的硬币&#xff0c;第i种硬币的币值是Vi(其中V1&#61;1),重量是Wi,i&#61;1,2&#xff0c;...n且现在购买某种总币值为y的商品&#xff0c;需要用这些硬币付款&#xff0c;如果每种钱币使用的个数不限&#xff0c;那么如何选择付款的方法使得付出钱币的总重量最轻&#xff1f;使用动态规划设计策略设计一个求解该问题的算法。假设问题的输入实例是&#xff1a;</p><p style=\"margin-left:.38in\">V1&#61;1&#xff0c; V2&#61;4&#xff0c; V3&#61;6&#xff0c; V4&#61;8</p><p style=\"margin-left:.38in\">W1&#61;1&#xff0c; W2&#61;2&#xff0c;W3&#61;4&#xff0c;W4&#61;6</p><p style=\"margin-left:.38in\">Y&#61;12</p><p>要求输出优化函数表和标记函数表、以及硬币支付方式。</p>\n",
"difficulty": "困难",
"answer_id": 53378192,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\nvoid strcpy(int *a, int *b, int Y){\n for(int i&#61;0;i&lt;&#61;Y;i&#43;&#43;) *(a&#43;i) &#61; *(b&#43;i);\n}\nvoid solve(){\n int n; scanf(&#34;%d&#34;,&amp;n);\n int type[n], weight[n], Y, i, j, k;\n for(i&#61;0;i&lt;n;i&#43;&#43;) scanf(&#34;%d&#34;,&amp;type[i]);\n for(i&#61;0;i&lt;n;i&#43;&#43;) scanf(&#34;%d&#34;,&amp;weight[i]);\n scanf(&#34;%d&#34;,&amp;Y);\n int Min[Y&#43;1], Min_Path[Y&#43;1], path[n][Y&#43;1];\n for(i&#61;0;i&lt;&#61;Y;i&#43;&#43;) Min[i] &#61; 9999;\n Min[0] &#61; 0;\n printf(&#34;\\n&#34;);\n for(j&#61;0;j&lt;n;j&#43;&#43;){\n for(i&#61;type[j]; i&lt;&#61;Y; i&#43;&#43;)\n if(Min[i] &gt; Min[i-type[j]]&#43;weight[j]){\n Min_Path[i] &#61; type[j];\n Min[i] &#61; Min[i-type[j]]&#43;weight[j];\n }\n for(k&#61;1;k&lt;&#61;Y;k&#43;&#43;) printf(&#34;%-3d&#34;,Min[k]);\n printf(&#34;\\n&#34;);\n strcpy(path[j],Min_Path,Y);\n }\n\n printf(&#34;\\n&#34;);\n for(i&#61;0;i&lt;n;i&#43;&#43;){\n for(j&#61;1;j&lt;&#61;Y;j&#43;&#43;)\n printf(&#34;%-3d&#34;,path[i][j]);\n printf(&#34;\\n&#34;);\n }\n\n int y&#61;Y;\n printf(&#34;\\n支付方式:&#34;);\n while (y){\n printf(&#34;%d &#34;,Min_Path[y]);\n y -&#61; Min_Path[y];\n }\n printf(&#34;\\n总重量:%d\\n&#34;,Min[Y]);\n}\nint main(){\n solve();\n return 1;\n}\n/*\n4\n1 4 6 8\n1 3 2 6\n12\n */</code></pre>\n\n<p style=\"text-align:center\"><img alt=\"\" height=\"492\" src=\"https://img-ask.csdnimg.cn/upload/1619149096414.png\" width=\"449\" /></p>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nvoid strcpy(int *a, int *b, int Y){\n\tfor(int i=0;i<=Y;i++) *(a+i) = *(b+i);\n}\nvoid solve(){\n\tint n; scanf(\"%d\",&n);\n\tint type[n], weight[n], Y, i, j, k;\n\tfor(i=0;i<n;i++) scanf(\"%d\",&type[i]);\n\tfor(i=0;i<n;i++) scanf(\"%d\",&weight[i]);\n\tscanf(\"%d\",&Y);\n\tint Min[Y+1], Min_Path[Y+1], path[n][Y+1];\n\tfor(i=0;i<=Y;i++) Min[i] = 9999;\n\tMin[0] = 0;\n\tprintf(\"\\n\");\n\tfor(j=0;j<n;j++){\n\t\tfor(i=type[j]; i<=Y; i++)\n\t\t\tif(Min[i] > Min[i-type[j]]+weight[j]){\n\t\t\t\tMin_Path[i] = type[j];\n\t\t\t\tMin[i] = Min[i-type[j]]+weight[j];\n\t\t\t}\n\t\tfor(k=1;k<=Y;k++) printf(\"%-3d\",Min[k]);\n\t\tprintf(\"\\n\");\n\t\tstrcpy(path[j],Min_Path,Y);\n\t}\n\tprintf(\"\\n\");\n\tfor(i=0;i<n;i++){\n\t\tfor(j=1;j<=Y;j++)\n\t\t\tprintf(\"%-3d\",path[i][j]);\n\t\tprintf(\"\\n\");\n\t}\n\tint y=Y;\n\tprintf(\"\\n支付方式:\");\n\twhile (y){\n\t\tprintf(\"%d \",Min_Path[y]);\n\t\ty -= Min_Path[y];\n\t}\n\tprintf(\"\\n总重量:%d\\n\",Min[Y]);\n}\nint main(){\n\tsolve();\n\treturn 1;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470163",
"status": 1,
"keywords": "算法中阶,动态规划,动态规划原理,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/70.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7457297,
"question_title": "找出小于平均值的数。",
"question_content": "<p>从键盘输入一个正整数存入变量n中&#xff0c;再输入n个整数&#xff0c;然后找出所有小于平均值的数&#xff0c;并按输入顺序输出。\n \n </p>",
"difficulty": "简单",
"answer_id": 53441440,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\n\nint main()\n{\n\tint i,n,x,sum&#61;0,a[100];\n\tfloat ave;\n\tscanf(&#34;%d&#34;,&amp;n);\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;){\n scanf(&#34;%d&#34;,&amp;a[i]);\n sum&#43;&#61;a[i];\n\t}\n\tave&#61;sum*1.0/n;\n\tfor(i&#61;0;i&lt;n;i&#43;&#43;){\n if(a[i]&lt;ave)\n printf(&#34;%d&#34;,a[i]);\n\t}\n\n\treturn 0;\n\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main()\n{\n\tint i,n,sum=0,a[100];\n\tfloat ave;\n\tscanf(\"%d\",&n);\n\tfor(i=0;i<n;i++){\n\t\tscanf(\"%d\",&a[i]);\n\t\tsum+=a[i];\n\t}\n\tave=sum*1.0/n;\n\tfor(i=0;i<n;i++){\n\t\tif(a[i]<ave)\n\t\t\tprintf(\"%d\",a[i]);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470164",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/71.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7460415,
"question_title": "编写一万年历系统(2021年)",
"question_content": "<p>要求&#xff1a;模仿现实生活中的挂历。\n当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几)。\n当系统日期变到下一月时,系统自动翻页到下一月。\n </p>",
"difficulty": "简单",
"answer_id": 53445491,
"answer_content": "<p>到底要C版本&#xff0c;还是C&#43;&#43;版本啊</p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n \nint year(int y)\n{\n\tif ((y%4&#61;&#61;0) &amp;&amp; (y%100!&#61;0) || y%400&#61;&#61;0)\n\t\treturn 366;\n\telse\n\t\treturn 365;\n}\n \nint main()\n{\n\tint y;\n\tint i,j,sum&#61;0;\t\n\tint begin,week;\n\tint days[12]&#61;{31,28,31,30,31,30,31,31,30,31,30,31};\n \n\tscanf(&#34;%d&#34;,&amp;y);\n\tfor(i&#61;1;i&lt;y;i&#43;&#43;)\n\t\tsum&#43;&#61;year(i);\n\t\n\tweek&#61;(sum&#43;1)%7;\t\t//表示该年1月1日为星期几\n\t\n\tif(year(y)&#61;&#61;366)\n\t\tdays[1]&#61;29;\t\n \n\tprintf(&#34;\\n%d年日历如下&#xff1a;\\n\\n&#34;,y);\n \n\tfor(i&#61;0;i&lt;12;i&#43;&#43;)\n\t{\n\t\tprintf(&#34; %d月 \\n&#34;,i&#43;1);\n\t\tprintf(&#34; 7 1 2 3 4 5 6\\n&#34;);\n\t\tprintf(&#34;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;\\n&#34;);\n\t\tbegin&#61;1;\n\t\tfor(j&#61;0;j&lt;week;j&#43;&#43;)\n\t\t\tprintf(&#34; &#34;);\n\t\twhile(begin&lt;&#61;days[i])\n\t\t{\n\t\t\tprintf(&#34;%3d&#34;,begin);\n\t\t\tbegin&#43;&#43;;\n\t\t\tweek&#61;(week&#43;1)%7;\n\t\t\tif(week%7&#61;&#61;0)\n\t\t\t\tprintf(&#34;\\n&#34;);\n\t\t}\n\t\tprintf(&#34;\\n\\n&#34;);\n \n\t}\n\t\n\t\n \n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint year(int y)\n{\n\tif ((y%4==0) && (y%100!=0) || y%400==0)\n\t\treturn 366;\n\telse\n\t\treturn 365;\n}\nint main()\n{\n\tint y;\n\tint i,j,sum=0;\t\n\tint begin,week;\n\tint days[12]={31,28,31,30,31,30,31,31,30,31,30,31};\n\tscanf(\"%d\",&y);\n\tfor(i=1;i<y;i++)\n\t\tsum+=year(i);\n\tweek=(sum+1)%7;\t\t\n\tif(year(y)==366)\n\t\tdays[1]=29;\t\n\tprintf(\"\\n%d年日历如下:\\n\\n\",y);\n\tfor(i=0;i<12;i++)\n\t{\n\t\tprintf(\"\t %d月\t\t \\n\",i+1);\n\t\tprintf(\" 7 1 2 3 4 5 6\\n\");\n\t\tprintf(\"=====================\\n\");\n\t\tbegin=1;\n\t\tfor(j=0;j<week;j++)\n\t\t\tprintf(\" \");\n\t\twhile(begin<=days[i])\n\t\t{\n\t\t\tprintf(\"%3d\",begin);\n\t\t\tbegin++;\n\t\t\tweek=(week+1)%7;\n\t\t\tif(week%7==0)\n\t\t\t\tprintf(\"\\n\");\n\t\t}\n\t\tprintf(\"\\n\\n\");\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469950",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,RSA公钥加密系统",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/72.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7455446,
"question_title": "优雅的字符串",
"question_content": "<p>对于一个字符串&#xff0c;如果这个字符串在ASCII码的意义上是有序的&#xff0c;即升序或降序&#xff0c;则称该字符串为“优雅的字符串”。一个长为n的字符串s&#xff0c;对于1&lt;&#61; i &lt;&#61; n - 1&#xff0c;如果总有s[i] &lt;&#61; s[i&#43;1]&#xff0c;则字符串为升序&#xff1b;如果总有s[i] &gt;&#61; s[i&#43;1]&#xff0c;则字符串降序。现给定一个字符串&#xff0c;请你判断该字符串是否“优雅”&#xff0c;如果该字符串是“优雅”的&#xff0c;则判断是“正优雅”&#xff08;升序&#xff09;还是“负优雅”&#xff08;降序&#xff09;。\n输入描述\n一个非空&#xff0c;不包含空格的字符串s&#xff0c;保证字符串中的字符都是ASCII标准中的字符。数据保证不会出现所有字符相同的字符串。&#xff08;2&lt;&#61; length(s) &lt;&#61;1e5&#xff09;\n输出描述\n如果字符串“正优雅”&#xff0c;则输出“Positive elegance”&#xff0c;如果“负优雅”&#xff0c;则输出“Negative elegance”&#xff0c;否则输出“Non elegance”&#xff08;均不含引号&#xff09;。\n样例输入 (*&#43;12356ASdfz\n样例输出\nPositive elegance</p>",
"difficulty": "困难",
"answer_id": 53438399,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main()\n{\n\tchar a[100] &#61; {0};\n\tint i;\n\tint zyy &#61; 1;\n\tint fyy &#61; 1;\n\tprintf(&#34;请输入字符串:&#34;);\n\tgets(a);\n\t//判断是否正优雅\n\tfor (i&#61;1;i&lt;strlen(a);i&#43;&#43;)\n\t{\n\t\tif(a[i] &lt; a[i-1])\n\t\t{\n\t\t\tzyy &#61; 0;\n\t\t\tbreak;\n\t\t}\n\t}\n\t//判断是否负优雅\n\tfor (i&#61;1;i&lt;strlen(a);i&#43;&#43;)\n\t{\n\t\tif(a[i] &gt; a[i-1])\n\t\t{\n\t\t\tfyy &#61; 0;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (zyy &amp;&amp; !fyy)\n\t{\n\t\tprintf(&#34;Positive elegance\\n&#34;);\n\t}else if (!zyy &amp;&amp; fyy)\n\t{\n\t\tprintf(&#34;Negative elegance\\n&#34;);\n\t}else\n\t\tprintf(&#34;Non elegance\\n&#34;);\n\treturn 0;\n\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\n#include <string.h>\nint main()\n{\n\tchar a[100] = {0};\n\tint i;\n\tint zyy = 1;\n\tint fyy = 1;\n\tprintf(\"请输入字符串:\");\n\tgets(a);\n\tfor (i=1;i<strlen(a);i++)\n\t{\n\t\tif(a[i] < a[i-1])\n\t\t{\n\t\t\tzyy = 0;\n\t\t\tbreak;\n\t\t}\n\t}\n\tfor (i=1;i<strlen(a);i++)\n\t{\n\t\tif(a[i] > a[i-1])\n\t\t{\n\t\t\tfyy = 0;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (zyy && !fyy)\n\t{\n\t\tprintf(\"Positive elegance\\n\");\n\t}else if (!zyy && fyy)\n\t{\n\t\tprintf(\"Negative elegance\\n\");\n\t}else\n\t\tprintf(\"Non elegance\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470165",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/73.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1057668,
"question_title": "计算sin(x)",
"question_content": "\n描述\n计算sin(x)=x-x^3/3!+x^5/5!-X^7/7!+......,直到最后一项的绝对值小于10-7时停止计算。其中-2Π<=x<=2Π,^表示次方,如x^3表示x的3次方。\n输入\n一个实数x,-2Π<=x<=2Π\n输出\nsin(x)的值\n输入样例 1 \n3.142\n输出样例 1\n-0.000407347",
"difficulty": "中等",
"answer_id": 1254771,
"answer_content": "\n```\n#include<stdio.h>\n#include<math.h>\n\ndouble sin(double);\ndouble nResult(double,double);\n\nint main()\n{\n double x=0;\n scanf(\"%lf\",&x);\n printf(\"sin(%lf)=%lf\\n\",x,sin(x));\n return 0;\n}\n\ndouble sin(double x)\n{\n //sin(x)=x-x^3/3!+x^5/5!-x^7/7!+……+(-1)(n^2n+1)/(2n+1)!+……\n int i=0;\n double result=0,n=0;\n while( fabs( n=nResult(x,2*++i-1) ) > 0e-7 )//绝对值大于10^-7次方就循环\n result+=(i%2==1)?n:-n;\n return result;\n}\n\ndouble nResult(double x,double n)\n{\n //(n^2n+1)/(2n+1)!也就是n/1*n/2*n/3*n/4*.....n/(2n+1)\n return n==1?x:nResult(x,n-1)*x/n;\n}\n```",
"tag_name": "c++",
"cpp": "#include<stdio.h>\n#include<math.h>\ndouble sin(double);\ndouble nResult(double,double);\nint main()\n{\n\tdouble x=0;\n\tscanf(\"%lf\",&x);\n\tprintf(\"sin(%lf)=%lf\\n\",x,sin(x));\n\treturn 0;\n}\ndouble sin(double x)\n{\n\tint i=0;\n\tdouble result=0,n=0;\n\twhile( fabs( n=nResult(x,2*++i-1) ) > 0e-7 )\n\t\tresult+=(i%2==1)?n:-n;\n\treturn result;\n}\ndouble nResult(double x,double n)\n{\n\treturn n==1?x:nResult(x,n-1)*x/n;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470166",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/74.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1059365,
"question_title": "不喜欢带钱的小C",
"question_content": "题目描述:\n小C不喜欢带钱,有一次竟被他碰上了一家不能使用移动支付(也不能找钱)的神秘商店。请问小C至少准备多少张RMB才能恰好支付n元。RMB的面额有100元,50元,20元,10元,5元,1元。\n输入格式:\n输入一个整数n\n输出格式:\n最少带几张。\n样例输入1:\n50\n样例输出1:\n1\n约定:\n1<=n<=100",
"difficulty": "中等",
"answer_id": 1256674,
"answer_content": "\n```\n#include <iostream>\n\nusing namespace std;\n\nint solve(int tar, int * meta, int metan, int * seed = NULL, int seedn = 0)\n{\n\tif (tar == 0)\n\t{\n\t\t//for (int i = 0; i < seedn; i++) cout << seed[i] << \" \";\n\t\t//cout << endl;\n\t\treturn seedn;\n\t}\n\tint min = -1;\n\tint m;\n\tint * seed1 = new int[seedn + 1];\n\tif (seed)\n\t\tmemcpy(seed1, seed, sizeof(int) * seedn);\n\tfor (int i = 0; i < metan; i++)\n\t{\n\t\tif (meta[i] <= tar)\n\t\t{\n\t\t\tseed1[seedn] = meta[i];\n\t\t\tm = solve(tar - meta[i], meta, metan, seed1, seedn + 1);\n\t\t\tif (m != -1 && (min == -1 || min > m))\n\t\t\t\tmin = m;\n\t\t\tbreak;\n\t\t}\n\t}\n\tdelete[] seed1;\n\treturn min;\n}\n\nint main()\n{\n\tint arr[] = { 100, 50, 20, 10, 5, 1 };\n\tint n = 6;\n\tint total;\n\tcin >> total;\n\tint result = solve(total, arr, n);\n\tcout << result << endl;\n\treturn 0;\n}\n\n```\n# 问题解决的话,请点下`采纳`",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nint solve(int tar, int * meta, int metan, int * seed = NULL, int seedn = 0)\n{\n\tif (tar == 0)\n\t{\n\t\treturn seedn;\n\t}\n\tint min = -1;\n\tint m;\n\tint * seed1 = new int[seedn + 1];\n\tif (seed)\n\t\tmemcpy(seed1, seed, sizeof(int) * seedn);\n\tfor (int i = 0; i < metan; i++)\n\t{\n\t\tif (meta[i] <= tar)\n\t\t{\n\t\t\tseed1[seedn] = meta[i];\n\t\t\tm = solve(tar - meta[i], meta, metan, seed1, seedn + 1);\n\t\t\tif (m != -1 && (min == -1 || min > m))\n\t\t\t\tmin = m;\n\t\t\tbreak;\n\t\t}\n\t}\n\tdelete[] seed1;\n\treturn min;\n}\nint main()\n{\n\tint arr[] = { 100, 50, 20, 10, 5, 1 };\n\tint n = 6;\n\tint total;\n\tcin >> total;\n\tint result = solve(total, arr, n);\n\tcout << result << endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469951",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/75.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 995800,
"question_title": "Sine的奇怪朋友",
"question_content": "题目描述\nSine 的一个朋友疯了,最近他老爱说反话,别人说什么他就倒着说一遍,真是个奇怪的人。那他到底说了什么呢?\n(他说的话不超过 1000 个字符。)\n输入描述\n输入一个正整数 n,表示说话的次数。下面 n 行表示说的话\n输出描述\n占 n 行,每行表示对应输入的反话\n样例输入\n2\ni am speaking\nACMer\n样例输出\ngnikaeps ma i\n",
"difficulty": "中等",
"answer_id": 1169072,
"answer_content": "\n```\n#include<stdio.h>\n#include<string.h>\n#define N 1000\nint main()\n{\n int i,m,n;\n char st1[N];\n char st2[N];\n memset(st1,0x0,N);\n memset(st1,0x0,N);\n scanf(\"%d\",&m);\n getchar();\n while(m--)\n {\n gets(st1);\n i = strlen(st1);\n for (n=0;i>0;)\n st2[n++] = st1[--i];\n st2[n] = '\\0';\n printf(\"%s\\n\",st2);\n }\n return 0;\n}\n\n```",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<string.h>\n#define N 1000\nint main()\n{\n\tint i,m,n;\n\tchar st1[N];\n\tchar st2[N];\n\tmemset(st1,0x0,N);\n\tscanf(\"%d\",&m);\n\tgetchar();\n\twhile(m--)\n\t{\n\t\tgets(st1);\n\t\ti = strlen(st1);\n\t\tfor (n=0;i>0;)\n\t\t\tst2[n++] = st1[--i];\n\t\tst2[n] = '\\0';\n\t\tprintf(\"%s\\n\",st2);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470265",
"status": 0,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/76.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7456837,
"question_title": "按要求求质数",
"question_content": "求10-100之间个位数为7的质数",
"difficulty": "简单",
"answer_id": 53440905,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\nint isp(int n)\n{\n int i;\n if (n&lt;2)\n return 0;\n for (i&#61;2;i*i&lt;&#61;n;&#43;&#43;i)\n {\n if (n%i&#61;&#61;0)\n return 0;\n }\n return 1;\n}\nint main()\n{\n int i&#61;17;\n while (i&lt;&#61;100)\n {\n if (isp(i))\n printf(&#34;%d &#34;,i);\n i&#43;&#61;10; //只需要验证17,27,37,47,57,67,77,87,97这9个数\n }\n printf(&#34;\\n&#34;); \n return 0;\n}</code></pre>\n\n<p><img alt=\"\" height=\"135\" src=\"https://img-ask.csdnimg.cn/upload/1624363941556.png\" width=\"547\" /> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint isp(int n)\n{\n\tint i;\n\tif (n<2)\n\t\treturn 0;\n\tfor (i=2;i*i<=n;++i)\n\t{\n\t\tif (n%i==0)\n\t\t\treturn 0;\n\t}\n\treturn 1;\n}\nint main()\n{\n\tint i=17;\n\twhile (i<=100)\n\t{\n\t\tif (isp(i))\n\t\t\tprintf(\"%d \",i);\n\t\ti+=10; \n\t}\n\tprintf(\"\\n\"); \n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470167",
"status": 1,
"keywords": "质数,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/77.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1057453,
"question_title": "输入一个正整数n(代表图形的行数),输出如样例形式的图形",
"question_content": "输入:5\n输出:\n A\n ABA \n ABCBA \n ABCDCBA \n ABCDEDCBA",
"difficulty": "中等",
"answer_id": 1254581,
"answer_content": "\n```\n#include <stdio.h>\n#include <iostream>\n\nusing namespace std;\n\nint main() {\n\tint N;\n\tcin >> N;\n\tfor (int i = 0; i < N; i++)\n\t{\n\t\tfor (int j = 0; j < N - i; j++) printf(\" \");\n\t\tfor (int j = 0; j < i; j++) printf(\"%c\", (char)(j + 'A'));\n\t\tfor (int j = i; j >= 0; j--) printf(\"%c\", (char)(j + 'A'));\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}\n\n```\n# 问题解决的话,请点下采纳",
"tag_name": "c++",
"cpp": "#include <stdio.h>\n#include <iostream>\nusing namespace std;\nint main() {\n\tint N;\n\tcin >> N;\n\tfor (int i = 0; i < N; i++)\n\t{\n\t\tfor (int j = 0; j < N - i; j++) printf(\" \");\n\t\tfor (int j = 0; j < i; j++) printf(\"%c\", (char)(j + 'A'));\n\t\tfor (int j = i; j >= 0; j--) printf(\"%c\", (char)(j + 'A'));\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470266",
"status": 1,
"keywords": "数学运算,图形输出",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/78.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 227612,
"question_title": "用递归实现任意的正整数按反序输出",
"question_content": "编写一个递归函数,将任意的正整数按反序输出。例如,输入\"12345\"输出\"54321\"",
"difficulty": "简单",
"answer_id": 192049,
"answer_content": "\n\n```\n #include <iostream>\nusing namespace std;\n\nvoid revert(int n)\n{\n if ( n>= 0 && n<= 9 )\n {\n cout<<n;\n }\n else\n {\n cout<<n % 10;\n revert(n/10);\n } \n}\n\nint main()\n{\n int n = 12345 ;\n revert(n);\n}\n```\n另外给你个图你就明白用递归怎么实现了\n![图片说明](https://img-ask.csdn.net/upload/201512/20/1450618367_788393.png)\n",
"tag_name": "c++",
"cpp": " #include <iostream>\nusing namespace std;\nvoid revert(int n)\n{\n\tif ( n>= 0 && n<= 9 )\n\t{\n\t\tcout<<n;\n\t}\n\telse\n\t{\n\t\tcout<<n % 10;\n\t\trevert(n/10);\n\t} \n}\nint main()\n{\n\tint n = 12345 ;\n\trevert(n);\n}",
"topic_link": "https://bbs.csdn.net/topics/600469854",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编,确定任意一对线段是否相交",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/79.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 2046499,
"question_title": "输入一组含n个元素的数据,求这组数据连续上升元素个数的最大值,请问下面代码哪里写错了?",
"question_content": "输入一组含n个元素的数据,求这组数据连续上升元素个数的最大值,请问下面代码哪里写错了?",
"difficulty": "简单",
"answer_id": 11465862,
"answer_content": "<pre>\n<code>#include &lt;stdio.h&gt;\nint main(){\n int n;\n while (scanf(&#34;%d&#34;,&amp;n)!&#61;EOF) {\n int t&#61;0,sum&#61;0,a[1000];\n for(int i&#61;0; i&lt;n; i&#43;&#43;) {\n scanf(&#34;%d&#34;,&amp;a[i]);\n }\n for(int i&#61;1; i&lt;n; i&#43;&#43;) {\n if(a[i-1]&lt;a[i]) {\n t&#43;&#43;;\n if(sum&lt;t)\n sum&#61;t; //将最大的t赋值给sum \n } else\n t&#61;0;\n }\n if(sum&#61;&#61;0) {\n sum&#61;0;\n } else sum&#61;sum&#43;1;\n printf(&#34;%d\\n&#34;,sum);\n }\n return 0;\n}</code></pre>\n\n<p> </p>",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main(){\n\tint n;\n\twhile (scanf(\"%d\",&n)!=EOF) {\n\t\tint t=0,sum=0,a[1000];\n\t\tfor(int i=0; i<n; i++) {\n\t\t\tscanf(\"%d\",&a[i]);\n\t\t}\n\t\tfor(int i=1; i<n; i++) {\n\t\t\tif(a[i-1]<a[i]) {\n\t\t\t\tt++;\n\t\t\t\tif(sum<t)\n\t\t\t\t\tsum=t;\t \n\t\t\t} else\n\t\t\t\tt=0;\n\t\t}\n\t\tif(sum==0) {\n\t\t\tsum=0;\n\t\t} else sum=sum+1;\n\t\tprintf(\"%d\\n\",sum);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470246",
"status": 0,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/8.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 771514,
"question_title": "目标值与数组所有元素去比对,找出最接近的元素,输出下标",
"question_content": "举例如下:一个数组{915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227};目标值假设是1000,最接近元素为992,下标为4",
"difficulty": "中等",
"answer_id": 834113,
"answer_content": "如果不是有序的,那么比较绝对值,找到最小的。\n```\n#include <stdio.h>\n\nint main()\n{\n\tint min = (1 << 31) - 1;\n\tint idx = 0;\n\tint arr[] = {915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227};\n\tint n = 1000;\n\tfor (int i = 0; i < sizeof(arr) / sizeof(int); i++)\n\t{\n\t\tint diff = arr[i] - n;\n\t\tif (diff < 0) diff = -diff;\n\t\tif (diff < min)\n\t\t{\n\t\t\tmin = diff;\n\t\t\tidx = i;\n\t\t}\n\t}\n\tprintf(\"最接近的是%d 下标是%d\", arr[idx], idx);\n \treturn 0;\n}\n```\n最接近的是992 下标是4\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint min = (1 << 31) - 1;\n\tint idx = 0;\n\tint arr[] = {915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227};\n\tint n = 1000;\n\tfor (int i = 0; i < sizeof(arr) / sizeof(int); i++)\n\t{\n\t\tint diff = arr[i] - n;\n\t\tif (diff < 0) diff = -diff;\n\t\tif (diff < min)\n\t\t{\n\t\t\tmin = diff;\n\t\t\tidx = i;\n\t\t}\n\t}\n\tprintf(\"最接近的是%d 下标是%d\", arr[idx], idx);\n \treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470168",
"status": 1,
"keywords": "算法高阶,数论算法,元素的幂,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/80.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7433864,
"question_title": "移动数组中的字符",
"question_content": "在数组中有n个字符,使前面各字符顺序向后移动m个位置,并使最后m个字符变成最前面的 m 个字符",
"difficulty": "中等",
"answer_id": 53404201,
"answer_content": "<p>代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;iostream&gt;\nusing namespace std;\nvoid funShift(int m,char *p,int n)\n{\n\tchar c;\n\tfor (int j &#61; 0; j &lt; m;j&#43;&#43;)\n\t{\n\t\tc &#61; p[n-1];\n\t\tfor (int i &#61; n-1; i &gt; 0; i--)\n\t\t{\n\t\t\tp[i] &#61; p[i-1];\n\t\t}\n\t\tp[0] &#61; c;\n\t}\n\t\n}\nint main() \n{ \n\tint i,m,n; \n\tcin &gt;&gt; m &gt;&gt; n; \n\tchar *p &#61;new char[n&#43;1];\n\tp[n] &#61; 0;\n\tfor(i &#61; 0; i &lt; n; &#43;&#43;i) \n\t\tcin &gt;&gt; p[i]; \n\tfunShift(m,p,n); \n\tfor(i &#61; 0; i &lt; n; &#43;&#43;i) \n\t\tcout &lt;&lt; p[i] &lt;&lt; &#39; &#39;; \n\tcout &lt;&lt; endl; \n\tdelete [] p; \n\n\tgetchar();\n\tgetchar();\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <iostream>\nusing namespace std;\nvoid funShift(int m,char *p,int n)\n{\n\tchar c;\n\tfor (int j = 0; j < m;j++)\n\t{\n\t\tc = p[n-1];\n\t\tfor (int i = n-1; i > 0; i--)\n\t\t{\n\t\t\tp[i] = p[i-1];\n\t\t}\n\t\tp[0] = c;\n\t}\n}\nint main() \n{ \n\tint i,m,n; \n\tcin >> m >> n; \n\tchar *p =new char[n+1];\n\tp[n] = 0;\n\tfor(i = 0; i < n; ++i) \n\t\tcin >> p[i]; \n\tfunShift(m,p,n); \n\tfor(i = 0; i < n; ++i) \n\t\tcout << p[i] << ' '; \n\tcout << endl; \n\tdelete [] p; \n\tgetchar();\n\tgetchar();\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470169",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/81.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7434242,
"question_title": "计算出现次数最多的整数及其出现次数",
"question_content": "<p>【问题描述】\n输入一组无序的整数&#xff0c;编程输出其中出现次数最多的整数及其出现次数。\n【输入形式】\n先从标准输入读入整数的个数&#xff08;大于等于1&#xff0c;小于等于100&#xff09;&#xff0c;然后在下一行输入这些整数&#xff0c;各整数之间以一个空格分隔。\n【输出形式】\n在标准输出上输出出现次数最多的整数及其出现次数&#xff0c;两者以一个空格分隔&#xff1b;若出现次数最多的整数有多个&#xff0c;则按照整数升序分行输出。\n【样例输入】\n10\n0 -50 0 632 5813 -50 9 -50 0 632\n【样例输出】\n-50 3\n0 3\n【样例说明】\n输入了10个整数&#xff0c;其中出现次数最多的是-50和0&#xff0c;都是出现3次。</p>",
"difficulty": "困难",
"answer_id": 53404633,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n\nint main()\n\n{\n\n int a[50],b[50],c[50],n,i,j,t,max;\n\n scanf(&#34;%d&#34;,&amp;n);\n\n for(i&#61;0;i&lt;n;i&#43;&#43;)\n\n {\n\n scanf(&#34;%d&#34;,&amp;a[i]);\n\n }\n\n for(i&#61;1;i&lt;n;i&#43;&#43;)\n\n for(j&#61;0;j&lt;n-1;j&#43;&#43;)\n\n {\n\n if(a[j]&gt;a[j&#43;1])\n\n {\n\n t&#61;a[j];\n\n a[j]&#61;a[j&#43;1];\n\n a[j&#43;1]&#61;t;\n\n }\n\n }\n\n j&#61;0;\n\n t&#61;-1;\n\n for(i&#61;0;i&lt;n-1;i&#43;&#43;)\n\n {\n\n if(a[i]!&#61;a[i&#43;1])\n\n {\n\n b[j]&#61;i-t;\n\n c[j]&#61;i;\n\n t&#61;i;\n\n j&#43;&#43;;\n\n }\n\n }\n\n b[j]&#61;n-1-t;\n\n c[j]&#61;n-1;\n\n max&#61;b[0];\n\n for(i&#61;1;i&lt;&#61;j;i&#43;&#43;)\n\n {\n\n if(max&lt;b[i])\n\n {\n\n max&#61;b[i];\n\n }\n\n }\n\n for(i&#61;0;i&lt;&#61;j;i&#43;&#43;)\n\n if(b[i]&#61;&#61;max)\n\n {\n\n t&#61;c[i];\n\n printf(&#34;%d %d\\n&#34;,a[t],b[i]);\n\n }\n\n return 0;\n\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint a[50],b[50],c[50],n,i,j,t,max;\n\tscanf(\"%d\",&n);\n\tfor(i=0;i<n;i++)\n\t {\n\t\tscanf(\"%d\",&a[i]);\n\t }\n\tfor(i=1;i<n;i++)\n\t\tfor(j=0;j<n-1;j++)\n\t\t{\n\t\t if(a[j]>a[j+1])\n\t\t {\n\t\t\t t=a[j];\n\t\t\t a[j]=a[j+1];\n\t\t\t a[j+1]=t;\n\t\t }\n\t\t}\n\t\tj=0;\n\t\tt=-1;\n\tfor(i=0;i<n-1;i++)\n\t\t{\n\t\t\tif(a[i]!=a[i+1])\n\t\t\t{\n\t\t\t\tb[j]=i-t;\n\t\t\t\tc[j]=i;\n\t\t\t\tt=i;\n\t\t\t\tj++;\n\t\t\t}\n\t\t}\n\t\t b[j]=n-1-t;\n\t\t c[j]=n-1;\n\t\tmax=b[0];\n\tfor(i=1;i<=j;i++)\n\t{\n\t\tif(max<b[i])\n\t\t\t{\n\t\t\t\tmax=b[i];\n\t\t\t}\n\t}\n\tfor(i=0;i<=j;i++)\n\t\tif(b[i]==max)\n\t{\n\t\tt=c[i];\n\t\tprintf(\"%d %d\\n\",a[t],b[i]);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470267",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/82.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 756103,
"question_title": "求分数数列的前N项和",
"question_content": "有一分数序列:2/1,-3/2,5/3,-8/5,13/8,-21/13,…, 由用户输入项目数N,求这个数列的前N 项之和",
"difficulty": "简单",
"answer_id": 811223,
"answer_content": "判断for循环的迭代变量即可。当迭代变量为偶数的时候,乘上一个-1。\n\n```\n#include<stdlib.h> \n#include<stdio.h>\nint main(){\n\tint n;\n\tscanf(\"%d\",&n);\n\t\n\tint i;\n\tdouble a1 = 2, b1 = 1;\n\tdouble a2 = 3, b2 = 2;\n\tdouble sum = a1/b1 - a2/b2;\n\t\n\tif(n==1) printf(\"%f\\n\",a1/b1);\n\telse if (n==2) printf(\"%f\\n\",sum);\n\telse{\n\t\tfor(i = 0;i<n-2;i++){\n\t\t\tdouble exp = a2 / b2;\n\t\t\tif(i%2==0) exp *= -1;\n\t\t\tsum += exp;\n\t\t\tdouble a = a1 + a2;\n\t\t\tdouble b = b1 + b2;\n\t\t\ta1 = a2;b1 = b2;\n\t\t\ta2 = a; b2 = b;\n\t\t}\n\t\tprintf(\"%f\\n\",sum);\n\t}\n\treturn 0;\n}\n\n```",
"tag_name": "c语言",
"cpp": "#include<stdlib.h> \n#include<stdio.h>\nint main(){\n\tint n;\n\tscanf(\"%d\",&n);\n\tint i;\n\tdouble a1 = 2, b1 = 1;\n\tdouble a2 = 3, b2 = 2;\n\tdouble sum = a1/b1 - a2/b2;\n\tif(n==1) printf(\"%f\\n\",a1/b1);\n\telse if (n==2) printf(\"%f\\n\",sum);\n\telse{\n\t\tfor(i = 0;i<n-2;i++){\n\t\t\tdouble exp = a2 / b2;\n\t\t\tif(i%2==0) exp *= -1;\n\t\t\tsum += exp;\n\t\t\tdouble a = a1 + a2;\n\t\t\tdouble b = b1 + b2;\n\t\t\ta1 = a2;b1 = b2;\n\t\t\ta2 = a; b2 = b;\n\t\t}\n\t\tprintf(\"%f\\n\",sum);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470268",
"status": 1,
"keywords": "算法初阶,基础知识,特征序列,概率分析和随机算法,概率分析和指示器随机变量的进一步使用",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/83.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1104214,
"question_title": "按要求排序数组",
"question_content": "<p>&nbsp;给你一个整数数组 arr 。请你将数组中的元素按照其二进制表示中,数字 1 的数目升序排序。 如果存在多个数字二进制中 1 的数目相同,则必须将它们按照数值大小升序排列。请你返回排序后的数组。</p>",
"difficulty": "简单",
"answer_id": 1318491,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include&lt;stdio.h&gt;\n#include&lt;stdlib.h&gt;\nint oneNum(int x)\n{\n int cnt = 0;\n while(x)\n {\n cnt++;\n x = x&amp;(x-1);\n }\n return cnt;\n}\n\nint cmp(const void *a,const void*b)\n{\n int al,bl;\n int ret;\n al = *(int*)a;\n bl = *(int*)b;\n ret = oneNum(al) - oneNum(bl);\n return ret ? ret : al - bl;\n}\n\nint main()\n{\n int s[]={1,2,3,5,6,7,8},i;\n int len = sizeof(s)/sizeof(*s);\n qsort(s,len,4,cmp);\n for(i = 0 ; i &lt; len ; i++)\n {\n printf(\"%d\\n\",s[i]);\n }\n}</code></pre>\n\n<p>&nbsp;</p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\n#include<stdlib.h>\nint oneNum(int x)\n{\n\tint cnt = 0;\n\twhile(x)\n\t{\n\t\tcnt++;\n\t\tx = x&(x-1);\n\t}\n\treturn cnt;\n}\nint cmp(const void *a,const void*b)\n{\n\tint al,bl;\n\tint ret;\n\tal = *(int*)a;\n\tbl = *(int*)b;\n\tret = oneNum(al) - oneNum(bl);\n\treturn ret ? ret : al - bl;\n}\nint main()\n{\n\tint s[]={1,2,3,5,6,7,8},i;\n\tint len = sizeof(s)/sizeof(*s);\n\tqsort(s,len,4,cmp);\n\tfor(i = 0 ; i < len ; i++)\n\t{\n\t\tprintf(\"%d\\n\",s[i]);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469952",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/84.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7446208,
"question_title": "求数列第n项值",
"question_content": "<p>求数列第n项值&#xff1a;1,2,3,6,11,20,37,68,125,230,.....例如:第7项为37&#xff0c;第9项为125。</p>",
"difficulty": "简单",
"answer_id": 53425424,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n\nint main(void) { \n int n;\n printf(&#34;请输入n的值&#xff1a;&#34;);\n scanf(&#34;%d&#34;,&amp;n);\n if(n&#61;&#61;1){\n printf(&#34;第1项为1\\n&#34;);\n }else if(n&#61;&#61;2){\n printf(&#34;第2项为2\\n&#34;);\n }else if(n&#61;&#61;3){\n printf(&#34;第3项为3\\n&#34;);\n }else{\n int f1&#61;1,f2&#61;2,f3&#61;3;\n int i,fn;\n for(i&#61;4;i&lt;&#61;n;i&#43;&#43;){\n fn&#61;f1&#43;f2&#43;f3;\n f1&#61;f2;\n f2&#61;f3;\n f3&#61;fn;\n }\n printf(&#34;第%d项为%d\\n&#34;,n,fn);\n }\n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main(void) { \n\tint n;\n\tprintf(\"请输入n的值:\");\n\tscanf(\"%d\",&n);\n\tif(n==1){\n\t\tprintf(\"第1项为1\\n\");\n\t}else if(n==2){\n\t\tprintf(\"第2项为2\\n\");\n\t}else if(n==3){\n\t\tprintf(\"第3项为3\\n\");\n\t}else{\n\t\tint f1=1,f2=2,f3=3;\n\t\tint i,fn;\n\t\tfor(i=4;i<=n;i++){\n\t\t\tfn=f1+f2+f3;\n\t\t\tf1=f2;\n\t\t\tf2=f3;\n\t\t\tf3=fn;\n\t\t}\n\t\tprintf(\"第%d项为%d\\n\",n,fn);\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469855",
"status": 1,
"keywords": "数列,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/85.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 237871,
"question_title": "从指定位置插入字符串",
"question_content": "编写程序,输入字符串S1和S2以及插入位置n,在字符串S1中的指定位置n处插入字符串S2。例如,输入“jiangsu”、“123”和位置3,则输出“ji123angsu”。",
"difficulty": "简单",
"answer_id": 219299,
"answer_content": "\n\n```\n #include <iostream>\n#include <string>\nusing namespace std;\n\nint main()\n{\n\tstring str1;\n\tstring str2;\n\tint pos;\n\tdo\n\t{\n\t\tif ((cin >> str1 >> str2 >> pos) && (pos >= 1))\n\t\t{\n\t\t\tstr1.insert(pos - 1, str2);\n\t\t\tcout << str1 << endl;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout << \"Invalid Input\" << endl;\n\t\t\tbreak;\n\t\t}\n\t\t\n\t} while (false);\n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint main()\n{\n\tstring str1;\n\tstring str2;\n\tint pos;\n\tdo\n\t{\n\t\tif ((cin >> str1 >> str2 >> pos) && (pos >= 1))\n\t\t{\n\t\t\tstr1.insert(pos - 1, str2);\n\t\t\tcout << str1 << endl;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout << \"Invalid Input\" << endl;\n\t\t\tbreak;\n\t\t}\n\t} while (false);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469953",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/86.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 237824,
"question_title": "统计某一单科成绩各分数段的分布人数",
"question_content": "设某班有若干人,写一程序统计某一单科成绩各分数段的分布人数,每人的成绩随机输入,输入负数表示输入结束。要求按下面的格式输出统计结果(“**”表示实际分布人数)\n0~39\t**\n40~49\t**\n50~59\t**\n……\n90~100\t**",
"difficulty": "简单",
"answer_id": 218762,
"answer_content": "这是上课题目?\n```\n #include <string>\n#include <iostream>\nusing namespace std;\nint main()\n{\n int result[12] = {0};\n int gold;\n while (cin>>gold) {\n if (gold < 0) {\n break;\n }\n \n int code = gold / 10;\n if (code < 4) {\n result[3] ++;\n }\n else if(code == 10) {\n result[9] ++;\n }\n else {\n result[code] ++;\n }\n }\n \n string word[] = {\"0~39\",\n \"40~49\",\n \"50~59\",\n \"60~69\",\n \"70~79\",\n \"80~89\",\n \"90~100\"};\n \n for (int i=0; i<7; i++) {\n cout<<word[i]<<\" \"<<result[i+3]<<endl;\n }\n \n return 0;\n}\n```\n这样写应该还好理解\n",
"tag_name": "c++",
"cpp": "#include <string>\n#include <iostream>\nusing namespace std;\nint main()\n{\n\tint result[12] = {0};\n\tint gold;\n\twhile (cin>>gold) {\n\t\tif (gold < 0) {\n\t\t\tbreak;\n\t\t}\n\t\tint code = gold / 10;\n\t\tif (code < 4) {\n\t\t\tresult[3] ++;\n\t\t}\n\t\telse if(code == 10) {\n\t\t\tresult[9] ++;\n\t\t}\n\t\telse {\n\t\t\tresult[code] ++;\n\t\t}\n\t}\n\tstring word[] = {\"0~39\",\n\t\"40~49\",\n\t\"50~59\",\n\t\"60~69\",\n\t\"70~79\",\n\t\"80~89\",\n\t\"90~100\"};\n\tfor (int i=0; i<7; i++) {\n\t\tcout<<word[i]<<\" \"<<result[i+3]<<endl;\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470170",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/87.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7447975,
"question_title": "体操打分求最高,最低,平均,中位",
"question_content": "<p>体操比赛中&#xff0c;10名评委为5名选手打分&#xff0c;其分数已经存放在一个二维数组中&#xff0c;列出每个选手的&#xff1a;最高分&#xff0c;最低分&#xff0c;平均分&#xff0c;中位分。<br />\n平均分算法&#xff1a;除去最高分和最低分后&#xff0c;剩下8位评委的分数平均值。<br />\n中位分&#xff1a;10个评分从高到低排列&#xff0c;第5位和第6位的平均分为中位分。</p>",
"difficulty": "简单",
"answer_id": 53427952,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n \nint main()\n{\n float a[1][10],max,min,sum;\n int i,aver;\n max&#61;0;\n min&#61;100;\n sum&#61;0;\n for(i&#61;0;i&lt;10;i&#43;&#43;)\n {\n scanf(&#34;%f&#34;,&amp;a[0][i]);\n if(a[0][i]&gt;max) \n max&#61;a[0][i];\n if(a[0][i]&lt;min)\n min&#61;a[0][i]; \n sum&#43;&#61;a[0][i];\n }\n aver &#61; (sum-max-min)/8 ;\n printf(&#34;aver:%d\\n&#34;,aver);\n return 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <stdio.h>\nint main()\n{\n\tfloat a[1][10],max,min,sum;\n\tint i,aver;\n\tmax=0;\n\tmin=100;\n\tsum=0;\n\tfor(i=0;i<10;i++)\n\t{\n\t\tscanf(\"%f\",&a[0][i]);\n\t\tif(a[0][i]>max) \n\t\t\tmax=a[0][i];\n\t\tif(a[0][i]<min)\n\t\t\tmin=a[0][i]; \n\t\tsum+=a[0][i];\n\t}\n\taver = (sum-max-min)/8 ;\n\tprintf(\"aver:%d\\n\",aver);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470269",
"status": 0,
"keywords": "算法初阶,基础知识,算法基础,设计算法,分析分治算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/88.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7442662,
"question_title": "用递归求第n项的值",
"question_content": "1,2,4,2,3,6,12,6,3,....求第n项值",
"difficulty": "简单",
"answer_id": 53419757,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n\nint fun(int n,int *x,int *y)\n{\n\tint sum &#61; 0,i;\n\tint size &#61; 0;\n\tint dd &#61; 1;\n\tfor (i &#61; 1; i &lt;&#61; (*x);i&#43;&#43;)\n\t{\n\t\tsum &#43;&#61; (2*i-1);\n\t}\n\tif (sum &#61;&#61; n)\n\t{\n\t\t*y &#61; 2*(*x) -1;\n\t\treturn (*x);\n\t}else if (sum &gt; n)\n\t{\n\t\t(*y) &#61; n - (sum - (2 * (*x) -1));\n\t\tsize &#61; 2* (*x) -1;\n\t\tdd &#61; (*x); //第x行的第一个数\n\t\tfor (i &#61; 2; i &lt;&#61; (*y);i&#43;&#43;)\n\t\t{\n\t\t\tif(i &lt;&#61; (*x))\n\t\t\t\tdd *&#61; 2;\n\t\t\telse\n\t\t\t\tdd /&#61; 2;\n\t\t}\n\t\treturn dd;\n\t}else\n\t{\n\t\t(*x)&#43;&#43;;\n\t\treturn fun(n,x,y);\n\t}\n}\n\nint main()\n{\n\tint n; //n项(从第1项开始&#xff09;\n\tint row &#61; 1,col &#61; 0;\n\tint val;\n\n\trow &#61; 1;\n\tcol &#61; 0;\n\tprintf(&#34;请输入n&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;n);\n\tval &#61; fun(n,&amp;row,&amp;col);\n\tprintf(&#34;第%d项是&#xff1a;%d\\n&#34;,n,val);\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint fun(int n,int *x,int *y)\n{\n\tint sum = 0,i;\n\tint size = 0;\n\tint dd = 1;\n\tfor (i = 1; i <= (*x);i++)\n\t{\n\t\tsum += (2*i-1);\n\t}\n\tif (sum == n)\n\t{\n\t\t*y = 2*(*x) -1;\n\t\treturn (*x);\n\t}else if (sum > n)\n\t{\n\t\t(*y) = n - (sum - (2 * (*x) -1));\n\t\tsize = 2* (*x) -1;\n\t\tdd = (*x); \n\t\tfor (i = 2; i <= (*y);i++)\n\t\t{\n\t\t\tif(i <= (*x))\n\t\t\t\tdd *= 2;\n\t\t\telse\n\t\t\t\tdd /= 2;\n\t\t}\n\t\treturn dd;\n\t}else\n\t{\n\t\t(*x)++;\n\t\treturn fun(n,x,y);\n\t}\n}\nint main()\n{\n\tint n;\t\t\n\tint row = 1,col = 0;\n\tint val;\n\trow = 1;\n\tcol = 0;\n\tprintf(\"请输入n:\");\n\tscanf(\"%d\",&n);\n\tval = fun(n,&row,&col);\n\tprintf(\"第%d项是:%d\\n\",n,val);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470270",
"status": 1,
"keywords": "数列,递归,数学运算",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/89.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7452224,
"question_title": "设计一个“石头剪刀布”游戏程序。",
"question_content": "<p>设计一个“石头剪刀布”游戏程序。用户和程序分别扮演猜拳双方&#xff0c;用户选择</p><p>石头、剪刀和布中的一项&#xff0c;程序随机选择另一项&#xff0c;与用户选择作比较&#xff0c;在界面中</p><p>显示最终的胜负判定。</p>",
"difficulty": "简单",
"answer_id": 53434132,
"answer_content": "<p>代码如下&#xff1a;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n\nint main()\n{\n\tint id;\n\tint a[3]&#61;{1,2,3};\n\tint au &#61; 0;\n\twhile(1)\n\t{\n\t\tprintf(&#34;1.剪刀,2.石头,3.布,0.退出\\n&#34;);\n\t\tscanf(&#34;%d&#34;,&amp;id);\n\t\tif(id &#61;&#61; 0)\n\t\t\tbreak;\n\t\tau &#61; a[rand()%3]; //机器\n\t\tif (au &#61;&#61; 1)\n\t\t{\n\t\t\tprintf(&#34;机器&#xff1a;剪刀\\t&#34;);\n\t\t\tif(id &#61;&#61; 1)\n\t\t\t\tprintf(&#34;玩家:剪刀\\t平局\\n&#34;);\n\t\t\telse if(id &#61;&#61; 2)\n\t\t\t\tprintf(&#34;玩家:石头\\t玩家赢\\n&#34;);\n\t\t\telse\n\t\t\t\tprintf(&#34;玩家:布\\t机器赢\\n&#34;);\n\t\t}\n\t\telse if(au &#61;&#61; 2)\n\t\t{\n\t\t\tprintf(&#34;机器&#xff1a;石头\\t&#34;);\n\t\t\tif(id &#61;&#61; 1)\n\t\t\t\tprintf(&#34;玩家:剪刀\\t机器赢\\n&#34;);\n\t\t\telse if(id &#61;&#61; 2)\n\t\t\t\tprintf(&#34;玩家:石头\\t平局\\n&#34;);\n\t\t\telse\n\t\t\t\tprintf(&#34;玩家:布\\t玩家赢\\n&#34;);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprintf(&#34;机器&#xff1a;布\\t&#34;);\n\t\t\tif(id &#61;&#61; 1)\n\t\t\t\tprintf(&#34;玩家:剪刀\\t玩家赢\\n&#34;);\n\t\t\telse if(id &#61;&#61; 2)\n\t\t\t\tprintf(&#34;玩家:石头\\t机器赢\\n&#34;);\n\t\t\telse\n\t\t\t\tprintf(&#34;玩家:布\\t平局\\n&#34;);\n\t\t}\n\t}\n\t\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c语言",
"cpp": "#include <stdlib.h>\n#include <stdio.h>\nint main()\n{\n\tint id;\n\tint a[3]={1,2,3};\n\tint au = 0;\n\twhile(1)\n\t{\n\t\tprintf(\"1.剪刀,2.石头,3.布,0.退出\\n\");\n\t\tscanf(\"%d\",&id);\n\t\tif(id == 0)\n\t\t\tbreak;\n\t\tau = a[rand()%3]; \n\t\tif (au == 1)\n\t\t{\n\t\t\tprintf(\"机器:剪刀\\t\");\n\t\t\tif(id == 1)\n\t\t\t\tprintf(\"玩家:剪刀\\t平局\\n\");\n\t\t\telse if(id == 2)\n\t\t\t\tprintf(\"玩家:石头\\t玩家赢\\n\");\n\t\t\telse\n\t\t\t\tprintf(\"玩家:布\\t机器赢\\n\");\n\t\t}\n\t\telse if(au == 2)\n\t\t{\n\t\t\tprintf(\"机器:石头\\t\");\n\t\t\tif(id == 1)\n\t\t\t\tprintf(\"玩家:剪刀\\t机器赢\\n\");\n\t\t\telse if(id == 2)\n\t\t\t\tprintf(\"玩家:石头\\t平局\\n\");\n\t\t\telse\n\t\t\t\tprintf(\"玩家:布\\t玩家赢\\n\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprintf(\"机器:布\\t\");\n\t\t\tif(id == 1)\n\t\t\t\tprintf(\"玩家:剪刀\\t玩家赢\\n\");\n\t\t\telse if(id == 2)\n\t\t\t\tprintf(\"玩家:石头\\t机器赢\\n\");\n\t\t\telse\n\t\t\t\tprintf(\"玩家:布\\t平局\\n\");\n\t\t}\n\t}\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470247",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/9.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7450354,
"question_title": "字符串匹配",
"question_content": "<p>输入两个字符串s1和s2&#xff0c;在s1中查找s2对应的字符串是否存在&#xff0c;若存在则输出它第一次出现的位置&#xff1b;若不存在&#xff0c;则输出“没有找到该字符串”。</p>",
"difficulty": "简单",
"answer_id": 53431020,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;string&gt;\n#include &lt;iostream&gt;\n\nusing namespace std;\n\nint main()\n{\n\tstring s1, s2;\n\tcout &lt;&lt; &#34;请输入第一个字符串&#xff1a;&#34;;\n\tcin &gt;&gt; s1;\n\tcout &lt;&lt; &#34;请输入第二个字符串&#xff1a;&#34;;\n\tcin &gt;&gt; s2;\n\tif (s1.find(s2) !&#61; -1)\n\t\tcout &lt;&lt; s1.find(s2)&lt;&lt;endl;\n\telse\n\t\tcout &lt;&lt; &#34;没有找到该字符串&#34;&lt;&lt;endl;\n\tsystem(&#34;pause&#34;);\n return 0;\n}</code></pre>\n\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623807958048.png\" /></p>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <string>\n#include <iostream>\nusing namespace std;\nint main()\n{\n\tstring s1, s2;\n\tcout << \"请输入第一个字符串:\";\n\tcin >> s1;\n\tcout << \"请输入第二个字符串:\";\n\tcin >> s2;\n\tif (s1.find(s2) != -1)\n\t\tcout << s1.find(s2)<<endl;\n\telse\n\t\tcout << \"没有找到该字符串\"<<endl;\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470271",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/90.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7449614,
"question_title": "计算函数的值",
"question_content": "<p>编程输入实数x&#xff0c;计算下面函数的值&#xff0c;并输出y的值&#xff0c;并输出y的值&#xff1b;\nx<sup>2</sup>         x&lt;1\n3x-1      1≦x≦10\nx/5        x&gt;10</p>",
"difficulty": "简单",
"answer_id": 53429821,
"answer_content": "<p><u><strong>代码如下&#xff0c;请采纳&#xff0c;谢谢&#xff0c;不明白地方随时交流</strong></u></p>\n\n<pre>\n<code># include&lt;stdio.h&gt;\n# include&lt;stdlib.h&gt;\n\nint main(void)\n{\n\tfloat x,y;\n\n\tprintf(&#34;请输入x的值&#xff1a;\\n&#34;);\n\tscanf(&#34;%f&#34;,&amp;x);\n\n\tif(x&lt;1)\n\t{\n\t\ty &#61; x * x;\n\t}\n\telse if(x&lt;&#61;10)\n\t{\n\t\ty&#61;3*x-1;\n\t}\n\telse\n\t{\n\t\ty&#61; x / 5;\n\t}\n\n\tprintf(&#34;y的值为&#xff1a;%f\\n&#34;,y);\n\n\tsystem(&#34;pause&#34;);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "# include<stdio.h>\n# include<stdlib.h>\nint main(void)\n{\n\tfloat x,y;\n\tprintf(\"请输入x的值:\\n\");\n\tscanf(\"%f\",&x);\n\tif(x<1)\n\t{\n\t\ty = x * x;\n\t}\n\telse if(x<=10)\n\t{\n\t\ty=3*x-1;\n\t}\n\telse\n\t{\n\t\ty= x / 5;\n\t}\n\tprintf(\"y的值为:%f\\n\",y);\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470171",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/91.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7447326,
"question_title": "字符数组",
"question_content": "<p>编写一个以两个字符数组作为输入的函数。\n如果第二个数组包含在第一个数组中&#xff0c;则函数返回第一个数组中第二个数组开始的第一个索引。\n如果第二个数组不被包含在第一个数组&#xff0c;然后函数应该return -1\n输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’l’,’l’]  就 return 1.\n输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’n’] 就 return -1.</p>",
"difficulty": "简单",
"answer_id": 53427371,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n\n\nusing namespace std;\n\nint main()\n{\n\tchar a[128],b[128];\n\tint numA, numB;\n\tcout &lt;&lt; &#34;请输入第一个数组元素个数&#xff1a;&#34;;\n\tcin &gt;&gt; numA;\n\tcout &lt;&lt; &#34;请输入第一个数组元素&#xff1a;&#34;;\n\tfor (int i &#61; 0; i &lt; numA; &#43;&#43;i)\n\t\tcin &gt;&gt; a[i];\n cin.clear();\n\tcin.sync();\n\tcout &lt;&lt; &#34;请输入第二个数组元素个数&#xff1a;&#34;;\n\tcin &gt;&gt; numB;\n\tcout &lt;&lt; &#34;请输入第二个数组元素&#xff1a;&#34;;\n\tfor (int i &#61; 0; i &lt; numB; &#43;&#43;i)\n\t\tcin &gt;&gt; b[i];\n\tint num &#61; 0;\t\t//第二个数组包含在第一个数组的个数\n\tstring index;\t\t//存放第二个数组在第一个数组的索引\n\tfor (int j &#61; 0; j &lt; numB; j&#43;&#43;)\t\t//第二个数组的元素与第一数组的元素遍历\n\t{\n\t\tfor (int k &#61; 0; k &lt; numA; k&#43;&#43;)\n\t\t{\n\t\t\tif (b[j] &#61;&#61; a[k])\n\t\t\t{\n\t\t\t\tindex &#43;&#61; to_string(k);\n\t\t\t\tnum&#43;&#43;;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (num &#61;&#61; numB)\t\n\t{\n\t\tcout &lt;&lt; &#34;第二个数组包含在第一个数组中&#34; &lt;&lt; endl;\n\t\tcout &lt;&lt; &#34;第一个数组中第二个数组开始的第一个索引为:&#34; &lt;&lt; index.substr(0,1) &lt;&lt; endl;\n\t}\n\telse\n\t\tcout &lt;&lt; &#34;第二个数组不被包含在第一个数组&#34;;\n\n\tsystem(&#34;pause&#34;);\n\treturn 0;\n}</code></pre>\n\n<p> </p>\n\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623392427399.png\" /></p>\n\n<p> </p>\n",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint main()\n{\n\tchar a[128],b[128];\n\tint numA, numB;\n\tcout << \"请输入第一个数组元素个数:\";\n\tcin >> numA;\n\tcout << \"请输入第一个数组元素:\";\n\tfor (int i = 0; i < numA; ++i)\n\t\tcin >> a[i];\n\tcin.clear();\n\tcin.sync();\n\tcout << \"请输入第二个数组元素个数:\";\n\tcin >> numB;\n\tcout << \"请输入第二个数组元素:\";\n\tfor (int i = 0; i < numB; ++i)\n\t\tcin >> b[i];\n\tint num = 0;\t\t\n\tstring index;\t\t\n\tfor (int j = 0; j < numB; j++)\t\t\n\t{\n\t\tfor (int k = 0; k < numA; k++)\n\t\t{\n\t\t\tif (b[j] == a[k])\n\t\t\t{\n\t\t\t\tindex += to_string(k);\n\t\t\t\tnum++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif (num == numB)\t\n\t{\n\t\tcout << \"第二个数组包含在第一个数组中\" << endl;\n\t\tcout << \"第一个数组中第二个数组开始的第一个索引为:\" << index.substr(0,1) << endl;\n\t}\n\telse\n\t\tcout << \"第二个数组不被包含在第一个数组\";\n\tsystem(\"pause\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469954",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/92.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7446071,
"question_title": "不同方式求n的阶乘",
"question_content": "<p>求n的阶乘&#xff08;用三种不同的循环实现&#xff0c;提示&#xff1a;先从键盘输入n的值&#xff09;while、do while和for</p>",
"difficulty": "简单",
"answer_id": 53425388,
"answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n\nint main(void) { \n int n;\n printf(&#34;请输入n的值&#xff1a;&#34;);\n scanf(&#34;%d&#34;,&amp;n);\n // while循环\n int temp&#61;n,sum&#61;1;\n while(temp&gt;1){\n sum*&#61;temp;\n temp--;\n }\n printf(&#34;%d的阶乘是%d\\n&#34;,n,sum);\n \n // do while循环\n temp&#61;n;\n sum&#61;1;\n do{\n sum*&#61;temp;\n temp--;\n }while(temp&gt;1);\n printf(&#34;%d的阶乘是%d\\n&#34;,n,sum);\n \n // for循环\n temp&#61;n;\n sum&#61;1;\n for(;temp&gt;1;temp--){\n sum*&#61;temp;;\n }\n printf(&#34;%d的阶乘是%d\\n&#34;,n,sum);\n return 0;\n}</code></pre>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main(void) { \n\tint n;\n\tprintf(\"请输入n的值:\");\n\tscanf(\"%d\",&n);\n\tint temp=n,sum=1;\n\twhile(temp>1){\n\t\tsum*=temp;\n\t\ttemp--;\n\t}\n\tprintf(\"%d的阶乘是%d\\n\",n,sum);\n\ttemp=n;\n\tsum=1;\n\tdo{\n\t\tsum*=temp;\n\t\ttemp--;\n\t}while(temp>1);\n\tprintf(\"%d的阶乘是%d\\n\",n,sum);\n\ttemp=n;\n\tsum=1;\n\tfor(;temp>1;temp--){\n\t\tsum*=temp;;\n\t}\n\tprintf(\"%d的阶乘是%d\\n\",n,sum);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469955",
"status": 1,
"keywords": "循环",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/93.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 884449,
"question_title": "数字归类",
"question_content": "题目描述\n一个数里面若含有数字1,则归类到1字类,含有数字2,则归类到2字类,所以一个数可能同时归类到不同的数字类。对于0、1、2、3、4、5、6、7、8、9这十个数字类,因研究需要,急于想知道某一堆数中,究竟归类到这些数字类的个数。\n样例输入\n123 456 175 2 61 9998 12 5053 382\n样例输出\n0: 1\n1: 4\n2: 4\n3: 3\n4: 1\n5: 3\n6: 2\n7: 1\n8: 2\n9: 1\n提示\n注意:输出结果中冒号后面有空格",
"difficulty": "中等",
"answer_id": 1023453,
"answer_content": "\n```\n#include <stdio.h>\n#include <string.h>\n\nint result[10];\n\nint main(void) {\n\tmemset(result, 0, sizeof(int) * 10);\n\tint n;\n\tint arr[10];\n\twhile (scanf(\"%d\", &n) != EOF)\n\t{\n\t\tmemset(arr, 0, sizeof(int) * 10);\n\t\tif (n == 0) arr[0] = 1;\n\t\twhile (n > 0)\n\t\t{\n\t\t\tarr[n % 10] = 1;\n\t\t\tn = n / 10;\n\t\t}\n\t\tfor (int i = 0; i < 10; i++)\n\t\t\tresult[i] += arr[i];\n\t}\n\tfor (int i = 0; i < 10; i++)\n\t\tprintf(\"%d: %d\\n\", i, result[i]);\n\treturn 0;\n}\n\n```\n\nhttps://www.ideone.com/EMaE5P \n在线编译调试通过\n\n# 如果问题解决,请采纳",
"tag_name": "c++",
"cpp": "#include <stdio.h>\n#include <string.h>\nint result[10];\nint main(void) {\n\tmemset(result, 0, sizeof(int) * 10);\n\tint n;\n\tint arr[10];\n\twhile (scanf(\"%d\", &n) != EOF)\n\t{\n\t\tmemset(arr, 0, sizeof(int) * 10);\n\t\tif (n == 0) arr[0] = 1;\n\t\twhile (n > 0)\n\t\t{\n\t\t\tarr[n % 10] = 1;\n\t\t\tn = n / 10;\n\t\t}\n\t\tfor (int i = 0; i < 10; i++)\n\t\t\tresult[i] += arr[i];\n\t}\n\tfor (int i = 0; i < 10; i++)\n\t\tprintf(\"%d: %d\\n\", i, result[i]);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469856",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序的描述,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/94.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7458543,
"question_title": "输入一个数查找比它小的元素",
"question_content": "<p>一个整型数组有10元素&#xff0c;请先给这10个元素赋值&#xff0c;然后随便输入一个数&#xff0c;最后输出数组中比这个数小的所有元素&#xff0c;每个数用空格隔开&#xff0c;如果没有找到&#xff0c;则输出not find。<br />\n </p>\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1624519041254.jpg\" />\n </p>",
"difficulty": "中等",
"answer_id": 53443165,
"answer_content": "<p>代码如下&#xff1a;</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\nint main()\n{\n\tint a[10],i,n;\n\tint isfind &#61; 0;\n\tprintf(&#34;please set array values:&#34;);\n\tfor(i&#61;0;i&lt;10;i&#43;&#43;)\n\t\tscanf(&#34;%d&#34;,&amp;a[i]);\n\n\tprintf(&#34;please enter one num:&#34;);\n\tscanf(&#34;%d&#34;,&amp;n);\n\tfor (i&#61;0;i&lt;10;i&#43;&#43;)\n\t{\n\t\tif(a[i] &lt; n)\n\t\t{\n\t\t\tisfind &#61; 1;\n\t\t\tprintf(&#34;%d &#34;,a[i]);\n\t\t}\n\t}\n\t\n\tif(isfind)\n\t\tprintf(&#34;\\n&#34;);\n\telse\n\t\tprintf(&#34;not find\\n&#34;);\n\t\n\t\n\treturn 0;\n}</code></pre>\n",
"tag_name": "c语言",
"cpp": "#include <stdio.h>\nint main()\n{\n\tint a[10],i,n;\n\tint isfind = 0;\n\tprintf(\"please set array values:\");\n\tfor(i=0;i<10;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tprintf(\"please enter one num:\");\n\tscanf(\"%d\",&n);\n\tfor (i=0;i<10;i++)\n\t{\n\t\tif(a[i] < n)\n\t\t{\n\t\t\tisfind = 1;\n\t\t\tprintf(\"%d \",a[i]);\n\t\t}\n\t}\n\tif(isfind)\n\t\tprintf(\"\\n\");\n\telse\n\t\tprintf(\"not find\\n\");\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600470172",
"status": 1,
"keywords": "算法高阶,数论算法,元素的幂,算法问题选编",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/95.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 237869,
"question_title": "编写程序,输入任意一个含有空格的字符串(至少10个字符),删除指定字符后输出该字符串。",
"question_content": "编写程序,输入任意一个含有空格的字符串(至少10个字符),删除指定字符后输出该字符串。例如,输入“jiangsu123”和删除位置5,则输出“jiansu123”。",
"difficulty": "简单",
"answer_id": 218941,
"answer_content": "\n\n```\n #include <iostream>\n#include <string>\nusing namespace std;\n\nint main()\n{\n char s1[100];\n char s2[100];\n int n = 0;\n\tcin.getline(s1, 100);\n\tcin.getline(s2, 100);\n cin >> n;\n\tint i;\n\tint ls2 = strlen(s2);\n\tfor (i = strlen(s1); i >= n; i--)\n\t\ts1[i + ls2] = s1[i];\n\tfor (i = 0; i < ls2; i++)\n\t\ts1[n + i] = s2[i];\n cout << s1 << endl;\n return 0;\n}\n```\n\n",
"tag_name": "c++",
"cpp": " #include <iostream>\n#include <string>\nusing namespace std;\nint main()\n{\n\tchar s1[100];\n\tchar s2[100];\n\tint n = 0;\n\tcin.getline(s1, 100);\n\tcin.getline(s2, 100);\n\tcin >> n;\n\tint i;\n\tint ls2 = strlen(s2);\n\tfor (i = strlen(s1); i >= n; i--)\n\t\ts1[i + ls2] = s1[i];\n\tfor (i = 0; i < ls2; i++)\n\t\ts1[n + i] = s2[i];\n\tcout << s1 << endl;\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469857",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/96.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1080248,
"question_title": "擅长编码的小k",
"question_content": "题目描述\n小k不仅擅长数学,也擅长编码。有一种编码方式如下:\n首先写下文本中间的字符(如果文本中的字符编号为1..n,那么中间一个字符的编号为(n+1)DIV 2,其中DIV为整除的意思),然后 用这个方法递归地写下左边,最后再按这个方法递归地写下右边。例如,单词为orthography则其编码为gtorhoprahy。即先写中间的那个字符g,再对ortho递归地编码,最后将raphy递归地编码就得到了gtorhoprahy。\n给一个原来的文本,求出编码后的 文本。\n输入\n一行字符,表示原始的文本内容。\n输出\n一行字符,表示编码后的文本内容。\n样例\n输入 \northography\n输出 \ngtorhoprahy\n提示\n100%的数据,字符串长度不超过20000",
"difficulty": "困难",
"answer_id": 1285384,
"answer_content": "利用递归,先输出中间的字符,然后输出递归输出左边的,再递归输出右边的。代码如下:\n\n```\n#include <iostream>\n#include <cstring>\nusing namespace std;\n\nvoid shuchu(char *a,int m, int n)\n{\n if(n<=0||m<=0||m>n)\n {\n return;\n }\n else\n {\n cout << a[(m+n)/2];\n shuchu(a,m,(m+n)/2-1);\n shuchu(a,(m+n)/2+1,n);\n }\n}\n\nint main()\n{\n char a[20000];\n char b[20001];\n cin >> a;\n for(int i=0; i<20000; i++)\n {\n b[i+1] = a[i];\n }\n int n = strlen(a);\n shuchu(b,1,n);\n return 0;\n}\n\n\n```",
"tag_name": "c++",
"cpp": "#include <iostream>\n#include <cstring>\nusing namespace std;\nvoid shuchu(char *a,int m, int n)\n{\n\tif(n<=0||m<=0||m>n)\n\t{\n\t\treturn;\n\t}\n\telse\n\t{\n\t\tcout << a[(m+n)/2];\n\t\tshuchu(a,m,(m+n)/2-1);\n\t\tshuchu(a,(m+n)/2+1,n);\n\t}\n}\nint main()\n{\n\tchar a[20000];\n\tchar b[20001];\n\tcin >> a;\n\tfor(int i=0; i<20000; i++)\n\t{\n\t\tb[i+1] = a[i];\n\t}\n\tint n = strlen(a);\n\tshuchu(b,1,n);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469956",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,用主方法求解递归式",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/97.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7412708,
"question_title": "水果计费系统",
"question_content": "<p>本关任务&#xff1a;编写程序&#xff0c;有五种水果&#xff0c;apple、banana、orage、strawberry、pear&#xff0c;每一种有一个价格&#xff08;浮点小数&#xff09;&#xff0c;由老板输入&#xff0c;请提示用户选择什么水果&#xff0c;购买数量&#xff08;按照斤两&#xff09;&#xff0c;然后将总价显示出来。</p>",
"difficulty": "简单",
"answer_id": 53364151,
"answer_content": "<pre>\n<code>#include&lt;stdio.h&gt;\n\nint main(){\n\ttypedef enum {apple, banana, orange, strawberry, pear} fruits;//水果枚举类型 \n\tdouble prices[5]; //价格数组\n\tfruits purchase; //枚举购买水果 \n\tint fruit; //水果编号的输入 \n\tdouble amount; //总价 \n\tfor(int i &#61; 0; i &lt; 5; i&#43;&#43;)\n\t\tscanf(&#34;%lf&#34;, &amp;prices[i]);//老板输入价格 \n\tprintf(&#34;水果编号&#xff1a;1.苹果 2.香蕉 3.橘子 4.草莓 5.梨\\n&#34;);\n\tprintf(&#34;请输入购买的水果(1~5)&#xff0c;以及购买数量&#xff08;按照斤两&#xff09;&#xff1a;\\n&#34;);//提示\n\tscanf(&#34;%d %lf&#34;, &amp;fruit, &amp;amount);\n\tpurchase &#61; (fruits)fruit; //输入的编号对应水果 \n\tprintf(&#34;总价为&#xff1a;%.3lf&#34;, prices[purchase-1]*amount);\n\treturn 0; \n}</code></pre>\n\n<p> </p>",
"tag_name": "c语言",
"cpp": "#include<stdio.h>\nint main(){\n\ttypedef enum {apple, banana, orange, strawberry, pear} fruits;\n\tdouble prices[5];\t\t\t \n\tfruits purchase;\t\t\t \n\tint fruit;\t\t\t\t \n\tdouble amount;\t\t\t \n\tfor(int i = 0; i < 5; i++)\n\t\tscanf(\"%lf\", &prices[i]);\n\tprintf(\"水果编号:1.苹果 2.香蕉 3.橘子 4.草莓 5.梨\\n\");\n\tprintf(\"请输入购买的水果(1~5),以及购买数量(按照斤两):\\n\");\n\tscanf(\"%d %lf\", &fruit, &amount);\n\tpurchase = (fruits)fruit;\t \n\tprintf(\"总价为:%.3lf\", prices[purchase-1]*amount);\n\treturn 0; \n}",
"topic_link": "https://bbs.csdn.net/topics/600470173",
"status": 1,
"keywords": "算法中阶,贪心算法,活动选择问题,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/98.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1057027,
"question_title": "已知一个浮点数A(0<A<5),求它由哪两个整数B/C相除的值最接近",
"question_content": "已知一个浮点数A(0<A<5),求它由哪两个整数B/C相除的值最接近,有相同值时要求B最小\n例如:\nA=0.2\nB=1 C=5",
"difficulty": "中等",
"answer_id": 1254068,
"answer_content": "\n```\n#include <stdio.h>\n#include <math.h>\nint main () {\nfloat A = 0.2f;\nint x = 0;\nif (A < 0) { x = 1; A = 1/A; }\nfloat delta = 1;\nint B = 1, C = 1;\ndo\n{\nC = (int)(B * A);\ndelta = fabs(C/(float)B-A);\nB++;\n}\nwhile (delta > 0.000001); //如果你要高精度,就改小,否则改大\nif (x == 0)\nprintf(\"%d / %d\", C, B - 1);\nelse\nprintf(\"%d / %d\", B - 1, C);\nreturn 0;\n}\n\n```\n# 问题解决请点采纳",
"tag_name": "c++",
"cpp": "#include <stdio.h>\n#include <math.h>\nint main () {\n\tfloat A = 0.2f;\n\tint x = 0;\n\tif (A < 0) { x = 1; A = 1/A; }\n\tfloat delta = 1;\n\tint B = 1, C = 1;\n\tdo\n\t{\n\t\tC = (int)(B * A);\n\t\tdelta = fabs(C/(float)B-A);\n\t\tB++;\n\t}\n\twhile (delta > 0.000001); \n\tif (x == 0)\n\t\tprintf(\"%d / %d\", C, B - 1);\n\telse\n\t\tprintf(\"%d / %d\", B - 1, C);\n\treturn 0;\n}",
"topic_link": "https://bbs.csdn.net/topics/600469957",
"status": 1,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/99.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 382917,
"question_title": "改写字符串",
"question_content": "键盘录入一个字符串,将字符串中的大写改成小写,小写改成大写,数字改成*。例如heLLO123,输出后为HEllo***",
"difficulty": "简单",
"answer_id": 418046,
"answer_content": "\n\n```\n import java.util.Scanner;\n\n/**\n *\n * @author sunzc 转换字符串,大写变小写,小写变大写,数字不变,其他变为*\n */\npublic class Transfer {\n public static void main(String[] args) {\n // String str = \"ABC123abcfadfjbJBHJHJDsa\";\n String str = \"\";\n Scanner s = new Scanner(System.in);\n System.out.println(\"请输入您想输入的字符串:\");\n str = s.next();\n StringBuffer sb = new StringBuffer();\n int i;\n // char ch;\n for (i = 0; i <= str.length() - 1; i++) {// 遍历字符串\n char ch;\n\n // 通过str.charAt(i)遍历出字符串中每个字符\n if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {// 判断字符是否在a-z之间(小写)\n ch = (char) (str.charAt(i) - 32); // 如果为小写则转换为相应大写,赋值给ch\n } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {// 判断字符是否在A-Z之间(大写)\n ch = (char) (str.charAt(i) + 32); // 如果为大写则转换为相应小写,赋值给ch\n } else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {// 判断字符是否在0-9之间(数字)\n ch = '*'; // 如果为数字,则转为*号\n } else {\n ch = str.charAt(i); // 将原字符赋值给ch\n }\n sb.append(ch); // 将字符追加到sb序列\n }\n String trStr = sb.toString(); // 将StringBuffer转换为String类型\n System.out.println(sb.toString());\n }\n\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.Scanner;\npublic class Transfer {\n\tpublic static void main(String[] args) {\n\tString str = \"\";\n\tScanner s = new Scanner(System.in);\n\tSystem.out.println(\"请输入您想输入的字符串:\");\n\tstr = s.next();\n\tStringBuffer sb = new StringBuffer();\n\tint i;\n\tfor (i = 0; i <= str.length() - 1; i++) {\n\t\tchar ch;\n\t\tif (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {\n\t\t\tch = (char) (str.charAt(i) - 32); \n\t\t} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {\n\t\t\tch = (char) (str.charAt(i) + 32); \n\t\t} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {\n\t\t\tch = '*'; \n\t\t} else {\n\t\t\tch = str.charAt(i); \n\t\t}\n\t\tsb.append(ch); \n\t}\n\tString trStr = sb.toString(); \n\tSystem.out.println(sb.toString());\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600469971",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/0.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1071564,
"question_title": "显示是哪个线程输出的字母",
"question_content": "编写一个程序,创建两个线程,要求分别输出26个字母。在输出结果时,要显示是哪个线程输出的字母。注:26个字母可以首先定义一个字符a,在a的基础上循环加1实现。",
"difficulty": "中等",
"answer_id": 1274548,
"answer_content": "\n```\npackage help;\n\npublic class help {\n\tpublic static void main(String[] args) {\n\t\tThread a=new ThreadA();//创建线程\n\t\ta.start();//线程启动\n\t\tThread b=new ThreadB();\n\t\tb.start();\n\t}\n}\nclass ThreadA extends Thread implements Runnable{//利用Runnable接口使这个类变成一个可运行的类\n\tpublic void run() {//线程的标配,run方法\n\t\tchar[] arr= {'a','b','c','d'};//先创建一个字符数组,为下面的遍历做准备(把a到z都写在里面就可以了,这里我没有写全)\n\t\tfor(char x:arr) {//用foreach循环遍历上面的字符数组,就可以轮流输出了\n\t\t\tSystem.out.println(\"线程A输出:\"+x);//输出每个遍历出来的元素\n\t\t}\n\t}\n}\nclass ThreadB extends Thread implements Runnable{//与上面的原理一样\n\tpublic void run() {\n\t\tchar[] arr= {'a','b','c','d'};\n\t\tfor(char x:arr) {\n\t\t\tSystem.out.println(\"线程B输出:\"+x);\n\t\t}\n\t}\n}\n\n```",
"tag_name": "java",
"java": "public class help {\n\tpublic static void main(String[] args) {\n\t\tThread a=new ThreadA();\n\t\ta.start();\n\t\tThread b=new ThreadB();\n\t\tb.start();\n\t}\n}\nclass ThreadA extends Thread{\n\tpublic void run() {\n\t\tchar[] arr= {'a','b','c','d'};\n\t\tfor(char x:arr) {\n\t\t\tSystem.out.println(\"线程A输出:\"+x);\n\t\t}\n\t}\n}\nclass ThreadB extends Thread{\n\tpublic void run() {\n\t\tchar[] arr= {'a','b','c','d'};\n\t\tfor(char x:arr) {\n\t\t\tSystem.out.println(\"线程B输出:\"+x);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469972",
"status": 0,
"keywords": "算法高阶,多线程算法,算法问题选编,动态多线程基础",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/1.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1086384,
"question_title": "按要求编写字符界面",
"question_content": "编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。",
"difficulty": "简单",
"answer_id": 1293295,
"answer_content": "\n```\nimport java.util.Scanner;\npublic class MaxMin {\n public static void main(String[] args) {\n Arrays array = new Arrays();\n array.setArr();\n int max=array.getMax();\n int min=array.getMin();\n System.out.println(\"数组中最大值=\"+max);\n System.out.println(\"数组中最小值=\"+min);\n }\n }\n class Arrays {\n private int[] arr;\n // 构造方法初始化\n public Arrays() {\n arr = new int[10] ;\n for(int i = 0; i<arr.length;i++) {\n arr[i] =0;\n } \n }\n // 成员变量初始化\n public void setArr() {\n Scanner sc = new Scanner(System.in); \n System.out.println(\"请输入数组元素的值:\"); \n arr = new int[10] ;\n for(int i = 0; i<arr.length;i++) {\n arr[i] = sc.nextInt();\n }\n }\n // 计算最大数\n public int getMax() {\n int max = arr[0];\n for(int i : arr) {\n if(max < i)\n max = i;\n }\n return max; \n }\n // 计算最小数\n public int getMin() {\n int min= arr[0];\n for(int i : arr) {\n if(min > i)\n min= i;\n }\n return min; \n }\n}\n\n```\nhttps://blog.csdn.net/qq_44473695/article/details/102326683",
"tag_name": "java",
"java": "import java.util.Scanner;\npublic class MaxMin {\n\tpublic static void main(String[] args) {\n\t\tArrays array = new Arrays();\n\t\tarray.setArr();\n\t\tint max=array.getMax();\n\t\tint min=array.getMin();\n\t\tSystem.out.println(\"数组中最大值=\"+max);\n\t\tSystem.out.println(\"数组中最小值=\"+min);\n\t}\n}\nclass Arrays {\n\tprivate int[] arr;\n\tpublic Arrays() {\n\t\tarr = new int[10] ;\n\t\tfor(int i = 0; i<arr.length;i++) {\n\t\t\tarr[i] =0;\n\t\t}\n\t}\n\tpublic void setArr() {\n\t\tScanner sc = new Scanner(System.in); \n\t\tSystem.out.println(\"请输入数组元素的值:\"); \n\t\tarr = new int[10] ;\n\t\tfor(int i = 0; i<arr.length;i++) {\n\t\t\tarr[i] = sc.nextInt();\n\t\t}\n\t}\n\tpublic int getMax() {\n\t\tint max = arr[0];\n\t\tfor(int i : arr) {\n\t\t\tif(max < i)\n\t\t\t\tmax = i;\n\t\t}\n\t\treturn max; \n\t}\n\tpublic int getMin() {\n\t\tint min= arr[0];\n\t\tfor(int i : arr) {\n\t\t\tif(min > i)\n\t\t\t\tmin= i;\n\t\t}\n\t\treturn min; \n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470281",
"status": 1,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/10.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 384519,
"question_title": "生成随机字符串",
"question_content": "生成一个由大写字母和数字组成的6位随机字符串,并且字符串不重复",
"difficulty": "简单",
"answer_id": 420998,
"answer_content": "public static char[] generate() {\n\t\t\n\t\tchar[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',\n\t\t\t\t'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V',\n\t\t\t\t'W', 'X', 'Y', 'Z','0','1','2','3','4','5','6','7','8','9'};\n\t\tboolean[] flags = new boolean[letters.length];\n\t\tchar[] chs = new char[6];\n\t\tfor (int i = 0; i < chs.length; i++) {\n\t\t\tint index;\n\t\t\tdo {\n\t\t\t\tindex = (int) (Math.random() * (letters.length));\n\t\t\t} while (flags[index]);// 判断生成的字符是否重复\n\t\t\tchs[i] = letters[index];\n\t\t\tflags[index] = true;\n\t\t}\n\t\treturn chs;\n\t}",
"tag_name": "java",
"java": "class java_384519 {\n\tpublic static char[] generate() {\n\tchar[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',\n\t\t'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };\n\tboolean[] flags = new boolean[letters.length];\n\tchar[] chs = new char[6];\n\tfor (int i = 0; i < chs.length; i++) {\n\t int index;\n\t do {\n\t\tindex = (int) (Math.random() * (letters.length));\n\t } while (flags[index]);\n\t chs[i] = letters[index];\n\t flags[index] = true;\n\t}\n\treturn chs;\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600470188",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/11.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7443813,
"question_title": "求公式的值",
"question_content": "求 1-1/2!-1/3! -... -1/10! ",
"difficulty": "简单",
"answer_id": 53421121,
"answer_content": "<p>代码如下&#xff1a;</p>\n\n<pre>\n<code>\npublic class TEST {\n\t\n\tpublic static double jiecheng(int n){\n\t\tdouble s &#61; 1;\n\t\tfor(int i &#61; 1; i &lt;&#61;n;i&#43;&#43;)\n\t\t\ts *&#61; i;\n\t\treturn s;\n\t}\n\tpublic static double sum(int n){\n\t\tdouble sum &#61; 0.0;\n\t\tint s &#61; 1;\n\t\tfor(int i &#61; 1; i &lt;&#61;n; i&#43;&#43;){\n\t\t\tsum &#43;&#61; s/jiecheng(i);\n\t\t\ts &#61; -s;\n\t\t}\n\t\treturn sum;\n\t}\n\t\n\t\n\tpublic static void main(String[] args) throws Exception \n\t{\n\t\tint n &#61; 10;\n\t\tdouble ss &#61; sum(n);\n\t\tSystem.out.println(ss);\n\t\t\n\t\t\n\t}\n\t\n\t\n}\n</code></pre>\n",
"tag_name": "java",
"java": "public class TEST {\n\tpublic static double jiecheng(int n) {\n\t\tdouble s = 1;\n\t\tfor (int i = 1; i <= n; i++)\n\t\t\ts *= i;\n\t\treturn s;\n\t}\n\tpublic static double sum(int n) {\n\t\tdouble sum = 0.0;\n\t\tint s = 1;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tsum += s / jiecheng(i);\n\t\t\ts = -s;\n\t\t}\n\t\treturn sum;\n\t}\n\tpublic static void main(String[] args) throws Exception {\n\t\tint n = 10;\n\t\tdouble ss = sum(n);\n\t\tSystem.out.println(ss);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470282",
"status": 1,
"keywords": "数学,阶乘,算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/12.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7454234,
"question_title": "用数组写水仙花数",
"question_content": "定义一个整型数组 a[7],在控制台输入任意的 7 个整数给数组赋值,输出数组中所 有的“水仙花数”。“水仙花数”是指一个三位数其各位数字的立方和等于该数本身。",
"difficulty": "简单",
"answer_id": 53436906,
"answer_content": "<p><strong>如有帮助&#xff0c;请采纳。点击我回答右上角【采纳】按钮。</strong></p>\n\n<pre>\n<code>import java.util.Scanner;\npublic class Test {\n\tpublic static void main(String[] args) {\n Scanner in&#61;new Scanner(System.in);\n int arr[]&#61;new int[7];\n System.out.println(&#34;请输入7个三位数&#xff1a;&#34;);\n for(int i&#61;0;i&lt;arr.length;i&#43;&#43;){\n \tarr[i]&#61;in.nextInt();\n }\n System.out.println(&#34;数组中的水花仙数为&#xff1a;&#34;);\n for(int i&#61;0;i&lt;arr.length;i&#43;&#43;){\n int x &#61;arr[i]/100;\n int y &#61;arr[i]/10%10;\n int z &#61;arr[i]%10;\n if(i&#61;&#61;x*x*x&#43;y*y*y&#43;z*z*z)\n System.out.println(i);\n }\n }\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "import java.util.Scanner;\npublic class Test {\n\tpublic static void main(String[] args) {\n\t\tScanner in = new Scanner(System.in);\n\t\tint arr[] = new int[7];\n\t\tSystem.out.println(\"请输入7个三位数:\");\n\t\tfor (int i = 0; i < arr.length; i++) {\n\t\t\tarr[i] = in.nextInt();\n\t\t}\n\t\tSystem.out.println(\"数组中的水花仙数为:\");\n\t\tfor (int i = 0; i < arr.length; i++) {\n\t\t\tint x = arr[i] / 100;\n\t\t\tint y = arr[i] / 10 % 10;\n\t\t\tint z = arr[i] % 10;\n\t\t\tif (arr[i] == x * x * x + y * y * y + z * z * z)\n\t\t\t\tSystem.out.println(i);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470189",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/13.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 150738,
"question_title": "对给定的两个日期之间的日期进行遍历",
"question_content": "对给定的两个日期之间的日期进行遍历,比如startTime 是 2014-07-11;endTime 是 2014-08-11 如何把他们之间的日期获取并遍历出来。",
"difficulty": "简单",
"answer_id": 72266,
"answer_content": "import java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\n\npublic class SplitTime {\n\n\tprivate static List<Date> dateSplit(Date startDate, Date endDate)\n\t\t\tthrows Exception {\n\t\tif (!startDate.before(endDate))\n\t\t\tthrow new Exception(\"开始时间应该在结束时间之后\");\n\t\tLong spi = endDate.getTime() - startDate.getTime();\n\t\tLong step = spi / (24 * 60 * 60 * 1000);// 相隔天数\n\n\t\tList<Date> dateList = new ArrayList<Date>();\n\t\tdateList.add(endDate);\n\t\tfor (int i = 1; i <= step; i++) {\n\t\t\tdateList.add(new Date(dateList.get(i - 1).getTime()\n\t\t\t\t\t- (24 * 60 * 60 * 1000)));// 比上一天减一\n\t\t}\n\t\treturn dateList;\n\t}\n\n\tpublic static void main(String[] args) throws ParseException {\n\t\ttry {\n\t\t\tSimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\");\n\t\t\tDate start = sdf.parse(\"2015-4-20\");\n\t\t\tDate end = sdf.parse(\"2015-5-2\");\n\t\t\tList<Date> lists = dateSplit(start, end);\n\t\t\tif (!lists.isEmpty()) {\n\t\t\t\tfor (Date date : lists) {\n\t\t\t\t\tSystem.out.println(sdf.format(date));\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (Exception e) {\n\t\t}\n\t}\n\n}\n运行结果:\n2015-05-02\n2015-05-01\n2015-04-30\n2015-04-29\n2015-04-28\n2015-04-27\n2015-04-26\n2015-04-25\n2015-04-24\n2015-04-23\n2015-04-22\n2015-04-21\n2015-04-20",
"tag_name": "java",
"java": "import java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\npublic class SplitTime {\n\tprivate static List<Date> dateSplit(Date startDate, Date endDate) throws Exception {\n\t\tif (!startDate.before(endDate))\n\t\t\tthrow new Exception(\"开始时间应该在结束时间之后\");\n\t\tLong spi = endDate.getTime() - startDate.getTime();\n\t\tLong step = spi / (24 * 60 * 60 * 1000);\n\t\tList<Date> dateList = new ArrayList<Date>();\n\t\tdateList.add(endDate);\n\t\tfor (int i = 1; i <= step; i++) {\n\t\t\tdateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));\n\t\t}\n\t\treturn dateList;\n\t}\n\tpublic static void main(String[] args) throws ParseException {\n\t\ttry {\n\t\t\tSimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\");\n\t\t\tDate start = sdf.parse(\"2015-4-20\");\n\t\t\tDate end = sdf.parse(\"2015-5-2\");\n\t\t\tList<Date> lists = dateSplit(start, end);\n\t\t\tif (!lists.isEmpty()) {\n\t\t\t\tfor (Date date : lists) {\n\t\t\t\t\tSystem.out.println(sdf.format(date));\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (Exception e) {\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470283",
"status": 1,
"keywords": "遍历",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/14.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 707980,
"question_title": "字符串排序",
"question_content": "用main方法排序(按首字母或按字符串长度)\n要求:\n①输出原数组和排序后的数组\n②若有“,”“.”不能进行排序,并将不符合排序要求的数组打印出来\n③不能用API,可用选择、冒泡、快速",
"difficulty": "简单",
"answer_id": 641196,
"answer_content": "\n\n```\nclass Untitled {\n\tpublic static void main(String[] args) {\n\t\tint num;\n\t\tSystem.out.println(\"请输入数组元素长度:\");\n\t\tScanner in=new Scanner(System.in);\n\t\tnum=in.nextInt();\n\t\tSystem.out.println(\"请输入字符串:\");\n\t\tString str[]=new String[num];\n\t\tfor(int count=0;count<num;count++)\n\t\t{\n\t\t\tstr[count]=in.next();\n\t\t}\n\t\tin.close();\n\t\t\n\t\tSystem.out.println(\"before sorting:\");\n\t\t\n\t\tint c = 0;\n\t\tfor (int i = 0; i < num; i++)\n\t\t{\n\t\t\tif (str[i].contains(\",\")\n\t\t\t\t|| str[i].contains(\".\")) \n\t\t\t\tc++;\n\t\t\tSystem.out.println(str[i]);\t\n\t\t}\n\n\t\tfor (int i = 0; i < num - 1; i++)\n\t\t{\n\t\t\tint min = i;\n\t\t\tfor (int j = i + 1; j < num; j++)\n\t\t\t{\n\t\t\t\tif (str[min].contains(\",\")\n\t\t\t\t\t|| str[min].contains(\".\"))\n\t\t\t\t\tmin = j;\n\t\t\t\telse if (str[j].compareTo(str[min]) < 0 && !str[j].contains(\",\")\n\t\t\t\t\t&& !str[j].contains(\".\"))\n\t\t\t\t\tmin = j;\n\t\t\t}\n\t\t\tif (min != i)\n\t\t\t{\n\t\t\t\tString t = str[i];\n\t\t\t\tstr[i] = str[min];\n\t\t\t\tstr[min] = t;\n\t\t\t}\n\t\t}\n\t\t\n\t\tSystem.out.println(\"after sorting:\");\n\t\tfor (int i = 0; i < num - c; i++)\n\t\t\tSystem.out.println(str[i]);\n\t\tSystem.out.println(\"invalid items:\");\n\t\tfor (int i = num - c; i < num; i++)\n\t\t\tSystem.out.println(str[i]);\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.Scanner;\nclass Untitled {\n public static void main(String[] args) {\n\t int num;\n\t System.out.println(\"请输入数组元素长度:\");\n\t Scanner in = new Scanner(System.in);\n\t num = in.nextInt();\n\t System.out.println(\"请输入字符串:\");\n\t String str[] = new String[num];\n\t for (int count = 0; count < num; count++) {\n\t\t str[count] = in.next();\n\t }\n\t in.close();\n\t System.out.println(\"before sorting:\");\n\t int c = 0;\n\t for (int i = 0; i < num; i++) {\n\t\t if (str[i].contains(\",\") || str[i].contains(\".\"))\n\t\t\tc++;\n\t\t System.out.println(str[i]);\n\t }\n\t for (int i = 0; i < num - 1; i++) {\n\t\t int min = i;\n\t\t for (int j = i + 1; j < num; j++) {\n\t\t\tif (str[min].contains(\",\") || str[min].contains(\".\"))\n\t\t\t min = j;\n\t\t\telse if (str[j].compareTo(str[min]) < 0 && !str[j].contains(\",\") && !str[j].contains(\".\"))\n\t\t\t min = j;\n\t\t }\n\t\t if (min != i) {\n\t\t\tString t = str[i];\n\t\t\tstr[i] = str[min];\n\t\t\tstr[min] = t;\n\t\t }\n\t }\n\t System.out.println(\"after sorting:\");\n\t for (int i = 0; i < num - c; i++)\n\t\t System.out.println(str[i]);\n\t System.out.println(\"invalid items:\");\n\t for (int i = num - c; i < num; i++)\n\t\t System.out.println(str[i]);\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600470190",
"status": 1,
"keywords": "算法初阶,快速排序,快速排序分析,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/15.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 751134,
"question_title": "拆分数字",
"question_content": "比如99 可以拆分为 9和9 9*9=81 81可以拆分为8和1 8*1=8 不能拆分了,得出结果为2\n65可以拆分为6和5,6*5=30 30可以拆分为3和0,3*0=0 不能拆分了,得出结果也为2\n实现这个功能 返回结果(结果为可拆分的次数)",
"difficulty": "简单",
"answer_id": 804601,
"answer_content": "\n```\npublic class HelloWorld {\n\tpublic static int splitmul(int n)\n\t{\n\t\tint r = 1;\n\t\twhile (n > 0)\n\t\t{\n\t\t\tr *= (n % 10);\n\t\t\tn /= 10;\n\t\t}\n\t\treturn r;\n\t}\n public static void main(String []args) {\n\t\tint n = 99;\n\t\tint x = n;\n\t\tint t = 0;\n\t\twhile (x >= 10)\n\t\t{\n\t\t\tx = splitmul(n);\n\t\t\tSystem.out.println(x);\n\t\t\tn = x;\n\t\t\tt++;\n\t\t}\n\t\tSystem.out.println(t + \"\");\n\n }\n}\n```\n\n81\n8\n2次",
"tag_name": "java",
"java": "public class HelloWorld {\n\tpublic static int splitmul(int n) {\n\t\tint r = 1;\n\t\twhile (n > 0) {\n\t\t\tr *= (n % 10);\n\t\t\tn /= 10;\n\t\t}\n\t\treturn r;\n\t}\n\tpublic static void main(String[] args) {\n\t\tint n = 99;\n\t\tint x = n;\n\t\tint t = 0;\n\t\twhile (x >= 10) {\n\t\t\tx = splitmul(n);\n\t\t\tSystem.out.println(x);\n\t\t\tn = x;\n\t\t\tt++;\n\t\t}\n\t\tSystem.out.println(t + \"\");\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469974",
"status": 1,
"keywords": "算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/16.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 803899,
"question_title": "随机抽样",
"question_content": "从数组a[]={1,2,3,4,5,6,7,8,9,10}中随机抽取5个数且不重复",
"difficulty": "简单",
"answer_id": 898226,
"answer_content": "这里只有十个元素,用 Random 的 nextInt() 对 10 求余,产生 10 以内的元素作为访问角标。\n同时需要维护一个 Set 判断是否重复,demo:\n\n```\npublic static void main(String[] args) {\n int a[]={1,2,3,4,5,6,7,8,9,10};\n Random r = new Random();\n int count = 10;\n while(count-->0){\n Set<Integer> result = new HashSet<Integer>(5);\n while(result.size()<5){\n int index = r.nextInt(10)%10;//范围为 10\n int currentData = a[index];\n if(result.contains(currentData)){\n continue;\n }\n\n System.out.print(\"随机加入:\"+ currentData);\n result.add(currentData);\n }\n System.out.println(\"\"+count+\"次,last随机数:\"+result);\n }\n\n\n }\n```\nrandom 的随机性还是可以的,基本上很少重复的。",
"tag_name": "java",
"java": "import java.util.HashSet;\nimport java.util.Random;\nimport java.util.Set;\nclass java_803899 {\n\tpublic static void main(String[] args) {\n\t\tint a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\n\t\tRandom r = new Random();\n\t\tint count = 10;\n\t\twhile (count-- > 0) {\n\t\t\tSet<Integer> result = new HashSet<Integer>(5);\n\t\t\twhile (result.size() < 5) {\n\t\t\t\tint index = r.nextInt(10) % 10;\n\t\t\t\tint currentData = a[index];\n\t\t\t\tif (result.contains(currentData)) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tSystem.out.print(\"随机加入:\" + currentData);\n\t\t\t\tresult.add(currentData);\n\t\t\t}\n\t\t\tSystem.out.println(\"\" + count + \"次,last随机数:\" + result);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470284",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/17.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 707984,
"question_title": "发奖金问题",
"question_content": "过年了,村里要庆祝。村长说,村里有一笔钱作为奖金,让每个人写一个纸条上来,谁写的数目与奖金最接近,就算猜中,这笔奖金就归谁,如果多人猜中,则平分。编写程序,算算都有哪些人得到奖金?多少?",
"difficulty": "中等",
"answer_id": 641210,
"answer_content": "\n\n```\nimport java.util.Collections;\nimport java.util.Comparator;\nimport java.util.Arrays;\n\nclass Q707984 {\n\tpublic static void main(String[] args) {\n\t\tint award = 100;\n\t\tString[] people = { \"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\" };\n\t\tInteger[] guess = { 75, 70, 80, 120, 100, 110, 100, 45 };\n\t\t\n\t\tInteger[] ordered = new Integer[people.length];\n\t\tfor (int i = 0; i < ordered.length; i++) ordered[i] = i;\n\t\tArrays.sort(ordered, new Comparator<Integer>() {\n\t\t\t@Override\n\t\t\tpublic int compare(Integer a, Integer b) {\n\t\t\t\tint x = guess[a] - award > 0 ? guess[a] - award : award - guess[a];\n\t\t\t\tint y = guess[b] - award > 0 ? guess[b] - award : award - guess[b];\n\t\t\t\treturn x - y;\n\t\t\t}\n\t\t});\n\t\tint maxp = 0;\n\t\tint i = 0;\n\t\twhile (guess[ordered[i++]] == award) maxp++;\n\t\tif (maxp <= 1)\n\t\t\tSystem.out.println(people[ordered[0]] + \"一人得奖\" + award + \"元。\");\n\t\telse\n\t\t{\n\t\t\tfor (i = 0; i < maxp; i++)\n\t\t\t\tSystem.out.print(people[ordered[i]] + \" \");\n\t\t\tSystem.out.println(\"共同得奖\" + award / (float)(maxp) + \"元。\");\n\t\t}\n\t}\n}\n```\n![图片说明](https://img-ask.csdn.net/upload/201811/04/1541340739_340119.gif)\n\n如果多人一样,但是都有误差,也平分,那么\nint i = 0;\nwhile (guess[ordered[i++]] == award) maxp++;\n修改为\nint i = 1;\nwhile (guess[ordered[i++]] == guess[0]) maxp++;\n\n`如果问题得到解决,请点我回答左上角的采纳,谢谢`\n",
"tag_name": "java",
"java": "import java.util.Collections;\nimport java.util.Comparator;\nimport java.util.Arrays;\nclass Q707984 {\n\tpublic static void main(String[] args) {\n\t\tint award = 100;\n\t\tString[] people = { \"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\" };\n\t\tInteger[] guess = { 75, 70, 80, 120, 100, 110, 100, 45 };\n\t\tInteger[] ordered = new Integer[people.length];\n\t\tfor (int i = 0; i < ordered.length; i++)\n\t\t\tordered[i] = i;\n\t\tArrays.sort(ordered, new Comparator<Integer>() {\n\t\t\t@Override\n\t\t\tpublic int compare(Integer a, Integer b) {\n\t\t\t\tint x = guess[a] - award > 0 ? guess[a] - award : award - guess[a];\n\t\t\t\tint y = guess[b] - award > 0 ? guess[b] - award : award - guess[b];\n\t\t\t\treturn x - y;\n\t\t\t}\n\t\t});\n\t\tint maxp = 0;\n\t\tint i = 0;\n\t\twhile (guess[ordered[i++]] == award)\n\t\t\tmaxp++;\n\t\tif (maxp <= 1)\n\t\t\tSystem.out.println(people[ordered[0]] + \"一人得奖\" + award + \"元。\");\n\t\telse {\n\t\t\tfor (i = 0; i < maxp; i++)\n\t\t\t\tSystem.out.print(people[ordered[i]] + \" \");\n\t\t\tSystem.out.println(\"共同得奖\" + award / (float) (maxp) + \"元。\");\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469975",
"status": 1,
"keywords": "算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/18.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 159444,
"question_title": "自动获取计算机的ip地址和mac地址",
"question_content": "自动获取计算机的ip地址和mac地址",
"difficulty": "简单",
"answer_id": 24936,
"answer_content": "\n\n```\n public static String getMacAddressIP(String remotePcIP) {\n String str = \"\";\n String macAddress = \"\";\n try {\n Process pp = Runtime.getRuntime().exec(\"nbtstat -A \" + remotePcIP);\n InputStreamReader ir = new InputStreamReader(pp.getInputStream());\n LineNumberReader input = new LineNumberReader(ir);\n for (int i = 1; i < 100; i++) {\n str = input.readLine();\n if (str != null) {\n if (str.indexOf(\"MAC Address\") > 1) {\n macAddress = str.substring(\n str.indexOf(\"MAC Address\") + 14, str.length());\n break;\n }\n }\n }\n } catch (IOException ex) {\n }\n return macAddress;\n }\n```\n\n",
"tag_name": "java",
"java": "import java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.LineNumberReader;\nclass java_15944{\n public static String getMacAddressIP(String remotePcIP) {\n String str = \"\";\n String macAddress = \"\";\n try {\n Process pp = Runtime.getRuntime().exec(\"nbtstat -A \" + remotePcIP);\n InputStreamReader ir = new InputStreamReader(pp.getInputStream());\n LineNumberReader input = new LineNumberReader(ir);\n for (int i = 1; i < 100; i++) {\n\tstr = input.readLine();\n\tif (str != null) {\n\t if (str.indexOf(\"MAC Address\") > 1) {\n\t macAddress = str.substring(\n\t\tstr.indexOf(\"MAC Address\") + 14, str.length());\n\t break;\n\t }\n\t}\n }\n } catch (IOException ex) {\n }\n return macAddress;\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600469874",
"status": 0,
"keywords": "ip地址,mac地址",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/19.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7412958,
"question_title": "打印1000以内的所有素数,并从键盘输入一个正整数,判断是否为素数。",
"question_content": "<p>1、主类内至少两个方法&#xff0c;boolean prime(int p)方法和main()方法&#xff0c;prime方法判断参数p是否为素数。 \n2、1000以内的素数放入一个数组再打印。 \n3、单行风格,良好可读性,运行结果正确。</p>",
"difficulty": "简单",
"answer_id": 53364537,
"answer_content": "<pre>\n<code>public class Main {\n public static void main(String[] args) {\n List&lt;Integer&gt; numList &#61; new ArrayList&lt;&gt;();\n for(int i&#61; 2;i&lt;&#61;1000;i&#43;&#43;){\n for(int j&#61;2;j&lt;&#61;i;j&#43;&#43;){\n if(i%j&#61;&#61;0&amp;&amp;j!&#61;i){\n break;\n }else if(j&#61;&#61;i){\n numList.add(i);\n }\n }\n }\n System.out.println(&#34;1000以内的素数&#34;);\n for(Integer integer : numList){\n System.out.print(integer&#43;&#34; &#34;);\n }\n System.out.println();\n Scanner scanner &#61; new Scanner(System.in);\n int i &#61; scanner.nextInt();\n function function &#61; new function();\n System.out.println(function.prime(i));\n }\n\n}\nclass function{\n public boolean prime(int p){\n for(int i &#61; 2;i&lt;&#61;(p/2);i&#43;&#43;){\n if(p%i&#61;&#61;0&amp;&amp;p!&#61;i){\n return false;\n }\n }\n return true;\n }\n}</code></pre>\n\n<p> </p>",
"tag_name": "java",
"java": "import java.util.*;\npublic class Main {\n\tpublic static void main(String[] args) {\n\t\tList<Integer> numList = new ArrayList<>();\n\t\tfor(int i= 2;i<=1000;i++){\n\t\t\tfor(int j=2;j<=i;j++){\n\t\t\t\tif(i%j==0&&j!=i){\n\t\t\t\t\tbreak;\n\t\t\t\t}else if(j==i){\n\t\t\t\t\tnumList.add(i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(\"1000以内的素数\");\n\t\tfor(Integer integer : numList){\n\t\t\tSystem.out.print(integer+\" \");\n\t\t}\n\t\tSystem.out.println();\n\t\tScanner scanner = new Scanner(System.in);\n\t\tint i = scanner.nextInt();\n\t\tfunction function = new function();\n\t\tSystem.out.println(function.prime(i));\n\t}\n}\nclass function{\n\tpublic boolean prime(int p){\n\t\tfor(int i = 2;i<=(p/2);i++){\n\t\t\tif(p%i==0&&p!=i){\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469870",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/2.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7453032,
"question_title": "按要求编写User类",
"question_content": "<p>编写User类&#xff0c;要求具有私有属性&#xff1a;用户名&#xff08;account&#xff09;和密码&#xff08;password&#xff09;&#xff1b;具有行为&#xff1a;密码的setPassword()和getPassword()方法。编写一个测试类&#xff0c;要求实例化一个User用户&#xff0c;并且使用构造方法对其进行初始化&#xff0c;输出用户密码信息。</p>",
"difficulty": "简单",
"answer_id": 53435568,
"answer_content": "<p>例子代码&#xff1a;</p>\n\n<pre>\n<code class=\"language-java\">public class User {\n private String account;\n private String password;\n\n public String getAccount() {\n return account;\n }\n\n public void setAccount(String account) {\n this.account &#61; account;\n }\n\n public String getPassword() {\n return password;\n }\n\n public void setPassword(String password) {\n this.password &#61; password;\n }\n\n public User() {\n }\n\n public User(String account, String password) {\n this.account &#61; account;\n this.password &#61; password;\n }\n //登录方法\n public void login(String account,String password){\n if(account&#61;&#61;&#34;admin&#34;&amp;&amp;password&#61;&#61;&#34;123456&#34;){\n System.out.println(&#34;登录成功&#34;);\n }else {\n System.out.println(&#34;账号密码不对&#34;);\n }\n }\n\n public static void main(String[] args) {\n User user&#61;new User();\n user.login(&#34;admin&#34;,&#34;123456&#34;);\n }\n}\n</code></pre>\n\n<p>运行结果如图&#xff1a;</p>\n\n<p><img alt=\"\" height=\"516\" src=\"https://img-ask.csdnimg.cn/upload/1624019375117.png\" width=\"690\" /></p>\n",
"tag_name": "java",
"java": "public class User {\n\tprivate String account;\n\tprivate String password;\n\tpublic String getAccount() {\n\t\treturn account;\n\t}\n\tpublic void setAccount(String account) {\n\t\tthis.account = account;\n\t}\n\tpublic String getPassword() {\n\t\treturn password;\n\t}\n\tpublic void setPassword(String password) {\n\t\tthis.password = password;\n\t}\n\tpublic User() {\n\t}\n\tpublic User(String account, String password) {\n\t\tthis.account = account;\n\t\tthis.password = password;\n\t}\n\tpublic void login(String account, String password) {\n\t\tif (account == \"admin\" && password == \"123456\") {\n\t\t\tSystem.out.println(\"登录成功\");\n\t\t} else {\n\t\t\tSystem.out.println(\"账号密码不对\");\n\t\t}\n\t}\n\tpublic static void main(String[] args) {\n\t\tUser user = new User();\n\t\tuser.login(\"admin\", \"123456\");\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470285",
"status": 0,
"keywords": "算法高阶,基本方法,高级数据结构,van Emde Boas树",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/20.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 214128,
"question_title": "素数判断",
"question_content": "判断任意整数是否是素数",
"difficulty": "简单",
"answer_id": 157880,
"answer_content": "public class Num{\npublic static void main(String args[]){\n int m;\n boolean tag=true;\n System.out.print(\"Please enter a num:\");\n try{\n m=(int)System.in.read();\n for(int i=m-1;i>1;i--)\n if(m%i==0)\n tag=false;\n if(tag==true)\n System.out.println(m+\"is a sushu\");\n }catch(IOExceptimn e){}\n}\n}",
"tag_name": "java",
"java": "import java.io.IOException;\npublic class Num{\npublic static void main(String args[]){\n int m;\n boolean tag=true;\n System.out.print(\"Please enter a num:\");\n try{\n\tm=(int)System.in.read();\n\tfor(int i=m-1;i>1;i--)\n\t if(m%i==0)\n\t\t tag=false;\n\tif(tag==true)\n\t System.out.println(m+\"is a sushu\");\n }catch(IOException e){}\n}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470286",
"status": 0,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/21.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7436836,
"question_title": "数组元素统计",
"question_content": "<p>定义一个长度为5的数组arr1&#xff0c;用于存放5个1~9的随机整数&#xff08;范围包含1和9&#xff09;&#xff0c;再定义一个长度为2的数组arr2&#xff0c;统计arr1中的元素对2求余等于0的个数,保存到arr2[0], 统计arr1中的元素对3求余等于0的个数,保存到arr2[1],在控制台打印输出arr2的所有元素</p>",
"difficulty": "简单",
"answer_id": 53409011,
"answer_content": "<pre>\n<code class=\"language-java\">package com.auskat.demo.cc;\n\nimport java.util.Random;\n\n/**\n * 类文件: RandomTest\n * &lt;p&gt;\n * &lt;p&gt;\n * 类描述&#xff1a;\n * &lt;p&gt;\n * 作 者&#xff1a; AusKa_T\n * &lt;p&gt;\n * 日 期&#xff1a; 2021/5/27 0027\n * &lt;p&gt;\n * 时 间&#xff1a; 13:29\n * &lt;p&gt;\n */\npublic class RandomTest {\n\n public static void main(String[] args) {\n\n int arr1[] &#61; new int[5];\n for (int i &#61; 0; i &lt; arr1.length; i&#43;&#43;) {\n arr1[i] &#61; new Random().nextInt(9) &#43; 1;\n }\n\n\n int i2 &#61; 0;\n int j3 &#61; 0;\n for (int i &#61; 0; i &lt; arr1.length; i&#43;&#43;) {\n if(arr1[i] % 2 &#61;&#61; 0) {\n i2&#43;&#43;;\n }\n if(arr1[i] % 3 &#61;&#61; 0) {\n j3&#43;&#43;;\n }\n }\n\n int arr2[] &#61; new int[2];\n arr2[0] &#61; i2;\n arr2[1] &#61; j3;\n\n for (int i &#61; 0; i &lt; arr2.length; i&#43;&#43;) {\n System.out.println(arr2[i]);\n }\n\n }\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "package com.auskat.demo.cc;\nimport java.util.Random;\npublic class RandomTest {\n\tpublic static void main(String[] args) {\n\t\tint arr1[] = new int[5];\n\t\tfor (int i = 0; i < arr1.length; i++) {\n\t\t\tarr1[i] = new Random().nextInt(9) + 1;\n\t\t}\n\t\tint i2 = 0;\n\t\tint j3 = 0;\n\t\tfor (int i = 0; i < arr1.length; i++) {\n\t\t\tif (arr1[i] % 2 == 0) {\n\t\t\t\ti2++;\n\t\t\t}\n\t\t\tif (arr1[i] % 3 == 0) {\n\t\t\t\tj3++;\n\t\t\t}\n\t\t}\n\t\tint arr2[] = new int[2];\n\t\tarr2[0] = i2;\n\t\tarr2[1] = j3;\n\t\tfor (int i = 0; i < arr2.length; i++) {\n\t\t\tSystem.out.println(arr2[i]);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469875",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/22.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 159655,
"question_title": "买蛋",
"question_content": "100元怎么买100个蛋,鸡蛋1毛一个,鸭蛋3元一个,鹅蛋6元一个",
"difficulty": "简单",
"answer_id": 25451,
"answer_content": "```\n public class Egg{\n\tpublic static void main(String[] args){\n\t\tint chicken=0,duck=0,goose=0;\n\t\tfor(int i=0;i<100;i++){\n\t\t\tchicken = i;\n\t\t\t\tfor(int j=0;j<100;j++){\n\t\t\t\t\tduck = j;\n\t\t\t\t\tgoose = 100-duck-chicken;\n\t\t\t\t\tif(chicken+duck*30+goose*60==1000&&chicken>0&&duck>0&&goose>0){\t\n\t\t\t\t\t\tSystem.out.println(\"鸡:\"+chicken+\" 鸭:\"+duck+\" 鹅:\"+goose);\n\t\t\t\t\t}\t\t\t\t\t\n\t\t\t\t}\n\t\t}\t\t\t\t\n\t}\t\n}\n```\n\n",
"tag_name": "java",
"java": "public class Egg {\n\tpublic static void main(String[] args) {\n\t\tint chicken = 0, duck = 0, goose = 0;\n\t\tfor (int i = 0; i < 100; i++) {\n\t\t\tchicken = i;\n\t\t\tfor (int j = 0; j < 100; j++) {\n\t\t\t\tduck = j;\n\t\t\t\tgoose = 100 - duck - chicken;\n\t\t\t\tif (chicken + duck * 30 + goose * 60 == 1000 && chicken > 0 && duck > 0 && goose > 0) {\n\t\t\t\t\tSystem.out.println(\"鸡:\" + chicken + \" 鸭:\" + duck + \" 鹅:\" + goose);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469876",
"status": 1,
"keywords": "算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/23.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 714578,
"question_title": "计算阶乘的和",
"question_content": "编写程序计算1!+2!+3!+...+n!,并输出计算结果。(要求:n从键盘输入,0<=n<=50,如果输入的n值不在此范围,提示再次输入)",
"difficulty": "简单",
"answer_id": 706569,
"answer_content": "`如果问题得到解决,请点我回答左上角的采纳,谢谢`\n\n```\n/* package whatever; // don't place package name! */\n\nimport java.util.*;\nimport java.lang.*;\nimport java.io.*;\n\n/* Name of the class has to be \"Main\" only if the class is public. */\nclass Ideone\n{\n\tpublic static double p(int x)\n\t{\n\t\tdouble r = 1.0;\n\t\tfor (int i = 2; i <= x; i++) r *= i;\n\t\treturn r;\n\t}\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\t// your code goes here\n\t\tScanner s = new Scanner(System.in);\n\t\tint n = s.nextInt();\n\t\tif (n > 50 || n < 0)\n\t\t{\n\t\t\tSystem.out.println(\"不在范围!\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdouble d = 0;\n\t\t\tfor (int i = 1; i <= n; i++)\n\t\t\t\td += p(i);\n\t\t\tSystem.out.println(\"结果=\" + d);\n\t\t}\n\t}\n}\n```\n输入4\n输出结果=33.0\n在线验证\nhttps://ideone.com/ylntIF\n",
"tag_name": "java",
"java": "import java.util.*;\nimport java.lang.*;\nimport java.io.*;\nclass Ideone {\n\tpublic static double p(int x) {\n\t\tdouble r = 1.0;\n\t\tfor (int i = 2; i <= x; i++)\n\t\t\tr *= i;\n\t\treturn r;\n\t}\n\tpublic static void main(String[] args) throws java.lang.Exception {\n\t\tScanner s = new Scanner(System.in);\n\t\tint n = s.nextInt();\n\t\tif (n > 50 || n < 0) {\n\t\t\tSystem.out.println(\"不在范围!\");\n\t\t} else {\n\t\t\tdouble d = 0;\n\t\t\tfor (int i = 1; i <= n; i++)\n\t\t\t\td += p(i);\n\t\t\tSystem.out.println(\"结果=\" + d);\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469976",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/24.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 234859,
"question_title": "用0到9生成十位数的所有排列组合",
"question_content": "用 0到9 生成 十位数的所有排列组合,数字0不能在第一个,这个生成的十位数,不能有重复的数字。\n",
"difficulty": "简单",
"answer_id": 212594,
"answer_content": "\n\n```\n public static void main(String[] args) {\n\t\tString str[] = { \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\" };\n\t\tpermutation(str, 0, str.length);\n\t}\n\n\tstatic void swap(String[] str, int start, int end) {\n\t\tString tmep = str[start];\n\t\tstr[start] = str[end];\n\t\tstr[end] = tmep;\n\t}\n\n\tstatic void permutation(String[] str, int start, int end) {\n\n\t\tif (start == end - 1) {\n\t\t\tfor (int i = 0; i < end; i++) {\n\t\t\t\tSystem.out.print(str[i]);\n\t\t\t}\n\t\t\tSystem.out.println();\n\t\t} else {\n\n\t\t\tfor (int i = start; i < end; i++) {\n\t\t\t\tif (i == 0 && str[0].equals(\"0\"))\n\t\t\t\t\tcontinue;\n\t\t\t\tswap(str, start, i);\n\t\t\t\tpermutation(str, start + 1, end);\n\n\t\t\t\tswap(str, start, i);\n\t\t\t}\n\t\t}\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": "class java_234859 {\n public static void main(String[] args) {\n\t String str[] = { \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\" };\n\t permutation(str, 0, str.length);\n }\n static void swap(String[] str, int start, int end) {\n\t String tmep = str[start];\n\t str[start] = str[end];\n\t str[end] = tmep;\n }\n static void permutation(String[] str, int start, int end) {\n\t if (start == end - 1) {\n\t\t for (int i = 0; i < end; i++) {\n\t\t\tSystem.out.print(str[i]);\n\t\t }\n\t\t System.out.println();\n\t } else {\n\t\t for (int i = start; i < end; i++) {\n\t\t\tif (i == 0 && str[0].equals(\"0\"))\n\t\t\t continue;\n\t\t\tswap(str, start, i);\n\t\t\tpermutation(str, start + 1, end);\n\t\t\tswap(str, start, i);\n\t\t }\n\t }\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600470287",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/25.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1056822,
"question_title": "统计022022的个数",
"question_content": "一组数里面是0,0,0,1,0,0,2,2,0,2,2,0,0,3,0,0,0,0 (0,2,2,0,2,2就算一个室早三联)如果022022是连续出现的话也只算一个比如022022022022这样也只算一个不连续的022022才++,统计022022的个数,",
"difficulty": "中等",
"answer_id": 1253873,
"answer_content": "\n```\npackage com.lee.study;\n\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class Study20 {\n public static int getAmount(int[] numbers) {\n\n Pattern p = Pattern.compile(\"(?<!022022)022022(?!022022)\");\n\n StringBuilder stringBuilder = new StringBuilder(numbers.length);\n\n for (int number : numbers) {\n stringBuilder.append(number);\n }\n\n Matcher m = p.matcher(stringBuilder.toString());\n\n int counter = 0;\n\n while (m.find()) {\n counter++;\n }\n\n return counter;\n }\n\n public static void main(String[] args) {\n int[] numbers = {0, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2,0, 2, 2, 0, 2, 2, 0, 0, 3, 0, 0, 0, 0};\n System.out.println(\"应该输出1\");\n System.out.println(getAmount(numbers));\n\n }\n}\n\n```\n如果采纳,希望给个赞哦!@@",
"tag_name": "java",
"java": "package com.lee.study;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\npublic class Study20 {\n\tpublic static int getAmount(int[] numbers) {\n\t\tPattern p = Pattern.compile(\"(?<!022022)022022(?!022022)\");\n\t\tStringBuilder stringBuilder = new StringBuilder(numbers.length);\n\t\tfor (int number : numbers) {\n\t\t\tstringBuilder.append(number);\n\t\t}\n\t\tMatcher m = p.matcher(stringBuilder.toString());\n\t\tint counter = 0;\n\t\twhile (m.find()) {\n\t\t\tcounter++;\n\t\t}\n\t\treturn counter;\n\t}\n\tpublic static void main(String[] args) {\n\t\tint[] numbers = { 0, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2, 0, 2, 2, 0, 2, 2, 0, 0, 3, 0, 0, 0, 0 };\n\t\tSystem.out.println(\"应该输出1\");\n\t\tSystem.out.println(getAmount(numbers));\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470191",
"status": 0,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/26.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 673486,
"question_title": "逆序输出",
"question_content": "如:abcd1234,逆序输出:4321dcba",
"difficulty": "简单",
"answer_id": 501515,
"answer_content": "可以这样:\n\n\n\n```\nimport java.lang.*;\nimport java.io.*;\nimport java.util.*;\n \nclass ReverseString\n{\n public static void main(String[] args)\n {\n String input = \"abcd1234\";\n \n // convert String to character array\n // by using toCharArray\n char[] try1 = input.toCharArray();\n \n for (int i = try1.length-1; i>=0; i--)\n System.out.print(try1[i]);\n }\n}\n```\n**如果对您有帮助,请点击采纳答案好吗,谢谢~~**\n",
"tag_name": "java",
"java": "import java.lang.*;\nimport java.io.*;\nimport java.util.*;\nclass ReverseString {\n\tpublic static void main(String[] args) {\n\t\tString input = \"abcd1234\";\n\t\tchar[] try1 = input.toCharArray();\n\t\tfor (int i = try1.length - 1; i >= 0; i--)\n\t\t\tSystem.out.print(try1[i]);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470192",
"status": 1,
"keywords": "排序",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/27.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7451564,
"question_title": "判断偶数",
"question_content": "<p>判断偶数。定义一个长度为10的整型数组&#xff0c;随机产生10个100之内的整数&#xff0c;然后判断这个数组中有几个偶数&#xff0c;并输出偶数个数及所有偶数。</p>",
"difficulty": "简单",
"answer_id": 53432874,
"answer_content": "<p>我给你写下&#xff0c;稍等。</p>\n\n<pre>\n<code>public static void main(String args[]){\n int arr[] &#61; new int[10];\n int count &#61; 0;\n System.out.println(&#34;随机产生10个整数&#xff1a;&#34;);\n for(int i &#61; 0; i &lt; 10; i&#43;&#43;){\n arr[i] &#61;(int)(Math.random() * 100 &#43; 1);\n }\n for(int i &#61; 0; i &lt; arr.length; i&#43;&#43;){\n if(i % 2 &#61;&#61; 0) {\n System.out.println(i);\n count&#43;&#43;;\n }\n }\n System.out.println(&#34;偶数个数&#xff1a;&#34; &#43; count);\n }</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "class java_7451564 {\n\tpublic static void main(String args[]) {\n\t\tint arr[] = new int[10];\n\t\tint count = 0;\n\t\tSystem.out.println(\"随机产生10个整数:\");\n\t\tfor (int i = 0; i < 10; i++) {\n\t\t\tarr[i] = (int) (Math.random() * 100 + 1);\n\t\t}\n\t\tfor (int i = 0; i < arr.length; i++) {\n\t\t\tif (arr[i] % 2 == 0) {\n\t\t\t\tSystem.out.println(arr[i]);\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(\"偶数个数:\" + count);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470288",
"status": 0,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/28.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7445932,
"question_title": "按以下要求实现程序功能",
"question_content": "<p>从键盘输入5个整型值\n1&#xff09;按从大到小顺序排序方法&#xff1b;\n2&#xff09;计算这些数的平均值的方法&#xff1b;\n3&#xff09;在主方法中调用这些方法&#xff0c;并输出相应的值。</p>",
"difficulty": "简单",
"answer_id": 53425337,
"answer_content": "<pre>\n<code> public static void main(String[] args) {\n List&lt;Integer&gt; list &#61; new ArrayList&lt;&gt;();\n Scanner scanner &#61; new Scanner(System.in);\n for (int a &#61; 1; a &lt; 6; a&#43;&#43;){\n System.out.print(&#34;请输入第 &#34; &#43; a &#43; &#34; 个值&#xff1a;&#34;);\n list.add(scanner.nextInt());\n }\n System.out.println(descending(list));\n System.out.println(getAvg(list));\n\n }\n // 降序\n public static List&lt;Integer&gt; descending(List&lt;Integer&gt; list){\n list.sort(Collections.reverseOrder());\n return list;\n }\n // 平均值\n public static Double getAvg(List&lt;Integer&gt; list){\n return list.stream().mapToInt(Integer::new).average().getAsDouble();\n }</code></pre>\n",
"tag_name": "java",
"java": "import java.util.*;\nclass java_7445932 {\n\tpublic static void main(String[] args) {\n\t\tList<Integer> list = new ArrayList<>();\n\t\tScanner scanner = new Scanner(System.in);\n\t\tfor (int a = 1; a < 6; a++) {\n\t\t\tSystem.out.print(\"请输入第 \" + a + \" 个值:\");\n\t\t\tlist.add(scanner.nextInt());\n\t\t}\n\t\tSystem.out.println(descending(list));\n\t\tSystem.out.println(getAvg(list));\n\t}\n\tpublic static List<Integer> descending(List<Integer> list) {\n\t\tlist.sort(Collections.reverseOrder());\n\t\treturn list;\n\t}\n\tpublic static Double getAvg(List<Integer> list) {\n\t\treturn list.stream().mapToInt(Integer::new).average().getAsDouble();\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469877",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/29.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 357568,
"question_title": "计算距离下个生日还有多少天",
"question_content": "怎么写出出生年月日来计算距离下个生日还有多少天的方法?\n出生年月日的格式是yyyy-MM-dd。\n返回值为String类型",
"difficulty": "简单",
"answer_id": 374359,
"answer_content": "// 用来计算两个日期之间相差的毫秒数\n public static void test(String date1,String date2) throws ParseException\n {\n DateFormat df = DateFormat.getDateInstance();\n df= new SimpleDateFormat(\"yyyy-MM-dd\");\n \n Date date_1 = df.parse(date1);\n Date date_2 = df.parse(date2);\n \n long time1 = date_1.getTime();\n long time2 = date_2.getTime();\n long time3 = Math.abs(time1-time2);\n \n int day = getDay(time3);\n System.out.println(\"两日期相隔\"+day+\"\");\n }\n//将两个日期之间的毫秒数换算成天数\n private static int getDay(long time)\n {\n int day = (int)(time/1000/60/60/24);\n return day;\n }",
"tag_name": "java",
"java": "import java.sql.Date;\nimport java.text.DateFormat;\nimport java.text.ParseException;\nimport java.text.SimpleDateFormat;\nclass java_357568{\n public static void test(String date1,String date2) throws ParseException\n {\n DateFormat df = DateFormat.getDateInstance();\n df= new SimpleDateFormat(\"yyyy-MM-dd\");\n Date date_1 = (Date) df.parse(date1);\n Date date_2 = (Date) df.parse(date2);\n long time1 = date_1.getTime();\n long time2 = date_2.getTime();\n long time3 = Math.abs(time1-time2);\n int day = getDay(time3);\n\t System.out.println(\"两日期相隔\"+day+\"\");\n }\n private static int getDay(long time)\n {\n int day = (int)(time/1000/60/60/24);\n return day;\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600470185",
"status": 0,
"keywords": "算法初阶,基础知识,生日悖论,概率分析和随机算法,概率分析和指示器随机变量的进一步使用",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/3.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 701664,
"question_title": "外出采摘的日本人",
"question_content": "二战后的某一天,N个日本人来到了一个山洞休息,为了派出一个日本人去外面充满危险的丛林中采摘食物,他们设置如下游戏产生外出采摘的人: \n1、首先,所有参加游戏的日本人按顺序编号为1、2、3…N; \n2、接下来每个日本人心里产生一个数字,这个数字称为序号为 N的人的密码P; \n3、所有参加游戏的人按照编号站成一个圈,长老为游戏设置初始密码K,从编号为 1的人开始报数,报到 K的人退出队伍,然后将自己心中的密码P说出来,由下一个人继续从 1开始报数,报到P的人退出队伍,以此类推; \n4、当队伍中剩下一个人的时候,这个人就是今天要出去采摘的日本人,他可能回不来了! \n请各位同学设计程序并使用Java语言实现改程序,在用户输入了人数N、每个人的密码P和初始密码K的情况下,自动完成上面的选择过程,输出先后离开队伍的人的序号序列,最后输出要去采摘的日本人,输出他的编号。",
"difficulty": "中等",
"answer_id": 591605,
"answer_content": "```\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.Scanner;\n\npublic class JosephCircle {\n\n public int newJoseph(int n, int[] p, int k) {\n List<Integer> num = new ArrayList<Integer>();\n for( int i = 0; i < n; i++ ) num.add(i);\n\n int t = k, index = 0;\n for( int i = 0; i < n - 1; i++ ) {\n index = ( index + t ) % num.size();\n num.remove(index);\n t = p[index];\n if(index == num.size()) index=0;\n }\n return num.get(0)+1;\n }\n\n public int oldJoseph(int n, int k){\n int w = 0;\n for( int i = 2; i <= n; i++ ){\n w = (w+k)%i;\n }\n return w;\n }\n\n public static void main(String[] args) {\n JosephCircle jc = new JosephCircle();\n Scanner scan = new Scanner(System.in);\n\n int n,k;\n int[] p = new int[100];\n System.out.print(\"Enter N = \");n = scan.nextInt();\n System.out.print(\"Enter P[] = \");\n for(int i = 0; i < n; i++){\n p[i] = scan.nextInt();\n }\n System.out.print(\"Enter K = \");k = scan.nextInt();\n System.out.println();\n System.out.println(\"Survivor is No.\" + jc.newJoseph(n,p,k));\n }\n}\n\n```\n\n",
"tag_name": "java",
"java": "import java.util.ArrayList;\nimport java.util.List;\nimport java.util.Scanner;\npublic class JosephCircle {\n\tpublic int newJoseph(int n, int[] p, int k) {\n\t\tList<Integer> num = new ArrayList<Integer>();\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tnum.add(i);\n\t\tint t = k, index = 0;\n\t\tfor (int i = 0; i < n - 1; i++) {\n\t\t\tindex = (index + t) % num.size();\n\t\t\tnum.remove(index);\n\t\t\tt = p[index];\n\t\t\tif (index == num.size())\n\t\t\t\tindex = 0;\n\t\t}\n\t\treturn num.get(0) + 1;\n\t}\n\tpublic int oldJoseph(int n, int k) {\n\t\tint w = 0;\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tw = (w + k) % i;\n\t\t}\n\t\treturn w;\n\t}\n\tpublic static void main(String[] args) {\n\t\tJosephCircle jc = new JosephCircle();\n\t\tScanner scan = new Scanner(System.in);\n\t\tint n, k;\n\t\tint[] p = new int[100];\n\t\tSystem.out.print(\"Enter N = \");\n\t\tn = scan.nextInt();\n\t\tSystem.out.print(\"Enter P[] = \");\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tp[i] = scan.nextInt();\n\t\t}\n\t\tSystem.out.print(\"Enter K = \");\n\t\tk = scan.nextInt();\n\t\tSystem.out.println();\n\t\tSystem.out.println(\"Survivor is No.\" + jc.newJoseph(n, p, k));\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469977",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量,最坏情况为线性时间的选择算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/30.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1058612,
"question_title": "求素数",
"question_content": "求1-100内的素数",
"difficulty": "简单",
"answer_id": 1255940,
"answer_content": "如楼上所说到平方根即可,性能更好,可以参考下:https://blog.csdn.net/qq_42889294/article/details/86578832\n\n```\npublic static void main(String[] args){\n\n for(int i=0;i<100;i++) {\n checkPrime(i);\n }\n \n }\n\n private static void checkPrime(int x){\n boolean isPrime = true;\n if(x ==1 || x %2 ==0 && x !=2 )\n {\n isPrime = false;\n }\n else\n {\n for( int i =3; i< Math.sqrt(x); i+=2)\n {\n if( x % i == 0)\n {\n isPrime = false;\n break;\n }\n }\n }\n if( isPrime)\n {\n System.out.println(x +\"是素数\");\n }\n else\n {\n System.out.println(x+ \"不是素数\");\n }\n }\n\n\n```\n以下是输入20以内返回的结果:\n![图片说明](https://img-ask.csdn.net/upload/202003/14/1584172080_762422.jpg)\n",
"tag_name": "java",
"java": "public static void main(String[] args){\n\t\tfor(int i=0;i<100;i++) {\n\t\t\tcheckPrime(i);\n\t\t}\n\t}\n\tprivate static void checkPrime(int x){\n\t\tboolean isPrime = true;\n\t\tif(x ==1 || x %2 ==0 && x !=2 )\n\t\t{\n\t\t\tisPrime = false;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor( int i =3; i< Math.sqrt(x); i+=2)\n\t\t\t{\n\t\t\t\tif( x % i == 0)\n\t\t\t\t{\n\t\t\t\t\tisPrime = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif( isPrime)\n\t\t{\n\t\t\tSystem.out.println(x +\"是素数\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\tSystem.out.println(x+ \"不是素数\");\n\t\t}\n\t}",
"topic_link": "https://bbs.csdn.net/topics/600470193",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/31.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7431662,
"question_title": "定义一个类Generator",
"question_content": "<p>定义一个类 Generator&#xff08;生成器类&#xff09;&#xff0c;它可以在每次调用其 next()方法时&#xff0c;产生由你 最喜欢的电影&#xff08;如 Snow White 或 Star Wars&#xff09;的字符构成的名字&#xff08;作为 String 对象&#xff09;。在字 符列表中的电影名用完之后&#xff0c;循环到这个字符列表的开始处。使用这个生成器来填充数组、 ArrayList、LinkedList、HashSet&#xff0c;然后打印每一个容器。</p>",
"difficulty": "简单",
"answer_id": 53400496,
"answer_content": "<pre>\n<code>import java.util.*;\npublic class MovieGenerator {\n private String[] movies &#61; new String[] { &#34;SS&#34;, &#34;DD&#34;, &#34;HH&#34;, &#34;FF&#34;, &#34;XX&#34;, &#34;ZZ&#34; };\n private int i &#61; 0;\n public String next() {\n return movies[i&#43;&#43; % movies.length];\n }\n public String[] getMovies() {\n return movies;\n }\n public Collection&lt;String&gt; fill(Collection&lt;String&gt; collection) {\n for (int i &#61; 0; i &lt; 8; i&#43;&#43;) {\n collection.add(next());\n }\n return collection;\n }\n public static void main(String[] args) {\n MovieGenerator generator &#61; new MovieGenerator();\n System.out.println(Arrays.toString(generator.getMovies()));\n System.out.println(generator.fill(new ArrayList&lt;String&gt;()));\n System.out.println(generator.fill(new LinkedList&lt;String&gt;()));\n System.out.println(generator.fill(new HashSet&lt;String&gt;()));\n }\n}</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "import java.util.*;\npublic class MovieGenerator {\n\tprivate String[] movies = new String[] { \"SS\", \"DD\", \"HH\", \"FF\", \"XX\", \"ZZ\" };\n\tprivate int i = 0;\n\tpublic String next() {\n\t\treturn movies[i++ % movies.length];\n\t}\n\tpublic String[] getMovies() {\n\t\treturn movies;\n\t}\n\tpublic Collection<String> fill(Collection<String> collection) {\n\t\tfor (int i = 0; i < 8; i++) {\n\t\t\tcollection.add(next());\n\t\t}\n\t\treturn collection;\n\t}\n\tpublic static void main(String[] args) {\n\t\tMovieGenerator generator = new MovieGenerator();\n\t\tSystem.out.println(Arrays.toString(generator.getMovies()));\n\t\tSystem.out.println(generator.fill(new ArrayList<String>()));\n\t\tSystem.out.println(generator.fill(new LinkedList<String>()));\n\t\tSystem.out.println(generator.fill(new HashSet<String>()));\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469978",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/32.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 243824,
"question_title": "字符串统计",
"question_content": "编写一个程序,对于输入的一段英语文本,可以统计:\n1、该文本中有多少英语单词;\n2、该文本中有多少不同的英语单词。\n如,输入 I am a good student. I am in Zhengzhou.\n则可以统计出有9个英语单词、7个不同的英语单词。",
"difficulty": "简单",
"answer_id": 232730,
"answer_content": "首先,需要对输入信息进行处理,去掉输入的标点符号,并以空格替换,需要用正则表达式,进行全部替换;\n其次,就是对输入单词进行统计,使用字符串的分割函数split(\" \"),以空格分割;\n最后,就是遍历分割结果,进行统计,用Map,以单词为key,出现次数为value。\n实例代码如下:\n```\n import java.util.HashMap;\nimport java.util.Map;\n\npublic class Tee {\n\t/**\n\t * 正则去除所有的英文标点符号\n\t */\n\tpublic static String formatInput(String input) {\n\t\tif (input == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn input.replaceAll(\"[.|;|\\\\?]\", \" \");\n\t}\n\n\tpublic static Map<String, Integer> countWords(String input) {\n\t\tMap<String, Integer> result = new HashMap<String, Integer>();\n\t\tif (input == null || input.length() == 0) {\n\t\t\treturn result;\n\t\t}\n\t\t// 用字符串的分割函数\n\t\tString[] split = input.split(\" \");\n\t\tif (split == null || split.length == 0) {\n\t\t\treturn result;\n\t\t}\n\n\t\t// 统计存入Map,word为key,出现次数为value\n\t\tfor (String value : split) {\n\t\t\tif (result.containsKey(value)) {\n\t\t\t\t// 出现过,直接累计+1\n\t\t\t\tresult.put(value, result.get(value) + 1);\n\t\t\t} else {\n\t\t\t\t// 没出现过存入\n\t\t\t\tresult.put(value, 1);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\n\tpublic static void main(String[] args) {\n\t\tString value = \"I am a good student.I am in Zhengzhou.Ha?\";\n\t\tString format = formatInput(value);\n\t\tSystem.out.println(format);\n\t\tMap<String, Integer> r = countWords(format);\n\t\tSystem.out.println(r.toString());\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.HashMap;\nimport java.util.Map;\npublic class Tee {\n\tpublic static String formatInput(String input) {\n\t\tif (input == null) {\n\t\t\treturn null;\n\t\t}\n\t\treturn input.replaceAll(\"[.|;|\\\\?]\", \" \");\n\t}\n\tpublic static Map<String, Integer> countWords(String input) {\n\t\tMap<String, Integer> result = new HashMap<String, Integer>();\n\t\tif (input == null || input.length() == 0) {\n\t\t\treturn result;\n\t\t}\n\t\tString[] split = input.split(\" \");\n\t\tif (split == null || split.length == 0) {\n\t\t\treturn result;\n\t\t}\n\t\tfor (String value : split) {\n\t\t\tif (result.containsKey(value)) {\n\t\t\t\tresult.put(value, result.get(value) + 1);\n\t\t\t} else {\n\t\t\t\tresult.put(value, 1);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\tpublic static void main(String[] args) {\n\t\tString value = \"I am a good student.I am in Zhengzhou.Ha?\";\n\t\tString format = formatInput(value);\n\t\tSystem.out.println(format);\n\t\tMap<String, Integer> r = countWords(format);\n\t\tSystem.out.println(r.toString());\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469878",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/33.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7455237,
"question_title": "数组排序",
"question_content": "<p>编写一个JavaApplication程序,将随机生成的无序数组使用冒泡排序,将这个混乱的数组变成一个从小到大排列的有序的数组并输出。</p>",
"difficulty": "简单",
"answer_id": 53438050,
"answer_content": "<p>其实就是个冒泡排序呗</p>\n\n<pre>\n<code class=\"language-java\">class demo_sort {\n public static void main(String[] args) {\n //冒泡排序算法\n int[] numbers&#61;new int[]{1,5,8,2,3,9,4};\n //需进行length-1次冒泡\n for(int i&#61;0;i&lt;numbers.length-1;i&#43;&#43;)\n {\n for(int j&#61;0;j&lt;numbers.length-1-i;j&#43;&#43;)\n {\n if(numbers[j]&gt;numbers[j&#43;1])\n {\n int temp&#61;numbers[j];\n numbers[j]&#61;numbers[j&#43;1];\n numbers[j&#43;1]&#61;temp;\n }\n }\n }\n System.out.println(&#34;从小到大排序后的结果是:&#34;);\n for(int i&#61;0;i&lt;numbers.length;i&#43;&#43;)\n System.out.print(numbers[i]&#43;&#34; &#34;);\n }\n }</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "class demo_sort {\n\tpublic static void main(String[] args) {\n\t\tint[] numbers = new int[] { 1, 5, 8, 2, 3, 9, 4 };\n\t\tfor (int i = 0; i < numbers.length - 1; i++) {\n\t\t\tfor (int j = 0; j < numbers.length - 1 - i; j++) {\n\t\t\t\tif (numbers[j] > numbers[j + 1]) {\n\t\t\t\t\tint temp = numbers[j];\n\t\t\t\t\tnumbers[j] = numbers[j + 1];\n\t\t\t\t\tnumbers[j + 1] = temp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(\"从小到大排序后的结果是:\");\n\t\tfor (int i = 0; i < numbers.length; i++)\n\t\t\tSystem.out.print(numbers[i] + \" \");\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470289",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/34.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 717710,
"question_title": "单词反转",
"question_content": "随便输出一个字符串\nString str =\"45abc,+de==fg\";\n里面含有 abc,de,fg 三个单词\n怎么处理能让单词反转,其他顺序不变呢\n输出 “45cba,+ed==gf”;",
"difficulty": "中等",
"answer_id": 711099,
"answer_content": "\n```\npublic class HelloWorld {\n\tpublic static String revstr(String s)\n\t{\n\t\tchar[] ch = s.toCharArray();\n\t\tfor (int i = 0; i < ch.length; i++)\n\t\t{\n\t\t\tif ((ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] >= 'a' && ch[i] <= 'z'))\n\t\t\t{\n\t\t\t\tint j = i + 1;\n\t\t\t\twhile (j < ch.length && ((ch[j] >= 'A' && ch[j] <= 'Z') || (ch[j] >= 'a' && ch[j] <= 'z'))) j++;\n\t\t\t\tj--;\n\t\t\t\tif (i != j)\n\t\t\t\t{\n\t\t\t\t\tfor (int k = i; k <= (j - i) / 2 + i; k++)\n\t\t\t\t\t{\n\t\t\t\t\t\t//System.out.println(\"i\" + i + \"j\" + j + \"k\" + k);\n\t\t\t\t\t\tchar temp = ch[k];\n\t\t\t\t\t\tch[k] = ch[j - k + i];\n\t\t\t\t\t\tch[j - k + i] = temp;\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ti = j;\n\t\t\t}\n\t\t}\n\t\treturn new String(ch);\n\t}\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(revstr(\"45abc,+de==fg\"));\n\t}\n}\n```\n45cba,+ed==gf\n",
"tag_name": "java",
"java": "public class HelloWorld {\n\tpublic static String revstr(String s) {\n\t\tchar[] ch = s.toCharArray();\n\t\tfor (int i = 0; i < ch.length; i++) {\n\t\t\tif ((ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] >= 'a' && ch[i] <= 'z')) {\n\t\t\t\tint j = i + 1;\n\t\t\t\twhile (j < ch.length && ((ch[j] >= 'A' && ch[j] <= 'Z') || (ch[j] >= 'a' && ch[j] <= 'z')))\n\t\t\t\t\tj++;\n\t\t\t\tj--;\n\t\t\t\tif (i != j) {\n\t\t\t\t\tfor (int k = i; k <= (j - i) / 2 + i; k++) {\n\t\t\t\t\t\tchar temp = ch[k];\n\t\t\t\t\t\tch[k] = ch[j - k + i];\n\t\t\t\t\t\tch[j - k + i] = temp;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ti = j;\n\t\t\t}\n\t\t}\n\t\treturn new String(ch);\n\t}\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(revstr(\"45abc,+de==fg\"));\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469879",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/35.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1065837,
"question_title": "多线程问题",
"question_content": "1.程序中需要开启两个线程(线程1和线程2)\n2.线程1固定5秒钟执行一次\n3.线程2固定10秒钟执行一次\n4.开启程序如何做到线程1执行完成后再执行线程2并且在之后无论谁先执行都需等待对方执行完成后才可以开始执行\n",
"difficulty": "中等",
"answer_id": 1265160,
"answer_content": "两个线程在执行操作的时候共享同一把锁就可以保证先后顺序了,demo:\n\n```\npublic class TestThreadJoin {\n\tpublic static void main(String[] args) {\n\t\tfinal Object lock = new Object();\n\t\tRunnable r1 = ()->{\n\t\t\twhile(true) {\n\t\t\t\t// 执行时获取锁\n\t\t\t\tsynchronized(lock) {\n\t\t\t\t\tSystem.out.println(\"线程1执行。\");\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tThread.sleep(5000);\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t\t// TODO Auto-generated catch block\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\t\n\t\tRunnable r2 = ()->{\n\t\t\twhile(true) {\n\t\t\t\t// 执行时获取锁\n\t\t\t\tsynchronized(lock) {\n\t\t\t\t\tSystem.out.println(\"线程2执行。\");\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tThread.sleep(10000);\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t\t// TODO Auto-generated catch block\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t};\n\t\t\n\t\tnew Thread(r1).start();\n\t\tnew Thread(r2).start();\n\t}\n\n}\n```",
"tag_name": "java",
"java": "public class TestThreadJoin {\n\tpublic static void main(String[] args) {\n\t\tfinal Object lock = new Object();\n\t\tRunnable r1 = () -> {\n\t\t\twhile (true) {\n\t\t\t\tsynchronized (lock) {\n\t\t\t\t\tSystem.out.println(\"线程1执行。\");\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tThread.sleep(5000);\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tRunnable r2 = () -> {\n\t\t\twhile (true) {\n\t\t\t\tsynchronized (lock) {\n\t\t\t\t\tSystem.out.println(\"线程2执行。\");\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tThread.sleep(10000);\n\t\t\t\t} catch (InterruptedException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tnew Thread(r1).start();\n\t\tnew Thread(r2).start();\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470290",
"status": 1,
"keywords": "算法高阶,多线程算法,算法问题选编,动态多线程基础",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/36.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 202236,
"question_title": "字符串交错组成",
"question_content": "对于三个字符串A,B,C。我们称C由A和B交错组成当且仅当C包含且仅包含A,B中所有字符,且对应的顺序不改变。请编写一个高效算法,判断C串是否由A和B交错组成。\n给定三个字符串A,B和C,及他们的长度。请返回一个bool值,代表C是否由A和B交错组成。保证三个串的长度均小于等于100。\n测试样例:\n\"ABC\",3,\"12C\",3,\"A12BCC\",6\n返回:true",
"difficulty": "简单",
"answer_id": 126531,
"answer_content": "\n\n```\n import java.util.*;\n \npublic class Mixture {\n public boolean chkMixture(String A, int n, String B, int m, String C, int v) {\n // write code here\n return chkMixture(A,B,C,A.length()-1,B.length()-1,C.length()-1);\n }\n public static boolean chkMixture(String A,String B, String C,int i,int j,int k){\n if(k == 0){\n return true;\n }\n if(i == 0){\n if(B.charAt(j) == C.charAt(k)){\n return chkMixture(A,B,C,0,j-1,k-1);\n }else{\n return false;\n }\n }\n if(j == 0){\n if(A.charAt(i) == C.charAt(k)){\n return chkMixture(A,B,C,i-1,0,k-1);\n }else{\n return false;\n }\n }\n boolean res1 = false;\n boolean res2 = false;\n if(A.charAt(i) == C.charAt(k)){\n res1 = chkMixture(A,B,C,i-1,j,k-1);\n }\n if(B.charAt(j) == C.charAt(k)){\n res2 = chkMixture(A,B,C,i,j-1,k-1);\n }\n return res1||res2;\n }\n}\n```\n\n",
"tag_name": "java",
"java": "public class Mixture {\n\tpublic boolean chkMixture(String A, int n, String B, int m, String C, int v) {\n\t\treturn chkMixture(A,B,C,A.length()-1,B.length()-1,C.length()-1);\n\t}\n\tpublic static boolean chkMixture(String A,String B, String C,int i,int j,int k){\n\t\tif(k == 0){\n\t\t\treturn true;\n\t\t}\n\t\tif(i == 0){\n\t\t\tif(B.charAt(j) == C.charAt(k)){\n\t\t\t\treturn chkMixture(A,B,C,0,j-1,k-1);\n\t\t\t}else{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\tif(j == 0){\n\t\t\tif(A.charAt(i) == C.charAt(k)){\n\t\t\t\treturn chkMixture(A,B,C,i-1,0,k-1);\n\t\t\t}else{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\tboolean res1 = false;\n\t\tboolean res2 = false;\n\t\tif(A.charAt(i) == C.charAt(k)){\n\t\t\tres1 = chkMixture(A,B,C,i-1,j,k-1);\n\t\t}\n\t\tif(B.charAt(j) == C.charAt(k)){\n\t\t\tres2 = chkMixture(A,B,C,i,j-1,k-1);\n\t\t}\n\t\treturn res1||res2;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469979",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,朴素字符串匹配算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/37.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 507751,
"question_title": "怎么样能在一个字符串里判断是否包含数组里的内容?",
"question_content": "例如:\n\nstring a=\"ILOVEU\";\nstring[] F={\"I\",\"L\",\"U\"};\n循环条件?\n if(怎么判断a是否在数组F里)\n {\n 在F里并且提取出来\n }ELSE{不在}\n",
"difficulty": "简单",
"answer_id": 438340,
"answer_content": "\n\n```\n // 判断字符串在数组内\n\tpublic static void stringInArray() {\n\t\tString str = \"HELL\";\n\t\tString[] arr = { \"H\", \"E\", \"L\", \"L\", \"O\" };\n\t\tboolean flag = true;\n\t\tfor (int i = 0; i < str.length() && flag; i++) {\n\t\t\tString item = String.valueOf(str.charAt(i));\n\t\t\tfor (String aa : arr) {\n\t\t\t\tif (aa.equals(item)) {\n\t\t\t\t\tSystem.out.println(item + \"在数组内\");\n\t\t\t\t\tflag = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tflag = false;\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tSystem.out.println(\"字符串在数组内。\");\n\t\t} else {\n\t\t\tSystem.out.println(\"字符串不在数组内。\");\n\t\t}\n\t}\n\n\t// 判断数组在字符串内\n\tpublic static void arrayInString() {\n\t\tString str = \"HELLO\";\n\t\tString[] arr = { \"H\", \"E\", \"L\", \"L\", \"O\" };\n\t\tboolean flag = true;\n\t\tfor (String aa : arr) {\n\t\t\tif (!str.contains(aa)) {\n\t\t\t\tSystem.out.println(\"字符串不包含:\" + aa);\n\t\t\t\tflag = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tSystem.out.println(\"数组在字符串内。\");\n\t\t} else {\n\t\t\tSystem.out.println(\"数组不在字符串内。\");\n\t\t}\n\t}\n```\n感觉这是最笨的方法了。\n",
"tag_name": "java",
"java": "\tpublic static void stringInArray() {\n\t\tString str = \"HELL\";\n\t\tString[] arr = { \"H\", \"E\", \"L\", \"L\", \"O\" };\n\t\tboolean flag = true;\n\t\tfor (int i = 0; i < str.length() && flag; i++) {\n\t\t\tString item = String.valueOf(str.charAt(i));\n\t\t\tfor (String aa : arr) {\n\t\t\t\tif (aa.equals(item)) {\n\t\t\t\t\tSystem.out.println(item + \"在数组内\");\n\t\t\t\t\tflag = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tflag = false;\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tSystem.out.println(\"字符串在数组内。\");\n\t\t} else {\n\t\t\tSystem.out.println(\"字符串不在数组内。\");\n\t\t}\n\t}\n\tpublic static void arrayInString() {\n\t\tString str = \"HELLO\";\n\t\tString[] arr = { \"H\", \"E\", \"L\", \"L\", \"O\" };\n\t\tboolean flag = true;\n\t\tfor (String aa : arr) {\n\t\t\tif (!str.contains(aa)) {\n\t\t\t\tSystem.out.println(\"字符串不包含:\" + aa);\n\t\t\t\tflag = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tSystem.out.println(\"数组在字符串内。\");\n\t\t} else {\n\t\t\tSystem.out.println(\"数组不在字符串内。\");\n\t\t}\n\t}",
"topic_link": "https://bbs.csdn.net/topics/600470194",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/38.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 375089,
"question_title": "替换任何位置的字符串",
"question_content": "字符串形如 \"00??00\", \"0?00?0\", \"0??00?\", 6位定长,'?' 数量和位置都不确定。\n如何将所有的 '?' 都替换成0~9 并输出?\n例如\n输入:\"0?00?0\", \n输出:\"000000\", \"000010\"...\"000090\", \"010000\"...\"010090\"...\"090090\", \n即 第一个为0,第二个从0增加到9;然后第一个为1,第二个从0增加到9....; 第一个为9,第二个从0增加到9。 ",
"difficulty": "中等",
"answer_id": 404427,
"answer_content": "\n**楼主自己看看吧,只实现了部分累加,工作原因,不能全部实现了,逻辑上没有问题了,剩下的交给你了,原创!**\n```\n import java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo1 {\n\n\tpublic static void main(String[] args) {\n\n\t\tStringBuffer str = new StringBuffer(\"?0?0?0\");\n\n\t\tList<Integer> numList = new ArrayList<Integer>();\n\n\t\twhile (true) {\n\n\t\t\tint num = str.indexOf(\"?\");\n\t\t\tif (num == -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tnumList.add(num);\n\t\t\tstr = str.replace(num, num + 1, \"0\");\n\t\t}\n\t\tint j = 0;\n\t\tfor (int i = numList.size() - 1; i >= 0; i--) {\n\t\t\tfor (; j <= 9; j++) {\n\t\t\t\tstr = str.replace(numList.get(i), numList.get(i) + 1, \"\" + j);\n\t\t\t\tSystem.out.println(str);\n\t\t\t\tif (j == 9) {\n\t\t\t\t\tstr = str.replace(numList.get(i), numList.get(i) + 1, \"\" + 0);\n\t\t\t\t\tj=1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo1 {\n\tpublic static void main(String[] args) {\n\t\tStringBuffer str = new StringBuffer(\"0??00?\");\n\t\tList<Integer> numList = new ArrayList<Integer>();\n\t\twhile (true) {\n\t\t\tint num = str.indexOf(\"?\");\n\t\t\tif (num == -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tnumList.add(num);\n\t\t\tstr = str.replace(num, num + 1, \"0\");\n\t\t}\n\t\tint j = 0;\n\t\tfor (int i = numList.size() - 1; i >= 0; i--) {\n\t\t\tfor (; j <= 9; j++) {\n\t\t\t\tstr = str.replace(numList.get(i), numList.get(i) + 1, \"\" + j);\n\t\t\t\tSystem.out.println(str);\n\t\t\t\tif (j == 9) {\n\t\t\t\t\tstr = str.replace(numList.get(i), numList.get(i) + 1, \"\" + 0);\n\t\t\t\t\tj = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470195",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/39.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7463161,
"question_title": "求输入数字的平均值、最大值",
"question_content": "<p>输入若干个数&#xff0c;设输入的第一个数n为后面要输入的数的个数&#xff0c;求平均值及最大值&#xff0c;并在屏幕输出来</p>",
"difficulty": "简单",
"answer_id": 53448940,
"answer_content": "<p>在测试类中&#xff0c;从键盘输入一些数就好了</p>\n\n<pre>\n<code>import java.util.Scanner;\n public class Test{\n public static void main(String[] args) {\n Scanner in&#61;new Scanner(System.in);\n System.out.println(&#34;输入n:&#34;);\n int n&#61;in.nextInt();\n int temp,max&#61;0,sum&#61;0;\n for(int i&#61;0;i&lt;n;i&#43;&#43;){\n temp&#61;in.nextInt();\n if (temp&gt;max){\n max&#61;temp;\n }\n sum&#43;&#61;temp;\n }\n System.out.println(&#34;最大值为&#xff1a;&#34;&#43;max&#43;&#34;,平均值为&#xff1a;&#34;&#43;sum*1.0/n);\n }\n }\n</code></pre>\n\n<p><img alt=\"\" height=\"145\" src=\"https://img-ask.csdnimg.cn/upload/1624968882373.png\" width=\"550\" /></p>\n",
"tag_name": "java",
"java": "import java.util.Scanner;\npublic class Test{\n\tpublic static void main(String[] args) {\n\t\tScanner in=new Scanner(System.in);\n\t\tSystem.out.println(\"输入n:\");\n\t\tint n=in.nextInt();\n\t\tint temp,max=0,sum=0;\n\t\tfor(int i=0;i<n;i++){\n\t\t\ttemp=in.nextInt();\n\t\t\tif (temp>max){\n\t\t\t\tmax=temp;\n\t\t\t}\n\t\t\tsum+=temp;\n\t\t}\n\t\tSystem.out.println(\"最大值为:\"+max+\",平均值为:\"+sum*1.0/n);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469973",
"status": 1,
"keywords": "算法初阶,最小值和最大值,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/4.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1442463,
"question_title": "字符串排序",
"question_content": "<ol><li>将一组乱序的字符进行排序&#xff0c;使其变成有序&#xff08;假设有8个字符并都不相同&#xff09;&#xff0c;然后任意输入一个字符&#xff0c;按字母顺序找到其合适的位置插入到字符序列中。</li></ol>\n<p style=\"margin-left:0cm; margin-right:0cm\">System.out.println(&#34;请输入要插入的字符:&#34;);</p>\n<p style=\"margin-left:0cm; margin-right:0cm\">      char num&#61;input.next().charAt(0);<br />\n如&#xff1a;原始字符序列位O T Y A E V B Z&#xff0c;首先排序输出A B E O T V Y Z&#xff0c;然后输入K&#xff0c;找到K所在的位置并插入&#xff0c;最终结果为 A B E K O T V Y Z</p>",
"difficulty": "中等",
"answer_id": 5034877,
"answer_content": "<pre>\n<code> public static void test(String s, char c) { //冒泡排序\n\n char[] arr &#61; s.toCharArray(); //字符串-&gt;字符串数组\n for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {\n for (int j &#61; i &#43; 1; j &lt; arr.length; j&#43;&#43;) {\n if (arr[i] &gt; arr[j]) {\n char temp &#61; arr[i];\n arr[i] &#61; arr[j];\n arr[j] &#61; temp;\n }\n }\n\n }\n String b &#61; new String(arr);\n System.out.println(&#34;对字符串排序后:&#34; &#43; b);\n StringBuilder sb &#61; new StringBuilder(b);\n if (c &lt;&#61; b.charAt(0)) {\n sb.insert(0, c);\n }\n else if (c &gt;&#61; b.charAt(b.length() - 1)) {\n sb.insert(b.length(), c);\n }else {\n for (int i &#61; 0; i &lt; b.length() - 1; i&#43;&#43;) {\n if (c &gt; b.charAt(i) &amp;&amp; c &lt; b.charAt(i &#43; 1)) {\n sb.insert(i &#43; 1, c);\n break;\n }\n }\n }\n System.out.println(String.format(&#34;插入字符串%c后&#xff1a;%s&#34;, c, sb));\n }\n\n public static void main(String[] args) {\n test(&#34;gdjfgkjryitybdsgsfdgpiop&#34;, &#39;z&#39;);\n test(&#34;gdjfgkjryitytommybdsgsfdgpiop&#34;, &#39;a&#39;);\n test(&#34;uyrgfsdyzwsgkjk&#34;, &#39;h&#39;);\n }</code></pre>\n\n<p>希望对你有帮助</p>",
"tag_name": "java",
"java": "class java_1442463 {\n\tpublic static void test(String s, char c) {\n\t\tchar[] arr = s.toCharArray();\n\t\tfor (int i = 0; i < arr.length; i++) {\n\t\t\tfor (int j = i + 1; j < arr.length; j++) {\n\t\t\t\tif (arr[i] > arr[j]) {\n\t\t\t\t\tchar temp = arr[i];\n\t\t\t\t\tarr[i] = arr[j];\n\t\t\t\t\tarr[j] = temp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tString b = new String(arr);\n\t\tSystem.out.println(\"对字符串排序后:\" + b);\n\t\tStringBuilder sb = new StringBuilder(b);\n\t\tif (c <= b.charAt(0)) {\n\t\t\tsb.insert(0, c);\n\t\t} else if (c >= b.charAt(b.length() - 1)) {\n\t\t\tsb.insert(b.length(), c);\n\t\t} else {\n\t\t\tfor (int i = 0; i < b.length() - 1; i++) {\n\t\t\t\tif (c > b.charAt(i) && c < b.charAt(i + 1)) {\n\t\t\t\t\tsb.insert(i + 1, c);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(String.format(\"插入字符串%c后:%s\", c, sb));\n\t}\n\tpublic static void main(String[] args) {\n\t\ttest(\"gdjfgkjryitybdsgsfdgpiop\", 'z');\n\t\ttest(\"gdjfgkjryitytommybdsgsfdgpiop\", 'a');\n\t\ttest(\"uyrgfsdyzwsgkjk\", 'h');\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470291",
"status": 0,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/40.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 186251,
"question_title": "把ArrayList集合写入txt文件",
"question_content": "如何把ArrayList集合写入txt文件",
"difficulty": "简单",
"answer_id": 79188,
"answer_content": "\n\n```\n import java.io.BufferedWriter;\nimport java.io.File;\nimport java.io.FileWriter;\nimport java.util.ArrayList;\n \npublic class ArrayToTxt {\n public static void main(String[] args) throws Exception{\n File f=new File(\"d:/1.txt\");\n ArrayList<String> al=new ArrayList<String>();\n al.add(\"first\");\n al.add(\"second\");\n al.add(\"third\");\n BufferedWriter bw=new BufferedWriter(new FileWriter(f));\n for(int i=0;i<al.size();i++){\n bw.write(al.get(i));\n bw.newLine();\n }\n bw.close();\n }\n}\n```\n\n",
"tag_name": "java",
"java": " import java.io.BufferedWriter;\nimport java.io.File;\nimport java.io.FileWriter;\nimport java.util.ArrayList;\npublic class ArrayToTxt {\n\tpublic static void main(String[] args) throws Exception{\n\t\tFile f=new File(\"d:/1.txt\");\n\t\tArrayList<String> al=new ArrayList<String>();\n\t\tal.add(\"first\");\n\t\tal.add(\"second\");\n\t\tal.add(\"third\");\n\t\tBufferedWriter bw=new BufferedWriter(new FileWriter(f));\n\t\tfor(int i=0;i<al.size();i++){\n\t\t\tbw.write(al.get(i));\n\t\t\tbw.newLine();\n\t\t}\n\t\tbw.close();\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469880",
"status": 0,
"keywords": "算法高阶,高级数据结构,不相交集合森林,用于不相交集合的数据结构",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/41.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 256052,
"question_title": "找出出现次数最多的字符并计算次数",
"question_content": "给定一个字符串“today is a special day”,长度任意,要求找出其出现次数最多的字符及计算次数。",
"difficulty": "简单",
"answer_id": 260861,
"answer_content": "思路:\n\t1、将当前的字符串转换为字符数组;\n\t2、创建一个HashMap<Character, Integer>,key为每一个字符,value为字符出现的字数;\n\t3、遍历当前的数组,将数组中每一个不重复的字符作为HashMap对象中的key,判断当前的HashMap对象中是否存在当前的key\n\t\t\t1)若存在,取出对应的value,将value值加1后,再保存到对应的key上;\n\t\t\t2)若不存在,则将当前的key值对应的value赋值为1;\n\t4、遍历统计结束的HashMap对象,取出所有key中value最大的对象即可;\n\n代码如下:\n\npublic static void main(String[] args) {\n\t\tString string = \"Thinking in Java\";\n\t\tchar[] ch = string.toCharArray();\n\t\tMap<Character, Integer> result = new HashMap<Character, Integer>();\n\t\tfor (int i = 0; i < ch.length; i++) {\n\t\t\tif (result.containsKey(ch[i])) {\n\t\t\t\tint count = result.get(ch[i]) + 1;\n\t\t\t\tresult.put(ch[i], count);\n\t\t\t} else {\n\t\t\t\tresult.put(ch[i], 1);\n\t\t\t}\n\t\t}\n\t\tCharacter maxChar = null;\n\t\tInteger maxCount = 0;\n\t\tfor (Entry<Character, Integer> entry : result.entrySet()) {\n\t\t\tif (entry.getValue() > maxCount) {\n\t\t\t\tmaxChar = entry.getKey();\n\t\t\t\tmaxCount = entry.getValue();\n\t\t\t}\n\t\t}\n\t\t\n\t\tSystem.out.println(\"字符: \" + maxChar + \" 出现的字数最多,为 \" + maxCount + \"\");\n\t}\n\n希望对你有帮助!",
"tag_name": "java",
"java": "import java.util.HashMap;\nimport java.util.Map;\nclass java_256052 {\n public static void main(String[] args) {\n\t String string = \"Thinking in Java\";\n\t char[] ch = string.toCharArray();\n\t Map<Character, Integer> result = new HashMap<Character, Integer>();\n\t for (int i = 0; i < ch.length; i++) {\n\t\t if (result.containsKey(ch[i])) {\n\t\t\tint count = result.get(ch[i]) + 1;\n\t\t\tresult.put(ch[i], count);\n\t\t } else {\n\t\t\tresult.put(ch[i], 1);\n\t\t }\n\t }\n\t Character maxChar = null;\n\t Integer maxCount = 0;\n\t for (java.util.Map.Entry<Character, Integer> entry : result.entrySet()) {\n\t\t if (entry.getValue() > maxCount) {\n\t\t\tmaxChar = entry.getKey();\n\t\t\tmaxCount = entry.getValue();\n\t\t }\n\t }\n\t System.out.println(\"字符: \" + maxChar + \" 出现的字数最多,为 \" + maxCount + \"\");\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600469881",
"status": 1,
"keywords": "算法高阶,计算几何学,算法问题选编,确定任意一对线段是否相交",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/42.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7415408,
"question_title": "猜数字游戏",
"question_content": "<p>1、猜数字游戏<br />\n一个类A有一个实例变量v&#xff0c;从键盘接收一个正整数作为实例变量v的初始值。<br />\n另外再定义一个类B&#xff0c;对A类的实例变量v进行猜测。<br />\n    如果大了则提示大了<br />\n    小了则提示小了<br />\n    等于则提示猜测成功</p>",
"difficulty": "简单",
"answer_id": 53368860,
"answer_content": "<pre>\n<code class=\"language-java\">import java.util.Random;\nimport java.util.Scanner;\n\n\npublic class Demo {\n\n public static void main(String[] args) {\n // 开始游戏\n System.out.println(&#34;猜数字游戏开始&#34;);\n System.out.println(&#34;输入1-100之间的数据:&#34;);\n\n // 创建Random类变量(获取随机数)\n Random ran &#61; new Random();\n // 创建 0-100 随机数(初始值)\n int v &#61; ran.nextInt(100)&#43;1;\n\n // 创建Scanner类变量(用户输入猜测值)\n Scanner in &#61; new Scanner(System.in);\n\n // 循环猜测\n while(true) {\n // 得到用户输入猜测值\n int b &#61; in.nextInt();\n // 比较用户输入猜测值和随机数(初始值)\n if(b &gt; v) {\n System.out.println(&#34;您猜测得数字较大&#xff0c;请继续猜测&#xff01;&#34;);\n }else if(b &lt; v) {\n System.out.println(&#34;您猜测得数字较小&#xff0c;请继续猜测&#xff01;&#34;);\n } else {\n System.out.println(&#34;恭喜你&#xff0c;猜对了&#xff01;&#34;);\n System.out.println(&#34;游戏结束&#xff01;&#34;);\n \n // 用户输入猜测值和随机数(初始值) 相等时,结束程序\n break;\n }\n }\n }\n}\n</code></pre>\n\n<p> </p>",
"tag_name": "java",
"java": "import java.util.Random;\nimport java.util.Scanner;\npublic class Demo {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"猜数字游戏开始\");\n\t\tSystem.out.println(\"输入1-100之间的数据:\");\n\t\tRandom ran = new Random();\n\t\tint v = ran.nextInt(100) + 1;\n\t\tScanner in = new Scanner(System.in);\n\t\twhile (true) {\n\t\t\tint b = in.nextInt();\n\t\t\tif (b > v) {\n\t\t\t\tSystem.out.println(\"您猜测得数字较大,请继续猜测!\");\n\t\t\t} else if (b < v) {\n\t\t\t\tSystem.out.println(\"您猜测得数字较小,请继续猜测!\");\n\t\t\t} else {\n\t\t\t\tSystem.out.println(\"恭喜你,猜对了!\");\n\t\t\t\tSystem.out.println(\"游戏结束!\");\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470292",
"status": 0,
"keywords": "B树,算法高阶,B树的定义,高级数据结构",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/43.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 339311,
"question_title": "找质数",
"question_content": "找出大于200的最小的质数",
"difficulty": "简单",
"answer_id": 337929,
"answer_content": "\n\n```\n /* package whatever; // don't place package name! */\n\nimport java.util.*;\nimport java.lang.*;\nimport java.io.*;\n\n/* Name of the class has to be \"Main\" only if the class is public. */\nclass Ideone\n{\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\t// your code goes here\n\t\tint n = 201;\n\t\twhile (true)\n\t\t{\n\t\t\tboolean b = true;\n\t\t\tfor (int i = 2; i < n / 2; i++)\n\t\t\t{\n\t\t\t\tif (n % i == 0) b = false;\n\t\t\t}\n\t\t\tif (b) break;\n\t\t\tn++;\n\t\t}\n\t\tSystem.out.println(n);\n\t}\n}\n```\n\n\n\n\n在线验证\nhttp://ideone.com/pVj7iy\n\n211",
"tag_name": "java",
"java": "import java.util.*;\nimport java.lang.*;\nimport java.io.*;\nclass Ideone {\n\tpublic static void main(String[] args) throws java.lang.Exception {\n\t\tint n = 201;\n\t\twhile (true) {\n\t\t\tboolean b = true;\n\t\t\tfor (int i = 2; i < n / 2; i++) {\n\t\t\t\tif (n % i == 0)\n\t\t\t\t\tb = false;\n\t\t\t}\n\t\t\tif (b)\n\t\t\t\tbreak;\n\t\t\tn++;\n\t\t}\n\t\tSystem.out.println(n);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469980",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/44.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7457133,
"question_title": "设计学生类Student和它的一个子类Undergraduate",
"question_content": "<p>设计一个学生类Student和它的一个子类Undergraduate&#xff0c;要求如下&#xff1a;\n(1)Student类有name和age属性&#xff0c;一个包含两个人参数的构造器&#xff0c;用于给两属性赋值&#xff0c;一个show()方法打印Student的属性信息。\n(2)本科生类Undergraduate增加一个degree(学位)属性。有一个包含三参数的构造器&#xff0c;前两个参数用于给集成的属性赋值&#xff0c;第三个参数给degree专业见值&#xff0c;一个show()方法用于打印Undergraduate的属性信息。\n(3)在测试类中分别打印Undergraduate和Student对象&#xff0c;调用它们的show()\n </p>",
"difficulty": "简单",
"answer_id": 53441266,
"answer_content": "<pre>\n<code class=\"language-java\">package T1;\n\npublic class Test {\n\tpublic static void main(String[] args) {\n\t\t// 有参构造\n\t\tStudent stu &#61; new Student(&#34;student&#34;, 100);\n\t\tstu.show();\n\t\tUndergraduate undergraduate &#61; new Undergraduate(&#34;Undergraduate&#34;, 20, &#34;本科&#34;);\n\t\tundergraduate.show();\n\t}\n\n}\n\nclass Student {\n\n\t// 姓名\n\tprivate String name;\n\n\t// 成绩\n\tprivate int age;\n\n\t// 无参构造\n\tpublic Student() {\n\t\tsuper();\n\t}\n\n\t// 全参构造\n\tpublic Student(String name, int age) {\n\t\tthis.name &#61; name;\n\t\tthis.age &#61; age;\n\t}\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name &#61; name;\n\t}\n\n\tpublic double getAge() {\n\t\treturn age;\n\t}\n\n\tpublic void setAge(int age) {\n\t\tthis.age &#61; age;\n\t}\n\n\tpublic void show() {\n\t\tSystem.out.println(&#34;名字&#xff1a;&#34; &#43; this.name &#43; &#34;\\t年龄&#xff1a;&#34; &#43; this.age);\n\t}\n\n}\n\nclass Undergraduate extends Student {\n\tprivate String degree;\n\n\tpublic Undergraduate(String name, int age, String degree) {\n\t\tsuper(name, age);\n\t\tthis.degree &#61; degree;\n\t}\n\n\tpublic void show() {\n\t\tSystem.out.println(&#34;名字&#xff1a;&#34; &#43; super.getName() &#43; &#34;\\t年龄&#xff1a;&#34; &#43; super.getAge() &#43; &#34;\\t学位:&#34; &#43; this.degree);\n\t}\n\n}\n</code></pre>\n\n<p> </p>\n",
"tag_name": "java",
"java": "package T1;\npublic class Test {\n\tpublic static void main(String[] args) {\n\t\tStudent stu = new Student(\"student\", 100);\n\t\tstu.show();\n\t\tUndergraduate undergraduate = new Undergraduate(\"Undergraduate\", 20, \"本科\");\n\t\tundergraduate.show();\n\t}\n}\nclass Student {\n\tprivate String name;\n\tprivate int age;\n\tpublic Student() {\n\t\tsuper();\n\t}\n\tpublic Student(String name, int age) {\n\t\tthis.name = name;\n\t\tthis.age = age;\n\t}\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\tpublic double getAge() {\n\t\treturn age;\n\t}\n\tpublic void setAge(int age) {\n\t\tthis.age = age;\n\t}\n\tpublic void show() {\n\t\tSystem.out.println(\"名字:\" + this.name + \"\\t年龄:\" + this.age);\n\t}\n}\nclass Undergraduate extends Student {\n\tprivate String degree;\n\tpublic Undergraduate(String name, int age, String degree) {\n\t\tsuper(name, age);\n\t\tthis.degree = degree;\n\t}\n\tpublic void show() {\n\t\tSystem.out.println(\"名字:\" + super.getName() + \"\\t年龄:\" + super.getAge() + \"\\t学位:\" + this.degree);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470293",
"status": 1,
"keywords": "算法初阶,基础知识,算法基础,设计算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/45.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 187492,
"question_title": "求素数和",
"question_content": "求第m个到第n个素数之间的素数和",
"difficulty": "中等",
"answer_id": 82090,
"answer_content": "加个变量控制一下就行了。\n\n```\npublic class All {\n\tpublic static void main(String[] args) {\n\t\tint a[] = new int[200];\n\t\tint index = 0;\n\t\t// 将前200个数中的素数提取出来放入数组a中\n\t\tfor (int i = 1; i < 200; i++) {\n\t\t\tboolean isPrime = true;\n\t\t\tfor (int k = 2; k < i; k++) {\n\t\t\t\tif (i % k == 0) {\n\t\t\t\t\tisPrime = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (isPrime) {\n\t\t\t\ta[index++] = i;\n\t\t\t\tSystem.out.println(a[index-1]); // 打印确认\n\t\t\t}\n\t\t}\n\t\t// 输入两个数 m,n 求第m个素数到底n个素数之间的素数和\n\t\tScanner small = new Scanner(System.in);\n\t\tScanner large = new Scanner(System.in);\n\t\tint m = small.nextInt();\n\t\tint n = large.nextInt();\n\t\tint sums = 0;\n\t\tint suml = 0;\n\t\tint sum = 0;\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tsums += a[i];\n\t\t\tSystem.out.print(a[i] + \"*\");\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tsuml += a[i];\n\t\t\tSystem.out.print(a[i] + \" \");\n\t\t}\n\t\tsum = suml - sums;\n\t\tSystem.out.println(sum);\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.Scanner;\npublic class All {\n public static void main(String[] args) {\n\t int a[] = new int[200];\n\t int index = 0;\n\t for (int i = 1; i < 200; i++) {\n\t\t boolean isPrime = true;\n\t\t for (int k = 2; k < i; k++) {\n\t\t\tif (i % k == 0) {\n\t\t\t isPrime = false;\n\t\t\t break;\n\t\t\t}\n\t\t }\n\t\t if (isPrime) {\n\t\t\ta[index++] = i;\n\t\t\tSystem.out.println(a[index - 1]);\n\t\t }\n\t }\n\t Scanner small = new Scanner(System.in);\n\t Scanner large = new Scanner(System.in);\n\t int m = small.nextInt();\n\t int n = large.nextInt();\n\t int sums = 0;\n\t int suml = 0;\n\t int sum = 0;\n\t for (int i = 0; i < m; i++) {\n\t\t sums += a[i];\n\t\t System.out.print(a[i] + \"*\");\n\t }\n\t for (int i = 0; i < n; i++) {\n\t\t suml += a[i];\n\t\t System.out.print(a[i] + \" \");\n\t }\n\t sum = suml - sums;\n\t System.out.println(sum);\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600470196",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/46.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7396323,
"question_title": "String字符串 数字+英文字符组合,如何排序(字母按A-Z,数字按从小到大)",
"question_content": " 举例:\n \n \"A1,A2,A5,A4,A3,B2,B3,B5,B1,B4,C1,C3,C5,C2\"\n \n \n \n 排序后:\n \n \n \"A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,C1,C2,C3,C5\"",
"difficulty": "中等",
"answer_id": 53339978,
"answer_content": "<pre>\n<code class=\"language-java\">import org.apache.commons.lang3.StringUtils;\n\nimport java.util.Arrays;\nimport java.util.Comparator;\n\npublic class Test {\n public static void main(String[] args) {\n String[] str &#61; &#34;A1,A11,A2,A5,A4,A3,B2,B3,B5,B1,B4,C1,C3,C5,C2&#34;.split(&#34;,&#34;);//先分割出来\n //Arrays.sort(str);//从小到大排序&#xff0c;按ACS码值\n //重写比较器&#xff0c;定义怎么比较\n Arrays.sort(str, new Comparator&lt;String&gt;()\n {\n &#64;Override\n public int compare(String o1, String o2) {\n char s1 &#61; o1.charAt(0);//第一个字符\n char s2 &#61; o2.charAt(0);\n if(s1 &#61;&#61; s2){\n Integer i1 &#61; Integer.valueOf(o1.substring(1));//取剩下的字符串&#xff0c;也就是数字部分\n Integer i2 &#61; Integer.valueOf(o2.substring(1));\n return i1.compareTo(i2);\n }else{\n return o1.compareTo(o2);\n }\n }\n });\n String str2 &#61; StringUtils.join(str, &#34;,&#34;);//数组转字符串&#xff08;逗号分隔&#xff09;\n System.out.println(str2);\n }\n}</code></pre>\n\n<p>测试结果&#xff1a;</p>\n\n<p><img alt=\"\" height=\"151\" src=\"https://img-ask.csdnimg.cn/upload/1611114050435.png\" width=\"723\" /></p>",
"tag_name": "java",
"java": "import org.apache.commons.lang3.StringUtils;\nimport java.util.Arrays;\nimport java.util.Comparator;\npublic class Test {\n\tpublic static void main(String[] args) {\n\t\tString[] str = \"A1,A11,A2,A5,A4,A3,B2,B3,B5,B1,B4,C1,C3,C5,C2\".split(\",\");\n\t\tArrays.sort(str, new Comparator<String>()\n\t\t{\n\t\t\t@Override\n\t\t\tpublic int compare(String o1, String o2) {\n\t\t\t\tchar s1 = o1.charAt(0);\n\t\t\t\tchar s2 = o2.charAt(0);\n\t\t\t\tif(s1 == s2){\n\t\t\t\t\tInteger i1 = Integer.valueOf(o1.substring(1));\n\t\t\t\t\tInteger i2 = Integer.valueOf(o2.substring(1));\n\t\t\t\t\treturn i1.compareTo(i2);\n\t\t\t\t}else{\n\t\t\t\t\treturn o1.compareTo(o2);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\tString str2 = StringUtils.join(str, \",\");\n\t\tSystem.out.println(str2);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470197",
"status": 0,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/47.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 379228,
"question_title": "实现一元二次方程ax*x+b*x+c=0",
"question_content": "用面向对象的思想实现一元二次方程ax*x+b*x+c=0\n a,b,c从键盘输入\n",
"difficulty": "简单",
"answer_id": 411678,
"answer_content": "代码如下,希望对你有用\n\n```\n package question;\n//创建Function类,用以实现功能\npublic class Function {\n\t//私有化成员变量a,b,c\n\tprivate double a;\n\tprivate double b;\n\tprivate double c;\n\t//为每个成员变量提供set/get方法\n\tpublic double getA() {\n\t\treturn a;\n\t}\n\tpublic void setA(double a) {\n\t\tthis.a = a;\n\t}\n\tpublic double getB() {\n\t\treturn b;\n\t}\n\tpublic void setB(double b) {\n\t\tthis.b = b;\n\t}\n\tpublic double getC() {\n\t\treturn c;\n\t}\n\tpublic void setC(double c) {\n\t\tthis.c = c;\n\t}\n\t//提供无参、有参构造方法\n\tpublic Function() {}\n\tpublic Function(double a, double b, double c) {\n\t\tthis.a = a;\n\t\tthis.b = b;\n\t\tthis.c = c;\n\t}\n\t//提供成员方法,用以解决二次函数问题\n\tpublic void quadratic(){\n\t\tdouble d=b*b-4*a*c;\n\t\tif (Math.pow(d, 0.5)>=0) {\n\t\t\t//求根公式\n\t\t\tdouble q1=(-b)+Math.pow(d, 0.5);\n\t\t\tdouble q2=(-b)-Math.pow(d, 0.5);\n\t\t\tSystem.out.println(q1+\" \"+q2);\t\n\t\t}else {\n\t\t\tSystem.out.println(\"没有实根\");\n\t\t}\n\t}\n}\n\n```\n然后,\n\n```\n package question;\n//创建Test类,用以测试程序\nimport java.util.Scanner;//导包\n\npublic class Test {\n\tpublic static void main(String[] args) {\n\t\tScanner sc=new Scanner(System.in);//创建键盘录入对象\n\t\tSystem.out.println(\"请输入a的值\");//接收键盘录入数据\n\t\tdouble a=sc.nextDouble();\n\t\tSystem.out.println(\"请输入b的值\");//接收键盘录入数据\n\t\tdouble b=sc.nextDouble();\n\t\tSystem.out.println(\"请输入c的值\");//接收键盘录入数据\n\t\tdouble c=sc.nextDouble();\n\t\tFunction f = new Function(a,b,c);//调用方法Function\n\t\tf.quadratic();\n\t}\n\n}\n\n```\n\n\n",
"tag_name": "java",
"java": "public class Function {\n private double a;\n private double b;\n private double c;\n public double getA() {\n return a;\n }\n public void setA(double a) {\n this.a = a;\n }\n public double getB() {\n return b;\n }\n public void setB(double b) {\n this.b = b;\n }\n public double getC() {\n return c;\n }\n public void setC(double c) {\n this.c = c;\n }\n public Function() {}\n public Function(double a, double b, double c) {\n this.a = a;\n this.b = b;\n this.c = c;\n }\n public void quadratic(){\n double d=b*b-4*a*c;\n if (Math.pow(d, 0.5)>=0) {\n double q1=(-b)+Math.pow(d, 0.5);\n double q2=(-b)-Math.pow(d, 0.5);\n System.out.println(q1+\" \"+q2); \n }else {\n System.out.println(\"没有实根\");\n }\n }\n}",
"topic_link": "https://bbs.csdn.net/topics/600469882",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/48.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 658117,
"question_title": "获取硬盘信息",
"question_content": "获取硬盘的分区信息,以及每个分区的磁盘空间及剩余空间",
"difficulty": "简单",
"answer_id": 471171,
"answer_content": "\n\n```\n import java.io.File;\nimport java.text.DecimalFormat;\n\npublic class Ypxx {\n public static void main(String[] args) {\n File[] roots = File.listRoots();// 获取磁盘分区列表\n for (File file : roots) {\n System.out.println(file.getPath() + \"信息如下:\");\n long free = file.getFreeSpace();\n long total = file.getTotalSpace();\n long use = total - free;\n System.out.println(\"空闲未使用 = \" + change(free) + \"G\");// 空闲空间\n System.out.println(\"已经使用 = \" + change(use) + \"G\");// 可用空间\n System.out.println(\"总容量 = \" + change(total) + \"G\");// 总空间\n System.out.println(\"使用百分比 = \" + bfb(use, total));\n System.out.println();\n }\n }\n\n public static long change(long num) {\n // return num;\n return num / 1024 / 1024 / 1024;\n }\n\n public static String bfb(Object num1, Object num2) {\n double val1 = Double.valueOf(num1.toString());\n double val2 = Double.valueOf(num2.toString());\n if (val2 == 0) {\n return \"0.0%\";\n } else {\n DecimalFormat df = new DecimalFormat(\"#0.00\");\n return df.format(val1 / val2 * 100) + \"%\";\n }\n }\n}\n\n```\n\n",
"tag_name": "java",
"java": "import java.io.File;\nimport java.text.DecimalFormat;\npublic class Ypxx {\n\tpublic static void main(String[] args) {\n\t\tFile[] roots = File.listRoots();\n\t\tfor (File file : roots) {\n\t\t\tSystem.out.println(file.getPath() + \"信息如下:\");\n\t\t\tlong free = file.getFreeSpace();\n\t\t\tlong total = file.getTotalSpace();\n\t\t\tlong use = total - free;\n\t\t\tSystem.out.println(\"空闲未使用 = \" + change(free) + \"G\");\n\t\t\tSystem.out.println(\"已经使用 = \" + change(use) + \"G\");\n\t\t\tSystem.out.println(\"总容量 = \" + change(total) + \"G\");\n\t\t\tSystem.out.println(\"使用百分比 = \" + bfb(use, total));\n\t\t\tSystem.out.println();\n\t\t}\n\t}\n\tpublic static long change(long num) {\n\t\treturn num / 1024 / 1024 / 1024;\n\t}\n\tpublic static String bfb(Object num1, Object num2) {\n\t\tdouble val1 = Double.valueOf(num1.toString());\n\t\tdouble val2 = Double.valueOf(num2.toString());\n\t\tif (val2 == 0) {\n\t\t\treturn \"0.0%\";\n\t\t} else {\n\t\t\tDecimalFormat df = new DecimalFormat(\"#0.00\");\n\t\t\treturn df.format(val1 / val2 * 100) + \"%\";\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470294",
"status": 1,
"keywords": "计算机",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/49.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 194042,
"question_title": "java String字符串截取",
"question_content": "比如这样一个字符串\nString a = \"你好,今天是@2015@年@7@月@15@日\";\nString b = \"时间为@16@@:@@05@\";\n怎么样截取2个一对的@中间的字符串 a = 2015,7,15 b = 16,:,05",
"difficulty": "简单",
"answer_id": 100456,
"answer_content": "\n\n```\n public static String getResult(String str,String splitStr){\n \tString[] arr = (\"1\"+str+\"1\").split(splitStr);\n \tStringBuilder sd = new StringBuilder();\n \tfor(int i=1;i<arr.length-1;i++){\n \t\tsd.append(arr[i]);\n \t}\n \treturn sd.toString();\n }\n public static void main(String[] args) {\n \tString a = \"你好,今天是@2015@年@7@月@15@日\";\n \tString b = \"时间为@16@@:@@05@\";\n \ta = getResult(a,\"@\");\n \tb = getResult(b,\"@\");\n \tSystem.out.println(\"a=\"+a);\n \tSystem.out.println(\"b=\"+b);\n }\n```\n\n",
"tag_name": "java",
"java": "class java_194042{\n\tpublic static String getResult(String str,String splitStr){\n\t\tString[] arr = (\"1\"+str+\"1\").split(splitStr);\n\t\tStringBuilder sd = new StringBuilder();\n\t\tfor(int i=1;i<arr.length-1;i++){\n\t\t\tsd.append(arr[i]);\n\t\t}\n\t\treturn sd.toString();\n\t}\n\tpublic static void main(String[] args) {\n\t\tString a = \"你好,今天是@2015@年@7@月@15@日\";\n\t\tString b = \"时间为@16@@:@@05@\";\n\t\ta = getResult(a,\"@\");\n\t\tb = getResult(b,\"@\");\n\t\tSystem.out.println(\"a=\"+a);\n\t\tSystem.out.println(\"b=\"+b);\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470186",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/5.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1066540,
"question_title": "模拟计算器",
"question_content": "模拟简单的计算器。\n要求:\n(1)定义名为Number的类,在其中定义两个私有的整型数据成员n1和n2;\n(2)在Number类中编写构造方法,赋予n1和n2初始值;\n(3)再为Number类定义加(addition)、减(subtration)、乘(multiplication)、除(division)四个公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。注意:除法运算时要抛出除数为0的异常。\n(4)在主方法中创建Number类的对象,调用上述各个方法,测试并输出计算结果,注意进行必要的异常捕获与处理。",
"difficulty": "简单",
"answer_id": 1266530,
"answer_content": "\n```\npublic class Number {\n\n private int n1;\n\n private int n2;\n\n public Number(int n1, int n2) {\n this.n1 = n1;\n this.n2 = n2;\n }\n\n public int addition(){\n return n1 + n2;\n }\n\n public int subtration(){\n return n1-n2;\n }\n\n /**\n * 如果n1或n2 > 2的16次方需要用long接收返回值\n * @return\n */\n public int multiplication(){\n return n1*n2;\n }\n\n private int division(){\n if (n2 == 0){\n throw new IllegalArgumentException(\"除数参数不合法\");\n }\n return n1/n2;\n }\n\n /**\n * 如果要返回小数,用这种\n */\n/* private double division(){\n if (n2 == 0){\n throw new IllegalArgumentException(\"除数参数不合法\");\n }\n return n1/(n2*1.0);\n }\n */\n public static void main(String[] args) {\n Number number = new Number(6,0);\n System.out.println(number.addition());\n System.out.println(number.subtration());\n System.out.println(number.multiplication());\n try {\n System.out.println(number.division());\n }catch (Exception e){\n System.out.println(e.getMessage());\n }\n }\n}\n```",
"tag_name": "java",
"java": "public class Number {\n\tprivate int n1;\n\tprivate int n2;\n\tpublic Number(int n1, int n2) {\n\t\tthis.n1 = n1;\n\t\tthis.n2 = n2;\n\t}\n\tpublic int addition() {\n\t\treturn n1 + n2;\n\t}\n\tpublic int subtration() {\n\t\treturn n1 - n2;\n\t}\n\tpublic int multiplication() {\n\t\treturn n1 * n2;\n\t}\n\tprivate int division() {\n\t\tif (n2 == 0) {\n\t\t\tthrow new IllegalArgumentException(\"除数参数不合法\");\n\t\t}\n\t\treturn n1 / n2;\n\t}\n\tpublic static void main(String[] args) {\n\t\tNumber number = new Number(6, 0);\n\t\tSystem.out.println(number.addition());\n\t\tSystem.out.println(number.subtration());\n\t\tSystem.out.println(number.multiplication());\n\t\ttry {\n\t\t\tSystem.out.println(number.division());\n\t\t} catch (Exception e) {\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469981",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,用主方法求解递归式",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/50.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 201530,
"question_title": "输出给定字符串在哪些文件中出现过,把文件名称输出,包括路径",
"question_content": "输入:\n1) 给定一个文件夹,此文件夹下面有若干个文件或者文件夹,文件夹下面也有文件。文件全部为文本文件,文件中的内容不确定。如C:\\TxtFiles\n2) 给定一个字符串,如test\n\n要求输出给定字符串在哪些文件中出现过,把文件名称输出,包括路径。",
"difficulty": "简单",
"answer_id": 124339,
"answer_content": "\n\n```\n import java.util.*;\nimport java.io.*;\n\npublic class ReadStringAndPrintPath \n{\n\tpublic static void main(String[] args) \n\t{\n\t\tSystem.out.println(\"Hello World!\");\n\t\tString str = \"test\";\n\t\tFile file = new File(\"a\");\n\t\tFile[] list = file.listFiles();\n\t\tsearchString(list,str);\n\t}\n\tpublic static void searchString(File[] files,String str){\n\t\tfor(int i = 0 ; i < files.length; i ++){\n\t\t\tif(files[i].isFile()){\n\t\t\t\t//是文件,搜索是否存在字符串\n\t\t\t\ttry{\n\t\t\t\t\tScanner scan = new Scanner(files[i]);\n\t\t\t\t\twhile(scan.hasNext()){\n\t\t\t\t\t\tif(scan.nextLine().indexOf(str) != -1){\n\t\t\t\t\t\t\tSystem.out.println(files[i].getAbsolutePath());\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}catch(IOException ex){\n\t\t\t\t\tex.printStackTrace();\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\t//是文件夹\n\t\t\t\tFile[] list = files[i].listFiles();\n\t\t\t\tsearchString(list,str);\n\t\t\t}\n\t\t}\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": " import java.util.*;\nimport java.io.*;\npublic class ReadStringAndPrintPath \n{\n\tpublic static void main(String[] args) \n\t{\n\t\tSystem.out.println(\"Hello World!\");\n\t\tString str = \"test\";\n\t\tFile file = new File(\"a\");\n\t\tFile[] list = file.listFiles();\n\t\tsearchString(list,str);\n\t}\n\tpublic static void searchString(File[] files,String str){\n\t\tfor(int i = 0 ; i < files.length; i ++){\n\t\t\tif(files[i].isFile()){\n\t\t\t\ttry{\n\t\t\t\t\tScanner scan = new Scanner(files[i]);\n\t\t\t\t\twhile(scan.hasNext()){\n\t\t\t\t\t\tif(scan.nextLine().indexOf(str) != -1){\n\t\t\t\t\t\t\tSystem.out.println(files[i].getAbsolutePath());\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}catch(IOException ex){\n\t\t\t\t\tex.printStackTrace();\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tFile[] list = files[i].listFiles();\n\t\t\t\tsearchString(list,str);\n\t\t\t}\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469871",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,Rabin\\Karp算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/6.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7458996,
"question_title": "输出每天是应该学习还是休息还是锻炼",
"question_content": "<p>30天中&#xff0c;从第一天开始五天学习&#xff0c;一天休息、一天锻炼&#xff0c;输出每天是应该学习还是休息还是锻炼</p>",
"difficulty": "简单",
"answer_id": 53443776,
"answer_content": "<pre>\n<code>public class HelloWorld {\n public static void main(String []args) {\n\t\tint n1&#61;0,n2&#61;0,n3&#61;0,i;\n for(i&#61;1;i&lt;&#61;30;i&#43;&#43;){\n\t if(n1&lt;5){\n\t\tSystem.out.println(&#34;学习&#34;);\n\t\t n1&#43;&#43;;\n\t\t continue;\n\t }\n\t\telse{\n\t\tSystem.out.println(&#34;休息&#34;);\n\t\tSystem.out.println(&#34;锻炼&#34;);\n\t\t\tn1&#61;0;\n\t\t\ti&#43;&#43;;\n\t\t}\n }\n\t}}</code></pre>\n\n<p><img alt=\"\" height=\"589\" src=\"https://img-ask.csdnimg.cn/upload/1624544217953.png\" width=\"617\" /> </p>\n",
"tag_name": "java",
"java": "public class HelloWorld {\n\tpublic static void main(String []args) {\n\t\tint n1=0,n2=0,n3=0,i;\n\t\tfor(i=1;i<=30;i++){\n\t\t\tif(n1<5){\n\t\t\t\tSystem.out.println(\"学习\");\n\t\t\t\tn1++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tSystem.out.println(\"休息\");\n\t\t\t\tSystem.out.println(\"锻炼\");\n\t\t\t\tn1=0;\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600470187",
"status": 1,
"keywords": "数学",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/7.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 371986,
"question_title": "用随机数相关知识解答随机分组问题",
"question_content": "已知有16只男子足球队参加2008年奥运会。写一段程序将球队随机分成4组",
"difficulty": "简单",
"answer_id": 398940,
"answer_content": "刚写的代码附上,亲测可行,希望有帮助。\n\n\n\n```\n import java.util.*;\n\nclass StringToDateDemo {\n\n\tpublic static void main(String args[]) {\n\t\t//假设a-p为16个队名称\n\t\tArrayList<String> teams = new ArrayList<String>() {\n\t\t\t{\n\t\t\t\tadd(\"a\");\n\t\t\t\tadd(\"b\");\n\t\t\t\tadd(\"c\");\n\t\t\t\tadd(\"d\");\n\t\t\t\tadd(\"e\");\n\t\t\t\tadd(\"f\");\n\t\t\t\tadd(\"g\");\n\t\t\t\tadd(\"h\");\n\t\t\t\tadd(\"i\");\n\t\t\t\tadd(\"j\");\n\t\t\t\tadd(\"k\");\n\t\t\t\tadd(\"l\");\n\t\t\t\tadd(\"m\");\n\t\t\t\tadd(\"n\");\n\t\t\t\tadd(\"o\");\n\t\t\t\tadd(\"p\");\n\t\t\t}\n\t\t};\n\n\t\tCollections.shuffle(teams);// **这行是关键,shuffle函数将16个队随机打乱**\n\n\t\t// 将16个队分成4组\n\t\tArrayList<String> group1 = new ArrayList<String>();\n\t\tArrayList<String> group2 = new ArrayList<String>();\n\t\tArrayList<String> group3 = new ArrayList<String>();\n\t\tArrayList<String> group4 = new ArrayList<String>();\n\n\t\t// 添加第一组成员\n\t\tgroup1.addAll(teams.subList(0, teams.size() / 4 + teams.size() % 4));\n\n\t\t// 添加第二组成员\n\t\tgroup2.addAll(teams.subList(teams.size() / 4 + teams.size() % 4, 2 * teams.size() / 4 + teams.size() % 4));\n\n\t\t// 添加第三组成员\n\t\tgroup3.addAll(teams.subList(2*teams.size() / 4 + teams.size() % 4, 3 * teams.size() / 4 + teams.size() % 4));\n\n\t\t// 添加第四组成员\n\t\tgroup4.addAll(teams.subList(3*teams.size() / 4 + teams.size() % 4, teams.size()));\n\t}\n}\n```\n\n",
"tag_name": "java",
"java": "import java.util.*;\nclass StringToDateDemo {\n\tpublic static void main(String args[]) {\n\t\tArrayList<String> teams = new ArrayList<String>() {\n\t\t\t{\n\t\t\t\tadd(\"a\");\n\t\t\t\tadd(\"b\");\n\t\t\t\tadd(\"c\");\n\t\t\t\tadd(\"d\");\n\t\t\t\tadd(\"e\");\n\t\t\t\tadd(\"f\");\n\t\t\t\tadd(\"g\");\n\t\t\t\tadd(\"h\");\n\t\t\t\tadd(\"i\");\n\t\t\t\tadd(\"j\");\n\t\t\t\tadd(\"k\");\n\t\t\t\tadd(\"l\");\n\t\t\t\tadd(\"m\");\n\t\t\t\tadd(\"n\");\n\t\t\t\tadd(\"o\");\n\t\t\t\tadd(\"p\");\n\t\t\t}\n\t\t};\n\t\tCollections.shuffle(teams);\n\t\tArrayList<String> group1 = new ArrayList<String>();\n\t\tArrayList<String> group2 = new ArrayList<String>();\n\t\tArrayList<String> group3 = new ArrayList<String>();\n\t\tArrayList<String> group4 = new ArrayList<String>();\n\t\tgroup1.addAll(teams.subList(0, teams.size() / 4 + teams.size() % 4));\n\t\tgroup2.addAll(teams.subList(teams.size() / 4 + teams.size() % 4, 2 * teams.size() / 4 + teams.size() % 4));\n\t\tgroup3.addAll(teams.subList(2*teams.size() / 4 + teams.size() % 4, 3 * teams.size() / 4 + teams.size() % 4));\n\t\tgroup4.addAll(teams.subList(3*teams.size() / 4 + teams.size() % 4, teams.size()));\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469872",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/8.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7410647,
"question_title": "定义一个学生类,其中有学号、姓名、年龄3个数据成员,以及若干成员函数。",
"question_content": "<p>同时编写主函数使用这个类&#xff0c;实现对学生数据的赋值和输出。要求&#xff1a;使用构造函数实现对数据的初始化&#xff0c;使用成员函数实现对数据的输出。</p>",
"difficulty": "简单",
"answer_id": 53361432,
"answer_content": "<p>完整代码如下&#xff1a;</p>\n\n<pre>\npackage com.test;\n\n/**\n * &#64;author 杨俊\n * &#64;create 2021-03-27-11:14\n */\npublic class Student {\n public String stuNo;\n public String stuname;\n public int age;\n\n public Student(String stuNo,String stuname,int age){\n this.stuNo&#61;stuNo;\n this.stuname&#61;stuname;\n this.age&#61;age;\n }\n\n public static void printStudent(Student stu){\n System.out.println(&#34;学号&#xff1a;&#34;&#43;stu.getStuNo());\n System.out.println(&#34;姓名&#xff1a;&#34;&#43;stu.getStuname());\n System.out.println(&#34;年龄&#xff1a;&#34;&#43;stu.getAge());\n }\n\n public static void main(String[] args) {\n Student stu &#61; new Student(&#34;101&#34;,&#34;杨俊&#34;,28);\n printStudent(stu);\n }\n\n public String getStuNo() {\n return stuNo;\n }\n\n public void setStuNo(String stuNo) {\n this.stuNo &#61; stuNo;\n }\n\n public String getStuname() {\n return stuname;\n }\n\n public void setStuname(String stuname) {\n this.stuname &#61; stuname;\n }\n\n public int getAge() {\n return age;\n }\n\n public void setAge(int age) {\n this.age &#61; age;\n }\n}</pre>\n\n<p> </p>\n\n<p>运行截图</p>\n\n<p><img alt=\"\" height=\"174\" src=\"https://img-ask.csdnimg.cn/upload/1616815782705.png\" width=\"573\" /></p>",
"tag_name": "java",
"java": "public class Student {\n\tpublic String stuNo;\n\tpublic String stuname;\n\tpublic int age;\n\tpublic Student(String stuNo,String stuname,int age){\n\t\tthis.stuNo=stuNo;\n\t\tthis.stuname=stuname;\n\t\tthis.age=age;\n\t}\n\tpublic static void printStudent(Student stu){\n\t\tSystem.out.println(\"学号:\"+stu.getStuNo());\n\t\tSystem.out.println(\"姓名:\"+stu.getStuname());\n\t\tSystem.out.println(\"年龄:\"+stu.getAge());\n\t}\n\tpublic static void main(String[] args) {\n\t\tStudent stu = new Student(\"101\",\"杨俊\",28);\n\t\tprintStudent(stu);\n\t}\n\tpublic String getStuNo() {\n\t\treturn stuNo;\n\t}\n\tpublic void setStuNo(String stuNo) {\n\t\tthis.stuNo = stuNo;\n\t}\n\tpublic String getStuname() {\n\t\treturn stuname;\n\t}\n\tpublic void setStuname(String stuname) {\n\t\tthis.stuname = stuname;\n\t}\n\tpublic int getAge() {\n\t\treturn age;\n\t}\n\tpublic void setAge(int age) {\n\t\tthis.age = age;\n\t}\n}",
"topic_link": "https://bbs.csdn.net/topics/600469873",
"status": 0,
"keywords": "算法初阶,基础知识,分治策略,证明主定理,对b的幂证明主定理",
"license": "csdn.net",
"notebook": {
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/9.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1,
"question_title": "两数之和",
"difficulty": "简单",
"question_content": "<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值</strong> 的那 <strong>两个</strong> 整数,并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,7,11,15], target = 9<strong><br />输出:</strong>[0,1]<strong><br />解释:</strong>因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,4], target = 6<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [3,3], target = 6<strong><br />输出:</strong>[0,1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>2 <= nums.length <= 10<sup>3</sup></code></li>\t<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\t<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>\t<li><strong>只会存在一个有效答案</strong></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470217",
"cpp": "#include <unordered_map>\nclass Solution\n{\npublic:\n\tvector<int> twoSum(vector<int> &nums, int target)\n\t{\n\t\tstd::unordered_map<int, int> hset;\n\t\tvector<int> r;\n\t\tfor (int i = 0; i < nums.size(); ++i)\n\t\t{\n\t\t\tint c = target - nums[i];\n\t\t\tauto iter = hset.find(c);\n\t\t\tif (iter != hset.end() && iter->second != i)\n\t\t\t{\n\t\t\t\tr.push_back(i);\n\t\t\t\tr.push_back(iter->second);\n\t\t\t\treturn r;\n\t\t\t}\n\t\t\thset.insert(std::make_pair(nums[i], i));\n\t\t}\n\t\treturn r;\n\t}\n};",
"java": "class Solution {\n\tpublic int[] twoSum(int[] nums, int target) {\n\t\tMap<Integer, Integer> cache = new HashMap<>();\n\t\tfor (int i = 0; i < nums.length; i++) {\n\t\t\tint distance = target - nums[i];\n\t\t\tif (cache.containsKey(distance)) {\n\t\t\t\treturn new int[] { cache.get(distance), i };\n\t\t\t} else {\n\t\t\t\tcache.put(nums[i], i);\n\t\t\t}\n\t\t}\n\t\treturn new int[] {};\n\t}\n}",
"js": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nvar twoSum = function(nums, target) {\n const cache={};\n for(let i=0;i<nums.length;i++){\n const right = target-nums[i];\n if(cache[right]!=null){\n return [cache[right],i]\n }else{\n cache[nums[i]] = i;\n }\n }\n};\n",
"python": "def twoSum(nums, target):\n\tcache = {}\n\ti = 0\n\twhile i < len(nums):\n\t\tright = target-nums[i]\n\t\tif cache.get(right) is not None:\n\t\t\treturn [cache[right], i]\n\t\telse:\n\t\t\tcache[nums[i]] = i\n\t\ti += 1\n\treturn [] \n# %%\nprint(twoSum([2,7,11,15], 9))\nprint(twoSum([3,2,4], 6))",
"status": 1,
"keywords": "数组,哈希表",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/0/0_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/0/0_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/0/0_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 2,
"question_title": "两数相加",
"difficulty": "中等",
"question_content": "<p>给你两个 <strong>非空</strong> 的链表,表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的,并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加,并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外,这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg\" style=\"width: 483px; height: 342px;\" /><pre><strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出:</strong>[7,0,8]<strong><br />解释:</strong>342 + 465 = 807.</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [0], l2 = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出:</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>每个链表中的节点数在范围 <code>[1, 100]</code> 内</li>\t<li><code>0 <= Node.val <= 9</code></li>\t<li>题目数据保证列表表示的数字不含前导零</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469916",
"cpp": "struct ListNode\n{\n\tint val;\n\tstruct ListNode *next;\n};\nstruct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)\n{\n\tstruct ListNode *pp = NULL, *p = l1;\n\tstruct ListNode *qp = NULL, *q = l2;\n\tint carry = 0;\n\twhile (p != NULL && q != NULL)\n\t{\n\t\tp->val += q->val + carry;\n\t\tcarry = 0;\n\t\tif (p->val >= 10)\n\t\t{\n\t\t\tcarry = 1;\n\t\t\tp->val -= 10;\n\t\t}\n\t\tpp = p;\n\t\tp = p->next;\n\t\tqp = q;\n\t\tq = q->next;\n\t}\n\tif (q)\n\t{\n\t\tpp->next = p = q;\n\t\tqp->next = NULL;\n\t}\n\twhile (carry && p)\n\t{\n\t\tp->val += carry;\n\t\tcarry = 0;\n\t\tif (p->val >= 10)\n\t\t{\n\t\t\tcarry = 1;\n\t\t\tp->val -= 10;\n\t\t}\n\t\tpp = p;\n\t\tp = p->next;\n\t}\n\tif (carry)\n\t{\n\t\tstruct ListNode *n = (struct ListNode *)malloc(sizeof(struct ListNode));\n\t\tn->val = 1;\n\t\tn->next = NULL;\n\t\tpp->next = n;\n\t}\n\treturn l1;\n}",
"java": "class ListNode {\n\tint val;\n\tListNode next;\n\tListNode() {\n\t}\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\nclass Solution {\n\tpublic ListNode addTwoNumbers(ListNode l1, ListNode l2) {\n\t\tListNode lrr = l1;\n\t\twhile (true) {\n\t\t\tl1.val = l1.val + l2.val;\n\t\t\tif (l1.next == null && l2.next == null && l1.val < 10) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (l1.next == null) {\n\t\t\t\tl1.next = new ListNode(0);\n\t\t\t}\n\t\t\tif (l2.next == null) {\n\t\t\t\tl2.next = new ListNode(0);\n\t\t\t}\n\t\t\tif (l1.val >= 10) {\n\t\t\t\tl1.val = l1.val - 10;\n\t\t\t\tl1.next.val += 1;\n\t\t\t}\n\t\t\tl1 = l1.next;\n\t\t\tl2 = l2.next;\n\t\t}\n\t\treturn lrr;\n\t}\n}",
"js": "function ListNode(val) {\n this.val = val;\n this.next = null;\n}\n\n/**\n * @param {ListNode} l1\n * @param {ListNode} l2\n * @return {ListNode}\n */\nvar addTwoNumbers = function(l1, l2) {\n\n let lrr = l1;\n\n while(true){\n\n l1.val = l1.val + l2.val;\n \n if(l1.next==null&&l2.next==null&&l1.val<10){\n break;\n }\n \n if(l1.next==null){\n l1.next=new ListNode(0);\n }\n \n if(l2.next==null){\n l2.next=new ListNode(0);\n }\n \n if(l1.val>=10){\n l1.val = l1.val - 10;\n l1.next.val+=1;\n }\n \n l1 = l1.next;\n l2 = l2.next;\n }\n \n return lrr;\n};\n",
"python": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n\t\tlrr = l1\n\t\twhile True:\n\t\t\tl1.val = l1.val + l2.val\n\t\t\tif l1.next is None and l2.next is None and l1.val < 10:\n\t\t\t\tbreak\n\t\t\tif l1.next is None:\n\t\t\t\tl1.next = ListNode(0)\n\t\t\tif l2.next is None:\n\t\t\t\tl2.next = ListNode(0)\n\t\t\tif l1.val >= 10:\n\t\t\t\tl1.val = l1.val - 10\n\t\t\t\tl1.next.val += 1\n\t\t\tl1 = l1.next\n\t\t\tl2 = l2.next\n\t\treturn lrr\n# %%\nl = LinkList()\nlist1 = [2,4,3]\nlist2 = [5,6,4]\nl1 = l.initList(list1)\nl2 = l.initList(list2)\ns = Solution()\nprint(l.convert_list(s.addTwoNumbers(l1, l2)))",
"status": 1,
"keywords": "递归,链表,数学",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/1/1_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/1/1_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/1/1_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 11,
"question_title": "盛最多水的容器",
"difficulty": "中等",
"question_content": "<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p><p><strong>说明:</strong>你不能倾斜容器。</p><p> </p><p><strong>示例 1:</strong></p><p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0011.Container%20With%20Most%20Water/images/question_11.jpg\" style=\"height: 287px; width: 600px;\" /></p><pre><strong>输入:</strong>[1,8,6,2,5,4,8,3,7]<strong><br />输出:</strong>49 <strong><br />解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [1,1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>height = [4,3,2,1,4]<strong><br />输出:</strong>16</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>height = [1,2,1]<strong><br />输出:</strong>2</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>n = height.length</code></li>\t<li><code>2 <= n <= 3 * 10<sup>4</sup></code></li>\t<li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469816",
"cpp": "#define MAX(a, b) (((a) < (b)) ? (b) : (a))\n#define MIN(a, b) (((a) > (b)) ? (b) : (a))\nint maxArea(int *height, int heightSize)\n{\n\tint max = 0;\n\tint i = 0, j = heightSize - 1;\n\tint a;\n\twhile (i < j)\n\t{\n\t\ta = MIN(height[i], height[j]) * (j - i);\n\t\tmax = MAX(max, a);\n\t\tif (height[i] > height[j])\n\t\t\t--j;\n\t\telse\n\t\t\t++i;\n\t}\n\treturn max;\n}",
"java": "class Solution {\n\tpublic int maxArea(int[] height) {\n\t\tint N = height.length;\n\t\tint i = 0;\n\t\tint j = N - 1;\n\t\tint max = 0;\n\t\twhile (i < j) {\n\t\t\tint c = (j - i) * Math.min(height[i], height[j]);\n\t\t\tif (c > max) {\n\t\t\t\tmax = c;\n\t\t\t}\n\t\t\tif (height[i] > height[j]) {\n\t\t\t\tj--;\n\t\t\t} else {\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn max;\n\t}\n}",
"js": "/**\n * @param {number[]} height\n * @return {number}\n */\nvar maxArea = function(height) {\n let N = height.length;\n \n let i=0;\n let j=N-1;\n let max=0;\n while(i<j){\n let c = (j-i)*Math.min(height[i],height[j]);\n if(c>max){\n max = c;\n }\n if(height[i]>height[j]){\n j--;\n }else{\n i++;\n }\n }\n return max;\n};\n",
"python": "from typing import List\nclass Solution:\n\tdef maxArea(self, height: List[int]) -> int:\n\t\tN = len(height)\n\t\ti = 0\n\t\tj = N-1\n\t\tmax_area = 0\n\t\twhile i < j:\n\t\t\tc = (j-i)*min(height[i], height[j])\n\t\t\tif c > max_area:\n\t\t\t\tmax_area = c\n\t\t\tif height[i] > height[j]:\n\t\t\t\tj -= 1\n\t\t\telse:\n\t\t\t\ti += 1\n\t\treturn max_area\n# %%\ns = Solution()\nprint(s.maxArea([1,8,6,2,5,4,8,3,7]))",
"status": 1,
"keywords": "贪心,数组,双指针",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 12,
"question_title": "整数转罗马数字",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>罗马数字包含以下七种字符:&nbsp;<code>I</code>,&nbsp;<code>V</code>,&nbsp;<code>X</code>,&nbsp;<code>L</code>,<code>C</code>,<code>D</code>&nbsp;和&nbsp;<code>M</code>。\n </p>\n\n <pre><strong>字符</strong> <strong>数值</strong>\nI 1\nV 5\nX 10\nL 50\nC 100\nD 500\nM 1000</pre>\n\n <p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12\n 写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27\n 写做&nbsp;&nbsp;<code>XXVII</code>,\n 即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>\n\n <p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5\n 减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>\n\n <ul>\n <li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>\n <li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40\n 和&nbsp;90。&nbsp;</li>\n <li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400\n 和&nbsp;900。</li>\n </ul>\n\n <p>给你一个整数,将其转为罗马数字。</p>\n\n <p>&nbsp;</p>\n\n <p><strong>示例&nbsp;1:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 3\n<strong><br />输出:</strong> \"III\"</pre>\n\n <p><strong>示例&nbsp;2:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 4\n<strong><br />输出:</strong> \"IV\"</pre>\n\n <p><strong>示例&nbsp;3:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 9\n<strong><br />输出:</strong> \"IX\"</pre>\n\n <p><strong>示例&nbsp;4:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 58\n<strong><br />输出:</strong> \"LVIII\"\n<strong><br />解释:</strong> L = 50, V = 5, III = 3.\n </pre>\n\n <p><strong>示例&nbsp;5:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 1994\n<strong><br />输出:</strong> \"MCMXCIV\"\n<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= num &lt;= 3999</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600469817",
"cpp": "struct rmap\n{\n\tchar *r;\n\tint v;\n} units[] = {\n\t{\"M\", 1000},\n\t{\"CM\", 900},\n\t{\"D\", 500},\n\t{\"CD\", 400},\n\t{\"C\", 100},\n\t{\"XC\", 90},\n\t{\"L\", 50},\n\t{\"XL\", 40},\n\t{\"X\", 10},\n\t{\"IX\", 9},\n\t{\"V\", 5},\n\t{\"IV\", 4},\n\t{\"I\", 1}};\n#include <string.h>\nchar result[64];\nchar *intToRoman(int num)\n{\n\tresult[0] = 0;\n\tint ri = 0;\n\tint i = 0;\n\twhile (num)\n\t{\n\t\tif (num >= units[i].v)\n\t\t{\n\t\t\tstrcat(result, units[i].r);\n\t\t\tnum -= units[i].v;\n\t\t}\n\t\telse\n\t\t{\n\t\t\ti++;\n\t\t}\n\t}\n\treturn result;\n}",
"java": "class Solution {\n\tpublic String intToRoman(int num) {\n\t\tint f = 1000;\n\t\tint f2 = 1000;\n\t\tchar[] sym = new char[] { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };\n\t\tint fsi = 0;\n\t\tint[] s = new int[] { 2, 5 };\n\t\tint si = 0;\n\t\tint[] s2 = new int[] { 10, 1 };\n\t\tint si2 = 0;\n\t\tStringBuilder roman = new StringBuilder();\n\t\twhile (num > 0) {\n\t\t\tint d = (int) Math.floor(num / f);\n\t\t\tint r = num % f;\n\t\t\tint d2 = (int) Math.floor(num / f2);\n\t\t\tint r2 = num % f2;\n\t\t\tif (d > 0) {\n\t\t\t\tif (d == 4) {\n\t\t\t\t\troman.append(sym[fsi]);\n\t\t\t\t\troman.append(sym[fsi - 1]);\n\t\t\t\t\tnum = r;\n\t\t\t\t} else if (d2 == 9) {\n\t\t\t\t\troman.append(sym[fsi + 1]);\n\t\t\t\t\troman.append(sym[fsi - 1]);\n\t\t\t\t\tnum = r2;\n\t\t\t\t} else {\n\t\t\t\t\tfor (int i = 0; i < d; i++) {\n\t\t\t\t\t\troman.append(sym[fsi]);\n\t\t\t\t\t}\n\t\t\t\t\tnum = r;\n\t\t\t\t}\n\t\t\t}\n\t\t\tf = f / s[si];\n\t\t\tsi++;\n\t\t\tsi %= 2;\n\t\t\tf2 = f2 / s2[si2];\n\t\t\tsi2++;\n\t\t\tsi2 %= 2;\n\t\t\tfsi++;\n\t\t}\n\t\treturn roman.toString();\n\t}\n}",
"js": "/**\n * @param {number} num\n * @return {string}\n */\nvar intToRoman = function(num) {\n \n let f = 1000;\n let f2 = 1000;\n \n let sym = ['M','D','C','L','X','V','I'];\n let fsi = 0;\n \n let s = [2,5];\n let si = 0;\n \n let s2 = [10,1];\n let si2 = 0;\n \n let roman = [];\n while(num>0){\n let d = Math.floor(num/f);\n let r = num%f;\n \n let d2 = Math.floor(num/f2);\n let r2 = num%f2;\n\n if(d>0){\n if(d===4){\n roman.push(sym[fsi]);\n roman.push(sym[fsi-1]); \n num = r;\n }else if(d2===9){\n roman.push(sym[fsi+1]);\n roman.push(sym[fsi-1]);\n num = r2;\n }else{\n for(let i=0;i<d;i++){\n roman.push(sym[fsi]); \n } \n num = r;\n }\n }\n \n f = f/s[si];\n si++;\n si%=2;\n \n f2 = f2/s2[si2];\n si2++;\n si2%=2;\n \n fsi++;\n \n }\n return roman.join('');\n};\n",
"python": "from math import floor\nclass Solution:\n\tdef intToRoman(self, num: int) -> str:\n\t\tf = 1000\n\t\tf2 = 1000\n\t\tsym = ['M', 'D', 'C', 'L', 'X', 'V', 'I']\n\t\tfsi = 0\n\t\ts = [2, 5]\n\t\tsi = 0\n\t\ts2 = [10, 1]\n\t\tsi2 = 0\n\t\troman = []\n\t\twhile num > 0:\n\t\t\td = floor(num/f)\n\t\t\tr = num % f\n\t\t\td2 = floor(num/f2)\n\t\t\tr2 = num % f2\n\t\t\tif d > 0:\n\t\t\t\tif d == 4:\n\t\t\t\t\troman.append(sym[fsi])\n\t\t\t\t\troman.append(sym[fsi-1])\n\t\t\t\t\tnum = r\n\t\t\t\telif d2 == 9:\n\t\t\t\t\troman.append(sym[fsi+1])\n\t\t\t\t\troman.append(sym[fsi-1])\n\t\t\t\t\tnum = r2\n\t\t\t\telse:\n\t\t\t\t\ti = 0\n\t\t\t\t\twhile i < d:\n\t\t\t\t\t\troman.append(sym[fsi])\n\t\t\t\t\t\ti += 1\n\t\t\t\t\tnum = r\n\t\t\tf = f/s[si]\n\t\t\tsi += 1\n\t\t\tsi %= 2\n\t\t\tf2 = f2/s2[si2]\n\t\t\tsi2 += 1\n\t\t\tsi2 %= 2\n\t\t\tfsi += 1\n\t\treturn ''.join(roman)\n# %%\ns = Solution()\nprint(s.intToRoman(num = 3))",
"status": 1,
"keywords": "哈希表,数学,字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/11/11_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/11/11_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/11/11_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 13,
"question_title": "罗马数字转整数",
"difficulty": "简单",
"question_content": "<div class=\"notranslate\">\n <p>罗马数字包含以下七种字符:&nbsp;<code>I</code>,&nbsp;<code>V</code>,&nbsp;<code>X</code>,&nbsp;<code>L</code>,<code>C</code>,<code>D</code>&nbsp;和&nbsp;<code>M</code>。\n </p>\n\n <pre><strong>字符</strong> <strong>数值</strong>\nI 1\nV 5\nX 10\nL 50\nC 100\nD 500\nM 1000</pre>\n\n <p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12\n 写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27\n 写做&nbsp;&nbsp;<code>XXVII</code>,\n 即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>\n\n <p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5\n 减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>\n\n <ul>\n <li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>\n <li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40\n 和&nbsp;90。&nbsp;</li>\n <li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400\n 和&nbsp;900。</li>\n </ul>\n\n <p>给你一个整数,将其转为罗马数字。</p>\n\n <p>&nbsp;</p>\n\n <p><strong>示例&nbsp;1:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 3\n<strong><br />输出:</strong> \"III\"</pre>\n\n <p><strong>示例&nbsp;2:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 4\n<strong><br />输出:</strong> \"IV\"</pre>\n\n <p><strong>示例&nbsp;3:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 9\n<strong><br />输出:</strong> \"IX\"</pre>\n\n <p><strong>示例&nbsp;4:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 58\n<strong><br />输出:</strong> \"LVIII\"\n<strong><br />解释:</strong> L = 50, V = 5, III = 3.\n </pre>\n\n <p><strong>示例&nbsp;5:</strong></p>\n\n <pre><strong>输入:</strong>&nbsp;num = 1994\n<strong><br />输出:</strong> \"MCMXCIV\"\n<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= num &lt;= 3999</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470218",
"cpp": "struct rmap\n{\n\tchar *r;\n\tint v;\n\tint l;\n} units[] = {\n\t{\"M\", 1000, 1},\n\t{\"CM\", 900, 2},\n\t{\"D\", 500, 1},\n\t{\"CD\", 400, 2},\n\t{\"C\", 100, 1},\n\t{\"XC\", 90, 2},\n\t{\"L\", 50, 1},\n\t{\"XL\", 40, 2},\n\t{\"X\", 10, 1},\n\t{\"IX\", 9, 2},\n\t{\"V\", 5, 1},\n\t{\"IV\", 4, 2},\n\t{\"I\", 1, 1}};\n#include <string.h>\nint romanToInt(char *s)\n{\n\tint len = strlen(s);\n\tchar *end = s + len;\n\tint i = 0;\n\tint r = 0;\n\twhile (i < 13)\n\t{\n\t\tif (end - s >= units[i].l && memcmp(s, units[i].r, units[i].l) == 0)\n\t\t{\n\t\t\tr += units[i].v;\n\t\t\ts += units[i].l;\n\t\t}\n\t\telse\n\t\t\ti++;\n\t}\n\treturn r;\n}",
"java": "class Solution {\n\tpublic int romanToInt(String s) {\n\t\tint n = 0;\n\t\tfor (int i = 0; i < s.length();) {\n\t\t\tchar c = s.charAt(i);\n\t\t\tif (c == 'I') {\n\t\t\t\tif (i + 1 < s.length()) {\n\t\t\t\t\tif (s.charAt(i + 1) == 'V') {\n\t\t\t\t\t\tn += 4;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else if (s.charAt(i + 1) == 'X') {\n\t\t\t\t\t\tn += 9;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tn += 1;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tn += 1;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t} else if (c == 'X') {\n\t\t\t\tif (i + 1 < s.length()) {\n\t\t\t\t\tif (s.charAt(i + 1) == 'L') {\n\t\t\t\t\t\tn += 40;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else if (s.charAt(i + 1) == 'C') {\n\t\t\t\t\t\tn += 90;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tn += 10;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tn += 10;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t} else if (c == 'C') {\n\t\t\t\tif (i + 1 < s.length()) {\n\t\t\t\t\tif (s.charAt(i + 1) == 'D') {\n\t\t\t\t\t\tn += 400;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else if (s.charAt(i + 1) == 'M') {\n\t\t\t\t\t\tn += 900;\n\t\t\t\t\t\ti += 2;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tn += 100;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tn += 100;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t} else if (c == 'V') {\n\t\t\t\tn += 5;\n\t\t\t\ti++;\n\t\t\t} else if (c == 'L') {\n\t\t\t\tn += 50;\n\t\t\t\ti++;\n\t\t\t} else if (c == 'D') {\n\t\t\t\tn += 500;\n\t\t\t\ti++;\n\t\t\t} else if (c == 'M') {\n\t\t\t\tn += 1000;\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn n;\n\t}\n}",
"js": "/**\n * @param {string} s\n * @return {number}\n */\nvar romanToInt = function(s) {\n let n = 0;\n for(let i=0;i<s.length;){\n let c = s[i];\n if(c==='I'){\n if(i+1<s.length ){\n if(s[i+1]==='V'){\n n+=4;\n i+=2;\n }else if(s[i+1]==='X'){\n n+=9;\n i+=2;\n }else {\n n+=1;\n i++;\n }\n }else{\n n+=1;\n i++;\n }\n }else if(c==='X'){\n if(i+1<s.length ){\n if(s[i+1]==='L'){\n n+=40;\n i+=2;\n }else if(s[i+1]==='C'){\n n+=90;\n i+=2;\n }else {\n n+=10;\n i++;\n }\n }else{\n n+=10;\n i++;\n }\n }else if(c==='C'){\n if(i+1<s.length ){\n if(s[i+1]==='D'){\n n+=400;\n i+=2;\n }else if(s[i+1]==='M'){\n n+=900;\n i+=2;\n }else {\n n+=100;\n i++;\n }\n }else{\n n+=100;\n i++;\n }\n }else if(c==='V'){\n n+=5;\n i++;\n }else if(c==='L'){\n n+=50;\n i++;\n }else if(c==='D'){\n n+=500;\n i++;\n }else if(c==='M'){\n n+=1000;\n i++;\n }\n }\n return n;\n};\n",
"python": "class Solution:\n\tdef romanToInt(self, s: str) -> int:\n\t\tunits = [\n\t\t\t[\"M\", 1000, 1],\n\t\t\t[\"CM\", 900, 2],\n\t\t\t[\"D\", 500, 1],\n\t\t\t[\"CD\", 400, 2],\n\t\t\t[\"C\", 100, 1],\n\t\t\t[\"XC\", 90, 2],\n\t\t\t[\"L\", 50, 1],\n\t\t\t[\"XL\", 40, 2],\n\t\t\t[\"X\", 10, 1],\n\t\t\t[\"IX\", 9, 2],\n\t\t\t[\"V\", 5, 1],\n\t\t\t[\"IV\", 4, 2],\n\t\t\t[\"I\", 1, 1]\n\t\t]\n\t\tend = len(s)\n\t\tstart = 0\n\t\ti = 0\n\t\tr = 0\n\t\twhile i < len(units):\n\t\t\tunit = units[i][0]\n\t\t\tvalue = units[i][1]\n\t\t\tstep = units[i][2]\n\t\t\tif end-start >= step and s[start:start+step] == unit:\n\t\t\t\tr += value\n\t\t\t\tstart += step\n\t\t\telse:\n\t\t\t\ti += 1\n\t\treturn r\n# %%\ns = Solution()\nprint(s.romanToInt(\"III\"))",
"status": 1,
"keywords": "哈希表,数学,字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/12/12_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/12/12_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/12/12_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 14,
"question_title": "最长公共前缀",
"difficulty": "简单",
"question_content": "<p>编写一个函数来查找字符串数组中的最长公共前缀。</p><p>如果不存在公共前缀,返回空字符串 <code>\"\"</code>。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>strs = [\"flower\",\"flow\",\"flight\"]<strong><br />输出:</strong>\"fl\"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>strs = [\"dog\",\"racecar\",\"car\"]<strong><br />输出:</strong>\"\"<strong><br />解释:</strong>输入不存在公共前缀。</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= strs.length <= 200</code></li>\t<li><code>0 <= strs[i].length <= 200</code></li>\t<li><code>strs[i]</code> 仅由小写英文字母组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469918",
"cpp": "class Solution\n{\npublic:\n\tstring longestCommonPrefix(vector<string> &strs)\n\t{\n\t\tstring lcp;\n\t\tif (strs.size() == 0)\n\t\t\treturn lcp;\n\t\tint min_len = INT_MAX;\n\t\tint min_idx = 0;\n\t\tfor (int i = 0; i < strs.size(); ++i)\n\t\t{\n\t\t\tauto &s = strs[i];\n\t\t\tif (s.size() < min_len)\n\t\t\t{\n\t\t\t\tmin_len = s.size();\n\t\t\t\tmin_idx = i;\n\t\t\t}\n\t\t}\n\t\tauto &smin = strs[min_idx];\n\t\tfor (int i = 0; i < min_len; ++i)\n\t\t{\n\t\t\tchar c = smin[i];\n\t\t\tint j;\n\t\t\tfor (j = 0; j < strs.size(); ++j)\n\t\t\t{\n\t\t\t\tauto &cs = strs[j];\n\t\t\t\tif (c != cs[i])\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (j == strs.size())\n\t\t\t\tlcp += c;\n\t\t\telse\n\t\t\t\tbreak;\n\t\t}\n\t\treturn lcp;\n\t}\n};",
"java": "class Solution {\n\tpublic String longestCommonPrefix(String[] strs) {\n\t\tif (strs.length == 0) {\n\t\t\treturn \"\";\n\t\t}\n\t\tint i = 0;\n\t\tStringBuilder lcp = new StringBuilder();\n\t\twhile (true) {\n\t\t\tboolean done = false;\n\t\t\tif (i >= strs[0].length()) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tfor (int j = 0; j < strs.length; j++) {\n\t\t\t\tif (i < strs[j].length()) {\n\t\t\t\t\tif (strs[j].charAt(i) != strs[0].charAt(i)) {\n\t\t\t\t\t\tdone = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tdone = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (done) {\n\t\t\t\tbreak;\n\t\t\t} else {\n\t\t\t\tlcp.append(strs[0].charAt(i));\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn lcp.toString();\n\t}\n}",
"js": "/**\n * @param {string[]} strs\n * @return {string}\n */\nvar longestCommonPrefix = function(strs) {\n if(strs.length===0){\n return '';\n }\n \n let i=0; \n let lcp=[];\n while(true){\n let done=false;\n \n if(i>=strs[0].length){\n break;\n }\n \n for(let j=0;j<strs.length;j++){\n if(i<strs[j].length){\n if(strs[j][i]!==strs[0][i]){\n done = true;\n break;\n }\n }else{\n done = true;\n break;\n }\n }\n if(done){\n break;\n }else{\n lcp.push(strs[0][i]);\n i++;\n }\n } \n return lcp.join('');\n};\n",
"python": "from typing import List\nclass Solution:\n\tdef longestCommonPrefix(self, strs: List[str]) -> str:\n\t\tif len(strs) == 0:\n\t\t\treturn ''\n\t\ti = 0\n\t\tlcp = []\n\t\twhile True:\n\t\t\tdone = False\n\t\t\tif i >= len(strs[0]):\n\t\t\t\tbreak\n\t\t\tj = 0\n\t\t\twhile j < len(strs):\n\t\t\t\tif i < len(strs[j]):\n\t\t\t\t\tif strs[j][i] != strs[0][i]:\n\t\t\t\t\t\tdone = True\n\t\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tdone = True\n\t\t\t\t\tbreak\n\t\t\t\tj += 1\n\t\t\tif not done:\n\t\t\t\tlcp.append(strs[0][i])\n\t\t\t\ti += 1\n\t\t\telse:\n\t\t\t\tbreak\n\t\treturn ''.join(lcp)\n# %%\ns = Solution()\nprint(s.longestCommonPrefix(strs = [\"flower\",\"flow\",\"flight\"]))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/13/13_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/13/13_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/13/13_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 15,
"question_title": "三数之和",
"difficulty": "中等",
"question_content": "<p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>,判断 <code>nums</code> 中是否存在三个元素 <em>a,b,c ,</em>使得 <em>a + b + c = </em>0 ?请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意:</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出:</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= nums.length <= 3000</code></li>\t<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470113",
"cpp": "#include <algorithm>\n#include <set>\nclass Solution\n{\npublic:\n\tvector<vector<int>> threeSum(vector<int> &nums)\n\t{\n\t\tvector<vector<int>> r;\n\t\tif (nums.size() == 0)\n\t\t\treturn r;\n\t\tsort(nums.begin(), nums.end());\n\t\tint cur, left, right;\n\t\tcur = 0;\n\t\twhile (cur < nums.size())\n\t\t{\n\t\t\tif (nums[cur] > 0)\n\t\t\t\tbreak;\n\t\t\tleft = cur + 1;\n\t\t\tright = nums.size() - 1;\n\t\t\twhile (left < right)\n\t\t\t{\n\t\t\t\tint n = nums[cur] + nums[left] + nums[right];\n\t\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\t\tr.emplace_back(vector<int>({nums[cur], nums[left], nums[right]}));\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t\tt = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t}\n\t\t\t\telse if (n > 0)\n\t\t\t\t{\n\t\t\t\t\tint t = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint t = cur + 1;\n\t\t\twhile (t < nums.size() && nums[t] == nums[cur])\n\t\t\t\tt++;\n\t\t\tcur = t;\n\t\t}\n\t\treturn r;\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> threeSum(int[] nums) {\n\t\tArrays.sort(nums);\n\t\tList<List<Integer>> result = new ArrayList<>();\n\t\tint cur, left, right;\n\t\tcur = 0;\n\t\twhile (cur < nums.length) {\n\t\t\tif (nums[cur] > 0)\n\t\t\t\tbreak;\n\t\t\tleft = cur + 1;\n\t\t\tright = nums.length - 1;\n\t\t\twhile (left < right) {\n\t\t\t\tint n = nums[cur] + nums[left] + nums[right];\n\t\t\t\tif (n == 0) {\n\t\t\t\t\tList<Integer> r = new ArrayList<Integer>();\n\t\t\t\t\tr.add(nums[cur]);\n\t\t\t\t\tr.add(nums[left]);\n\t\t\t\t\tr.add(nums[right]);\n\t\t\t\t\tresult.add(r);\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t\tt = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t} else if (n > 0) {\n\t\t\t\t\tint t = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t} else {\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint t = cur + 1;\n\t\t\twhile (t < nums.length && nums[t] == nums[cur])\n\t\t\t\tt++;\n\t\t\tcur = t;\n\t\t}\n\t\treturn result;\n\t}\n}",
"js": "\n\n\nconst fs = require('fs');\nconst path = require('path');\n\n/**\n * @param {number[]} nums\n * @return {number[][]}\n */\nvar threeSum = function(nums) {\n \n var result = [], \n unique = {},\n i,j,k,right,left,inv={}, last, list, hash;\n\n nums.sort((a,b)=>a-b);\n\n for(i=0;i<nums.length;i++){\n if(left==null&&nums[i]>=0){\n left = i;\n }\n if(right==null&&nums[i]>0){\n right = i;\n }\n inv[nums[i]] = i;\n }\n\n if(left===0){\n right = nums.length;\n }\n\n for(i=0;i<right;i++){\n\n // 1. a<=b<=c\n // a<=b<=0-a-b\n // b<=0-a-b\n // 2b<=0-a\n // 2b<=-a\n // \n // 2. c<=a<=b\n // 0-a-b<=a<=b\n // 0-a-b<=b\n // 0-a<=2b\n // -a<=2b\n // \n for(j=i+1; j<nums.length && (-nums[i] >= nums[j] * 2); j++){\n last = 0-nums[i]-nums[j];\n\n // do no use index of\n k = inv[last];\n if(k>j){\n list = [nums[i],nums[j],last];\n hash = `${list[0]}_${list[1]}_${list[2]}`;\n\n // do not use index of\n if(!unique[hash]){\n unique[hash] = true;\n result.push(list);\n }\n }\n }\n }\n\n return result;\n};\n\nlet testcases = [\n JSON.parse(fs.readFileSync(path.join(__dirname,'input.txt'))),\n // [-13,5,13,12,-2,-11,-1,12,-3,0,-3,-7,-7,-5,-3,-15,-2,14,14,13,6,-11,-11,5,-15,-14,5,-5,-2,0,3,-8,-10,-7,11,-5,-10,-5,-7,-6,2,5,3,2,7,7,3,-10,-2,2,-12,-11,-1,14,10,-9,-15,-8,-7,-9,7,3,-2,5,11,-13,-15,8,-3,-7,-12,7,5,-2,-6,-3,-10,4,2,-5,14,-3,-1,-10,-3,-14,-4,-3,-7,-4,3,8,14,9,-2,10,11,-10,-4,-15,-9,-1,-1,3,4,1,8,1],\n // [-2,0,0,2,2], // [[ -2, 0, 2 ]]\n // [0,0,0], // [[0,0,0]]\n // [3,0,-2,-1,1,2], // [[-2,-1,3],[-2,0,2],[-1,0,1]]\n];\n\n\nfor(const c of testcases){\n let v = threeSum(c);\n // console.log(v);\n console.log(v.length);\n}\n\n\n\n\n\n\n\n",
"python": "from typing import List\nclass Solution:\n\tdef threeSum(self, nums: List[int]) -> List[List[int]]:\n\t\tif len(nums) == 0:\n\t\t\treturn []\n\t\tresult = []\n\t\tunique = {}\n\t\tinv = {}\n\t\tleft = None\n\t\tright = None\n\t\tnums.sort()\n\t\ti = 0\n\t\twhile i < len(nums):\n\t\t\tif left == None and nums[i] >= 0:\n\t\t\t\tleft = i\n\t\t\tif right == None and nums[i] > 0:\n\t\t\t\tright = i\n\t\t\tinv[nums[i]] = i\n\t\t\ti += 1\n\t\tif left == 0:\n\t\t\tright = len(nums)\n\t\tif right is None:\n\t\t\treturn []\n\t\ti = 0\n\t\twhile i < right:\n\t\t\tj = i+1\n\t\t\twhile j < len(nums) and (-nums[i] >= nums[j] * 2):\n\t\t\t\tlast = 0-nums[i]-nums[j]\n\t\t\t\tk = inv.get(last)\n\t\t\t\tif k and k > j:\n\t\t\t\t\tlist = [nums[i], nums[j], last]\n\t\t\t\t\thash = f'{list[0]}_{list[1]}_{list[2]}'\n\t\t\t\t\tif unique.get(hash) is None:\n\t\t\t\t\t\tunique[hash] = True\n\t\t\t\t\t\tresult.append(list)\n\t\t\t\tj += 1\n\t\t\ti += 1\n\t\treturn result\n# %%\ns = Solution()\nprint(s.threeSum(nums = [-1,0,1,2,-1,-4]))",
"status": 1,
"keywords": "数组,双指针,排序",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/14/14_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/14/14_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/14/14_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 16,
"question_title": "最接近的三数之和",
"difficulty": "中等",
"question_content": "<p>给定一个包括&nbsp;<em>n</em> 个整数的数组&nbsp;<code>nums</code><em>&nbsp;</em>和 一个目标值&nbsp;<code>target</code>。找出&nbsp;<code>nums</code><em>&nbsp;</em>中的三个整数,使得它们的和与&nbsp;<code>target</code>&nbsp;最接近。返回这三个数的和。假定每组输入只存在唯一答案。</p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong>nums = [-1,2,1,-4], target = 1<strong><br />输出:</strong>2<strong><br />解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>\t<li><code>3 &lt;= nums.length &lt;= 10^3</code></li>\t<li><code>-10^3&nbsp;&lt;= nums[i]&nbsp;&lt;= 10^3</code></li>\t<li><code>-10^4&nbsp;&lt;= target&nbsp;&lt;= 10^4</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471102",
"cpp": "#include <cstdlib>\nclass Solution\n{\npublic:\n\tint threeSumClosest(vector<int> &nums, int target)\n\t{\n\t\tsort(nums.begin(), nums.end());\n\t\tint cur, left, right;\n\t\tcur = 0;\n\t\tint closest = nums[0] + nums[1] + nums[2];\n\t\twhile (cur < nums.size() - 2)\n\t\t{\n\t\t\tleft = cur + 1;\n\t\t\tright = nums.size() - 1;\n\t\t\tint n;\n\t\t\twhile (left < right)\n\t\t\t{\n\t\t\t\tn = nums[cur] + nums[left] + nums[right];\n\t\t\t\tif (abs(target - n) < abs(target - closest))\n\t\t\t\t{\n\t\t\t\t\tclosest = n;\n\t\t\t\t}\n\t\t\t\tif (n == target)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (n > target)\n\t\t\t\t{\n\t\t\t\t\tint t = right - 1;\n\t\t\t\t\twhile (t > left && nums[t] == nums[right])\n\t\t\t\t\t\tt--;\n\t\t\t\t\tright = t;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tint t = left + 1;\n\t\t\t\t\twhile (t < right && nums[t] == nums[left])\n\t\t\t\t\t\tt++;\n\t\t\t\t\tleft = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint t = cur + 1;\n\t\t\twhile (t < nums.size() && nums[t] == nums[cur])\n\t\t\t\tt++;\n\t\t\tcur = t;\n\t\t}\n\t\treturn closest;\n\t}\n};",
"java": "class Solution {\n\tint oneSumCloset(int[] nums, int i, int j, int start, int end, int target) {\n\t\tif (start == i || start == j)\n\t\t\tstart = start + 1;\n\t\tif (end == i || end == j)\n\t\t\tend = end - 1;\n\t\tif (start == end) {\n\t\t\treturn nums[start];\n\t\t} else if (end == start + 1 || end == start - 1) {\n\t\t\tif (Math.abs(nums[end] - target) > Math.abs(nums[start] - target)) {\n\t\t\t\treturn nums[start];\n\t\t\t} else {\n\t\t\t\treturn nums[end];\n\t\t\t}\n\t\t} else {\n\t\t\tint middle = (int) Math.floor((start + end) / 2);\n\t\t\tif (nums[middle] > target) {\n\t\t\t\tend = middle;\n\t\t\t} else {\n\t\t\t\tstart = middle;\n\t\t\t}\n\t\t\treturn oneSumCloset(nums, i, j, start, end, target);\n\t\t}\n\t}\n\tpublic int threeSumClosest(int[] nums, int target) {\n\t\tArrays.sort(nums);\n\t\tint minValue = 0;\n\t\tboolean hasMin = false;\n\t\tfor (int i = 0; i < nums.length - 2; i++) {\n\t\t\tfor (int j = i + 1; j < nums.length - 1; j++) {\n\t\t\t\tint twoSum = nums[i] + nums[j];\n\t\t\t\tint rest = target - twoSum;\n\t\t\t\tint restClost = oneSumCloset(nums, i, j, j + 1, nums.length - 1, rest);\n\t\t\t\tint newValue = restClost + twoSum;\n\t\t\t\t;\n\t\t\t\tif (!hasMin) {\n\t\t\t\t\tminValue = newValue;\n\t\t\t\t\thasMin = true;\n\t\t\t\t} else {\n\t\t\t\t\tint d1 = Math.abs(minValue - target);\n\t\t\t\t\tint d2 = Math.abs(newValue - target);\n\t\t\t\t\tif (d1 > d2) {\n\t\t\t\t\t\tminValue = newValue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn minValue;\n\t}\n}",
"js": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar threeSumClosest = function(nums, target) {\n\n\tconst oneSumCloset = (nums, i, j, start, end, target)=>{\n\t\tif(start===i||start===j) start = start+1;\n\t\tif(end===i||end===j) end = end-1;\n\t\t\n\t\tif(start===end){\n\t\t\treturn nums[start];\n\t\t}else if(end===start+1||end===start-1){\n\t\t\tif(Math.abs(nums[end]-target)>Math.abs(nums[start]-target)){\n\t\t\t\treturn nums[start];\n\t\t\t}else{\n\t\t\t\treturn nums[end];\n\t\t\t}\n\t\t}else{\n\t\t\tconst middle = Math.floor((start+end)/2);\n\t\t\tif(nums[middle]>target){\n\t\t\t\tend = middle;\n\t\t\t}else{\n\t\t\t\tstart = middle;\n\t\t\t}\n\t\t\treturn oneSumCloset(nums, i, j, start, end, target);\t\n\t\t}\n\t}\n\n nums.sort((a,b)=>a-b);\n\n let minValue;\n for(let i=0;i<nums.length-2;i++){\n \tfor(let j=i+1;j<nums.length-1;j++){\n\t\t\tconst twoSum = nums[i]+nums[j];\n\t\t\tconst rest = target - twoSum;\n\t\t\tconst restClost = oneSumCloset(nums, i, j, j+1, nums.length-1, rest);\n\t\t\tconst newValue = restClost+twoSum;;\n\t\t\t\n\t\t\tif(minValue==null){\n\t\t\t\tminValue = newValue;\n\t\t\t}else{\n\t\t\t\tconst d1 = Math.abs(minValue-target);\n\t\t\t\tconst d2 = Math.abs(newValue-target);\n\t\t\t\tif(d1>d2){\n \t\t\t\tminValue = newValue;\n \t\t\t}\n\t\t\t}\n \t}\n }\n\n return minValue;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[[-1,2,1,-4], 1, 2],\n\t\t[[-1,2,1,-4], -5, -4],\n\t\t[[0,2,1,-3],1,0],\n\t\t[[1,1,-1,-1,3],1,1],\n\t\t[[1,2,5,10,11],12,13],\n\t\t[\n\t\t\t[-73,-26,10,-40,-74,81,20,\n\t\t\t -52,80,32,-17,-20,19,34,-2,\n\t\t\t 94,-81,-66,-17,93,26,36,54,\n\t\t\t 40,40,32,3,77,-30,14,-28,-74,\n\t\t\t -60,-99,11,0,-31,-84,90,-51,\n\t\t\t -29,87,-67,55,6,96,9,-76,75,\n\t\t\t -44,32,89,13,46,29,66,-12,-90,\n\t\t\t 81,43,-46,54,74,22,70,-66,-43,97,93,\n\t\t\t -26,6,45,9,-64,-11,-43,-78,44,-92,98,-65,29,83,-30,35,-63,\n\t\t\t 13,-92,-9,79,95,-44,50,55,87,-69,81,-91,-57,5,-9,65,42,11,78,-4,-43,10,1,0,\n\t\t\t 50,-37,100,61,-82,-13,100,46,\n\t\t\t 0,-25,13,16,43,49,92,31,85,38,\n\t\t\t -63,6,-30,67,64,-4,-71,-74,-92,6,\n\t\t\t -50,-45,-71,-82,11,-39],\n\t\t\t299,\n\t\t\t298,\n\t\t],\n\t\t[\n\t\t\t[-21,7,-38,-13,15,30,-70,-48,-89,-11,\n\t\t\t-88,-92,-44,35,100,-86,42,41,-44,-94,\n\t\t\t-82,6,10,-61,78,35,99,24,-20,-81,76,\n\t\t\t-73,-92,-30,0,-95,46,99,20,-89,99,95,\n\t\t\t-83,-5,43,55,85,-70,73,10,90,87,71,\n\t\t\t24,78,-54,-35,4,33,52,-28,28,-66,-88,\n\t\t\t-29,-44,35,-54,-83,63,-90,-68,-31,-7,\n\t\t\t48,7,-99,-21,-99,-35,39,-12,77,-79,76,\n\t\t\t85,24,89,90,-86,88,81,94,82,43,-52,63,\n\t\t\t-55,-96,64,7,-2,32,-18,-19,25,63,-24,\n\t\t\t-40,81,-22,-30,62,90,-50,-95,33,20,19,\n\t\t\t87,-9,77,-50,61,-73,-4,-40,12,51,25,\n\t\t\t-21,91,-48,-86,-82,-38,15,-96,48,-20,\n\t\t\t-30,82,24,5,-61,8,-49,69,41,97,24,-46,\n\t\t\t-49,-71,30,84,48,97,91,-80,-65,32,52,\n\t\t\t-76,87,-82,-40,15,-35,-51,-76,92,31,\n\t\t\t-61,-22,-5,94,-66,3,-4,46,32,-13,40,\n\t\t\t-66,-13],\n\t\t\t-155,\n\t\t\t-155,\n\t\t]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = threeSumClosest(testCase[0], testCase[1]);\n\t\tif(v===testCase[2]){\n\t\t\t// console.log(`[OK]: ${v}===${testCase[2]}`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]: ${v}!==${testCase[2]}`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n}\n\nmain();\n\n",
"python": "from typing import List\nclass Solution:\n\tdef threeSumClosest(self, nums: List[int], target: int) -> int:\n\t\tnums.sort()\n\t\tcur = 0\n\t\tclosest = nums[0] + nums[1] + nums[2]\n\t\twhile cur < len(nums) - 2:\n\t\t\tleft = cur + 1\n\t\t\tright = len(nums) - 1\n\t\t\twhile left < right:\n\t\t\t\tn = nums[cur] + nums[left] + nums[right]\n\t\t\t\tif abs(target - n) < abs(target - closest):\n\t\t\t\t\tclosest = n\n\t\t\t\tif n == target:\n\t\t\t\t\tbreak\n\t\t\t\telif n > target:\n\t\t\t\t\tt = right - 1\n\t\t\t\t\twhile (t > left and nums[t] == nums[right]):\n\t\t\t\t\t\tt -= 1\n\t\t\t\t\tright = t\n\t\t\t\telse:\n\t\t\t\t\tt = left + 1\n\t\t\t\t\twhile (t < right and nums[t] == nums[left]):\n\t\t\t\t\t\tt += 1\n\t\t\t\t\tleft = t\n\t\t\tt = cur + 1\n\t\t\twhile (t < len(nums) and nums[t] == nums[cur]):\n\t\t\t\tt += 1\n\t\t\tcur = t\n\t\treturn closest\n# %%\ns = Solution()\nprint(s.threeSumClosest(nums = [-1,2,1,-4], target = 1))",
"status": 1,
"keywords": "数组,双指针,排序",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/15/15_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/15/15_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/15/15_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 17,
"question_title": "电话号码的字母组合",
"difficulty": "中等",
"question_content": "<p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png\" style=\"width: 200px;\" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = \"23\"<strong><br />输出:</strong>[\"ad\",\"ae\",\"af\",\"bd\",\"be\",\"bf\",\"cd\",\"ce\",\"cf\"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = \"\"<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = \"2\"<strong><br />输出:</strong>[\"a\",\"b\",\"c\"]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= digits.length <= 4</code></li>\t<li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471003",
"cpp": "class Solution\n{\npublic:\n\tvector<string> letterCombinations(string digits)\n\t{\n\t\tvector<string> nummap({\" \",\n\t\t\t\t\t\t\t \"\",\n\t\t\t\t\t\t\t \"abc\",\n\t\t\t\t\t\t\t \"def\",\n\t\t\t\t\t\t\t \"ghi\",\n\t\t\t\t\t\t\t \"jkl\",\n\t\t\t\t\t\t\t \"mno\",\n\t\t\t\t\t\t\t \"pqrs\",\n\t\t\t\t\t\t\t \"tuv\",\n\t\t\t\t\t\t\t \"wxyz\"});\n\t\tvector<string> rs;\n\t\tvector<string> empty;\n\t\tif (digits.size() == 0)\n\t\t\treturn empty;\n\t\tfor (auto d : digits)\n\t\t{\n\t\t\tif (d == '0')\n\t\t\t\treturn empty;\n\t\t\tif (d == '1')\n\t\t\t\treturn empty;\n\t\t\tauto &s = nummap[d - '0'];\n\t\t\tif (s.size() == 0)\n\t\t\t\tcontinue;\n\t\t\tif (rs.size() == 0)\n\t\t\t\tfor (auto c : s)\n\t\t\t\t{\n\t\t\t\t\tstring t;\n\t\t\t\t\tt.push_back(c);\n\t\t\t\t\trs.emplace_back(t);\n\t\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tvector<string> rn;\n\t\t\t\tfor (auto c : s)\n\t\t\t\t{\n\t\t\t\t\tfor (auto r : rs)\n\t\t\t\t\t{\n\t\t\t\t\t\tr.push_back(c);\n\t\t\t\t\t\trn.emplace_back(std::move(r));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tstd::swap(rs, rn);\n\t\t\t}\n\t\t}\n\t\treturn rs;\n\t}\n};",
"java": "class Solution {\n\tpublic List<String> letterCombinations(String digits) {\n\t\tCharacter[][] letters = { {}, {}, { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' },\n\t\t\t\t{ 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' }, };\n\t\tList<List<Character>> combinations = new ArrayList<>();\n\t\tfor (int i = 0; i < digits.length(); i++) {\n\t\t\tCharacter d = digits.charAt(i);\n\t\t\tint index = Character.getNumericValue(d);\n\t\t\tCharacter[] letter = letters[index];\n\t\t\tSystem.out.println(d);\n\t\t\tif (i == 0) {\n\t\t\t\tfor (int j = 0; j < letter.length; j++) {\n\t\t\t\t\tList<Character> c = new ArrayList<>();\n\t\t\t\t\tc.add(letter[j]);\n\t\t\t\t\tcombinations.add(c);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tList<List<Character>> added = new ArrayList<>();\n\t\t\t\tfor (int j = 0; j < combinations.size(); j++) {\n\t\t\t\t\tList<Character> c = combinations.get(j);\n\t\t\t\t\tList<Character> origin_c = new ArrayList<>(c);\n\t\t\t\t\tfor (int k = 0; k < letter.length; k++) {\n\t\t\t\t\t\tCharacter l = letter[k];\n\t\t\t\t\t\tif (k == 0) {\n\t\t\t\t\t\t\tc.add(l);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tList<Character> new_c = new ArrayList<>(origin_c);\n\t\t\t\t\t\t\tnew_c.add(l);\n\t\t\t\t\t\t\tadded.add(new_c);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcombinations.addAll(added);\n\t\t\t}\n\t\t}\n\t\tList<String> output = new ArrayList<>();\n\t\tfor (int i = 0; i < combinations.size(); i++) {\n\t\t\tList<Character> c = combinations.get(i);\n\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\tfor (Character l : c) {\n\t\t\t\tsb.append(l);\n\t\t\t}\n\t\t\toutput.add(sb.toString());\n\t\t}\n\t\treturn output;\n\t}\n}",
"js": "\n/**\n * @param {string} digits\n * @return {string[]}\n */\nvar letterCombinations = function(digits) {\n const letters = [\n \t[],\n \t[],\n \t['a','b','c'],\n \t['d','e','f'],\n \t['g','h','i'],\n \t['j','k','l'],\n \t['m','n','o'],\n \t['p','q','r','s'],\n \t['t','u','v'],\n \t['w','x','y','z'],\n ];\n\n let combinations = [];\n let i=0;\n for(let d of digits){\n \tconst letter = letters[parseInt(d)];\n\t\tif(i===0){\n\t\t\tfor(let l of letter){\n\t\t\t\tcombinations.push([l]);\n\t\t\t}\t\n\t\t}else{\n\t\t\tlet added = [];\n\t\t\tfor(let c of combinations){\n\t\t\t\tlet j=0;\n\t\t\t\tlet ccc = [...c];\n\t\t\t\tfor(let l of letter){\n\t\t\t\t\tif(j===0){\n\t\t\t\t\t\tc.push(l);\n\t\t\t\t\t}else{\n\t\t\t\t\t\tadded.push([...ccc, l]);\n\t\t\t\t\t}\n\t\t\t\t\tj++;\n\t\t\t\t}\n\t\t\t}\t\n\t\t\tcombinations = [...combinations, ...added];\n\t\t}\n\t\ti++;\n }\n\n let output=[];\n \tfor(const c of combinations){\n \t\toutput.push(c.join(''));\n \t}\n \treturn output;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\"23\",[\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"]]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = letterCombinations(testCase[0]);\n\t\tif(JSON.stringify(v.sort())===JSON.stringify(testCase[1].sort())){\n\t\t\tconsole.log(`[OK]:${v}===${testCase[1]}`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]:${v}===${testCase[1]}`);\n\t\t}\n\t}\n}\n\nmain();\n",
"python": "from typing import List\nclass Solution:\n\tdef letterCombinations(self, digits: str) -> List[str]:\n\t\tletters = [\n\t\t\t[],\n\t\t\t[],\n\t\t\t['a', 'b', 'c'],\n\t\t\t['d', 'e', 'f'],\n\t\t\t['g', 'h', 'i'],\n\t\t\t['j', 'k', 'l'],\n\t\t\t['m', 'n', 'o'],\n\t\t\t['p', 'q', 'r', 's'],\n\t\t\t['t', 'u', 'v'],\n\t\t\t['w', 'x', 'y', 'z'],\n\t\t]\n\t\tcombinations = []\n\t\ti = 0\n\t\tfor d in digits:\n\t\t\tletter = letters[int(d)]\n\t\t\tif i == 0:\n\t\t\t\tfor l in letter:\n\t\t\t\t\tcombinations.append([l])\n\t\t\telse:\n\t\t\t\tadded = []\n\t\t\t\tfor c in combinations:\n\t\t\t\t\tj = 0\n\t\t\t\t\torigin_c = []\n\t\t\t\t\torigin_c += c\n\t\t\t\t\tfor l in letter:\n\t\t\t\t\t\tif j == 0:\n\t\t\t\t\t\t\tc.append(l)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tnew_c = []\n\t\t\t\t\t\t\tnew_c += origin_c\n\t\t\t\t\t\t\tnew_c.append(l)\n\t\t\t\t\t\t\tadded.append(new_c)\n\t\t\t\t\t\tj += 1\n\t\t\t\tcombinations += added\n\t\t\ti += 1\n\t\toutput = []\n\t\tfor c in combinations:\n\t\t\toutput.append(''.join(c))\n\t\treturn output\n# %%\ns = Solution()\nprint(s.letterCombinations(digits = \"23\"))",
"status": 1,
"keywords": "哈希表,字符串,回溯",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/16/16_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/16/16_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/16/16_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 18,
"question_title": "四数之和",
"difficulty": "中等",
"question_content": "<p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= nums.length <= 200</code></li>\t<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\t<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469919",
"cpp": "class Solution\n{\npublic:\n\tvector<vector<int>> fourSum(vector<int> &nums, int target)\n\t{\n\t\tlong long l_target = target;\n\t\tsort(nums.begin(), nums.end());\n\t\tvector<vector<int>> results;\n\t\tint N = nums.size();\n\t\tfor (int i = 0; i < N - 3; i++)\n\t\t{\n\t\t\tif (i > 0 && nums[i] == nums[i - 1])\n\t\t\t\tcontinue;\n\t\t\tfor (int j = i + 1; j < N - 2; j++)\n\t\t\t{\n\t\t\t\tif (j > i + 1 && nums[j] == nums[j - 1])\n\t\t\t\t\tcontinue;\n\t\t\t\tfor (int k = j + 1, l = N - 1; k < l; k++)\n\t\t\t\t{\n\t\t\t\t\tif (k > j + 1 && nums[k] == nums[k - 1])\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\twhile (k < l &&\n\t\t\t\t\t\t (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tl--;\n\t\t\t\t\t}\n\t\t\t\t\tif (k >= l)\n\t\t\t\t\t{\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tresults.emplace_back(\n\t\t\t\t\t\t\tvector<int>({nums[i], nums[j], nums[k], nums[l]}));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn results;\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> fourSum(int[] nums, int target) {\n\t\tlong l_target = target;\n\t\tArrays.sort(nums);\n\t\tList<List<Integer>> results = new ArrayList<>();\n\t\tint N = nums.length;\n\t\tfor (int i = 0; i < N - 3; i++) {\n\t\t\tif (i > 0 && nums[i] == nums[i - 1])\n\t\t\t\tcontinue;\n\t\t\tfor (int j = i + 1; j < N - 2; j++) {\n\t\t\t\tif (j > i + 1 && nums[j] == nums[j - 1])\n\t\t\t\t\tcontinue;\n\t\t\t\tfor (int k = j + 1, l = N - 1; k < l; k++) {\n\t\t\t\t\tif (k > j + 1 && nums[k] == nums[k - 1])\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\twhile (k < l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0) {\n\t\t\t\t\t\tl--;\n\t\t\t\t\t}\n\t\t\t\t\tif (k >= l) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0) {\n\t\t\t\t\t\tList<Integer> item = new ArrayList<>();\n\t\t\t\t\t\titem.add(nums[i]);\n\t\t\t\t\t\titem.add(nums[j]);\n\t\t\t\t\t\titem.add(nums[k]);\n\t\t\t\t\t\titem.add(nums[l]);\n\t\t\t\t\t\tresults.add(item);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn results;\n\t}\n}",
"js": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[][]}\n */\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[][]}\n */\n var fourSum = function(nums, target) {\n nums.sort((a,b)=>a-b);\n\n let results = [];\n for(let i=0;i<nums.length-3;i++){\n\t\tif(i>0&&nums[i]===nums[i-1]) continue;\n \tfor(let j=i+1;j<nums.length-2;j++){\n\t\t\tif(j>i+1&&nums[j]===nums[j-1]) continue;\n \t\tfor(let k=j+1, l=nums.length-1;k<l;k++){\n\t\t\t\tif(k>j+1&&nums[k]===nums[k-1]) continue;\n\n\t\t\t\twhile(k<l && (target - nums[i] - nums[j] - nums[k] - nums[l])<0){\n\t\t\t\t\tl--;\n\t\t\t\t}\n\n\t\t\t\tif(k>=l){\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tif((target - nums[i] - nums[j] - nums[k] - nums[l])===0){\n\t\t\t\t\tresults.push([\n\t\t\t\t\t\tnums[i], nums[j], nums[k], nums[l]\n\t\t\t\t\t]);\n\t\t\t\t}\n \t\t}\n \t}\n }\n return results;\n};\n\nfunction main(){\n\tconst testCases =[\n\t\t[\n\t\t\t[1, 0, -1, 0, -2, 2], 0,\n\t\t\t[\n\t\t\t [-1, 0, 0, 1],\n\t\t\t [-2, -1, 1, 2],\n\t\t\t [-2, 0, 0, 2]\n\t\t\t]\n\t\t],\n\n\t\t[\n\t\t\t[-4,-3,-1,-2,-2,0,-3,0,1,3,3], 0,\n\t\t\t[\n\t\t\t\t[-3,-2,2,3],\n\t\t\t\t[-3,-1,1,3],\n\t\t\t\t[-3,0,0,3],\n\t\t\t\t[-3,0,1,2],\n\t\t\t\t[-2,-1,0,3],\n\t\t\t\t[-1,0,0,1],\n\n\t\t\t\t[-2,-1,1,2],\n\t\t\t\t[-2,0,0,2],\n\t\t\t]\n\t\t],\n\n\t\t[\n\t\t\t[-3,-2,-1,0,-3,0,1,2,3], 0,\n\t\t\t[\n\t\t\t\t[-3,-2,2,3],\n\t\t\t\t[-3,-1,1,3],\n\t\t\t\t[-3,0,0,3],\n\t\t\t\t[-3,0,1,2],\n\t\t\t\t[-2,-1,0,3],\n\t\t\t\t[-1,0,0,1],\n\n\t\t\t\t[-2,-1,1,2],\n\t\t\t\t[-2,0,0,2],\n\t\t\t]\n\t\t],\n\n\t\t[\n\t\t\t[0,1,5,0,1,5,5,-4],11,\n\t\t\t[\n\t\t\t\t[-4,5,5,5],\n\t\t\t\t[0,1,5,5]\n\t\t\t]\n\t\t],\n\n\t\t[\n\t\t\t[-489,-475,-469,-468,-467,\n\t\t\t -462,-456,-443,-439,-425,\n\t\t\t -425,-410,-401,-342,-341,\n\t\t\t -331,-323,-307,-299,-262,\n\t\t\t -254,-245,-244,-238,-229,\n\t\t\t -227,-225,-224,-221,-197,\n\t\t\t -173,-171,-160,-142,-142,\n\t\t\t -136,-134,-125,-114,-100,\n\t\t\t -86,-81,-66,-47,-37,-34,4,\n\t\t\t 7,11,34,60,76,99,104,113,\n\t\t\t 117,124,139,141,143,144,\n\t\t\t 146,157,157,178,183,185,189,\n\t\t\t 192,194,221,223,226,232,247,\n\t\t\t 249,274,281,284,293,298,319,\n\t\t\t 327,338,340,368,375,377,379,\n\t\t\t 388,390,392,446,469,480,490]\n\t\t\t, 2738\n\t\t\t, []\n\t\t]\n\t];\n\n\tfor(const testCase of testCases){\n\t\tconst v = fourSum(testCase[0], testCase[1]);\n\n\t\tconsole.log(v);\n\n\t\tif(v.length===testCase[2].length){\n\t\t\tconsole.log(`[OK]:${v.length}===${testCase[2].length}`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]:${v.length}===${testCase[2].length}`);\n\t\t}\n\t}\n}\n\nmain();\n",
"python": "class Solution(object):\n\tdef fourSum(self, nums, target):\n\t\t\"\"\"\n\t\t:type nums: List[int]\n\t\t:type target: int\n\t\t:rtype: List[List[int]]\n\t\t\"\"\"\n\t\tnums.sort()\n\t\tresults = []\n\t\tN = len(nums)\n\t\ti = 0\n\t\twhile i < N-3:\n\t\t\tif i > 0 and nums[i] == nums[i-1]:\n\t\t\t\ti += 1\n\t\t\t\tcontinue\n\t\t\tj = i+1\n\t\t\twhile j < N-2:\n\t\t\t\tif j > i+1 and nums[j] == nums[j-1]:\n\t\t\t\t\tj += 1\n\t\t\t\t\tcontinue\n\t\t\t\tk = j+1\n\t\t\t\tl = N-1\n\t\t\t\twhile k < l:\n\t\t\t\t\tif k > j+1 and nums[k] == nums[k-1]:\n\t\t\t\t\t\tk += 1\n\t\t\t\t\t\tcontinue\n\t\t\t\t\twhile k < l and (target - nums[i] - nums[j] - nums[k] - nums[l]) < 0:\n\t\t\t\t\t\tl -= 1\n\t\t\t\t\tif k >= l:\n\t\t\t\t\t\tbreak\n\t\t\t\t\tif target == nums[i] + nums[j] + nums[k] + nums[l]:\n\t\t\t\t\t\tresults.append([\n\t\t\t\t\t\t\tnums[i],\n\t\t\t\t\t\t\tnums[j],\n\t\t\t\t\t\t\tnums[k],\n\t\t\t\t\t\t\tnums[l]\n\t\t\t\t\t\t])\n\t\t\t\t\tk += 1\n\t\t\t\tj += 1\n\t\t\ti += 1\n\t\treturn results\n# %%\ns = Solution()\nprint(s.fourSum(nums = [1,0,-1,0,-2,2], target = 0))",
"status": 1,
"keywords": "数组,双指针,排序",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/17/17_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/17/17_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/17/17_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 19,
"question_title": "删除链表的倒数第 N 个结点",
"difficulty": "中等",
"question_content": "<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p><p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], n = 2<strong><br />输出:</strong>[1,2,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [1], n = 1<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2], n = 1<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中结点的数目为 <code>sz</code></li>\t<li><code>1 <= sz <= 30</code></li>\t<li><code>0 <= Node.val <= 100</code></li>\t<li><code>1 <= n <= sz</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469818",
"cpp": "struct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\n#include <vector>\nclass Solution\n{\npublic:\n\tListNode *removeNthFromEnd(ListNode *head, int n)\n\t{\n\t\tListNode empty_node(0, head);\n\t\tListNode *p = &empty_node;\n\t\tstd::vector<ListNode *> pv;\n\t\twhile (p != nullptr)\n\t\t{\n\t\t\tpv.push_back(p);\n\t\t\tp = p->next;\n\t\t}\n\t\tp = pv[pv.size() - 1 - n];\n\t\tp->next = p->next->next;\n\t\treturn empty_node.next;\n\t}\n};",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode() {\n\t}\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\nclass Solution {\n\tpublic ListNode removeNthFromEnd(ListNode head, int n) {\n\t\tListNode v = new ListNode(0, head);\n\t\tListNode handle = v;\n\t\tList<ListNode> index = new ArrayList<>();\n\t\twhile (v != null) {\n\t\t\tindex.add(v);\n\t\t\tv = v.next;\n\t\t}\n\t\tint pre = index.size() - n - 1;\n\t\tint next = index.size() - n + 1;\n\t\tindex.get(pre).next = next >= 0 && next < index.size() ? index.get(next) : null;\n\t\treturn handle.next;\n\t}\n}",
"js": "/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\n var removeNthFromEnd = function(head, n) {\n let v = {val:0, next: head};\n\tlet handle = v;\n\tlet index = [];\n\twhile(v!=null){\n\t\tindex.push(v);\n\t\tv = v.next;\n\t}\n\n\tconst pre = index.length-n-1;\n\tconst next = index.length-n+1;\n\tindex[pre].next = index[next]===undefined?null:index[next];\n\t\n return handle.next;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\n\t\t\t{val:1},\n\t\t\t1,\n\t\t\tundefined\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:1}},\n\t\t\t2,\n\t\t\t{val:1}\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:2,next:{val:3,next:{val:4,next:{val:5}}}}},\n\t\t\t2,\n\t\t\t{val:1,next:{val:2,next:{val:3,next:{val:5}}}},\n\t\t]\n\t];\t\n\tfor(const testCase of testCases){\n\t\tconst output = removeNthFromEnd(testCase[0], testCase[1]);\n\t\tif(JSON.stringify(output)===JSON.stringify(testCase[2])){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t}\n}\n\nmain();\n",
"python": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:\n\t\tv = ListNode(0, head)\n\t\thandle = v\n\t\tindex = []\n\t\twhile v is not None:\n\t\t\tindex.append(v)\n\t\t\tv = v.next\n\t\tpre = len(index)-n-1\n\t\tnext = len(index)-n+1\n\t\tindex[pre].next = index[next] if next >= 0 and next < len(\n\t\t\tindex) else None\n\t\treturn handle.next\n# %%\nl = LinkList()\nlist1 = [1,2,3,4,5]\nhead = l.initList(list1)\nn = 2\ns = Solution()\nprint(l.convert_list(s.removeNthFromEnd(head, n)))",
"status": 1,
"keywords": "链表,双指针",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/18/18_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 20,
"question_title": "有效的括号",
"difficulty": "简单",
"question_content": "<p>给定一个只包括 <code>'('</code>,<code>')'</code>,<code>'{'</code>,<code>'}'</code>,<code>'['</code>,<code>']'</code> 的字符串 <code>s</code> ,判断字符串是否有效。</p><p>有效字符串需满足:</p><ol>\t<li>左括号必须用相同类型的右括号闭合。</li>\t<li>左括号必须以正确的顺序闭合。</li></ol><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"()\"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"()[]{}\"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"(]\"<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \"([)]\"<strong><br />输出:</strong>false</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = \"{[]}\"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length <= 10<sup>4</sup></code></li>\t<li><code>s</code> 仅由括号 <code>'()[]{}'</code> 组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470114",
"cpp": "#include <stack>\nchar ascii_tab[128];\nclass Solution\n{\npublic:\n\tbool isValid(string s)\n\t{\n\t\tif (s.size() == 0)\n\t\t\treturn true;\n\t\tstd::stack<char> st;\n\t\tascii_tab['('] = 11;\n\t\tascii_tab['{'] = 12;\n\t\tascii_tab['['] = 13;\n\t\tascii_tab[')'] = 21;\n\t\tascii_tab['}'] = 22;\n\t\tascii_tab[']'] = 23;\n\t\tfor (auto c : s)\n\t\t{\n\t\t\tchar n = ascii_tab[c];\n\t\t\tif (n < 20)\n\t\t\t\tst.push(n);\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (st.empty())\n\t\t\t\t\treturn false;\n\t\t\t\tif (n != st.top() + 10)\n\t\t\t\t\treturn false;\n\t\t\t\tst.pop();\n\t\t\t}\n\t\t}\n\t\tif (st.empty())\n\t\t\treturn true;\n\t\treturn false;\n\t}\n};",
"java": "class Solution {\n\tpublic boolean isValid(String s) {\n\t\tchar[] parentheses = { '(', '[', '{', ')', ']', '}' };\n\t\tint i = 0;\n\t\tchar c;\n\t\tint[] sum = { 0, 0, 0 };\n\t\tStack<Integer> top = new Stack<Integer>();\n\t\twhile (i < s.length()) {\n\t\t\tc = s.charAt(i);\n\t\t\tfor (int j = 0; j <= 2; j++) {\n\t\t\t\tif (c == parentheses[j]) {\n\t\t\t\t\ttop.push(j);\n\t\t\t\t\tsum[j]++;\n\t\t\t\t} else if (c == parentheses[j + 3]) {\n\t\t\t\t\tif (top.size() == 0 || top.peek() != j) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\ttop.pop();\n\t\t\t\t\tsum[j]--;\n\t\t\t\t} else {\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tfor (int j = 0; j <= 2; j++) {\n\t\t\tif (sum[j] != 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
"js": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar isValid = function(s) {\n \tlet parentheses = [\n \t\t'(', '[', '{', \n \t\t')', ']', '}'\n \t];\n \tlet i=0;\n \tlet c;\n \tlet sum = [0,0,0]\n \tlet top = [];\n \twhile(i<s.length){\n \tc = s[i];\n \tfor(let j=0;j<=2;j++){\n \tif(c===parentheses[j]){\n \t\ttop.push(j);\n \t\tsum[j]++;\n \t}else if(c===parentheses[j+3]){\n \t\tif(top[top.length-1]!==j){\n \t\t\treturn false;\t\n \t\t}\n \t\ttop.pop();\n \t\tsum[j]--;\n \t}else {\n\n \t}\n \t}\n \ti++;\n \t} \n\n \tfor(let j=0;j<=2;j++){\n \t\tif(sum[j]!==0){\n \t\t\treturn false;\n \t\t}\n \t}\n \treturn true;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\"()\",true],\n\t\t[\"()[]{}\",true],\n\t\t[\"(]\",false],\n\t\t[\"([)]\",false],\n\t\t[\"{[]}\",true]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = isValid(testCase[0]);\n\t\tif(v===testCase[1]){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t}\n\t}\n}\n\nmain();\n",
"python": "class Solution:\n\tdef isValid(self, s: str) -> bool:\n\t\tparentheses = [\n\t\t\t'(', '[', '{',\n\t\t\t')', ']', '}'\n\t\t]\n\t\ti = 0\n\t\tsum = [0, 0, 0]\n\t\ttop = []\n\t\twhile i < len(s):\n\t\t\tc = s[i]\n\t\t\tj = 0\n\t\t\twhile j <= 2:\n\t\t\t\tif c == parentheses[j]:\n\t\t\t\t\ttop.append(j)\n\t\t\t\t\tsum[j] += 1\n\t\t\t\telif c == parentheses[j+3]:\n\t\t\t\t\tif len(top) == 0 or top[len(top)-1] != j:\n\t\t\t\t\t\treturn False\n\t\t\t\t\ttop.pop()\n\t\t\t\t\tsum[j] -= 1\n\t\t\t\tj += 1\n\t\t\ti += 1\n\t\tif sum[0] != 0 or sum[1] != 0 or sum[2] != 0:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n# %%\ns = Solution()\nprint(s.isValid(s = \"()[]{}\"))",
"status": 1,
"keywords": "栈,字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/19/19_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 3,
"question_title": "无重复字符的最长子串",
"difficulty": "中等",
"question_content": "<p>给定一个字符串,请你找出其中不含有重复字符的 <strong>最长子串 </strong>的长度。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入: </strong>s = \"abcabcbb\"<strong><br />输出: </strong>3 <strong><br />解释:</strong> 因为无重复字符的最长子串是 \"abc\",所以其长度为 3。</pre><p><strong>示例 2:</strong></p><pre><strong>输入: </strong>s = \"bbbbb\"<strong><br />输出: </strong>1<strong><br />解释: </strong>因为无重复字符的最长子串是 \"b\",所以其长度为 1。</pre><p><strong>示例 3:</strong></p><pre><strong>输入: </strong>s = \"pwwkew\"<strong><br />输出: </strong>3<strong><br />解释: </strong>因为无重复字符的最长子串是 \"wke\",所以其长度为 3。 \n请注意,你的答案必须是 <strong>子串 </strong>的长度,\"pwke\" 是一个<em>子序列,</em>不是子串。</pre><p><strong>示例 4:</strong></p><pre><strong>输入: </strong>s = \"\"<strong><br />输出: </strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= s.length <= 5 * 10<sup>4</sup></code></li>\t<li><code>s</code> 由英文字母、数字、符号和空格组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471011",
"cpp": "int hset[128];\nint lengthOfLongestSubstring(char *s)\n{\n\tint i = 0, j = 0;\n\tint m = 0;\n\tmemset(hset, 0, sizeof hset);\n\tfor (; s[j]; j++)\n\t{\n\t\ti = hset[s[j]] > i ? hset[s[j]] : i;\n\t\tm = m > j - i + 1 ? m : j - i + 1;\n\t\thset[s[j]] = j + 1;\n\t}\n\treturn m;\n}",
"java": "class Solution {\n\tpublic int lengthOfLongestSubstring(String s) {\n\t\tint i = 0;\n\t\tint j = 0;\n\t\tint m = 0;\n\t\tMap<Integer, Integer> hset = new HashMap<>();\n\t\tfor (; j < s.length(); j++) {\n\t\t\tint code = s.codePointAt(j);\n\t\t\tInteger o = hset.get(code);\n\t\t\tif (o != null && o > i) {\n\t\t\t\ti = o;\n\t\t\t}\n\t\t\tm = m > j - i + 1 ? m : j - i + 1;\n\t\t\thset.put(code, j + 1);\n\t\t}\n\t\treturn m;\n\t}\n}",
"js": "\n/**\n * @param {string} s\n * @return {number}\n */\nvar lengthOfLongestSubstring = function(s) {\n let i = 0;\n let j = 0;\n let m = 0;\n let hset = {};\n for (;s[j];j++) {\n i = hset[s[j]] > i ? hset[s[j]] : i;\n m = m > j - i + 1 ? m : j - i + 1;\n hset[s[j]] = j + 1;\n }\n return m;\n};\n",
"python": "class Solution:\n\tdef lengthOfLongestSubstring(self, s: str) -> int:\n\t\ti = 0\n\t\tj = 0\n\t\tm = 0\n\t\thset = {}\n\t\twhile j < len(s):\n\t\t\tchar = s[j]\n\t\t\tindex = hset.get(char)\n\t\t\tif index is not None and index > i:\n\t\t\t\ti = index\n\t\t\tm = m if m > j - i + 1 else j - i + 1\n\t\t\thset[char] = j + 1\n\t\t\tj += 1\n\t\treturn m\n# %%\ns = Solution()\nprint(s.lengthOfLongestSubstring('abcabcbb'))",
"status": 1,
"keywords": "哈希表,字符串,滑动窗口",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 21,
"question_title": "合并两个有序链表",
"difficulty": "简单",
"question_content": "<p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/images/merge_ex1.jpg\" style=\"width: 662px; height: 302px;\" /><pre><strong>输入:</strong>l1 = [1,2,4], l2 = [1,3,4]<strong><br />输出:</strong>[1,1,2,3,4,4]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = [0]<strong><br />输出:</strong>[0]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>两个链表的节点数目范围是 <code>[0, 50]</code></li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li><code>l1</code> 和 <code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469819",
"cpp": "struct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode(int x) : val(x), next(NULL) {}\n};\nclass Solution\n{\npublic:\n\tListNode *mergeTwoLists(ListNode *l1, ListNode *l2)\n\t{\n\t\tListNode h(0, nullptr);\n\t\tListNode *p = &h;\n\t\twhile (l1 && l2)\n\t\t{\n\t\t\tListNode **t;\n\t\t\tif (l1->val < l2->val)\n\t\t\t\tt = &l1;\n\t\t\telse\n\t\t\t\tt = &l2;\n\t\t\tp->next = *t;\n\t\t\tp = *t;\n\t\t\t*t = (*t)->next;\n\t\t}\n\t\tif (l1)\n\t\t\tp->next = l1;\n\t\telse\n\t\t\tp->next = l2;\n\t\treturn h.next;\n\t}\n};",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode() {\n\t}\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\nclass Solution {\n\tpublic ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n\t\tListNode h = new ListNode(0, null);\n\t\tListNode p = h;\n\t\twhile (l1 != null && l2 != null) {\n\t\t\tif (l1.val < l2.val) {\n\t\t\t\tp.next = l1;\n\t\t\t\tp = l1;\n\t\t\t\tl1 = l1.next;\n\t\t\t} else {\n\t\t\t\tp.next = l2;\n\t\t\t\tp = l2;\n\t\t\t\tl2 = l2.next;\n\t\t\t}\n\t\t}\n\t\tif (l1 != null) {\n\t\t\tp.next = l1;\n\t\t} else {\n\t\t\tp.next = l2;\n\t\t}\n\t\treturn h.next;\n\t}\n}",
"js": "/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode} l1\n * @param {ListNode} l2\n * @return {ListNode}\n */\nvar mergeTwoLists = function(l1, l2) {\n let v1=l1;\n let v2=l2;\n let v1pre = {val:0,next:v1};\n let root = v1pre;\n let d;\n\n const slide = (pre, node, target)=>{\n\n \tif(node.val>target){\n \t\treturn [1, pre, node];\n \t}\n\n \twhile(node.next!=null){\n \t\tif(node.next.val>=target){\n \t\t\treturn [0, pre, node]\n \t\t}else{\n \t\t\tnode = node.next;\n \t\t\tpre = pre.next;\n \t\t}\n \t}\n \treturn [0, pre, node];\n }\n\n while(true){\n \tif(v1!=null&&v2!=null){\n \t\t[d, v1pre, v1] = slide(v1pre, v1, v2.val);\n \t\tif(d===0){\n \t\t\tv1.next = {\n\t \t\t\tval: v2.val,\n\t \t\t\tnext: v1.next\n\t \t\t};\n \t\t}else{\n \t\t\tv1pre.next = {\n\t \t\t\tval: v2.val,\n\t \t\t\tnext: v1\n\t \t\t};\n\t \t\tv1 = v1pre.next;\n \t\t}\n \t\t\n \t\tv2=v2.next;\n \t}else if(v1!=null&&v2==null){\n \t\tbreak;\n \t}else if(v1==null&&v2!=null){\n \t\tv1pre.next=v2;\n \t\tbreak;\n \t}else{\n \t\tbreak;\n \t}\n }\n\n // console.log(v1pre.next);\n\n return root.next;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\n\t\t\tnull,\n\t\t\t{\"val\":0,\"next\":null},\n\t\t\t{\"val\":0,\"next\":null},\n\t\t],\n\n\t\t[\n\t\t\t{\"val\":2,\"next\":null},\n\t\t\t{\"val\":1,\"next\":null},\n\t\t\t{val:1,next:{val:2,next:null}}\n\t\t],\n\n\t\t[\n\t\t\t{\"val\":2,\"next\":null},\n\t\t\t{\"val\":2,\"next\":{val:1}},\n\t\t\t{val:1,next:{val:2,next:{val:2,next:null}}}\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:2,next:{val:4}}},\n\t\t\t{val:1,next:{val:3,next:{val:4}}},\n\t\t\t{val:1,next:{val:1,next:{val:2,next:{val:3,next:{val:4,next:{val:4}}}}}}\n\t\t],\n\n\t\t[\n\t\t\t{val: 5},\n\t\t\t{val:1,next:{val:2,next:{val:4}}},\n\t\t\t{val:1,next:{val:2,next:{val:4,next:{val:5}}}},\n\t\t]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = mergeTwoLists(testCase[0], testCase[1]);\n\t\tif(JSON.stringify(v)===JSON.stringify(testCase[2])){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t}\n\t}\n}\n\nmain();\n\n\n\n\n\n",
"python": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n\t\th = ListNode(0, None)\n\t\tp = h\n\t\twhile l1 and l2:\n\t\t\tif l1.val < l2.val:\n\t\t\t\tp.next = l1\n\t\t\t\tp = l1\n\t\t\t\tl1 = l1.next\n\t\t\telse:\n\t\t\t\tp.next = l2\n\t\t\t\tp = l2\n\t\t\t\tl2 = l2.next\n\t\tif l1:\n\t\t\tp.next = l1\n\t\telse:\n\t\t\tp.next = l2\n\t\treturn h.next\n# %%\nl = LinkList()\nlist1 = [1,2,4]\nlist2 = [1,3,4]\nl1 = l.initList(list1)\nl2 = l.initList(list2)\ns = Solution()\nprint(l.convert_list(s.mergeTwoLists(l1, l2)))",
"status": 1,
"keywords": "递归,链表",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 22,
"question_title": "括号生成",
"difficulty": "中等",
"question_content": "<p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>[\"((()))\",\"(()())\",\"(())()\",\"()(())\",\"()()()\"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[\"()\"]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= n <= 8</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470115",
"cpp": "class Solution\n{\npublic:\n\tvoid gen(string &p, int lc, int rc, vector<string> &r, int n)\n\t{\n\t\tif (lc > n)\n\t\t\treturn;\n\t\tif (lc == n && rc == n)\n\t\t{\n\t\t\tr.push_back(p);\n\t\t\treturn;\n\t\t}\n\t\tp.push_back('(');\n\t\tlc++;\n\t\tgen(p, lc, rc, r, n);\n\t\tp.pop_back();\n\t\tlc--;\n\t\tif (lc > rc)\n\t\t{\n\t\t\tp.push_back(')');\n\t\t\trc++;\n\t\t\tgen(p, lc, rc, r, n);\n\t\t\tp.pop_back();\n\t\t\trc--;\n\t\t}\n\t}\n\tvector<string> generateParenthesis(int n)\n\t{\n\t\tstring p;\n\t\tint lc = 0, rc = 0;\n\t\tvector<string> r;\n\t\tgen(p, lc, rc, r, n);\n\t\treturn r;\n\t}\n};",
"java": "class Solution {\n\tvoid gen(Stack<String> p, int lc, int rc, List<String> r, int n) {\n\t\tif (lc > n) {\n\t\t\treturn;\n\t\t}\n\t\tif (lc == n && rc == n) {\n\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\tfor (String l : p) {\n\t\t\t\tsb.append(l);\n\t\t\t}\n\t\t\tr.add(sb.toString());\n\t\t}\n\t\tp.push(\"(\");\n\t\tlc++;\n\t\tgen(p, lc, rc, r, n);\n\t\tp.pop();\n\t\tlc--;\n\t\tif (lc > rc) {\n\t\t\tp.push(\")\");\n\t\t\trc++;\n\t\t\tgen(p, lc, rc, r, n);\n\t\t\tp.pop();\n\t\t\trc--;\n\t\t}\n\t}\n\tpublic List<String> generateParenthesis(int n) {\n\t\tList<String> results = new ArrayList<String>();\n\t\tStack<String> p = new Stack<String>();\n\t\tgen(p, 0, 0, results, n);\n\t\treturn results;\n\t}\n}",
"js": "/**\n * @param {number} n\n * @return {string[]}\n */\n/**\n * @param {number} n\n * @return {string[]}\n */\n var generateParenthesis = function(n) {\n const gen = (p,lc,rc,r,n)=>{\n if(lc>n){\n return;\n }\n if(lc==n&&rc==n){\n r.push(p.join(''));\n }\n \n p.push('(');\n lc++;\n gen(p,lc,rc,r,n);\n \n p.pop();\n lc--;\n if(lc>rc){\n p.push(')');\n rc++;\n gen(p,lc,rc,r,n);\n \n p.pop();\n rc--;\n }\n }\n \n results = []\n gen([],0,0,results, n);\n return results;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t// [\n\t\t// \t3,\n\t\t// \t[\n\t\t// \t \"((()))\",\n\t\t// \t \"(()())\",\n\t\t// \t \"(())()\",\n\t\t// \t \"()(())\",\n\t\t// \t \"()()()\"\n\t\t// \t]\n\t\t// ],\n\t\t\n\t\t// [\n\t\t// \t4,\n\t\t// \t[\n\t\t// \t\t\"(((())))\",\n\t\t// \t\t\"((()))()\",\n\t\t// \t\t\"()((()))\",\n\t\t// \t\t\"(())(())\",\n\t\t// \t\t\"((()()))\",\n\t\t// \t\t\"((())())\",\n\t\t// \t\t\"(()(()))\",\n\t\t// \t\t\"(()()())\",\n\t\t// \t\t\"(()())()\",\n\t\t// \t\t\"(())()()\",\n\t\t// \t\t\"()(()())\",\n\t\t// \t\t\"()(())()\",\n\t\t// \t\t\"()()(())\",\n\t\t// \t\t\"()()()()\"\n\t\t// \t],\n\t\t// ],\n\t\t[\n\t\t\t10\n\t\t]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = generateParenthesis(testCase[0]);\n\t\tconsole.log(v);\n\t}\n}\n\nmain();\n",
"python": "from typing import List\nclass Solution:\n\tdef generateParenthesis(self, n: int) -> List[str]:\n\t\tdef gen(p, lc, rc, r, n):\n\t\t\tif lc > n:\n\t\t\t\treturn\n\t\t\tif lc == n and rc == n:\n\t\t\t\tr.append(''.join(p))\n\t\t\tp.append('(')\n\t\t\tlc += 1\n\t\t\tgen(p, lc, rc, r, n)\n\t\t\tp.pop()\n\t\t\tlc -= 1\n\t\t\tif lc > rc:\n\t\t\t\tp.append(')')\n\t\t\t\trc += 1\n\t\t\t\tgen(p, lc, rc, r, n)\n\t\t\t\tp.pop()\n\t\t\t\trc -= 1\n\t\tresults = []\n\t\tgen([], 0, 0, results, n)\n\t\treturn results\n# %%\ns = Solution()\nprint(s.generateParenthesis(n = 3))",
"status": 1,
"keywords": "字符串,动态规划,回溯",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/21/21_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/21/21_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/21/21_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 23,
"question_title": "合并K个升序链表",
"difficulty": "困难",
"question_content": "<p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>\t<li><code>k == lists.length</code></li>\t<li><code>0 &lt;= k &lt;= 10^4</code></li>\t<li><code>0 &lt;= lists[i].length &lt;= 500</code></li>\t<li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li>\t<li><code>lists[i]</code> 按 <strong>升序</strong> 排列</li>\t<li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470116",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *mergeKLists(vector<ListNode *> &lists)\n\t{\n\t\tauto cmp = [](struct ListNode *n1, struct ListNode *n2)\n\t\t{\n\t\t\treturn n1->val > n2->val;\n\t\t} priority_queue<struct ListNode *, vector<struct ListNode *>, decltype(cmp)>\n\t\t\tqueue(cmp);\n\t\tfor (int i = 0; i < lists.size(); i++)\n\t\t{\n\t\t\tif (lists[i] != nullptr)\n\t\t\t{\n\t\t\t\tqueue.push(lists[i]);\n\t\t\t}\n\t\t}\n\t\tstruct ListNode dummy, *p = &dummy;\n\t\t;\n\t\twhile (!queue.empty())\n\t\t{\n\t\t\tListNode *node = queue.top();\n\t\t\tqueue.pop();\n\t\t\tp->next = node;\n\t\t\tp = node;\n\t\t\tif (node->next != nullptr)\n\t\t\t{\n\t\t\t\tqueue.push(node->next);\n\t\t\t}\n\t\t}\n\t\treturn dummy.next;\n\t}\n};",
"java": "\npublic class ListNode {\n\tint val;\n\tListNode next;\n\n\tListNode() {\n\t}\n\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\n\nclass Solution {\n\tpublic ListNode mergeKLists(ListNode[] lists) {\n\t\tif (lists.length == 0)\n\t\t\treturn null;\n\n\t\treturn merge(lists, 0, lists.length - 1);\n\t}\n\n\tpublic ListNode merge(ListNode[] lists, int low, int high) {\n\t\tif (high - low == 0)\n\t\t\treturn lists[low];\n\t\telse if (high - low == 1)\n\t\t\treturn mergeTwoLists(lists[low], lists[high]);\n\t\telse {\n\t\t\tint mid = (low + high) / 2;\n\t\t\tListNode tmp1 = merge(lists, low, mid);\n\t\t\tListNode tmp2 = merge(lists, mid + 1, high);\n\t\t\treturn mergeTwoLists(tmp1, tmp2);\n\t\t}\n\t}\n\n\tpublic ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n\t\tListNode head = new ListNode();\n\t\tListNode p = head;\n\t\twhile (l1 != null && l2 != null) {\n\t\t\tif (l1.val > l2.val) {\n\t\t\t\tp.next = l2;\n\t\t\t\tl2 = l2.next;\n\t\t\t\tp = p.next;\n\t\t\t} else {\n\t\t\t\tp.next = l1;\n\t\t\t\tl1 = l1.next;\n\t\t\t\tp = p.next;\n\t\t\t}\n\t\t}\n\t\tif (l1 != null)\n\t\t\tp.next = l1;\n\t\tif (l2 != null)\n\t\t\tp.next = l2;\n\t\treturn head.next;\n\t}\n}\n",
"js": "",
"python": "from typing import List\nclass ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef mergeKLists(self, lists):\n\t\tif lists is None:\n\t\t\treturn None\n\t\telif len(lists) == 0:\n\t\t\treturn None\n\t\treturn self.mergeK(lists, 0, len(lists) - 1)\n\tdef mergeK(self, lists, low, high):\n\t\tif low == high:\n\t\t\treturn lists[int(low)]\n\t\telif low + 1 == high:\n\t\t\treturn self.mergeTwolists(lists[int(low)], lists[int(high)])\n\t\tmid = (low + high) / 2\n\t\treturn self.mergeTwolists(self.mergeK(lists, low, mid), self.mergeK(lists, mid + 1, high))\n\tdef mergeTwolists(self, l1, l2):\n\t\tl = LinkList()\n\t\tif type(l1) == list:\t\n\t\t\tl1 = l.initList(l1)\n\t\tif type(l2) == list:\t\n\t\t\tl2 = l.initList(l2)\n\t\tif l1 is None:\n\t\t\treturn l2\n\t\tif l2 is None:\n\t\t\treturn l1\n\t\thead = curr = ListNode(-1)\n\t\twhile l1 is not None and l2 is not None:\n\t\t\tif l1.val <= l2.val:\n\t\t\t\tcurr.next = l1\n\t\t\t\tl1 = l1.next\n\t\t\telse:\n\t\t\t\tcurr.next = l2\n\t\t\t\tl2 = l2.next\n\t\t\tcurr = curr.next\n\t\tif l1 is not None:\n\t\t\tcurr.next = l1\n\t\tif l2 is not None:\n\t\t\tcurr.next = l2\n\t\treturn head.next\n# %%\nl = LinkList()\nlist1 = [[1,4,5],[1,3,4],[2,6]]\ns = Solution()\nprint(l.convert_list(s.mergeKLists(list1)))",
"status": 1,
"keywords": "链表,分治,堆(优先队列),归并排序",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/22/22_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/22/22_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/22/22_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 24,
"question_title": "两两交换链表中的节点",
"difficulty": "中等",
"question_content": "<p>给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。</p><p><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际的进行节点交换。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/images/swap_ex1.jpg\" style=\"width: 422px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4]<strong><br />输出:</strong>[2,1,4,3]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点的数目在范围 <code>[0, 100]</code> 内</li>\t<li><code>0 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong>你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p>",
"topic_link": "https://bbs.csdn.net/topics/600470117",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *swapPairs(ListNode *head)\n\t{\n\t\tstruct ListNode dummy, *prev = &dummy, *p = head;\n\t\tdummy.next = head;\n\t\twhile (p != nullptr && p->next != nullptr)\n\t\t{\n\t\t\tstruct ListNode *q = p->next;\n\t\t\tp->next = q->next;\n\t\t\tq->next = prev->next;\n\t\t\tprev->next = q;\n\t\t\tprev = p;\n\t\t\tp = p->next;\n\t\t}\n\t\treturn dummy.next;\n\t}\n};",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode swapPairs(ListNode head) {\n\t\tListNode list1 = new ListNode(0);\n\t\tlist1.next = head;\n\t\tListNode list2 = list1;\n\t\twhile (head != null && head.next != null) {\n\t\t\tlist2.next = head.next;\n\t\t\thead.next = list2.next.next;\n\t\t\tlist2.next.next = head;\n\t\t\tlist2 = list2.next.next;\n\t\t\thead = list2.next;\n\t\t}\n\t\treturn list1.next;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef swapPairs(self, head):\n\t\tdummyHead = ListNode(-1)\n\t\tdummyHead.next = head\n\t\tprev, p = dummyHead, head\n\t\twhile p != None and p.next != None:\n\t\t\tq, r = p.next, p.next.next\n\t\t\tprev.next = q\n\t\t\tq.next = p\n\t\t\tp.next = r\n\t\t\tprev = p\n\t\t\tp = r\n\t\treturn dummyHead.next\n# %%\nl = LinkList()\nhead = [1,2,3,4]\nl1 = l.initList(head)\ns = Solution()\nprint(l.convert_list(s.swapPairs(l1)))",
"status": 1,
"keywords": "递归,链表",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/FaustoPatton/article/details/82993032"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/23/23_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 25,
"question_title": "K 个一组翻转链表",
"difficulty": "困难",
"question_content": "<p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul>\t<li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li>\t<li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul>\t<li>列表中节点的数量在范围 <code>sz</code> 内</li>\t<li><code>1 <= sz <= 5000</code></li>\t<li><code>0 <= Node.val <= 1000</code></li>\t<li><code>1 <= k <= sz</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471012",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *reverseGroup(ListNode *head, int k)\n\t{\n\t\tint len = 0;\n\t\tstruct ListNode dummy, *prev = &dummy;\n\t\tdummy.next = head;\n\t\tfor (; head != nullptr; head = head->next)\n\t\t{\n\t\t\tif (++len % k == 0)\n\t\t\t{\n\t\t\t\tstruct ListNode *p = prev->next;\n\t\t\t\twhile (prev->next != head)\n\t\t\t\t{\n\t\t\t\t\tstruct ListNode *q = p->next;\n\t\t\t\t\tp->next = q->next;\n\t\t\t\t\tq->next = prev->next;\n\t\t\t\t\tprev->next = q;\n\t\t\t\t}\n\t\t\t\tprev = p;\n\t\t\t\thead = p;\n\t\t\t}\n\t\t}\n\t\treturn dummy.next;\n\t}\n};",
"java": "\npublic class ListNode {\n\tint val;\n\tListNode next;\n\n\tListNode() {\n\t}\n\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\n\nclass Solution {\n\tpublic ListNode reverseKGroup(ListNode head, int k) {\n\t\tif (head == null) {\n\t\t\treturn null;\n\t\t}\n\t\tListNode a = head, b = head;\n\n\t\tfor (int i = 0; i < k; i++) {\n\t\t\tif (b == null) {\n\t\t\t\treturn a;\n\t\t\t}\n\t\t\tb = b.next;\n\t\t}\n\n\t\tListNode newHead = reverse(a, b);\n\n\t\ta.next = reverseKGroup(b, k);\n\t\treturn newHead;\n\t}\n\n\tpublic ListNode reverse(ListNode a, ListNode b) {\n\t\tListNode pre, cur, nxt;\n\t\tpre = null;\n\t\tcur = a;\n\t\tnxt = a;\n\t\twhile (nxt != b) {\n\t\t\tnxt = cur.next;\n\t\t\tcur.next = pre;\n\t\t\tpre = cur;\n\t\t\tcur = nxt;\n\t\t}\n\t\treturn pre;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef reverseKGroup(self, head, k):\n\t\tif head is None:\n\t\t\treturn None\n\t\tindex = 0\n\t\tlead, last = 0, 0\n\t\tpos = head\n\t\ttemp = ListNode(-1)\n\t\ttemp.next = head\n\t\thead = temp\n\t\tstart = head\n\t\twhile pos is not None:\n\t\t\tif index % k == k - 1:\n\t\t\t\tlast = pos.next\n\t\t\t\tstart = self.reverseList(start, last)\n\t\t\t\tpos = start\n\t\t\tpos = pos.next\n\t\t\tindex += 1\n\t\treturn head.next\n\tdef reverseList(self, head, end):\n\t\tpos = head.next\n\t\tlast = end\n\t\tnext_start = pos\n\t\twhile pos != end:\n\t\t\thead.next = pos\n\t\t\tlast_pos = pos\n\t\t\tpos = pos.next\n\t\t\tlast_pos.next = last\n\t\t\tlast = last_pos\n\t\treturn next_start\n# %%\nl = LinkList()\nhead = [1,2,3,4, 5]\nl1 = l.initList(head)\ns = Solution()\nprint(l.convert_list(s.reverseKGroup(l1, k = 2)))",
"status": 1,
"keywords": "递归,链表",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/24/24_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/24/24_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/24/24_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 26,
"question_title": "删除有序数组中的重复项",
"difficulty": "简单",
"question_content": "<div class=\"notranslate\">\n <p>给你一个有序数组 <code>nums</code> ,请你<strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">\n 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>只出现一次</strong> ,返回删除后数组的新长度。</p>\n\n <p>不要使用额外的数组空间,你必须在 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地\n </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>\n\n <p>&nbsp;</p>\n\n <p><strong>说明:</strong></p>\n\n <p>为什么返回数值是整数,但输出的答案是数组呢?</p>\n\n <p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>\n\n <p>你可以想象内部操作如下:</p>\n\n <pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝\nint len = removeDuplicates(nums);\n\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n</pre>\n &nbsp;\n\n <p><strong>示例 1:</strong></p>\n\n <pre><strong>输入:</strong>nums = [1,1,2]\n<strong><br />输出:</strong>2, nums = [1,2]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>2</strong> ,并且原数组 <em>nums </em>的前两个元素被修改为 <strong>1</strong>, <strong>2 </strong>。不需要考虑数组中超出新长度后面的元素。\n </pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>nums = [0,0,1,1,1,2,2,3,3,4]\n<strong><br />输出:</strong>5, nums = [0,1,2,3,4]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>5</strong> , 并且原数组 <em>nums </em>的前五个元素被修改为 <strong>0</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>4</strong> 。不需要考虑数组中超出新长度后面的元素。\n </pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>0 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>\n <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n <li><code>nums</code> 已按升序排列</li>\n </ul>\n\n <p>&nbsp;</p>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470118",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint removeDuplicates(vector<int> &nums)\n\t{\n\t\tif (nums.size() == 0)\n\t\t{\n\t\t\treturn 0;\n\t\t}\n\t\tint count = 1;\n\t\tfor (int i = 1; i < nums.size(); i++)\n\t\t{\n\t\t\tif (nums[i - 1] != nums[i])\n\t\t\t{\n\t\t\t\tnums[count++] = nums[i];\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n};",
"java": "class Solution {\n\tpublic int removeDuplicates(int[] nums) {\n\t\tif (nums == null || nums.length == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint a = 0;\n\t\tfor (int b = 1; b < nums.length; b++) {\n\t\t\tif (nums[a] != nums[b]) {\n\t\t\t\ta++;\n\t\t\t\tnums[a] = nums[b];\n\t\t\t}\n\t\t}\n\t\treturn a + 1;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef removeDuplicates(self, nums):\n\t\tif len(nums) == 0:\n\t\t\treturn 0\n\t\tleft = 0\n\t\tfor i in range(1, len(nums)):\n\t\t\tif nums[left] == nums[i]:\n\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\tleft += 1\n\t\t\t\tnums[left] = nums[i]\n\t\treturn left + 1\n# %%\ns = Solution()\nprint(s.removeDuplicates(nums = [1,1,2]))",
"status": 1,
"keywords": "数组,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/25/25_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/25/25_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/25/25_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 27,
"question_title": "移除元素",
"difficulty": "简单",
"question_content": "<div class=\"notranslate\">\n <p>给你一个数组 <code>nums</code><em>&nbsp;</em>和一个值 <code>val</code>,你需要 <strong><a\n href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地</a></strong>\n 移除所有数值等于&nbsp;<code>val</code><em>&nbsp;</em>的元素,并返回移除后数组的新长度。</p>\n\n <p>不要使用额外的数组空间,你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a\n href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地 </a>修改输入数组</strong>。</p>\n\n <p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>\n\n <p>&nbsp;</p>\n\n <p><strong>说明:</strong></p>\n\n <p>为什么返回数值是整数,但输出的答案是数组呢?</p>\n\n <p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>\n\n <p>你可以想象内部操作如下:</p>\n\n <pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参作任何拷贝\nint len = removeElement(nums, val);\n\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n\n <pre><strong>输入:</strong>nums = [3,2,2,3], val = 3\n<strong><br />输出:</strong>2, nums = [2,2]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>2</strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。\n </pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>nums = [0,1,2,2,3,0,4,2], val = 2\n<strong><br />输出:</strong>5, nums = [0,1,4,0,3]\n<strong><br />解释:</strong>函数应该返回新的长度 <strong>5</strong>, 并且 nums 中的前五个元素为 <strong>0</strong>, <strong>1</strong>, <strong>3</strong>, <strong>0</strong>, <strong>4</strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。\n </pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>0 &lt;= nums.length &lt;= 100</code></li>\n <li><code>0 &lt;= nums[i] &lt;= 50</code></li>\n <li><code>0 &lt;= val &lt;= 100</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470219",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint removeElement(vector<int> &nums, int val)\n\t{\n\t\tint count = 0;\n\t\tfor (int i = 0; i < nums.size(); i++)\n\t\t{\n\t\t\tif (nums[i] != val)\n\t\t\t{\n\t\t\t\tnums[count++] = nums[i];\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n};",
"java": "class Solution {\n\tpublic int removeElement(int[] nums, int val) {\n\n\t\tint len = nums.length;\n\t\tfor (int i = 0; i < len;) {\n\t\t\tif (nums[i] == val) {\n\t\t\t\tnums[i] = nums[len - 1];\n\t\t\t\tlen--;\n\t\t\t} else {\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t}\n\t\treturn len;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef removeElement(self, nums, val):\n\t\tls = len(nums)\n\t\tif ls == 0:\n\t\t\treturn ls\n\t\tcount = 0\n\t\tindex = 0\n\t\twhile index < ls - count:\n\t\t\tif nums[index] == val:\n\t\t\t\tnums[index] = nums[ls - 1 - count]\n\t\t\t\tcount += 1\n\t\t\telse:\n\t\t\t\tindex += 1\n\t\treturn ls - count\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.removeElement([3,2,2,3], 3))",
"status": 1,
"keywords": "数组,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/26/26_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/26/26_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/26/26_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 28,
"question_title": "实现 strStr()",
"difficulty": "简单",
"question_content": "<p>实现 <a href=\"https://baike.baidu.com/item/strstr/811469\" target=\"_blank\">strStr()</a> 函数。</p>\n<p>给你两个字符串 <code>haystack</code> 和 <code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code>\n 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回  <code>-1</code><strong> </strong>。</p>\n<p> </p>\n<p><strong>说明:</strong></p>\n<p>当 <code>needle</code> 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。</p>\n<p>对于本题而言,当 <code>needle</code> 是空字符串时我们应当返回 0 。这与 C 语言的 <a href=\"https://baike.baidu.com/item/strstr/811469\"\n target=\"_blank\">strstr()</a> 以及 Java 的 <a\n href=\"https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)\"\n target=\"_blank\">indexOf()</a> 定义相符。</p>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>haystack = \"hello\", needle = \"ll\"<strong><br />输出:</strong>2</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>haystack = \"aaaaa\", needle = \"bba\"<strong><br />输出:</strong>-1</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>haystack = \"\", needle = \"\"<strong><br />输出:</strong>0</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code></li>\n <li><code>haystack</code> 和 <code>needle</code> 仅由小写英文字符组成</li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470119",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint strStr(string haystack, string needle)\n\t{\n\t\treturn haystack.find(needle);\n\t}\n};",
"java": "class Solution {\n\tpublic int strStr(String haystack, String needle) {\n\t\tif (needle.length() < 1) {\n\t\t\treturn 0;\n\t\t}\n\t\tchar[] ha = haystack.toCharArray();\n\t\tchar[] ne = needle.toCharArray();\n\t\tint hi = 0;\n\t\tint ni = 0;\n\t\tint[] next = getNextArray(ne);\n\t\twhile (hi < ha.length && ni < ne.length) {\n\t\t\tif (ha[hi] == ne[ni]) {\n\t\t\t\thi++;\n\t\t\t\tni++;\n\t\t\t} else {\n\t\t\t\tif (next[ni] == -1) {\n\t\t\t\t\thi++;\n\t\t\t\t} else {\n\t\t\t\t\tni = next[ni];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ni == ne.length ? hi - ni : -1;\n\t}\n\tpublic static int[] getNextArray(char[] a) {\n\t\tif (a.length == 1) {\n\t\t\treturn new int[] { -1 };\n\t\t}\n\t\tint[] next = new int[a.length];\n\t\tnext[0] = -1;\n\t\tnext[1] = 0;\n\t\tint i = 2;\n\t\tint j = 0;\n\t\twhile (i < next.length) {\n\t\t\tif (a[i - 1] == a[j]) {\n\t\t\t\tnext[i++] = ++j;\n\t\t\t} else if (j > 0) {\n\t\t\t\tj = next[j];\n\t\t\t} else {\n\t\t\t\tnext[i++] = 0;\n\t\t\t}\n\t\t}\n\t\treturn next;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef strStr(self, haystack, needle):\n\t\tlsh, lsn = len(haystack), len(needle)\n\t\tif lsn == 0:\n\t\t\treturn 0\n\t\tnext = self.makeNext(needle)\n\t\ti = j = 0\n\t\twhile i < lsh:\n\t\t\tif j == -1 or haystack[i] == needle[j]:\n\t\t\t\ti += 1\n\t\t\t\tj += 1\n\t\t\t\tif j == lsn:\n\t\t\t\t\treturn i - lsn\n\t\t\tif i < lsh and haystack[i] != needle[j]:\n\t\t\t\tj = next[j]\n\t\treturn -1\n\tdef makeNext(self, needle):\n\t\tls = len(needle)\n\t\tnext = [0] * ls\n\t\tnext[0], i, j = -1, 0, -1\n\t\twhile i < ls - 1:\n\t\t\tif j == -1 or needle[i] == needle[j]:\n\t\t\t\tnext[i + 1] = j + 1\n\t\t\t\tif needle[i + 1] == needle[j + 1]:\n\t\t\t\t\tnext[i + 1] = next[j + 1]\n\t\t\t\ti += 1\n\t\t\t\tj += 1\n\t\t\tif needle[i] != needle[j]:\n\t\t\t\tj = next[j]\n\t\treturn next\n# %%\ns = Solution()\nprint(s.strStr(haystack = \"hello\", needle = \"ll\"))",
"status": 0,
"keywords": "双指针,字符串,字符串匹配",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_43780574/article/details/84397763"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/27/27_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/27/27_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/27/27_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 29,
"question_title": "两数相除",
"difficulty": "中等",
"question_content": "<p>给定两个整数,被除数&nbsp;<code>dividend</code>&nbsp;和除数&nbsp;<code>divisor</code>。将两数相除,要求不使用乘法、除法和 mod 运算符。</p><p>返回被除数&nbsp;<code>dividend</code>&nbsp;除以除数&nbsp;<code>divisor</code>&nbsp;得到的商。</p><p>整数除法的结果应当截去(<code>truncate</code>)其小数部分,例如:<code>truncate(8.345) = 8</code> 以及 <code>truncate(-2.7335) = -2</code></p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> dividend = 10, divisor = 3<strong><br />输出:</strong> 3<strong><br />解释: </strong>10/3 = truncate(3.33333..) = truncate(3) = 3</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> dividend = 7, divisor = -3<strong><br />输出:</strong> -2<strong><br />解释:</strong> 7/-3 = truncate(-2.33333..) = -2</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>\t<li>被除数和除数均为 32 位有符号整数。</li>\t<li>除数不为&nbsp;0。</li>\t<li>假设我们的环境只能存储 32 位有符号整数,其数值范围是 [&minus;2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>&minus; 1]。本题中,如果除法结果溢出,则返回 2<sup>31&nbsp;</sup>&minus; 1。</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470833",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint divide(int dividend, int divisor)\n\t{\n\t\tint signal = 1;\n\t\tunsigned int dvd = dividend;\n\t\tif (dividend < 0)\n\t\t{\n\t\t\tsignal *= -1;\n\t\t\tdvd = ~dvd + 1;\n\t\t}\n\t\tunsigned int dvs = divisor;\n\t\tif (divisor < 0)\n\t\t{\n\t\t\tsignal *= -1;\n\t\t\tdvs = ~dvs + 1;\n\t\t}\n\t\tint shift = 0;\n\t\twhile (dvd > dvs << shift)\n\t\t{\n\t\t\tshift++;\n\t\t}\n\t\tunsigned int res = 0;\n\t\twhile (dvd >= dvs)\n\t\t{\n\t\t\twhile (dvd < dvs << shift)\n\t\t\t{\n\t\t\t\tshift--;\n\t\t\t}\n\t\t\tres |= (unsigned int)1 << shift;\n\t\t\tdvd -= dvs << shift;\n\t\t}\n\t\tif (signal == 1 && res >= INT_MAX)\n\t\t{\n\t\t\treturn INT_MAX;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn res * signal;\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic int divide(int dividend, int divisor) {\n\t\tif (dividend == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tif (dividend == Integer.MIN_VALUE && divisor == -1) {\n\t\t\treturn Integer.MAX_VALUE;\n\t\t}\n\t\tboolean negative;\n\t\tnegative = (dividend ^ divisor) < 0;\n\t\tlong t = Math.abs((long) dividend);\n\t\tlong d = Math.abs((long) divisor);\n\t\tint result = 0;\n\t\tfor (int i = 31; i >= 0; i--) {\n\t\t\tif ((t >> i) >= d) {\n\t\t\t\tresult += 1 << i;\n\t\t\t\tt -= d << i;\n\t\t\t}\n\t\t}\n\t\treturn negative ? -result : result;\n\t}\n}",
"js": "",
"python": "import math\nclass Solution(object):\n\tdef divide(self, dividend, divisor):\n\t\tif divisor == 0:\n\t\t\treturn MAX_INT\n\t\tif dividend == 0:\n\t\t\treturn 0\n\t\tisPositive = (dividend < 0) == (divisor < 0)\n\t\tm = abs(dividend)\n\t\tn = abs(divisor)\n\t\tres = math.log(m) - math.log(n)\n\t\tres = int(math.exp(res))\n\t\tif isPositive:\n\t\t\treturn min(res, 2147483647)\n\t\treturn max(0 - res, -2147483648)\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.divide(1, 1))",
"status": 1,
"keywords": "位运算,数学",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104292233"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/28/28_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/28/28_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/28/28_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 30,
"question_title": "串联所有单词的子串",
"difficulty": "困难",
"question_content": "<p>给定一个字符串&nbsp;<strong>s&nbsp;</strong>和一些长度相同的单词&nbsp;<strong>words。</strong>找出 <strong>s\n </strong>中恰好可以由&nbsp;<strong>words </strong>中所有单词串联形成的子串的起始位置。</p>\n<p>注意子串要与&nbsp;<strong>words </strong>中的单词完全匹配,中间不能有其他字符,但不需要考虑&nbsp;<strong>words&nbsp;</strong>中单词串联的顺序。</p>\n<p>&nbsp;</p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入: s =</strong> &quot;barfoothefoobarman&quot;,<strong> words = </strong>[&quot;foo&quot;,&quot;bar&quot;]<strong><br />输出:</strong>[0,9]<strong><br />解释:</strong>从索引 0 和 9 开始的子串分别是 &quot;barfoo&quot; 和 &quot;foobar&quot; 。输出的顺序不重要, [9,0] 也是有效答案。</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470925",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> findSubstring(string s, vector<string> &words)\n\t{\n\t\tvector<int> res;\n\t\tif (s.empty() || words.empty())\n\t\t{\n\t\t\treturn res;\n\t\t}\n\t\tunordered_map<string, int> ht;\n\t\tfor (const auto &w : words)\n\t\t{\n\t\t\tht[w]++;\n\t\t}\n\t\tint len = words[0].length();\n\t\tfor (int i = 0, j = 0; i < s.length() - words.size() * len + 1; i++)\n\t\t{\n\t\t\tunordered_map<string, int> counting;\n\t\t\tfor (j = 0; j < words.size(); j++)\n\t\t\t{\n\t\t\t\tstring word = s.substr(i + j * len, len);\n\t\t\t\tif (++counting[word] > ht[word])\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (j == words.size())\n\t\t\t{\n\t\t\t\tres.push_back(i);\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};",
"java": "class Solution {\n\tpublic List<Integer> findSubstring(String s, String[] words) {\n\t\tList<Integer> res = new ArrayList<>();\n\t\tif (s == null || s.length() == 0 || words == null || words.length == 0)\n\t\t\treturn res;\n\t\tHashMap<String, Integer> map = new HashMap<>();\n\t\tint one_word = words[0].length();\n\t\tint word_num = words.length;\n\t\tint all_len = one_word * word_num;\n\t\tfor (String word : words) {\n\t\t\tmap.put(word, map.getOrDefault(word, 0) + 1);\n\t\t}\n\t\tfor (int i = 0; i < one_word; i++) {\n\t\t\tint left = i, right = i, count = 0;\n\t\t\tHashMap<String, Integer> tmp_map = new HashMap<>();\n\t\t\twhile (right + one_word <= s.length()) {\n\t\t\t\tString w = s.substring(right, right + one_word);\n\t\t\t\tright += one_word;\n\t\t\t\tif (!map.containsKey(w)) {\n\t\t\t\t\tcount = 0;\n\t\t\t\t\tleft = right;\n\t\t\t\t\ttmp_map.clear();\n\t\t\t\t} else {\n\t\t\t\t\ttmp_map.put(w, tmp_map.getOrDefault(w, 0) + 1);\n\t\t\t\t\tcount++;\n\t\t\t\t\twhile (tmp_map.getOrDefault(w, 0) > map.getOrDefault(w, 0)) {\n\t\t\t\t\t\tString t_w = s.substring(left, left + one_word);\n\t\t\t\t\t\tcount--;\n\t\t\t\t\t\ttmp_map.put(t_w, tmp_map.getOrDefault(t_w, 0) - 1);\n\t\t\t\t\t\tleft += one_word;\n\t\t\t\t\t}\n\t\t\t\t\tif (count == word_num)\n\t\t\t\t\t\tres.add(left);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef findSubstring(self, s, words):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:type words: List[str]\n\t\t:rtype: List[int]\n\t\t\"\"\"\n\t\tls = len(s)\n\t\tword_ls = len(words[0])\n\t\ttarget_dict = {}\n\t\tfor word in words:\n\t\t\ttry:\n\t\t\t\ttarget_dict[word] += 1\n\t\t\texcept KeyError:\n\t\t\t\ttarget_dict[word] = 1\n\t\tres = []\n\t\tfor start in range(ls - word_ls * len(words) + 1):\n\t\t\tcurr_dict = target_dict.copy()\n\t\t\tfor pos in range(start, start + word_ls * len(words), word_ls):\n\t\t\t\tcurr = s[pos:pos + word_ls]\n\t\t\t\ttry:\n\t\t\t\t\tcurr_dict[curr] -= 1\n\t\t\t\t\tif curr_dict[curr] < 0:\n\t\t\t\t\t\tbreak\n\t\t\t\texcept KeyError:\n\t\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tres.append(start)\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.findSubstring('wordgoodgoodgoodbestword', [\"word\", \"good\", \"best\", \"good\"]))",
"status": 1,
"keywords": "哈希表,字符串,滑动窗口",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104292954"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/29/29_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/29/29_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/29/29_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 4,
"question_title": "寻找两个正序数组的中位数",
"difficulty": "困难",
"question_content": "<p>给定两个大小分别为 <code>m</code> 和 <code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong> 。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>nums1.length == m</code></li>\t<li><code>nums2.length == n</code></li>\t<li><code>0 <= m <= 1000</code></li>\t<li><code>0 <= n <= 1000</code></li>\t<li><code>1 <= m + n <= 2000</code></li>\t<li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>",
"topic_link": "https://bbs.csdn.net/topics/600469814",
"cpp": "class Solution\n{\npublic:\n\tdouble findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)\n\t{\n\t\tint nums1Size = nums1.size();\n\t\tint nums2Size = nums2.size();\n\t\tint na = nums1Size + nums2Size;\n\t\tint *ns = (int *)malloc(4 * na);\n\t\tint i = 0, j = 0, d = 0;\n\t\tint m = na / 2 + 1;\n\t\twhile (d < m)\n\t\t{\n\t\t\tint n;\n\t\t\tif (i < nums1Size && j < nums2Size)\n\t\t\t{\n\t\t\t\tn = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];\n\t\t\t}\n\t\t\telse if (i < nums1Size)\n\t\t\t{\n\t\t\t\tn = nums1[i++];\n\t\t\t}\n\t\t\telse if (j < nums2Size)\n\t\t\t{\n\t\t\t\tn = nums2[j++];\n\t\t\t}\n\t\t\tns[d++] = n;\n\t\t}\n\t\tif (na % 2)\n\t\t{\n\t\t\treturn ns[d - 1];\n\t\t}\n\t\treturn (ns[d - 1] + ns[d - 2]) / 2.0;\n\t}\n};",
"java": "class Solution {\n\tpublic double findMedianSortedArrays(int[] nums1, int[] nums2) {\n\t\tint nums1Size = nums1.length;\n\t\tint nums2Size = nums2.length;\n\t\tint na = nums1Size + nums2Size;\n\t\tint[] ns = new int[4 * na];\n\t\tint i = 0, j = 0, d = 0;\n\t\tint m = na / 2 + 1;\n\t\twhile (d < m) {\n\t\t\tint n = 0;\n\t\t\tif (i < nums1Size && j < nums2Size) {\n\t\t\t\tn = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];\n\t\t\t} else if (i < nums1Size) {\n\t\t\t\tn = nums1[i++];\n\t\t\t} else if (j < nums2Size) {\n\t\t\t\tn = nums2[j++];\n\t\t\t}\n\t\t\tns[d++] = n;\n\t\t}\n\t\tif (na % 2 == 1) {\n\t\t\treturn ns[d - 1];\n\t\t}\n\t\treturn (ns[d - 1] + ns[d - 2]) / 2.0;\n\t}\n}",
"js": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar findMedianSortedArrays = function(nums1, nums2) {\n let A, B, m, n, l, start, end, cutA, cutBB, cutB, L1, R1, L2, R2, U;\n \n if(nums2.length<nums1.length){\n [A, B] = [nums2, nums1];\n }else{\n [A, B] = [nums1, nums2];\n }\n\n console.log('===> case:', A, B);\n \n [m, n] = [A.length, B.length];\n l = m+n;\n\n if(m===0||n===0){\n if(m!==0){\n U = A;\n }\n\n if(n!==0){\n U = B;\n }\n\n if(U==null){\n return 0;\n }\n\n if(l%2===0){\n return (U[l/2-1]+U[l/2])/2;\n }else{\n return U[(l-1)/2];\n }\n\n }\n\n start = 0;\n end = m;\n while(start<=end){\n cutA = Math.floor((start+end)/2);\n cutBB = Math.floor((l+1)/2);\n cutB = cutBB-cutA;\n\n L1 = cutA===0 ? Number.NEGATIVE_INFINITY : A[cutA-1];\n R1 = cutA===m ? Number.MAX_VALUE : A[cutA];\n\n L2 = cutB===0 ? Number.NEGATIVE_INFINITY : B[cutB-1];\n R2 = cutB===n ? Number.MAX_VALUE : B[cutB];\n\n console.log(`-->[${start}, ${end}], [${cutA},${cutB}, ${cutBB}], L1:${L1}, R1:${R1}, L2:${L2}, R2:${R2}`);\n\n if(cutA>0 && L1>R2){\n end--;\n }else if(L2>R1){\n start++;\n }else{\n if(l%2===0){\n return (Math.max(L1,L2)+Math.min(R1,R2))/2;\n }else{\n return Math.max(L1, L2);\n }\n }\n }\n\n return 0;\n};\n\n\nconst cases = [\n // 0, 0\n [[],[],0],\n\n // 0, x\n [[],[2,4],3],\n [[],[2,4,5],4],\n [[1],[2,3],2],\n\n // // x, 0\n [[2,4],[],3],\n\n // // 1,1\n [[2],[3],2.5],\n \n // 2,1\n [[1,3],[2],2],\n\n // 1,2\n [[2],[1,3],2],\n [[4],[2,2],2], // 2,2,4=>2\n [[4],[2,4],4], // 2,4,4=>4\n [[4],[2,3],3],\n [[1],[2,3,4],2.5],\n [[1],[2,3,4,5,6],3.5],\n\n // 2,2\n [[1,2],[3,4],2.5],\n [[1,4],[2,3],2.5],\n [[2,3],[1,4],2.5],\n\n // 3,2\n [[2,3,4],[1,4],3], // 1,2,3,4,4\n [[1,2,3],[1,2],2], // 1,1,2,2,3\n [[1,2],[1,2,3],2], // 1,1,2,2,3\n\n // 3,3\n [[1,1,1],[1,1,1],1],\n [[1,2,2],[1,2,3],2],\n [[1,2,2, 2],[1,2, 2, 2],2], // 1 1 2 2 2\n\n // 4, 2\n [[2,3,4,5],[1,4],3.5], // 1,2,3,4,4,5\n [ [1,2,3,7,8],\n [4,5,6,9,10],\n 5.5\n ]\n];\n\nfor(const c of cases){\n console.log('');\n const v = findMedianSortedArrays(c[0], c[1]);\n console.log('v:',v);\n console.assert(v===c[2]); \n}\n\n\n\n\n\n\n\n",
"python": "import math\nfrom typing import List\nclass Solution:\n\tdef findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:\n\t\tnums1Size = len(nums1)\n\t\tnums2Size = len(nums2)\n\t\tna = nums1Size + nums2Size\n\t\tns = []\n\t\ti = 0\n\t\tj = 0\n\t\tm = int(math.floor(na / 2 + 1))\n\t\twhile len(ns) < m:\n\t\t\tn = None\n\t\t\tif i < nums1Size and j < nums2Size:\n\t\t\t\tif nums1[i] < nums2[j]:\n\t\t\t\t\tn = nums1[i]\n\t\t\t\t\ti += 1\n\t\t\t\telse:\n\t\t\t\t\tn = nums2[j]\n\t\t\t\t\tj += 1\n\t\t\telif i < nums1Size:\n\t\t\t\tn = nums1[i]\n\t\t\t\ti += 1\n\t\t\telif j < nums2Size:\n\t\t\t\tn = nums2[j]\n\t\t\t\tj += 1\n\t\t\tns.append(n)\n\t\td = len(ns)\n\t\tif na % 2 == 1:\n\t\t\treturn ns[d - 1]\n\t\telse:\n\t\t\treturn (ns[d - 1] + ns[d - 2]) / 2.0\n# %%\ns = Solution()\nprint(s.findMedianSortedArrays([1,3], [2]))",
"status": 1,
"keywords": "数组,二分查找,分治",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/3/3_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/3/3_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/3/3_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 31,
"question_title": "下一个排列",
"difficulty": "中等",
"question_content": "<p>实现获取 <strong>下一个排列</strong> 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。</p><p>如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。</p><p>必须<strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"> 原地 </a></strong>修改,只允许使用额外常数空间。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[1,3,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,1]<strong><br />输出:</strong>[1,2,3]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [1,1,5]<strong><br />输出:</strong>[1,5,1]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 100</code></li>\t<li><code>0 <= nums[i] <= 100</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471007",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid nextPermutation(vector<int> &nums)\n\t{\n\t\tif (nums.size() < 2)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\tint i = nums.size() - 2;\n\t\twhile (i >= 0 && nums[i] >= nums[i + 1])\n\t\t{\n\t\t\ti--;\n\t\t}\n\t\tif (i >= 0)\n\t\t{\n\t\t\tint j = nums.size() - 1;\n\t\t\twhile (j >= 0 && nums[j] >= nums[i])\n\t\t\t{\n\t\t\t\tj--;\n\t\t\t}\n\t\t\tswap(nums.begin() + i, nums.begin() + j);\n\t\t}\n\t\treverse(nums.begin() + i + 1, nums.end());\n\t}\n};",
"java": "class Solution {\n\tpublic void nextPermutation(int[] nums) {\n\t\tint i = nums.length - 1;\n\t\twhile (i > 0 && nums[i] <= nums[i - 1]) {\n\t\t\ti--;\n\t\t}\n\t\tif (i > 0) {\n\t\t\tint j = nums.length - 1;\n\t\t\twhile (j >= 0 && nums[j] <= nums[i - 1]) {\n\t\t\t\tj--;\n\t\t\t}\n\t\t\tswap(nums, i - 1, j);\n\t\t}\n\t\treverse(nums, i);\n\t}\n\tprivate void reverse(int[] nums, int i) {\n\t\tint j = nums.length - 1;\n\t\twhile (i < j) {\n\t\t\tswap(nums, i, j);\n\t\t\ti++;\n\t\t\tj--;\n\t\t}\n\t}\n\tprivate void swap(int[] nums, int i, int j) {\n\t\tint temp = nums[i];\n\t\tnums[i] = nums[j];\n\t\tnums[j] = temp;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef nextPermutation(self, nums):\n\t\tls = len(nums)\n\t\tif ls <= 1:\n\t\t\treturn\n\t\tpair = []\n\t\tfor i in range(ls):\n\t\t\tfor j in range(i + 1, ls):\n\t\t\t\tif nums[i] < nums[j]:\n\t\t\t\t\tpair.append([i,j])\n\t\tpos = 0\n\t\tif len(pair) > 0:\n\t\t\tself.swap(nums, pair[-1][0], pair[-1][1])\n\t\t\tpos = pair[-1][0] + 1\n\t\tfor i in range(pos, ls):\n\t\t\tfor j in range(i + 1, ls):\n\t\t\t\tif nums[i] > nums[j]:\n\t\t\t\t\tself.swap(nums, i, j)\n\t\treturn nums\n\tdef swap(self, nums, index1, index2):\n\t\tif index1 == index2:\n\t\t\treturn\n\t\tnums[index1], nums[index2] = nums[index2], nums[index1]\n# %%\ns = Solution()\nprint(s.nextPermutation(nums = [1,2,3]))",
"status": 1,
"keywords": "数组,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/qq_41645636/article/details/98349631"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/30/30_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/30/30_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/30/30_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 32,
"question_title": "最长有效括号",
"difficulty": "困难",
"question_content": "<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class=\"original__bRMd\"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"(()\"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 \"()\"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \")()())\"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 \"()()\"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"\"<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>\t<li><code>s[i]</code> 为 <code>'('</code> 或 <code>')'</code></li></ul></div></div>",
"topic_link": "https://bbs.csdn.net/topics/600471008",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint longestValidParentheses(string s)\n\t{\n\t\tstack<int> stk;\n\t\tint invalid = -1;\n\t\tint len = 0, max_len = 0;\n\t\tfor (int i = 0; i < s.length(); i++)\n\t\t{\n\t\t\tif (s[i] == '(')\n\t\t\t{\n\t\t\t\tstk.push(i);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (stk.empty())\n\t\t\t\t{\n\t\t\t\t\tinvalid = i;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tstk.pop();\n\t\t\t\t\tif (stk.empty())\n\t\t\t\t\t{\n\t\t\t\t\t\tmax_len = max(i - invalid, max_len);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tmax_len = max(i - stk.top(), max_len);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn max_len;\n\t}\n};",
"java": "import java.util.*;\n\nclass Solution {\n\tpublic int longestValidParentheses(String s) {\n\t\tint left = 0, right = 0, max = 0;\n\t\tfor (int i = 0; i < s.length(); i++) {\n\t\t\tif (s.charAt(i) == '(')\n\t\t\t\tleft++;\n\t\t\telse\n\t\t\t\tright++;\n\t\t\tif (left == right)\n\t\t\t\tmax = Math.max(max, left * 2);\n\t\t\tif (right > left)\n\t\t\t\tleft = right = 0;\n\t\t}\n\t\tleft = 0;\n\t\tright = 0;\n\t\tfor (int i = s.length() - 1; i >= 0; i--) {\n\t\t\tif (s.charAt(i) == '(')\n\t\t\t\tleft++;\n\t\t\telse\n\t\t\t\tright++;\n\t\t\tif (left == right)\n\t\t\t\tmax = Math.max(max, left * 2);\n\t\t\tif (right < left)\n\t\t\t\tleft = right = 0;\n\t\t}\n\t\treturn max;\n\t}\n}\n",
"js": "",
"python": "import pdb\nclass Solution(object):\n\tdef longestValidParentheses(self, s):\n\t\tls = len(s)\n\t\tstack = []\n\t\tdata = [0] * ls\n\t\tfor i in range(ls):\n\t\t\tcurr = s[i]\n\t\t\tif curr == '(':\n\t\t\t\tstack.append(i)\n\t\t\telse:\n\t\t\t\tif len(stack) > 0:\n\t\t\t\t\tdata[i] = 1\n\t\t\t\t\tdata[stack.pop(-1)] = 1\n\t\ttep, res = 0, 0\n\t\tfor t in data:\n\t\t\tif t == 1:\n\t\t\t\ttep += 1\n\t\t\telse:\n\t\t\t\tres = max(tep, res)\n\t\t\t\ttep = 0\n\t\treturn max(tep, res)\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.longestValidParentheses(')()())'))",
"status": 1,
"keywords": "栈,字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/31/31_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/31/31_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/31/31_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 33,
"question_title": "搜索旋转排序数组",
"difficulty": "中等",
"question_content": "<p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong> 。</p>\n<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 <= k < nums.length</code>)上进行了 <strong>旋转</strong>,使数组变为\n <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0 开始</strong>\n 计数)。例如, <code>[0,1,2,4,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code> 。\n</p>\n<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值\n <code>target</code> ,则返回它的下标,否则返回 <code>-1</code> 。\n</p>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 0<strong><br />输出:</strong>4</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 3<strong><br />输出:</strong>-1</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>nums = [1], target = 0<strong><br />输出:</strong>-1</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 <= nums.length <= 5000</code></li>\n <li><code>-10^4 <= nums[i] <= 10^4</code></li>\n <li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>\n <li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>\n <li><code>-10^4 <= target <= 10^4</code></li>\n</ul>\n<p> </p>\n<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗?</p>",
"topic_link": "https://bbs.csdn.net/topics/600470220",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint search(vector<int> &nums, int target)\n\t{\n\t\tint lo = 0;\n\t\tint hi = nums.size() - 1;\n\t\tfor (lo <= hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (nums[mid] == target)\n\t\t\t{\n\t\t\t\treturn mid;\n\t\t\t}\n\t\t\tif (nums[lo] <= nums[mid])\n\t\t\t{\n\t\t\t\tif (nums[lo] <= target && target < nums[mid])\n\t\t\t\t{\n\t\t\t\t\thi = mid - 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlo = mid + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (nums[mid] < target && target <= nums[hi])\n\t\t\t\t{\n\t\t\t\t\tlo = mid + 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\thi = mid - 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n};",
"java": "class Solution {\n\tpublic int search(int[] nums, int target) {\n\t\tint start = 0;\n\t\tint end = nums.length - 1;\n\n\t\twhile (start <= end) {\n\t\t\tint mid = start + (end - start) / 2;\n\t\t\tif (nums[mid] == target) {\n\t\t\t\treturn mid;\n\t\t\t}\n\n\t\t\tif (nums[start] <= nums[mid]) {\n\t\t\t\tif (target >= nums[start] && target <= nums[mid]) {\n\t\t\t\t\tend = mid - 1;\n\t\t\t\t} else {\n\t\t\t\t\tstart = start + 1;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (nums[mid] <= nums[end]) {\n\t\t\t\tif (target >= nums[mid] && target <= nums[end]) {\n\t\t\t\t\tstart = mid + 1;\n\t\t\t\t} else {\n\t\t\t\t\tend = end - 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef search(self, nums, target):\n\t\tdef get(start, end):\n\t\t\tif start > end:\n\t\t\t\treturn -1\n\t\t\tmid = (start + end) / 2\n\t\t\tmid = int(mid)\n\t\t\tif nums[mid] == target:\n\t\t\t\treturn mid\n\t\t\telif nums[mid] >= nums[start]: \n\t\t\t\tif target >= nums[start] and target < nums[mid]:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\t\t\telse:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\telif nums[mid] <= nums[end]: \n\t\t\t\tif target > nums[mid] and target <= nums[end]:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\t\telse:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\treturn get(0, len(nums) - 1)\n# %%\ns = Solution()\nprint(s.search(nums = [4,5,6,7,0,1,2], target = 0))",
"status": 1,
"keywords": "数组,二分查找",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/32/32_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/32/32_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/32/32_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 34,
"question_title": "在排序数组中查找元素的第一个和最后一个位置",
"difficulty": "中等",
"question_content": "<p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>\n<p>如果数组中不存在目标值 <code>target</code>,返回 <code>[-1, -1]</code>。</p>\n<p><strong>进阶:</strong></p>\n<ul>\n <li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗?</li>\n</ul>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出:</strong>[3,4]</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出:</strong>[-1,-1]</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[-1,-1]</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>0 <= nums.length <= 10<sup>5</sup></code></li>\n <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\n <li><code>nums</code> 是一个非递减数组</li>\n <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470221",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> searchRange(vector<int> &nums, int target)\n\t{\n\t\tvector<int> res;\n\t\tres.push_back(binary_search_begin(nums, target));\n\t\tres.push_back(binary_search_end(nums, target));\n\t\treturn res;\n\t}\nprivate:\n\tint binary_search_begin(vector<int> nums, int target)\n\t{\n\t\tint lo = -1;\n\t\tint hi = nums.size();\n\t\twhile (lo + 1 < hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (target > nums[mid])\n\t\t\t{\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t}\n\t\tif (hi == nums.size() || nums[hi] != target)\n\t\t{\n\t\t\treturn -1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn hi;\n\t\t}\n\t}\n\tint binary_search_end(vector<int> nums, int target)\n\t{\n\t\tint lo = -1;\n\t\tint hi = nums.size();\n\t\twhile (lo + 1 < hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (target < nums[mid])\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t}\n\t\tif (lo == -1 || nums[lo] != target)\n\t\t{\n\t\t\treturn -1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn lo;\n\t\t}\n\t}\n};",
"java": "public class Solution {\n\n\tpublic int[] searchRange(int[] nums, int target) {\n\t\tint[] result = new int[2];\n\t\tresult[0] = floor(nums, target);\n\t\tresult[1] = ceil(nums, target);\n\t\treturn result;\n\t}\n\n\tprivate int floor(int[] nums, int target) {\n\t\tint left = -1;\n\t\tint right = nums.length - 1;\n\t\twhile (left < right) {\n\t\t\tint mid = left + (right - left + 1) / 2;\n\t\t\tif (target <= nums[mid]) {\n\t\t\t\tright = mid - 1;\n\t\t\t} else {\n\t\t\t\tleft = mid;\n\t\t\t}\n\t\t}\n\t\tif (left + 1 < nums.length && nums[left + 1] == target) {\n\t\t\treturn left + 1;\n\t\t} else {\n\t\t\treturn -1;\n\t\t}\n\t}\n\n\tprivate int ceil(int[] nums, int target) {\n\t\tint left = 0;\n\t\tint right = nums.length;\n\t\twhile (left < right) {\n\t\t\tint mid = left + (right - left) / 2;\n\t\t\tif (target >= nums[mid]) {\n\t\t\t\tleft = mid + 1;\n\t\t\t} else {\n\t\t\t\tright = mid;\n\t\t\t}\n\t\t}\n\t\tif (right - 1 >= 0 && nums[right - 1] == target) {\n\t\t\treturn right - 1;\n\t\t} else {\n\t\t\treturn -1;\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef searchRange(self, nums, target):\n\t\tlength = len(nums)\n\t\tif length == 0:\n\t\t\treturn [-1, -1]\n\t\tmin = 0\n\t\tmax = length - 1\n\t\twhile min <= max:\n\t\t\tpos = (min + max) / 2\n\t\t\tpos = int(pos)\n\t\t\tif nums[pos] > target:\n\t\t\t\tmax = pos - 1\n\t\t\telif nums[pos] < target:\n\t\t\t\tmin = pos + 1\n\t\t\telse:\n\t\t\t\tfor i in range(min, max + 1):\n\t\t\t\t\tif nums[i] == target:\n\t\t\t\t\t\tif min < i and nums[min] != nums[i]:\n\t\t\t\t\t\t\tmin = i\n\t\t\t\t\t\tmax = i\n\t\t\t\treturn [min, max]\n\t\treturn [-1, -1]\n# %%\ns = Solution()\nprint(s.searchRange(nums = [5,7,7,8,8,10], target = 8))",
"status": 1,
"keywords": "数组,二分查找",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/33/33_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/33/33_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/33/33_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 35,
"question_title": "搜索插入位置",
"difficulty": "简单",
"question_content": "<p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470222",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint searchInsert(vector<int> &nums, int target)\n\t{\n\t\tint lo = -1;\n\t\tint hi = nums.size();\n\t\twhile (lo + 1 < hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (target > nums[mid])\n\t\t\t{\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t}\n\t\treturn hi;\n\t}\n};",
"java": "class Solution {\n\tpublic int searchInsert(int[] nums, int target) {\n\t\tint left = 0, right = nums.length - 1;\n\t\tif (target < nums[left])\n\t\t\treturn 0;\n\t\tif (target > nums[right])\n\t\t\treturn nums.length;\n\t\twhile (left <= right) {\n\t\t\tint mid = (right - left) / 2 + left;\n\t\t\tif (target < nums[mid]) {\n\t\t\t\tright = mid - 1;\n\t\t\t} else if (target > nums[mid]) {\n\t\t\t\tleft = mid + 1;\n\t\t\t} else {\n\t\t\t\treturn mid;\n\t\t\t}\n\t\t}\n\t\treturn left;\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef searchInsert(self, nums, target):\n\t\tl, r = int(0), len(nums) - 1\n\t\twhile l < r:\n\t\t\tmid = int((l + r) / 2)\n\t\t\tif nums[mid] < target:\n\t\t\t\tl = mid + 1\n\t\t\telse:\n\t\t\t\tr = mid\n\t\tif nums[l] < target:\n\t\t\treturn l + 1\n\t\treturn l \nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.searchInsert([1,3,5,6],5))\t",
"status": 1,
"keywords": "数组,二分查找",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/34/34_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/34/34_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/34/34_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 36,
"question_title": "有效的数独",
"difficulty": "中等",
"question_content": "<p>请你判断一个 <code>9x9</code> 的数独是否有效。只需要<strong> 根据以下规则</strong> ,验证已经填入的数字是否有效即可。</p>\n<ol>\n <li>数字 <code>1-9</code> 在每一行只能出现一次。</li>\n <li>数字 <code>1-9</code> 在每一列只能出现一次。</li>\n <li>数字 <code>1-9</code> 在每一个以粗实线分隔的 <code>3x3</code> 宫内只能出现一次。(请参考示例图)</li>\n</ol>\n<p>数独部分空格内已填入了数字,空白格用 <code>'.'</code> 表示。</p>\n<p><strong>注意:</strong></p>\n<ul>\n <li>一个有效的数独(部分已被填充)不一定是可解的。</li>\n <li>只需要根据以上规则,验证已经填入的数字是否有效即可。</li>\n</ul>\n<p> </p>\n<p><strong>示例 1:</strong></p><img\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-sudoku-by-l2g-20050714svg.png\"\n style=\"height:250px; width:250px\" />\n<pre><strong>输入:</strong>board = \n [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"]\n ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"]\n ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"]\n ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"]\n ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"]\n ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"]\n ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"]\n ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"]\n ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\n<strong>输出:</strong>true\n</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>board = \n [[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"]\n ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"]\n ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"]\n ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"]\n ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"]\n ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"]\n ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"]\n ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"]\n ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\n<strong>输出:</strong>false\n<strong>解释:</strong>除了第一行的第一个数字从<strong> 5</strong> 改为 <strong>8 </strong>以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>board.length == 9</code></li>\n <li><code>board[i].length == 9</code></li>\n <li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600469920",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tbool isValidSudoku(vector<vector<char>> &board)\n\t{\n\t\tfor (int i = 0; i < board.size(); i++)\n\t\t{\n\t\t\tvector<bool> mark(10);\n\t\t\tfor (int j = 0; j < board.size(); j++)\n\t\t\t{\n\t\t\t\tif (!valid(board, mark, i, j))\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < board.size(); j++)\n\t\t{\n\t\t\tvector<bool> mark(10);\n\t\t\tfor (int i = 0; i < board.size(); i++)\n\t\t\t{\n\t\t\t\tif (!valid(board, mark, i, j))\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int k = 0; k < board.size(); k++)\n\t\t{\n\t\t\tint sr = k / 3 * 3;\n\t\t\tint sc = (k % 3) * 3;\n\t\t\tvector<bool> mark(10);\n\t\t\tfor (int i = sr; i < sr + 3; i++)\n\t\t\t{\n\t\t\t\tfor (int j = sc; j < sc + 3; j++)\n\t\t\t\t{\n\t\t\t\t\tif (!valid(board, mark, i, j))\n\t\t\t\t\t{\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\nprivate:\n\tbool valid(vector<vector<char>> &board, vector<bool> &mark, int i, int j)\n\t{\n\t\tif (board[i][j] != '.')\n\t\t{\n\t\t\tint index = board[i][j] - '0';\n\t\t\tif (mark[index])\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmark[index] = 1;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n};",
"java": "class Solution {\n\tpublic boolean isValidSudoku(char[][] board) {\n\t\tboolean[][] row = new boolean[9][9];\n\t\tboolean[][] col = new boolean[9][9];\n\t\tboolean[][] block = new boolean[9][9];\n\t\tfor (int i = 0; i < 9; i++) {\n\t\t\tfor (int j = 0; j < 9; j++) {\n\t\t\t\tif (board[i][j] != '.') {\n\t\t\t\t\tint num = board[i][j] - '1';\n\t\t\t\t\tint blockIndex = i / 3 * 3 + j / 3;\n\t\t\t\t\tif (row[i][num] || col[j][num] || block[blockIndex][num]) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t} else {\n\t\t\t\t\t\trow[i][num] = true;\n\t\t\t\t\t\tcol[j][num] = true;\n\t\t\t\t\t\tblock[blockIndex][num] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
"js": "",
"python": "from typing import List\nclass Solution:\n\tdef isValidSudoku(self, board):\n\t\t\"\"\"\n\t\t:type board: List[List[str]]\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\traw = [{},{},{},{},{},{},{},{},{}]\n\t\tcol = [{},{},{},{},{},{},{},{},{}]\n\t\tcell = [{},{},{},{},{},{},{},{},{}]\n\t\tfor i in range(9):\n\t\t\tfor j in range(9):\t\t\t\t\t\t\t\t \n\t\t\t\tnum = (3*(i//3) + j//3)\n\t\t\t\ttemp = board[i][j]\n\t\t\t\tif temp != \".\":\n\t\t\t\t\tif temp not in raw[i] and temp not in col[j] and temp not in cell[num]:\n\t\t\t\t\t\traw [i][temp] = 1\n\t\t\t\t\t\tcol [j][temp] = 1\n\t\t\t\t\t\tcell [num][temp] =1\n\t\t\t\t\telse:\n\t\t\t\t\t\treturn False\t\n\t\treturn True\n# %%\ns = Solution()\nboard = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"]\n\t\t,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"]\n\t\t,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"]\n\t\t,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"]\n\t\t,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"]\n\t\t,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"]\n\t\t,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"]\n\t\t,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"]\n\t\t,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\nprint(s.isValidSudoku(board))",
"status": 1,
"keywords": "数组,哈希表,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_45176257/article/details/106487272"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/35/35_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/35/35_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/35/35_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 37,
"question_title": "解数独",
"difficulty": "困难",
"question_content": "<p>编写一个程序,通过填充空格来解决数独问题。</p>\n<p>数独的解法需<strong> 遵循如下规则</strong>:</p>\n<ol>\n <li>数字 <code>1-9</code> 在每一行只能出现一次。</li>\n <li>数字 <code>1-9</code> 在每一列只能出现一次。</li>\n <li>数字 <code>1-9</code> 在每一个以粗实线分隔的 <code>3x3</code> 宫内只能出现一次。(请参考示例图)</li>\n</ol>\n<p>数独部分空格内已填入了数字,空白格用 <code>'.'</code> 表示。</p>\n<p> </p>\n<div class=\"top-view__1vxA\">\n <div class=\"original__bRMd\">\n <div>\n <p><strong>示例:</strong></p><img\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0037.Sudoku%20Solver/images/250px-sudoku-by-l2g-20050714svg.png\" />\n <pre><strong>输入:</strong>board = \n [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],\n [\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],\n [\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],\n [\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],\n [\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],\n [\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],\n [\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],\n [\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],\n [\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\n<strong>输出:</strong>\n [[\"5\",\"3\",\"4\",\"6\",\"7\",\"8\",\"9\",\"1\",\"2\"],\n [\"6\",\"7\",\"2\",\"1\",\"9\",\"5\",\"3\",\"4\",\"8\"],\n [\"1\",\"9\",\"8\",\"3\",\"4\",\"2\",\"5\",\"6\",\"7\"],\n [\"8\",\"5\",\"9\",\"7\",\"6\",\"1\",\"4\",\"2\",\"3\"],\n [\"4\",\"2\",\"6\",\"8\",\"5\",\"3\",\"7\",\"9\",\"1\"],\n [\"7\",\"1\",\"3\",\"9\",\"2\",\"4\",\"8\",\"5\",\"6\"],\n [\"9\",\"6\",\"1\",\"5\",\"3\",\"7\",\"2\",\"8\",\"4\"],\n [\"2\",\"8\",\"7\",\"4\",\"1\",\"9\",\"6\",\"3\",\"5\"],\n [\"3\",\"4\",\"5\",\"2\",\"8\",\"6\",\"1\",\"7\",\"9\"]]\n<strong>解释:</strong>输入的数独如上图所示,唯一有效的解决方案如下所示:\n<p> </p> \n</pre>\n <img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0037.Sudoku%20Solver/images/250px-sudoku-by-l2g-20050714_solutionsvg.png\"\n style=\"height:250px; width:250px\" />\n\n <p> </p>\n <p><strong>提示:</strong></p>\n <ul>\n <li><code>board.length == 9</code></li>\n <li><code>board[i].length == 9</code></li>\n <li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>\n <li>题目数据 <strong>保证</strong> 输入数独仅有一个解</li>\n </ul>\n </div>\n </div>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470827",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid solveSudoku(vector<vector<char>> &board)\n\t{\n\t\tint size = board.size();\n\t\tvector<vector<bool>> rows(size, vector<bool>(10));\n\t\tvector<vector<bool>> cols(size, vector<bool>(10));\n\t\tvector<vector<bool>> boxes(size, vector<bool>(10));\n\t\tfor (int i = 0; i < size; i++)\n\t\t{\n\t\t\tfor (int j = 0; j < size; j++)\n\t\t\t{\n\t\t\t\tif (board[i][j] != '.')\n\t\t\t\t{\n\t\t\t\t\tint num = board[i][j] - '0';\n\t\t\t\t\tint idx = i / 3 * 3 + j / 3;\n\t\t\t\t\trows[i][num] = true;\n\t\t\t\t\tcols[j][num] = true;\n\t\t\t\t\tboxes[idx][num] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdfs(board, 0, rows, cols, boxes);\n\t}\nprivate:\n\tbool valid(int num, int row, int col, int idx, vector<vector<bool>> &rows,\n\t\t\t vector<vector<bool>> &cols, vector<vector<bool>> &boxes)\n\t{\n\t\treturn !rows[row][num] && !cols[col][num] && !boxes[idx][num];\n\t}\n\tbool dfs(vector<vector<char>> &board, int size, vector<vector<bool>> &rows,\n\t\t\t vector<vector<bool>> &cols, vector<vector<bool>> &boxes)\n\t{\n\t\tif (size == 9 * 9)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbool ok = false;\n\t\t\tint row = size / 9;\n\t\t\tint col = size % 9;\n\t\t\tint idx = row / 3 * 3 + col / 3;\n\t\t\tif (board[row][col] == '.')\n\t\t\t{\n\t\t\t\tfor (int i = 1; i <= 9; i++)\n\t\t\t\t{\n\t\t\t\t\tif (valid(i, row, col, idx, rows, cols, boxes))\n\t\t\t\t\t{\n\t\t\t\t\t\tboard[row][col] = i + '0';\n\t\t\t\t\t\trows[row][i] = true;\n\t\t\t\t\t\tcols[col][i] = true;\n\t\t\t\t\t\tboxes[idx][i] = true;\n\t\t\t\t\t\tok = dfs(board, size + 1, rows, cols, boxes);\n\t\t\t\t\t\tif (!ok)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\trows[row][i] = false;\n\t\t\t\t\t\t\tcols[col][i] = false;\n\t\t\t\t\t\t\tboxes[idx][i] = false;\n\t\t\t\t\t\t\tboard[row][col] = '.';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tok = dfs(board, size + 1, rows, cols, boxes);\n\t\t\t}\n\t\t\treturn ok;\n\t\t}\n\t}\n};",
"java": "class Solution {\n\n\tboolean row[][] = new boolean[9][9];\n\tboolean col[][] = new boolean[9][9];\n\n\tboolean cell[][][] = new boolean[3][3][9];\n\n\tpublic void solveSudoku(char[][] board) {\n\n\t\tfor (int i = 0; i < 9; i++) {\n\t\t\tfor (int j = 0; j < 9; j++) {\n\t\t\t\tif (board[i][j] != '.') {\n\n\t\t\t\t\tint t = board[i][j] - '1';\n\n\t\t\t\t\trow[i][t] = col[j][t] = cell[i / 3][j / 3][t] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tdfs(board, 0, 0);\n\t}\n\n\tpublic boolean dfs(char[][] board, int x, int y) {\n\n\t\tif (y == 9) {\n\n\t\t\tx++;\n\t\t\ty = 0;\n\t\t}\n\n\t\tif (x == 9)\n\t\t\treturn true;\n\n\t\tif (board[x][y] != '.')\n\t\t\treturn dfs(board, x, y + 1);\n\n\t\tfor (int num = 0; num < 9; num++) {\n\n\t\t\tif (!row[x][num] && !col[y][num] && !cell[x / 3][y / 3][num]) {\n\n\t\t\t\tboard[x][y] = (char) (num + '1');\n\t\t\t\trow[x][num] = col[y][num] = cell[x / 3][y / 3][num] = true;\n\n\t\t\t\tif (dfs(board, x, y + 1))\n\t\t\t\t\treturn true;\n\n\t\t\t\tboard[x][y] = '.';\n\t\t\t\trow[x][num] = col[y][num] = cell[x / 3][y / 3][num] = false;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef solveSudoku(self, board):\n\t\t\tdef isvaild(i,j):\n\t\t\t\tfor m in range(9):\n\t\t\t\t\tif m!=i and board[m][j]==board[i][j]:\n\t\t\t\t\t\treturn False\n\t\t\t\tfor n in range(9):\n\t\t\t\t\tif n!=j and board[i][n]==board[i][j]:\n\t\t\t\t\t\treturn False\n\t\t\t\tfor m in range(i//3*3,i//3*3+3):\n\t\t\t\t\tfor n in range(j//3*3,j//3*3+3):\n\t\t\t\t\t\tif m!=i and n!=j and board[m][n]==board[i][j]:\n\t\t\t\t\t\t\treturn False\n\t\t\t\treturn True\n\t\t\tdef f(a,b,board):\n\t\t\t\tfor i in range(a,9):\n\t\t\t\t\tfor j in range(b,9):\n\t\t\t\t\t\tif board[i][j]=='.':\n\t\t\t\t\t\t\tfor c in '123456789':\n\t\t\t\t\t\t\t\tboard[i][j]=c\n\t\t\t\t\t\t\t\tif isvaild(i,j):\n\t\t\t\t\t\t\t\t\tif f(a,b,board):return True\n\t\t\t\t\t\t\t\t\telse: board[i][j] = '.'\n\t\t\t\t\t\t\t\telse:board[i][j] = '.'\n\t\t\t\t\t\t\treturn False\n\t\t\t\treturn True\n\t\t\tf(0,0,board)\n\t\t\treturn board\n# %%\ns = Solution()\nboard = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\nprint(s.solveSudoku(board))",
"status": 1,
"keywords": "数组,回溯,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/36/36_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/36/36_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/36/36_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 38,
"question_title": "外观数列",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>给定一个正整数 <code>n</code> ,输出外观数列的第 <code>n</code> 项。</p>\n\n <p>「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。</p>\n\n <p>你可以将其视作是由递归公式定义的数字字符串序列:</p>\n\n <ul>\n <li><code>countAndSay(1) = \"1\"</code></li>\n <li><code>countAndSay(n)</code> 是对 <code>countAndSay(n-1)</code> 的描述,然后转换成另一个数字字符串。</li>\n </ul>\n\n <p>前五项如下:</p>\n\n <pre>\n 1. 1\n 2. 11\n 3. 21\n 4. 1211\n 5. 111221\n 第一项是数字 1 \n 描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 \"11\"\n 描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 \"21\"\n 描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 \"1211\"\n 描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 \"111221\"\n </pre>\n\n <p>要 <strong>描述</strong> 一个数字字符串,首先要将字符串分割为 <strong>最小</strong> 数量的组,每个组都由连续的最多 <strong>相同字符</strong>\n 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。</p>\n\n <p>例如,数字字符串 <code>\"3322251\"</code> 的描述如下图:</p>\n <img alt=\"\"\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0038.Count%20and%20Say/images/countandsay.jpg\"\n style=\"width: 581px; height: 172px;\" />\n <ul>\n </ul>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n\n <pre><strong>输入:</strong>n = 1\n<strong>输出:</strong>\"1\"\n<strong>解释:</strong>这是一个基本样例。\n</pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>n = 4\n<strong>输出:</strong>\"1211\"\n<strong>解释:</strong>\ncountAndSay(1) = \"1\"\ncountAndSay(2) = 读 \"1\" = 一 个 1 = \"11\"\ncountAndSay(3) = 读 \"11\" = 二 个 1 = \"21\"\ncountAndSay(4) = 读 \"21\" = 一 个 2 + 一 个 1 = \"12\" + \"11\" = \"1211\"\n</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= n &lt;= 30</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470795",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic void parse(char *input, char *output)\n{\n\tchar *p = input;\n\tchar *q = output;\n\twhile (*p != '\\0')\n\t{\n\t\tint count = 1;\n\t\twhile (p[0] == p[1])\n\t\t{\n\t\t\tcount++;\n\t\t\tp++;\n\t\t}\n\t\tint n = 0;\n\t\twhile (count > 0)\n\t\t{\n\t\t\tn += count % 10;\n\t\t\tcount /= 10;\n\t\t}\n\t\twhile (n > 0)\n\t\t{\n\t\t\t*q++ = (n % 10) + '0';\n\t\t\tn /= 10;\n\t\t}\n\t\t*q++ = p[0];\n\t\tp++;\n\t}\n\t*q = '\\0';\n}\nstatic char *countAndSay(int n)\n{\n\tif (n < 1)\n\t{\n\t\treturn NULL;\n\t}\n\tchar *result;\n\tchar *prev = malloc(10000);\n\tchar *next = malloc(10000);\n\tstrcpy(prev, \"1\");\n\tif (n == 1)\n\t{\n\t\treturn prev;\n\t}\n\tint i;\n\tfor (i = 2; i <= n; i++)\n\t{\n\t\tif (i & 0x1)\n\t\t{\n\t\t\tparse(next, prev);\n\t\t\tresult = prev;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tparse(prev, next);\n\t\t\tresult = next;\n\t\t}\n\t}\n\treturn result;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", countAndSay(atoi(argv[1])));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic String countAndSay(int n) {\n\t\tString pre = \"1\";\n\t\tfor (int i = 1; i < n; i++) {\n\t\t\tStringBuilder temp = new StringBuilder();\n\t\t\tchar c = pre.charAt(0);\n\t\t\tint cnt = 1;\n\t\t\tfor (int j = 1; j < pre.length(); j++) {\n\t\t\t\tchar cc = pre.charAt(j);\n\t\t\t\tif (c == cc) {\n\t\t\t\t\tcnt++;\n\t\t\t\t} else {\n\t\t\t\t\ttemp.append(cnt).append(c);\n\t\t\t\t\tcnt = 1;\n\t\t\t\t\tc = cc;\n\t\t\t\t}\n\t\t\t}\n\t\t\ttemp.append(cnt).append(c);\n\t\t\tpre = temp.toString();\n\t\t}\n\t\treturn pre;\n\t}\n}",
"js": "",
"python": "class Solution:\n\tdef countAndSay(self, n):\n\t\tif n == 1:\n\t\t\treturn '1'\n\t\tx = '1'\n\t\twhile n > 1:\n\t\t\tx = self.count(x)\n\t\t\tn -= 1\n\t\treturn x\n\tdef count(self, x):\n\t\tm = list(x)\n\t\tres = []\n\t\tm.append(None)\n\t\ti , j = 0 , 0\n\t\twhile i < len(m) - 1:\n\t\t\tj += 1\n\t\t\tif m[j] != m[i]:\n\t\t\t\tres += [j - i, m[i]]\n\t\t\t\ti = j\n\t\treturn ''.join(str(s) for s in res)\n# %%\ns = Solution()\nprint(s.countAndSay(n = 4))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104314641"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/37/37_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/37/37_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/37/37_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 39,
"question_title": "组合总和",
"difficulty": "中等",
"question_content": "<p>给定一个<strong>无重复元素</strong>的数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。\n</p>\n<p><code>candidates</code>&nbsp;中的数字可以无限制重复被选取。</p>\n<p><strong>说明:</strong></p>\n<ul>\n <li>所有数字(包括&nbsp;<code>target</code>)都是正整数。</li>\n <li>解集不能包含重复的组合。&nbsp;</li>\n</ul>\n<p><strong>示例&nbsp;1:</strong></p>\n<pre><strong>输入:</strong>candidates = [2,3,6,7], target = 7,<strong><br />输出:</strong>[[7],[2,2,3]]</pre>\n<p><strong>示例&nbsp;2:</strong></p>\n<pre><strong>输入:</strong>candidates = [2,3,5], target = 8,<strong><br />输出:</strong>[[2,2,2,2],[2,3,3],[3,5]]</pre>\n<p>&nbsp;</p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 &lt;= candidates.length &lt;= 30</code></li>\n <li><code>1 &lt;= candidates[i] &lt;= 200</code></li>\n <li><code>candidate</code> 中的每个元素都是独一无二的。</li>\n <li><code>1 &lt;= target &lt;= 500</code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600471017",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> combinationSum(vector<int> &candidates, int target)\n\t{\n\t\tvector<vector<int>> res;\n\t\tdfs(candidates, 0, target, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &res)\n\t{\n\t\tif (target < 0)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\telse if (target == 0)\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = start; i < candidates.size(); i++)\n\t\t\t{\n\t\t\t\tstack.push_back(candidates[i]);\n\t\t\t\tdfs(candidates, i, target - candidates[i], res);\n\t\t\t\tstack.pop_back();\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> combinationSum(int[] candiates, int target) {\n\n\t\tList<List<Integer>> resultList = new ArrayList<>();\n\n\t\tList<Integer> result = new ArrayList<>();\n\n\t\tArrays.sort(candiates);\n\n\t\tdfs(candiates, resultList, result, 0, target);\n\t\treturn resultList;\n\n\t}\n\n\tprivate void dfs(int[] candiates, List<List<Integer>> resultList, List<Integer> result, int start, int target) {\n\n\t\tif (target < 0) {\n\t\t\treturn;\n\t\t}\n\n\t\telse if (target == 0) {\n\n\t\t\tresultList.add(new ArrayList<>(result));\n\t\t} else {\n\t\t\tfor (int i = start; i < candiates.length; i++) {\n\t\t\t\tresult.add(candiates[i]);\n\n\t\t\t\tdfs(candiates, resultList, result, i, target - candiates[i]);\n\n\t\t\t\tresult.remove(result.size() - 1);\n\t\t\t}\n\t\t}\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef combinationSum(self, candidates, target):\n\t\tcandidates.sort()\n\t\tdp = [[] for _ in range(target + 1)]\n\t\tdp[0].append([])\n\t\tfor i in range(1, target + 1):\n\t\t\tfor j in range(len(candidates)):\n\t\t\t\tif candidates[j] > i:\n\t\t\t\t\tbreak\n\t\t\t\tfor k in range(len(dp[i - candidates[j]])):\n\t\t\t\t\ttemp = dp[i - candidates[j]][k][:]\n\t\t\t\t\tif len(temp) > 0 and temp[-1] > candidates[j]:\n\t\t\t\t\t\tcontinue\n\t\t\t\t\ttemp.append(candidates[j])\n\t\t\t\t\tdp[i].append(temp)\n\t\treturn dp[target]\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.combinationSum([8,7,4,3], 11))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/38/38_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/38/38_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/38/38_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 40,
"question_title": "组合总和 II",
"difficulty": "中等",
"question_content": "<p>给定一个数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。\n</p>\n<p><code>candidates</code>&nbsp;中的每个数字在每个组合中只能使用一次。</p>\n<p><strong>说明:</strong></p>\n<ul>\n <li>所有数字(包括目标数)都是正整数。</li>\n <li>解集不能包含重复的组合。&nbsp;</li>\n</ul>\n<p><strong>示例&nbsp;1:</strong></p>\n<pre><strong>输入:</strong> candidates =&nbsp;[10,1,2,7,6,1,5], target =&nbsp;8,<strong><br />所求解集为:</strong>[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]</pre>\n<p><strong>示例&nbsp;2:</strong></p>\n<pre><strong>输入:</strong> candidates =&nbsp;[2,5,2,1,2], target =&nbsp;5,<strong><br />所求解集为:</strong>[[1,2,2],[5]]</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470928",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> combinationSum2(vector<int> &candidates, int target)\n\t{\n\t\tvector<vector<int>> res;\n\t\tsort(candidates.begin(), candidates.end());\n\t\tdfs(candidates, 0, target, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &res)\n\t{\n\t\tif (target < 0)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\telse if (target == 0)\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint last = INT_MIN;\n\t\t\tfor (int i = start; i < candidates.size(); i++)\n\t\t\t{\n\t\t\t\tif (last != candidates[i])\n\t\t\t\t{\n\t\t\t\t\tstack.push_back(candidates[i]);\n\t\t\t\t\tdfs(candidates, i + 1, target - candidates[i], res);\n\t\t\t\t\tstack.pop_back();\n\t\t\t\t}\n\t\t\t\tlast = candidates[i];\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> combinationSum2(int[] candidates, int target) {\n\t\tArrays.sort(candidates);\n\t\tList<List<Integer>> res = new ArrayList<List<Integer>>();\n\t\tif (candidates.length == 0 || target < candidates[0])\n\t\t\treturn res;\n\t\tList<Integer> tmp = new ArrayList<Integer>();\n\t\thelper(candidates, target, 0, tmp, res);\n\t\treturn res;\n\t}\n\n\tpublic void helper(int[] a, int target, int start, List<Integer> tmp, List<List<Integer>> res) {\n\t\tif (target < 0)\n\t\t\treturn;\n\t\tif (target == 0) {\n\t\t\tres.add(new ArrayList<Integer>(tmp));\n\t\t\treturn;\n\t\t}\n\t\tfor (int i = start; i < a.length; i++) {\n\t\t\ttmp.add(a[i]);\n\t\t\tint newtarget = target - a[i];\n\t\t\thelper(a, newtarget, i + 1, tmp, res);\n\t\t\ttmp.remove(tmp.size() - 1);\n\t\t\tif (newtarget <= 0)\n\t\t\t\tbreak;\n\t\t\twhile (i + 1 < a.length && a[i] == a[i + 1])// 组合中有重复元素,不要重复开头\n\t\t\t\ti++;\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef combinationSum2(self, candidates, target):\n\t\t\"\"\"\n\t\t:type candidates: List[int]\n\t\t:type target: int\n\t\t:rtype: List[List[int]]\n\t\t\"\"\"\n\t\tcandidates.sort()\n\t\tdp = [[] for _ in range(target + 1)]\n\t\tdp[0].append([])\n\t\tfor i in range(1, target + 1):\n\t\t\tfor j in range(len(candidates)):\n\t\t\t\tif candidates[j] > i:\n\t\t\t\t\tbreak\n\t\t\t\tfor k in range(len(dp[i - candidates[j]])):\n\t\t\t\t\ttemp = dp[i - candidates[j]][k][:]\n\t\t\t\t\tif len(temp) > 0 and temp[-1] >= j:\n\t\t\t\t\t\tcontinue\n\t\t\t\t\ttemp.append(j)\n\t\t\t\t\tdp[i].append(temp)\n\t\tres = []\n\t\tcheck = {}\n\t\tfor temp in dp[target]:\n\t\t\tvalue = [candidates[t] for t in temp]\n\t\t\ttry:\n\t\t\t\tcheck[str(value)] += 1\n\t\t\texcept KeyError:\n\t\t\t\tcheck[str(value)] = 1\n\t\t\t\tres.append(value)\n\t\treturn res\n# %%\ns = Solution()\nprint(s.combinationSum2(candidates = [2,5,2,1,2], target = 5))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/39/39_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/39/39_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/39/39_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 5,
"question_title": "最长回文子串",
"difficulty": "中等",
"question_content": "<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"babad\"<strong><br />输出:</strong>\"bab\"<strong><br />解释:</strong>\"aba\" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"cbbd\"<strong><br />输出:</strong>\"bb\"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"a\"<strong><br />输出:</strong>\"a\"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \"ac\"<strong><br />输出:</strong>\"a\"</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length <= 1000</code></li>\t<li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470834",
"cpp": "class Solution\n{\npublic:\n\tstring longestPalindrome(string s)\n\t{\n\t\tint ti = 0, maxlen = 0, i, t;\n\t\tfor (i = 0; s[i]; i++)\n\t\t{\n\t\t\tt = 1;\n\t\t\twhile (t <= i && s[i + t])\n\t\t\t{\n\t\t\t\tif (s[i + t] == s[i - t])\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t + 1 > maxlen)\n\t\t\t{\n\t\t\t\tti = i - t;\n\t\t\t\tmaxlen = 2 * t + 1;\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; s[i]; i++)\n\t\t{\n\t\t\tt = 1;\n\t\t\twhile (t <= i + 1 && s[i + t])\n\t\t\t{\n\t\t\t\tif (s[i - t + 1] == s[i + t])\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t > maxlen)\n\t\t\t{\n\t\t\t\tti = i - t + 1;\n\t\t\t\tmaxlen = 2 * t;\n\t\t\t}\n\t\t}\n\t\ts[ti + maxlen] = 0;\n\t\treturn s.c_str() + ti;\n\t}\n};",
"java": "class Solution {\n\tpublic String longestPalindrome(String s) {\n\t\tint ti = 0, maxlen = 0, i, t;\n\t\tfor (i = 0; i < s.length(); i++) {\n\t\t\tt = 1;\n\t\t\twhile (t <= i && i + t < s.length()) {\n\t\t\t\tif (s.charAt(i + t) == s.charAt(i - t))\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t + 1 > maxlen) {\n\t\t\t\tti = i - t;\n\t\t\t\tmaxlen = 2 * t + 1;\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < s.length(); i++) {\n\t\t\tt = 1;\n\t\t\twhile (t <= i + 1 && i + t < s.length()) {\n\t\t\t\tif (s.charAt(i - t + 1) == s.charAt(i + t))\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t > maxlen) {\n\t\t\t\tti = i - t + 1;\n\t\t\t\tmaxlen = 2 * t;\n\t\t\t}\n\t\t}\n\t\treturn s.substring(ti, ti + maxlen);\n\t}\n}",
"js": "/**\n * @param {string} s\n * @return {string}\n */\nvar longestPalindrome = function(s) {\n\tlet ti = 0;\n\tlet maxlen = 0;\n\tlet i, t;\n\n // odd length probe\n for (i = 0; s[i]; i++) {\n t = 1;\n while (t <= i && s[i + t]) {\n if (s[i + t] == s[i - t])\n t++;\n else\n break;\n }\n t--;\n if (2 * t + 1 > maxlen) {\n ti = i - t;\n maxlen = 2 * t + 1;\n }\n }\n\n // even length probe\n for (i = 0; s[i]; i++) {\n t = 1;\n while (t <= i + 1 && s[i + t]) {\n if (s[i - t + 1] == s[i + t])\n t++;\n else\n break;\n }\n t--;\n\n if (2 * t > maxlen) {\n ti = i - t + 1;\n maxlen = 2 * t;\n }\n }\n\n return s.substring(ti, ti + maxlen);\n}\n\nfunction main(){\n\tconst cases = [\n\t\t[\"cbbd\",\"bb\"],\n\t\t// ['a','a'],\n\t\t// ['bab','bab'],\n\t\t// ['baab','baab'],\n\t\t// ['babad','bab'],\n\t\t// ['bb','bb'],\n\t\t// ['cbbd','bb'],\n\t\t// ['cxxabbmxxd','xx'],\n\t\t// ['aaabaaaa','aaabaaa'],\n\t\t// ['zudfweormatjycujjirzjpyrmaxurectxrtqedmmgergwdvjmjtstdhcihacqnothgttgqfywcpgnuvwglvfiuxteopoyizgehkwuvvkqxbnufkcbodlhdmbqyghkojrgokpwdhtdrwmvdegwycecrgjvuexlguayzcammupgeskrvpthrmwqaqsdcgycdupykppiyhwzwcplivjnnvwhqkkxildtyjltklcokcrgqnnwzzeuqioyahqpuskkpbxhvzvqyhlegmoviogzwuiqahiouhnecjwysmtarjjdjqdrkljawzasriouuiqkcwwqsxifbndjmyprdozhwaoibpqrthpcjphgsfbeqrqqoqiqqdicvybzxhklehzzapbvcyleljawowluqgxxwlrymzojshlwkmzwpixgfjljkmwdtjeabgyrpbqyyykmoaqdambpkyyvukalbrzoyoufjqeftniddsfqnilxlplselqatdgjziphvrbokofvuerpsvqmzakbyzxtxvyanvjpfyvyiivqusfrsufjanmfibgrkwtiuoykiavpbqeyfsuteuxxjiyxvlvgmehycdvxdorpepmsinvmyzeqeiikajopqedyopirmhymozernxzaueljjrhcsofwyddkpnvcvzixdjknikyhzmstvbducjcoyoeoaqruuewclzqqqxzpgykrkygxnmlsrjudoaejxkipkgmcoqtxhelvsizgdwdyjwuumazxfstoaxeqqxoqezakdqjwpkrbldpcbbxexquqrznavcrprnydufsidakvrpuzgfisdxreldbqfizngtrilnbqboxwmwienlkmmiuifrvytukcqcpeqdwwucymgvyrektsnfijdcdoawbcwkkjkqwzffnuqituihjaklvthulmcjrhqcyzvekzqlxgddjoir','gykrkyg']\n\t];\n\n\tfor(const c of cases){\n\t\tconsole.log('input:',c[0],', expected result:',c[1]);\n\t\tconst v = longestPalindrome(c[0], 0);\n\t\tconsole.log('output:',v);\n\t\tconsole.assert(v===c[1]);\n\t}\t\n}\n\nmain();\n\n",
"python": "class Solution:\n\tdef longestPalindrome(self, s: str) -> str:\n\t\tti = 0\n\t\tmaxlen = 0\n\t\ti = 0\n\t\twhile i < len(s):\n\t\t\tt = 1\n\t\t\twhile t <= i and i + t < len(s):\n\t\t\t\tif s[i + t] == s[i - t]:\n\t\t\t\t\tt += 1\n\t\t\t\telse:\n\t\t\t\t\tbreak\n\t\t\tt -= 1\n\t\t\tif 2 * t + 1 > maxlen:\n\t\t\t\tti = i - t\n\t\t\t\tmaxlen = 2 * t + 1\n\t\t\ti += 1\n\t\ti = 0\n\t\twhile i < len(s):\n\t\t\tt = 1\n\t\t\twhile t <= i + 1 and i + t < len(s):\n\t\t\t\tif s[i - t + 1] == s[i + t]:\n\t\t\t\t\tt += 1\n\t\t\t\telse:\n\t\t\t\t\tbreak\n\t\t\tt -= 1\n\t\t\tif 2 * t > maxlen:\n\t\t\t\tti = i - t + 1\n\t\t\t\tmaxlen = 2 * t\n\t\t\ti += 1\n\t\treturn s[ti:ti+maxlen]\n# %%\ns = Solution()\nprint(s.longestPalindrome('babad'))\nprint(s.longestPalindrome('cbbd'))",
"status": 1,
"keywords": "字符串,动态规划",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 41,
"question_title": "缺失的第一个正数",
"difficulty": "困难",
"question_content": "<p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= nums.length <= 300</code></li>\t<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470223",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint firstMissingPositive(vector<int> &nums)\n\t{\n\t\tif (nums.size() == 0)\n\t\t{\n\t\t\treturn 1;\n\t\t}\n\t\tint i = 0;\n\t\twhile (i < nums.size())\n\t\t{\n\t\t\tif (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i])\n\t\t\t{\n\t\t\t\tswap(nums[i], nums[nums[i] - 1]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < nums.size(); i++)\n\t\t{\n\t\t\tif (nums[i] != i + 1)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn i + 1;\n\t}\n};",
"java": "class Solution {\n\tpublic int firstMissingPositive(int[] nums) {\n\t\tint n = nums.length;\n\t\tint contains = 0;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (nums[i] == 1) {\n\t\t\t\tcontains++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (contains == 0) {\n\t\t\treturn 1;\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif ((nums[i] <= 0) || (nums[i] > n)) {\n\t\t\t\tnums[i] = 1;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint a = Math.abs(nums[i]);\n\t\t\tnums[a - 1] = -Math.abs(nums[a - 1]);\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (nums[i] > 0) {\n\t\t\t\treturn i + 1;\n\t\t\t}\n\t\t}\n\t\treturn n + 1;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef firstMissingPositive(self, nums):\n\t\tls = len(nums)\n\t\tindex = 0\n\t\twhile index < ls:\n\t\t\tif nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]:\n\t\t\t\tindex += 1\n\t\t\telse:\n\t\t\t\tpos = nums[index] - 1\n\t\t\t\tnums[index], nums[pos] = nums[pos], nums[index]\n\t\tres = 0\n\t\twhile res < ls and nums[res] == res + 1:\n\t\t\tres += 1\n\t\treturn res + 1\n# %%\ns = Solution()\nprint(s.firstMissingPositive(nums = [1,2,0]))",
"status": 1,
"keywords": "数组,哈希表",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/shinef/article/details/106982112"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/40/40_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/40/40_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/40/40_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 42,
"question_title": "接雨水",
"difficulty": "困难",
"question_content": "<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png\" style=\"height: 161px; width: 412px;\" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>n == height.length</code></li>\t<li><code>0 <= n <= 3 * 10<sup>4</sup></code></li>\t<li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470120",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint trap(vector<int> &height)\n\t{\n\t\tint res = 0;\n\t\tint left = 0, left_max = 0;\n\t\tint right = height.size() - 1, right_max = 0;\n\t\twhile (left < right)\n\t\t{\n\t\t\tif (height[left] < height[right])\n\t\t\t{\n\t\t\t\tif (height[left] > left_max)\n\t\t\t\t{\n\t\t\t\t\tleft_max = height[left];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres += left_max - height[left];\n\t\t\t\t}\n\t\t\t\tleft++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (height[right] > right_max)\n\t\t\t\t{\n\t\t\t\t\tright_max = height[right];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres += right_max - height[right];\n\t\t\t\t}\n\t\t\t\tright--;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};",
"java": "class Solution {\n\tpublic int trap(int[] height) {\n\t\tif (height == null)\n\t\t\treturn 0;\n\t\tint len = height.length;\n\t\tif (len == 0)\n\t\t\treturn 0;\n\t\tint res = 0;\n\t\tint[] left_max = new int[len];\n\t\tint[] right_max = new int[len];\n\t\tleft_max[0] = height[0];\n\n\t\tfor (int i = 1; i < len; i++) {\n\t\t\tleft_max[i] = Math.max(height[i], left_max[i - 1]);\n\t\t}\n\t\tright_max[len - 1] = height[len - 1];\n\n\t\tfor (int i = len - 2; i >= 0; i--) {\n\t\t\tright_max[i] = Math.max(height[i], right_max[i + 1]);\n\t\t}\n\n\t\tfor (int i = 1; i < len - 1; i++) {\n\t\t\tres += Math.min(left_max[i], right_max[i]) - height[i];\n\t\t}\n\t\treturn res;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef trap(self, height):\n\t\tls = len(height)\n\t\tif ls == 0:\n\t\t\treturn 0\n\t\tres, left = 0, 0\n\t\twhile left < ls and height[left] == 0:\n\t\t\tleft += 1\n\t\tpos = left + 1\n\t\twhile pos < ls:\n\t\t\tif height[pos] >= height[left]:\n\t\t\t\tres += self.rain_water(height, left, pos)\n\t\t\t\tleft = pos\n\t\t\t\tpos += 1\n\t\t\telif pos == ls - 1:\n\t\t\t\tmax_value, max_index = 0, pos\n\t\t\t\tfor index in range(left + 1, ls):\n\t\t\t\t\tif height[index] > max_value:\n\t\t\t\t\t\tmax_value = height[index]\n\t\t\t\t\t\tmax_index = index\n\t\t\t\tres += self.rain_water(height, left, max_index)\n\t\t\t\tleft = max_index\n\t\t\t\tpos = left + 1\n\t\t\telse:\n\t\t\t\tpos += 1\n\t\treturn res\n\tdef rain_water(self, height, start, end):\n\t\tif end - start <= 1:\n\t\t\treturn 0\n\t\tmin_m = min(height[start], height[end])\n\t\tres = min_m * (end - start - 1)\n\t\tstep = 0\n\t\tfor index in range(start + 1, end):\n\t\t\tif height[index] > 0:\n\t\t\t\tstep += height[index]\n\t\treturn res - step\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.trap([2,6,3,8,2,7,2,5,0]))",
"status": 1,
"keywords": "栈,数组,双指针,动态规划,单调栈",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/41/41_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/41/41_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/41/41_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 43,
"question_title": "字符串相乘",
"difficulty": "中等",
"question_content": "<p>给定两个以字符串形式表示的非负整数&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>,返回&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>&nbsp;的乘积,它们的乘积也表示为字符串形式。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> num1 = &quot;2&quot;, num2 = &quot;3&quot;<strong><br />输出:</strong> &quot;6&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> num1 = &quot;123&quot;, num2 = &quot;456&quot;<strong><br />输出:</strong> &quot;56088&quot;</pre><p><strong>说明:</strong></p><ol>\t<li><code>num1</code>&nbsp;和&nbsp;<code>num2</code>&nbsp;的长度小于110。</li>\t<li><code>num1</code> 和&nbsp;<code>num2</code> 只包含数字&nbsp;<code>0-9</code>。</li>\t<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;均不以零开头,除非是数字 0 本身。</li>\t<li><strong>不能使用任何标准库的大数类型(比如 BigInteger)</strong>或<strong>直接将输入转换为整数来处理</strong>。</li></ol>",
"topic_link": "https://bbs.csdn.net/topics/600471020",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tstring multiply(string num1, string num2)\n\t{\n\t\tstring res(num1.length() + num2.length(), '0');\n\t\tfor (int i = num2.length() - 1; i >= 0; i--)\n\t\t{\n\t\t\tint j, carry = 0;\n\t\t\tfor (j = num1.length() - 1; j >= 0; j--)\n\t\t\t{\n\t\t\t\tcarry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0');\n\t\t\t\tres[i + j + 1] = carry % 10 + '0';\n\t\t\t\tcarry /= 10;\n\t\t\t}\n\t\t\tres[i + j + 1] = carry + '0';\n\t\t}\n\t\tint i;\n\t\tfor (i = 0; i < res.length() - 1; i++)\n\t\t{\n\t\t\tif (res[i] != '0')\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn res.substr(i);\n\t}\n};",
"java": "class Solution {\n\tpublic String multiply(String num1, String num2) {\n\t\tif (num1.equals(\"0\") || num2.equals(\"0\"))\n\t\t\treturn \"0\";\n\n\t\tint m = num1.length();\n\t\tint n = num2.length();\n\n\t\tint[] intRes = new int[m + n - 1];\n\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tintRes[i + j] += (num1.charAt(i) - 48) * (num2.charAt(j) - 48);\n\t\t\t}\n\t\t}\n\n\t\tfor (int i = intRes.length - 1; i > 0; i--) {\n\t\t\tif (intRes[i] >= 10) {\n\t\t\t\tintRes[i - 1] += intRes[i] / 10;\n\t\t\t\tintRes[i] %= 10;\n\t\t\t}\n\t\t}\n\n\t\tString res = \"\";\n\t\tfor (int i = 0; i < intRes.length; i++) {\n\t\t\tres += String.valueOf(intRes[i]);\n\t\t}\n\t\treturn res;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef multiply(self, num1, num2):\n\t\tif num1 == '0' or num2 == '0':\n\t\t\treturn '0'\n\t\tres = ''\n\t\tls1, ls2, = len(num1), len(num2)\n\t\tls = ls1 + ls2\n\t\tarr = [0] * ls\n\t\tfor i in reversed(range(ls1)):\n\t\t\tfor j in reversed(range(ls2)):\n\t\t\t\tarr[i + j + 1] += int(num1[i]) * int(num2[j])\n\t\tfor i in reversed(range(1, ls)):\n\t\t\tarr[i - 1] += arr[i] / 10\n\t\t\tarr[i] %= 10\n\t\tpos = 0\n\t\tif arr[pos] == 0:\n\t\t\tpos += 1\n\t\twhile pos < ls:\n\t\t\tres = res + str(arr[pos])\n\t\t\tpos += 1\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.multiply(\"98\", \"9\"))",
"status": 1,
"keywords": "数学,字符串,模拟",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/42/42_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/42/42_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/42/42_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 44,
"question_title": "通配符匹配",
"difficulty": "困难",
"question_content": "<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;和&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul>\t<li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li>\t<li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;和&nbsp;<code>*</code>。</li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;。</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>",
"topic_link": "https://bbs.csdn.net/topics/600471019",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool isMatch(char *s, char *p)\n{\n\tchar *last_s = NULL;\n\tchar *last_p = NULL;\n\twhile (*s != '\\0')\n\t{\n\t\tif (*p == '*')\n\t\t{\n\t\t\tif (*++p == '\\0')\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tlast_s = s;\n\t\t\tlast_p = p;\n\t\t}\n\t\telse if (*p == '?' || *s == *p)\n\t\t{\n\t\t\ts++;\n\t\t\tp++;\n\t\t}\n\t\telse if (last_s != NULL)\n\t\t{\n\t\t\tp = last_p;\n\t\t\ts = ++last_s;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\twhile (*p == '*')\n\t{\n\t\tp++;\n\t}\n\treturn *p == '\\0';\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test string pattern\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", isMatch(argv[1], argv[2]) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean isMatch(String s, String p) {\n\t\tboolean[][] value = new boolean[p.length() + 1][s.length() + 1];\n\t\tvalue[0][0] = true;\n\t\tfor (int i = 1; i <= s.length(); i++) {\n\t\t\tvalue[0][i] = false;\n\t\t}\n\t\tfor (int i = 1; i <= p.length(); i++) {\n\t\t\tif (p.charAt(i - 1) == '*') {\n\t\t\t\tvalue[i][0] = value[i - 1][0];\n\t\t\t\tfor (int j = 1; j <= s.length(); j++) {\n\t\t\t\t\tvalue[i][j] = (value[i][j - 1] || value[i - 1][j]);\n\t\t\t\t}\n\t\t\t} else if (p.charAt(i - 1) == '?') {\n\t\t\t\tvalue[i][0] = false;\n\t\t\t\tfor (int j = 1; j <= s.length(); j++) {\n\t\t\t\t\tvalue[i][j] = value[i - 1][j - 1];\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tvalue[i][0] = false;\n\t\t\t\tfor (int j = 1; j <= s.length(); j++) {\n\t\t\t\t\tvalue[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i - 1][j - 1];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn value[p.length()][s.length()];\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef isMatch(self, s, p):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:type p: str\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\ts_index, p_index = 0, 0\n\t\tstar, s_star = -1, 0\n\t\ts_len, p_len = len(s), len(p)\n\t\twhile s_index < s_len:\n\t\t\tif p_index < p_len and (s[s_index] == p[p_index] or p[p_index] == '?'):\n\t\t\t\ts_index += 1\n\t\t\t\tp_index += 1\n\t\t\telif p_index < p_len and p[p_index] == '*':\n\t\t\t\tstar = p_index\n\t\t\t\ts_star = s_index\n\t\t\t\tp_index += 1\n\t\t\telif star != -1:\n\t\t\t\tp_index = star + 1\n\t\t\t\ts_star += 1\n\t\t\t\ts_index = s_star\n\t\t\telse:\n\t\t\t\treturn False\n\t\twhile p_index < p_len and p[p_index] == '*':\n\t\t\tp_index += 1\n\t\treturn p_index == p_len\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.isMatch(s = \"aa\",p = \"a\"))",
"status": 1,
"keywords": "贪心,递归,字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104317836"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/43/43_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/43/43_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/43/43_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 45,
"question_title": "跳跃游戏 II",
"difficulty": "中等",
"question_content": "<p>给定一个非负整数数组,你最初位于数组的第一个位置。</p>\n<p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p>\n<p>你的目标是使用最少的跳跃次数到达数组的最后一个位置。</p>\n<p><strong>示例:</strong></p>\n<pre><strong>输入:</strong> [2,3,1,1,4]<strong><br />输出:</strong> 2<strong><br />解释:</strong> 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳&nbsp;1&nbsp;步,然后跳&nbsp;3&nbsp;步到达数组的最后一个位置。</pre>\n<p><strong>说明:</strong></p>\n<p>假设你总是可以到达数组的最后一个位置。</p>",
"topic_link": "https://bbs.csdn.net/topics/600469921",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint jump(vector<int> &nums)\n\t{\n\t\tint steps = 0;\n\t\tint lo = 0, hi = 0;\n\t\twhile (hi < nums.size() - 1)\n\t\t{\n\t\t\tint right = 0;\n\t\t\tfor (int i = lo; i <= hi; i++)\n\t\t\t{\n\t\t\t\tright = max(i + nums[i], right);\n\t\t\t}\n\t\t\tlo = hi + 1;\n\t\t\thi = right;\n\t\t\tsteps++;\n\t\t}\n\t\treturn steps;\n\t}\n};",
"java": "class Solution {\n\tpublic int jump(int[] nums) {\n\t\tint end = 0;\n\t\tint steps = 0;\n\t\tint maxPosition = 0;\n\t\tfor (int i = 0; i < nums.length - 1; i++) {\n\n\t\t\tmaxPosition = Math.max(maxPosition, i + nums[i]);\n\n\t\t\tif (i == end) {\n\t\t\t\tend = maxPosition;\n\t\t\t\tsteps++;\n\t\t\t}\n\t\t}\n\t\treturn steps;\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef jump(self, nums):\n\t\tif len(nums) <= 1:\n\t\t\treturn 0\n\t\tend = 0 + nums[0]\n\t\tstart = 0\n\t\tstep = 1\n\t\tmaxDis = 0 + nums[0]\n\t\twhile end < len(nums) - 1:\n\t\t\tfor i in range(start + 1, end + 1):\n\t\t\t\tmaxDis = max(maxDis, nums[i] + i)\n\t\t\tstart = end\n\t\t\tend = maxDis\n\t\t\tstep += 1\n\t\treturn step\n# %%\ns = Solution()\nprint(s.jump(nums = [2,3,0,1,4]))",
"status": 1,
"keywords": "贪心,数组,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/44/44_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/44/44_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/44/44_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 46,
"question_title": "全排列",
"difficulty": "中等",
"question_content": "<p>给定一个<strong> 没有重复</strong> 数字的序列,返回其所有可能的全排列。</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [1,2,3]<strong><br />输出:</strong>[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]</pre>",
"topic_link": "https://bbs.csdn.net/topics/600469820",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> permute(vector<int> &nums)\n\t{\n\t\tvector<vector<int>> res;\n\t\tvector<bool> used(nums.size());\n\t\tdfs(nums, used, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)\n\t{\n\t\tif (stack.size() == nums.size())\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < nums.size(); i++)\n\t\t\t{\n\t\t\t\tif (!used[i])\n\t\t\t\t{\n\t\t\t\t\tused[i] = true;\n\t\t\t\t\tstack.push_back(nums[i]);\n\t\t\t\t\tdfs(nums, used, res);\n\t\t\t\t\tstack.pop_back();\n\t\t\t\t\tused[i] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> permute(int[] nums) {\n\n\t\tList<List<Integer>> result = new ArrayList<List<Integer>>();\n\t\tArrays.sort(nums);\n\t\tList<Integer> first = new ArrayList<Integer>();\n\t\tfor (int r = 0; r < nums.length; r++) {\n\t\t\tfirst.add(nums[r]);\n\t\t}\n\t\tresult.add(first);\n\t\tint i = nums.length - 2;\n\t\twhile (i >= 0) {\n\t\t\tif (nums[i] < nums[i + 1]) {\n\t\t\t\tint temp = nums[i];\n\t\t\t\tfor (int j = nums.length - 1; j > i; j--) {\n\t\t\t\t\tif (nums[j] > temp) {\n\t\t\t\t\t\tnums[i] = nums[j];\n\t\t\t\t\t\tnums[j] = temp;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnums = quick_sort(nums, i + 1, nums.length - 1);\n\t\t\t\tList<Integer> sub = new ArrayList<Integer>();\n\t\t\t\tfor (int t = 0; t < nums.length; t++) {\n\t\t\t\t\tsub.add(nums[t]);\n\t\t\t\t}\n\t\t\t\tresult.add(sub);\n\t\t\t\ti = nums.length - 2;\n\n\t\t\t} else {\n\t\t\t\ti--;\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\n\t}\n\n\tpublic int[] quick_sort(int[] a, int left, int right) {\n\t\tif (left < right) {\n\t\t\tint l = left;\n\t\t\tint r = right;\n\t\t\tint temp = a[l];\n\t\t\twhile (l != r) {\n\t\t\t\twhile (l < r && a[r] > temp) {\n\t\t\t\t\tr--;\n\t\t\t\t}\n\t\t\t\tif (l < r) {\n\t\t\t\t\ta[l] = a[r];\n\t\t\t\t\tl++;\n\t\t\t\t}\n\n\t\t\t\twhile (l < r && a[l] < temp) {\n\t\t\t\t\tl++;\n\t\t\t\t}\n\t\t\t\tif (l < r) {\n\t\t\t\t\ta[r] = a[l];\n\t\t\t\t\tr--;\n\t\t\t\t}\n\n\t\t\t}\n\t\t\ta[l] = temp;\n\t\t\tquick_sort(a, left, l - 1);\n\t\t\tquick_sort(a, l + 1, right);\n\t\t}\n\t\treturn a;\n\t}\n}",
"js": "",
"python": "class Solution:\n\t def permute(self, nums):\n\t\t\te=[]\n\t\t\tif(len(nums)==1):\n\t\t\t\treturn [nums]\n\t\t\tfor i in range(len(nums)):\n\t\t\t\tq=self.permute(nums[:i]+nums[i+1:])\n\t\t\t\tfor c in q:\n\t\t\t\t\te.append([nums[i]]+c)\n\t\t\treturn e\n# %%\ns = Solution()\nprint(s.permute(nums = [1,2,3]))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/45/45_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/45/45_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/45/45_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 47,
"question_title": "全排列 II",
"difficulty": "中等",
"question_content": "<p>给定一个可包含重复数字的序列 <code>nums</code> ,<strong>按任意顺序</strong> 返回所有不重复的全排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,1,2]<strong><br />输出:</strong>[[1,1,2], [1,2,1], [2,1,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 8</code></li>\t<li><code>-10 <= nums[i] <= 10</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470224",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> permuteUnique(vector<int> &nums)\n\t{\n\t\tvector<vector<int>> res;\n\t\tvector<bool> used(nums.size());\n\t\tsort(nums.begin(), nums.end());\n\t\tdfs(nums, used, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)\n\t{\n\t\tif (stack.size() == nums.size())\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < nums.size(); i++)\n\t\t\t{\n\t\t\t\tif (!used[i])\n\t\t\t\t{\n\t\t\t\t\tif (i > 0 && !used[i - 1] && nums[i - 1] == nums[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tstack.push_back(nums[i]);\n\t\t\t\t\tused[i] = true;\n\t\t\t\t\tdfs(nums, used, res);\n\t\t\t\t\tstack.pop_back();\n\t\t\t\t\tused[i] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tList<List<Integer>> ans = new ArrayList<>();\n\n\tpublic List<List<Integer>> permuteUnique(int[] nums) {\n\t\tdfs(nums, 0);\n\t\treturn ans;\n\t}\n\n\tprivate void dfs(int[] nums, int cur) {\n\t\tif (cur == nums.length) {\n\t\t\tList<Integer> line = new ArrayList<>();\n\t\t\tfor (int i : nums) {\n\t\t\t\tline.add(i);\n\t\t\t}\n\t\t\tans.add(line);\n\t\t} else {\n\t\t\tfor (int i = cur; i < nums.length; i++) {\n\t\t\t\tif (canSwap(nums, cur, i)) {\n\t\t\t\t\tswap(nums, cur, i);\n\t\t\t\t\tdfs(nums, cur + 1);\n\t\t\t\t\tswap(nums, cur, i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate boolean canSwap(int nums[], int begin, int end) {\n\t\tfor (int i = begin; i < end; i++) {\n\t\t\tif (nums[i] == nums[end]) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tprivate void swap(int nums[], int i, int j) {\n\t\tint temp = nums[i];\n\t\tnums[i] = nums[j];\n\t\tnums[j] = temp;\n\t}\n}",
"js": "",
"python": "from typing import List\nclass Solution:\n\tdef permuteUnique(self, nums: List[int]) -> List[List[int]]:\n\t\tans = []\n\t\tif len(nums) == 0:\n\t\t\treturn\n\t\tif len(nums) == 1:\n\t\t\treturn [nums]\n\t\tfor index,item in enumerate(nums):\n\t\t\tres = nums[:index]+nums[index+1:]\n\t\t\tfor j in self.permuteUnique(res): \n\t\t\t\tans.append(j+[item])\n\t\trel = []\n\t\tfor i in ans:\n\t\t\tif i not in rel:\n\t\t\t\trel.append(i)\n\t\treturn rel\n# %%\ns = Solution()\nprint(s.permuteUnique(nums = [1,2,3]))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/46/46_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/46/46_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/46/46_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 48,
"question_title": "旋转图像",
"difficulty": "中等",
"question_content": "<p>给定一个 <em>n </em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</p><p>你必须在<strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"> 原地</a></strong> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg\" style=\"width: 642px; height: 242px;\" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg\" style=\"width: 800px; height: 321px;\" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>matrix.length == n</code></li>\t<li><code>matrix[i].length == n</code></li>\t<li><code>1 <= n <= 20</code></li>\t<li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470225",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid rotate(vector<vector<int>> &matrix)\n\t{\n\t\tint size = matrix.size();\n\t\tfor (int i = 0; i < size / 2; i++)\n\t\t{\n\t\t\tint low = i, high = size - i - 1;\n\t\t\tfor (int j = low; j < high; j++)\n\t\t\t{\n\t\t\t\tint tmp = matrix[i][j];\n\t\t\t\tmatrix[i][j] = matrix[size - 1 - j][i];\n\t\t\t\tmatrix[size - 1 - j][i] = matrix[size - 1 - i][size - 1 - j];\n\t\t\t\tmatrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i];\n\t\t\t\tmatrix[j][size - 1 - i] = tmp;\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "class Solution {\n\n\tpublic void rotate(int[][] matrix) {\n\t\tint len = matrix.length;\n\t\tint le = len - 1;\n\t\tfor (int i = 0; i < len / 2; i++) {\n\t\t\tfor (int j = 0; j < (len + 1) / 2; j++) {\n\t\t\t\tint temp = matrix[i][j];\n\t\t\t\tmatrix[i][j] = matrix[le - j][i];\n\t\t\t\tmatrix[le - j][i] = matrix[le - i][le - j];\n\t\t\t\tmatrix[le - i][le - j] = matrix[j][le - i];\n\t\t\t\tmatrix[j][le - i] = temp;\n\t\t\t}\n\t\t}\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef rotate(self, matrix):\n\t\tif matrix is None or len(matrix) == 1:\n\t\t\treturn\n\t\tls = len(matrix)\n\t\tfor i in range(int(ls / 2)):\n\t\t\tbegin, end = i, ls - 1 - i\n\t\t\tfor k in range(ls - 2 * i - 1):\n\t\t\t\ttemp = matrix[end - k][begin]\n\t\t\t\tmatrix[end - k][begin] = matrix[end][end - k]\n\t\t\t\tmatrix[end][end - k] = matrix[begin + k][end]\n\t\t\t\tmatrix[begin + k][end] = matrix[begin][begin + k]\n\t\t\t\tmatrix[begin][begin + k] = temp\n\t\treturn matrix\nif __name__ == '__main__':\n\ts = Solution()\n\tprint(s.rotate([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]))\n\tprint(s.rotate( [[1,2],[3,4]]))",
"status": 1,
"keywords": "数组,数学,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/47/47_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/47/47_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/47/47_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 49,
"question_title": "字母异位词分组",
"difficulty": "中等",
"question_content": "<p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p>\n<p><strong>示例:</strong></p>\n<pre><strong>输入:</strong>[eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]<strong><br />输出:</strong>[[ate&quot;,&quot;eat&quot;,&quot;tea&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;bat&quot;]]</pre>\n<p><strong>说明:</strong></p>\n<ul>\n <li>所有输入均为小写字母。</li>\n <li>不考虑答案输出的顺序。</li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470799",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<string>> groupAnagrams(vector<string> &strs)\n\t{\n\t\tvector<vector<string>> res;\n\t\tunordered_map<string, vector<string>> ht;\n\t\tfor (const auto &str : strs)\n\t\t{\n\t\t\tint counts[26] = {0};\n\t\t\tfor (char c : str)\n\t\t\t{\n\t\t\t\tcounts[c - 'a']++;\n\t\t\t}\n\t\t\tstring key;\n\t\t\tfor (int i : counts)\n\t\t\t{\n\t\t\t\tkey.push_back('#');\n\t\t\t\tkey.push_back(i + '0');\n\t\t\t}\n\t\t\tht[key].push_back(str);\n\t\t}\n\t\tfor (const auto &t : ht)\n\t\t{\n\t\t\tres.push_back(t.second);\n\t\t}\n\t\treturn res;\n\t}\n};",
"java": "\nimport java.util.*;\n\npublic class GroupAnagrams {\n\n\tpublic List<List<String>> groupAnagrams(String[] strs) {\n\t\tHashMap<String, ArrayList<String>> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tArrays.sort(cs);\n\n\t\t\tString key = String.valueOf(cs);\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n\n\tpublic List<List<String>> groupAnagrams2(String[] strs) {\n\t\tif (strs.length <= 0) {\n\t\t\treturn new ArrayList<>();\n\t\t}\n\t\tHashMap<String, ArrayList<String>> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tint[] count = new int[26];\n\t\t\tfor (char c : cs) {\n\t\t\t\t++count[c - 'a'];\n\t\t\t}\n\t\t\tStringBuilder s = new StringBuilder(\"\");\n\t\t\tfor (int num : count) {\n\t\t\t\ts.append(num);\n\t\t\t}\n\n\t\t\tString key = String.valueOf(s);\n\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef groupAnagrams(self, strs):\n\t\tstrs.sort()\n\t\thash = {}\n\t\tfor s in strs:\n\t\t\tkey = self.hash_key(s)\n\t\t\ttry:\n\t\t\t\thash[key].append(s)\n\t\t\texcept KeyError:\n\t\t\t\thash[key] = [s]\n\t\treturn hash.values()\n\tdef hash_key(self, s):\n\t\ttable = [0] * 26\n\t\tfor ch in s:\n\t\t\tindex = ord(ch) - ord('a')\n\t\t\ttable[index] += 1\n\t\treturn str(table)\n# %%\ns = Solution()\nprint(s.groupAnagrams(strs = [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]))",
"status": 1,
"keywords": "哈希表,字符串,排序",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 50,
"question_title": "Pow(x, n)",
"difficulty": "中等",
"question_content": "<p>实现 <a href=\"https://www.cplusplus.com/reference/valarray/pow/\" target=\"_blank\">pow(<em>x</em>, <em>n</em>)</a> ,即计算 x 的 n 次幂函数(即,x<sup><span style=\"font-size:10.8333px\">n</span></sup>)。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = 10<strong><br />输出:</strong>1024.00000</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = 2.10000, n = 3<strong><br />输出:</strong>9.26100</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = -2<strong><br />输出:</strong>0.25000<strong><br />解释:</strong>2<sup>-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>-100.0 < x < 100.0</code></li>\t<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code></li>\t<li><code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471013",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tdouble myPow(double x, int n)\n\t{\n\t\tif (n == INT_MIN)\n\t\t{\n\t\t\tdouble t = dfs(x, -(n / 2));\n\t\t\treturn 1 / t * 1 / t;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn n < 0 ? 1 / dfs(x, -n) : dfs(x, n);\n\t\t}\n\t}\nprivate:\n\tdouble dfs(double x, int n)\n\t{\n\t\tif (n == 0)\n\t\t{\n\t\t\treturn 1;\n\t\t}\n\t\telse if (n == 1)\n\t\t{\n\t\t\treturn x;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdouble t = dfs(x, n / 2);\n\t\t\treturn (n % 2) ? (x * t * t) : (t * t);\n\t\t}\n\t}\n};",
"java": "public class Solution {\n\tpublic double myPow(double x, int n) {\n\t\tif (n < 0) {\n\n\t\t\treturn 1 / pow(x, -n);\n\t\t} else {\n\n\t\t\treturn pow(x, n);\n\t\t}\n\t}\n\n\tprivate double pow(double x, int n) {\n\n\t\tif (n == 0) {\n\t\t\treturn 1.0;\n\t\t}\n\t\tif (n == 1) {\n\t\t\treturn x;\n\t\t}\n\t\tdouble val = pow(x, n / 2);\n\n\t\tif (n % 2 == 0) {\n\t\t\treturn val * val;\n\t\t} else {\n\t\t\treturn val * val * x;\n\t\t}\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef myPow(self, x, n):\n\t\tif n == 0:\n\t\t\treturn 1\n\t\tres ,curr = 1, abs(n)\n\t\twhile curr > 0:\n\t\t\tif curr & 1 == 1:\n\t\t\t\tres *= x\n\t\t\tcurr >>= 1\n\t\t\tx *= x\n\t\tif n < 0:\n\t\t\treturn 1 / res\n\t\treturn res\n# %%\ns = Solution()\nprint(s.myPow(x = 2.00000, n = 10))",
"status": 1,
"keywords": "递归,数学",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/49/49_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/49/49_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/49/49_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 6,
"question_title": "Z 字形变换",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ,以从上往下、从左到右进行&nbsp;Z 字形排列。</p>\n\n <p>比如输入字符串为 <code>\"PAYPALISHIRING\"</code>&nbsp;行数为 <code>3</code> 时,排列如下:</p>\n\n <pre>\n P A H N\n A P L S I I G\n Y I R</pre>\n\n <p>之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:<code>\"PAHNAPLSIIGYIR\"</code>。</p>\n\n <p>请你实现这个将字符串进行指定行数变换的函数:</p>\n\n <pre>string convert(string s, int numRows);</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n\n <pre><strong>输入:</strong>s = \"PAYPALISHIRING\", numRows = 3\n<strong>输出:</strong>\"PAHNAPLSIIGYIR\"\n</pre>\n <strong>示例 2:</strong>\n\n <pre><strong>输入:</strong>s = \"PAYPALISHIRING\", numRows = 4\n<strong>输出:</strong>\"PINALSIGYAHRPI\"\n<strong>解释:</strong>\nP I N\nA L S I G\nY A H R\nP I\n</pre>\n\n <p><strong>示例 3:</strong></p>\n\n <pre><strong>输入:</strong>s = \"A\", numRows = 1\n<strong>输出:</strong>\"A\"\n</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= s.length &lt;= 1000</code></li>\n <li><code>s</code> 由英文字母(小写和大写)、<code>','</code> 和 <code>'.'</code> 组成</li>\n <li><code>1 &lt;= numRows &lt;= 1000</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600469917",
"cpp": "class Solution\n{\npublic:\n\tstring convert(string s, int numRows)\n\t{\n\t\tif (numRows == 1)\n\t\t\treturn s;\n\t\tint len = s.size();\n\t\tif (len <= numRows)\n\t\t\treturn s;\n\t\tint cycle_len = 2 * numRows - 2;\n\t\tint full_cycles = len / cycle_len;\n\t\tint left = len % cycle_len;\n\t\tstring r;\n\t\tint i;\n\t\tfor (i = 0; i < full_cycles; ++i)\n\t\t{\n\t\t\tr += s[i * cycle_len];\n\t\t}\n\t\tif (left)\n\t\t\tr += s[i * cycle_len];\n\t\tfor (i = 0; i < numRows - 2; ++i)\n\t\t{\n\t\t\tint j;\n\t\t\tfor (j = 0; j < full_cycles; ++j)\n\t\t\t{\n\t\t\t\tr += s[j * cycle_len + i + 1];\n\t\t\t\tr += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];\n\t\t\t}\n\t\t\tif (left)\n\t\t\t{\n\t\t\t\tif (j * cycle_len + i + 1 < len)\n\t\t\t\t\tr += s[j * cycle_len + i + 1];\n\t\t\t\tif (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)\n\t\t\t\t\tr += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < full_cycles; ++i)\n\t\t\tr += s[i * cycle_len + numRows - 1];\n\t\tif (left >= numRows)\n\t\t\tr += s[i * cycle_len + numRows - 1];\n\t\treturn r;\n\t}\n};",
"java": "class Solution {\n\tpublic String convert(String s, int numRows) {\n\t\tif (numRows == 1)\n\t\t\treturn s;\n\t\tint len = s.length();\n\t\tif (len <= numRows)\n\t\t\treturn s;\n\t\tint cycle_len = 2 * numRows - 2;\n\t\tint full_cycles = len / cycle_len;\n\t\tint left = len % cycle_len;\n\t\tStringBuilder r = new StringBuilder();\n\t\tint i;\n\t\tfor (i = 0; i < full_cycles; ++i) {\n\t\t\tr.append(s.charAt(i * cycle_len));\n\t\t}\n\t\tif (left > 0) {\n\t\t\tr.append(s.charAt(i * cycle_len));\n\t\t}\n\t\tfor (i = 0; i < numRows - 2; ++i) {\n\t\t\tint j;\n\t\t\tfor (j = 0; j < full_cycles; ++j) {\n\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1));\n\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)));\n\t\t\t}\n\t\t\tif (left > 0) {\n\t\t\t\tif (j * cycle_len + i + 1 < len)\n\t\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1));\n\t\t\t\tif (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)\n\t\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)));\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < full_cycles; ++i)\n\t\t\tr.append(s.charAt(i * cycle_len + numRows - 1));\n\t\tif (left >= numRows)\n\t\t\tr.append(s.charAt(i * cycle_len + numRows - 1));\n\t\treturn r.toString();\n\t}\n}",
"js": "/**\n * @param {string} s\n * @param {number} numRows\n * @return {string}\n */\nvar convert = function(s, numRows) {\n let n = s.length;\n \n let N = numRows;\n\n if(s.length===1){\n return s;\n }\n if(N===1){\n return s;\n }\n \n let S = N-2;\n let C = 2*N-2;\n let R = Math.floor(n/C);\n let RS = n%(C);\n let CE = n-R*C; \n \n let RR = (RS<=N) ? 1 : 1+(RS-N);\n let RX = R*(N-1) + RR ;\n\n const output=[];\n\n for(let i=0;i<N;i++){\n for(let j=0,k=(N-1-i);j<RX;j+=(N-1),k+=(N-1)){\n let r = Math.floor(j/(N-1));\n let rs = j%(N-1);\n let offset = rs===0 ? i : N+rs-1;\n let index = r*C+offset;\n if(index<s.length){\n output.push(s[index]); \n }\n\n if(i>0&&i<N-1){\n r = Math.floor(k/(N-1));\n rs = k%(N-1);\n offset = rs===0 ? i : N+rs-1;\n index = r*C+offset;\n if(index<s.length){\n output.push(s[index]); \n }\n }\n }\n }\n\n return output.join('');\n};\n\n\nconst cases = [\n ['A',1,'A'],\n ['AB',1,'AB'],\n ['AB',2,'AB'],\n ['AB',3,'AB'],\n ['ABCDE',4,'ABCED'],\n ['PAYPALISHIRING',4,'PINALSIGYAHRPI'],\n [\n 'Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.',\n 10,\n 'A,tsaclmapdpohttsmetaltennarhreiheerilosnodlorornahwioawutiw.iwa,suttadnrajstosnasrefcdyr,endtarrdseeqoaaiewncaouderi,buenenhieerptddoenmecbrettgsouciimuneihfnv'\n\n ],\n \n [\n 'PAYPALISHIRING',\n 3,\n 'PAHNAPLSIIGYIR'\n ],\n];\n\n\nfor(const c of cases){\n \n const v = convert(c[0], c[1]); \n console.log('=>\\n',c[0], '\\n', c[2], '\\n', v);\n console.assert(v===c[2]);\n}\n\n// P I N\n// A L S I G \n// Y A H R\n// P I\n\n\n\n\n\n",
"python": "import math\nclass Solution:\n\tdef convert(self, s: str, numRows: int) -> str:\n\t\tn = len(s)\n\t\tN = numRows\n\t\tif n == 1 or N == 1:\n\t\t\treturn s\n\t\tS = N-2\n\t\tC = 2*N-2\n\t\tR = int(math.floor(n/C))\n\t\tRS = n % (C)\n\t\tCE = n-R*C\n\t\tRR = 1 if (RS <= N) else 1+(RS-N)\n\t\tRX = R*(N-1) + RR\n\t\toutput = []\n\t\ti = 0\n\t\twhile i < N:\n\t\t\tj = 0\n\t\t\tk = (N-1-i)\n\t\t\twhile j < RX:\n\t\t\t\tr = int(math.floor(j/(N-1)))\n\t\t\t\trs = j % (N-1)\n\t\t\t\toffset = i if rs == 0 else N+rs-1\n\t\t\t\tindex = r*C+offset\n\t\t\t\tif index < len(s):\n\t\t\t\t\toutput.append(s[index])\n\t\t\t\tif i > 0 and i < N-1:\n\t\t\t\t\tr = int(math.floor(k/(N-1)))\n\t\t\t\t\trs = k % (N-1)\n\t\t\t\t\toffset = i if rs == 0 else N+rs-1\n\t\t\t\t\tindex = r*C+offset\n\t\t\t\t\tif index < len(s):\n\t\t\t\t\t\toutput.append(s[index])\n\t\t\t\tj += (N-1)\n\t\t\t\tk += (N-1)\n\t\t\ti += 1\n\t\treturn ''.join(output)\n# %%\ns = Solution()\nprint(s.convert('PAYPALISHIRING', 3))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/5/5_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/5/5_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/5/5_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 51,
"question_title": "N 皇后",
"difficulty": "困难",
"question_content": "<p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class=\"original__bRMd\"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code> 和 <code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/11/13/queens.jpg\" style=\"width: 600px; height: 268px;\" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[\".Q..\",\"...Q\",\"Q...\",\"..Q.\"],[\"..Q.\",\"Q...\",\"...Q\",\".Q..\"]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[[\"Q\"]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= n <= 9</code></li>\t<li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div>",
"topic_link": "https://bbs.csdn.net/topics/600470831",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<string>> solveNQueens(int n)\n\t{\n\t\tvector<vector<string>> res;\n\t\tvector<int> stack(n);\n\t\tvector<string> solution(n, string(n, '.'));\n\t\tdfs(n, 0, stack, solution, res);\n\t\treturn res;\n\t}\nprivate:\n\tvoid dfs(int n, int row, vector<int> &stack, vector<string> &solution, vector<vector<string>> &res)\n\t{\n\t\tif (row == n)\n\t\t{\n\t\t\tres.push_back(solution);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tif (row == 0 || !conflict(stack, row, i))\n\t\t\t\t{\n\t\t\t\t\tsolution[row][i] = 'Q';\n\t\t\t\t\tstack[row] = i;\n\t\t\t\t\tdfs(n, row + 1, stack, solution, res);\n\t\t\t\t\tsolution[row][i] = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tbool conflict(vector<int> &stack, int row, int col)\n\t{\n\t\tfor (int i = 0; i < row; i++)\n\t\t{\n\t\t\tif (col == stack[i] || abs(row - i) == abs(col - stack[i]))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}",
"java": "import java.util.List;\nimport java.util.ArrayList;\npublic class Solution {\n\tpublic List<List<String>> solveNQueens(int n) {\n\t\tList<List<String>> res = new ArrayList<List<String>>();\n\t\tint[] queenList = new int[n];\n\t\tplaceQueen(queenList, 0, n, res);\n\t\treturn res;\n\t}\n\tprivate void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {\n\t\tif (row == n) {\n\t\t\tArrayList<String> list = new ArrayList<String>();\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tString str = \"\";\n\t\t\t\tfor (int col = 0; col < n; col++) {\n\t\t\t\t\tif (queenList[i] == col) {\n\t\t\t\t\t\tstr += \"Q\";\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstr += \".\";\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tlist.add(str);\n\t\t\t}\n\t\t\tres.add(list);\n\t\t}\n\t\tfor (int col = 0; col < n; col++) {\n\t\t\tif (isValid(queenList, row, col)) {\n\t\t\t\tqueenList[row] = col;\n\t\t\t\tplaceQueen(queenList, row + 1, n, res);\n\t\t\t}\n\t\t}\n\t}\n\tprivate boolean isValid(int[] queenList, int row, int col) {\n\t\tfor (int i = 0; i < row; i++) {\n\t\t\tint pos = queenList[i];\n\t\t\tif (pos == col) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (pos + row - i == col) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (pos - row + i == col) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef solveNQueens(self, n):\n\t\tif n == 0:\n\t\t\treturn 0\n\t\tres = []\n\t\tboard = [['.'] * n for t in range(n)]\n\t\tself.do_solveNQueens(res, board, n)\n\t\treturn res\n\tdef do_solveNQueens(self, res, board, num):\n\t\tif num == 0:\n\t\t\tres.append([''.join(t) for t in board])\n\t\t\treturn\n\t\tls = len(board)\n\t\tpos = ls - num\n\t\tcheck = [True] * ls\n\t\tfor i in range(pos):\n\t\t\tfor j in range(ls):\n\t\t\t\tif board[i][j] == 'Q':\n\t\t\t\t\tcheck[j] = False\n\t\t\t\t\tstep = pos - i\n\t\t\t\t\tif j + step < ls:\n\t\t\t\t\t\tcheck[j + step] = False\n\t\t\t\t\tif j - step >= 0:\n\t\t\t\t\t\tcheck[j - step] = False\n\t\t\t\t\tbreak\n\t\tfor j in range(ls):\n\t\t\tif check[j]:\n\t\t\t\tboard[pos][j] = 'Q'\n\t\t\t\tself.do_solveNQueens(res, board, num - 1)\n\t\t\t\tboard[pos][j] = '.'\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.solveNQueens(4))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104324976"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/50/50_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/50/50_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/50/50_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 52,
"question_title": "N皇后 II",
"difficulty": "困难",
"question_content": "<div class=\"notranslate\">\n <p><strong>n&nbsp;皇后问题</strong> 研究的是如何将 <code>n</code>&nbsp;个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p>\n\n <p>给你一个整数 <code>n</code> ,返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>\n\n <p>&nbsp;</p>\n\n <div class=\"original__bRMd\">\n <div>\n <p><strong>示例 1:</strong></p>\n <img style=\"width: 600px; height: 268px;\" src=\"https://assets.leetcode.com/uploads/2020/11/13/queens.jpg\"\n alt=\"\">\n <pre><strong>输入:</strong>n = 4\n<strong>输出:</strong>2\n<strong>解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。\n </pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>n = 1\n<strong>输出:</strong>1\n </pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= n &lt;= 9</code></li>\n <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li>\n </ul>\n </div>\n </div>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470922",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint totalNQueens(int n)\n\t{\n\t\tvector<int> stack(n);\n\t\treturn dfs(n, 0, stack);\n\t}\nprivate:\n\tint dfs(int n, int row, vector<int> &stack)\n\t{\n\t\tint count = 0;\n\t\tif (row == n)\n\t\t{\n\t\t\treturn count + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tif (row == 0 || !conflict(stack, row, i))\n\t\t\t\t{\n\t\t\t\t\tstack[row] = i;\n\t\t\t\t\tcount += dfs(n, row + 1, stack);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t}\n\tbool conflict(vector<int> &stack, int row, int col)\n\t{\n\t\tfor (int i = 0; i < row; i++)\n\t\t{\n\t\t\tif (col == stack[i] || abs(row - i) == abs(col - stack[i]))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}",
"java": "class Solution {\n\tprivate boolean col[];\n\tprivate boolean dia1[];\n\tprivate boolean dia2[];\n\tpublic int totalNQueens(int n) {\n\t\tcol = new boolean[n];\n\t\tdia1 = new boolean[2 * n - 1];\n\t\tdia2 = new boolean[2 * n - 1];\n\t\treturn putQueen(n, 0);\n\t}\n\tprivate int putQueen(int n, int index) {\n\t\tint res = 0;\n\t\tif (index == n) {\n\t\t\treturn 1;\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {\n\t\t\t\tcol[i] = true;\n\t\t\t\tdia1[i - index + n - 1] = true;\n\t\t\t\tdia2[i + index] = true;\n\t\t\t\tres += putQueen(n, index + 1);\n\t\t\t\tcol[i] = false;\n\t\t\t\tdia1[i - index + n - 1] = false;\n\t\t\t\tdia2[i + index] = false;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef __init__(self):\n\t\tself.count = 0\n\tdef totalNQueens(self, n):\n\t\tself.dfs(0, n, 0, 0, 0)\n\t\treturn self.count\n\tdef dfs(self, row, n, column, diag, antiDiag):\n\t\tif row == n:\n\t\t\tself.count += 1\n\t\t\treturn\n\t\tfor index in range(n):\n\t\t\tisColSafe = (1 << index) & column == 0\n\t\t\tisDigSafe = (1 << (n - 1 + row - index)) & diag == 0\n\t\t\tisAntiDiagSafe = (1 << (row + index)) & antiDiag == 0\n\t\t\tif isAntiDiagSafe and isColSafe and isDigSafe:\n\t\t\t\tself.dfs(row + 1, n, (1 << index) | column,\n\t\t\t\t\t\t (1 << (n - 1 + row - index)) | diag,\n\t\t\t\t\t\t (1 << (row + index)) | antiDiag)\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.totalNQueens(4))",
"status": 1,
"keywords": "回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104325309"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/51/51_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/51/51_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/51/51_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 53,
"question_title": "最大子序和",
"difficulty": "简单",
"question_content": "<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]<strong><br />输出:</strong>6<strong><br />解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [-1]<strong><br />输出:</strong>-1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums = [-100000]<strong><br />输出:</strong>-100000</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>\t<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul><p> </p><p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>",
"topic_link": "https://bbs.csdn.net/topics/600469821",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint maxSubArray(vector<int> &nums)\n\t{\n\t\tint sum = 0, max_sum = INT_MIN;\n\t\tfor (int i = 0; i < nums.size(); i++)\n\t\t{\n\t\t\tif (sum < 0)\n\t\t\t{\n\t\t\t\tsum = nums[i];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tsum += nums[i];\n\t\t\t}\n\t\t\tmax_sum = max(sum, max_sum);\n\t\t}\n\t\treturn max_sum;\n\t}\n};",
"java": "class Solution {\n\tpublic int maxSubArray(int[] nums) {\n\t\tint maxSum = nums[0];\n\t\tint curSum = 0;\n\n\t\tfor (int n : nums) {\n\t\t\tcurSum += n;\n\t\t\tif (curSum > maxSum) {\n\t\t\t\tmaxSum = curSum;\n\t\t\t}\n\t\t\tif (curSum < 0) {\n\t\t\t\tcurSum = 0;\n\t\t\t}\n\t\t}\n\n\t\treturn maxSum;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef maxSubArray(self, nums):\n\t\tmaxEndingHere = maxSofFar = nums[0]\n\t\tfor i in range(1, len(nums)):\n\t\t\tmaxEndingHere = max(maxEndingHere + nums[i], nums[i])\n\t\t\tmaxSofFar = max(maxEndingHere, maxSofFar)\n\t\treturn maxSofFar\n# %%\ns = Solution()\nprint(s.maxSubArray(nums = [-2,1,-3,4,-1,2,1,-5,4]))",
"status": 1,
"keywords": "数组,分治,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/52/52_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/52/52_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/52/52_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 54,
"question_title": "螺旋矩阵",
"difficulty": "中等",
"question_content": "<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ,请按照 <strong>顺时针螺旋顺序</strong> ,返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出:</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == matrix.length</code></li>\t<li><code>n == matrix[i].length</code></li>\t<li><code>1 <= m, n <= 10</code></li>\t<li><code>-100 <= matrix[i][j] <= 100</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470121",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> spiralOrder(vector<vector<int>> &matrix)\n\t{\n\t\tvector<int> res;\n\t\tint hor_top = 0;\n\t\tint hor_bottom = matrix.size() - 1;\n\t\tint ver_left = 0;\n\t\tint ver_right = matrix[0].size() - 1;\n\t\tint direction = 0;\n\t\twhile (hor_top <= hor_bottom && ver_left <= ver_right)\n\t\t{\n\t\t\tswitch (direction)\n\t\t\t{\n\t\t\tcase 0:\n\t\t\t\tfor (int i = ver_left; i <= ver_right; i++)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[hor_top][i]);\n\t\t\t\t}\n\t\t\t\thor_top++;\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (int i = hor_top; i <= hor_bottom; i++)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[i][ver_right]);\n\t\t\t\t}\n\t\t\t\tver_right--;\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tfor (int i = ver_right; i >= ver_left; i--)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[hor_bottom][i]);\n\t\t\t\t}\n\t\t\t\thor_bottom--;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tfor (int i = hor_bottom; i >= hor_top; i--)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[i][ver_left]);\n\t\t\t\t}\n\t\t\t\tver_left++;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdirection++;\n\t\t\tdirection %= 4;\n\t\t}\n\t\treturn res;\n\t}\n};",
"java": "class Solution {\n\tpublic List<Integer> spiralOrder(int[][] matrix) {\n\t\tList<Integer> res = new ArrayList<Integer>();\n\t\tif (matrix.length == 0 || (matrix.length == 1 && matrix[0].length == 0))\n\t\t\treturn res;\n\t\tint left = 0;\n\t\tint right = matrix[0].length - 1;\n\t\tint top = 0;\n\t\tint bottom = matrix.length - 1;\n\t\tint num = (right + 1) * (bottom + 1);\n\t\twhile (num > 0) {\n\t\t\tfor (int j = left; j <= right; j++) {\n\t\t\t\tres.add(matrix[top][j]);\n\t\t\t\tnum--;\n\t\t\t}\n\t\t\tif (num <= 0)\n\t\t\t\tbreak;\n\t\t\ttop++;\n\t\t\tfor (int i = top; i <= bottom; i++) {\n\t\t\t\tres.add(matrix[i][right]);\n\t\t\t\tnum--;\n\t\t\t}\n\t\t\tif (num <= 0)\n\t\t\t\tbreak;\n\t\t\tright--;\n\t\t\tfor (int j = right; j >= left; j--) {\n\t\t\t\tres.add(matrix[bottom][j]);\n\t\t\t\tnum--;\n\t\t\t}\n\t\t\tif (num <= 0)\n\t\t\t\tbreak;\n\t\t\tbottom--;\n\t\t\tfor (int i = bottom; i >= top; i--) {\n\t\t\t\tres.add(matrix[i][left]);\n\t\t\t\tnum--;\n\t\t\t}\n\t\t\tif (num <= 0)\n\t\t\t\tbreak;\n\t\t\tleft++;\n\t\t}\n\t\treturn res;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef spiralOrder(self, matrix):\n\t\t\"\"\"\n\t\t:type matrix: List[List[int]]\n\t\t:rtype: List[int]\n\t\t\"\"\"\n\t\tif matrix is None or len(matrix) == 0:\n\t\t\treturn matrix\n\t\tm, n = len(matrix), len(matrix[0])\n\t\treturn self.get_spiralOrder(matrix, 0, m - 1, 0, n - 1)\n\tdef get_spiralOrder(self, matrix, r_start, r_end, c_start, c_end):\n\t\tif r_start > r_end or c_start > c_end:\n\t\t\treturn []\n\t\telif r_start == r_end:\n\t\t\treturn matrix[r_start][c_start:c_end + 1]\n\t\telif c_start == c_end:\n\t\t\treturn [matrix[j][c_end] for j in range(r_start, r_end + 1)]\n\t\tcurr = matrix[r_start][c_start:c_end + 1] + [matrix[j][c_end] for j in range(r_start + 1, r_end)] +\\\n\t\t\t\tmatrix[r_end][c_start:c_end + 1][::-1] +\\\n\t\t\t\t[matrix[j][c_start] for j in reversed(range(r_start + 1, r_end))]\n\t\tres = curr + self.get_spiralOrder(matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 1)\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]))",
"status": 1,
"keywords": "数组,矩阵,模拟",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/53/53_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/53/53_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/53/53_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 55,
"question_title": "跳跃游戏",
"difficulty": "中等",
"question_content": "<p>给定一个非负整数数组 <code>nums</code> ,你最初位于数组的 <strong>第一个下标</strong> 。</p><p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p><p>判断你是否能够到达最后一个下标。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,3,1,1,4]<strong><br />输出:</strong>true<strong><br />解释:</strong>可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,1,0,4]<strong><br />输出:</strong>false<strong><br />解释:</strong>无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>\t<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470122",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\nstatic inline int max(int a, int b)\n{\n\treturn a > b ? a : b;\n}\nstatic bool canJump(int *nums, int numsSize)\n{\n\tint i, pos = 0;\n\tfor (i = 0; i < numsSize - 1; i++)\n\t{\n\t\tif (pos < i || pos >= numsSize - 1)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tpos = max(i + nums[i], pos);\n\t}\n\treturn pos >= numsSize - 1;\n}\nint main(int argc, char **argv)\n{\n\tint i, count = argc - 1;\n\tint *nums = malloc(count * sizeof(int));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tnums[i] = atoi(argv[i + 1]);\n\t}\n\tprintf(\"%s\\n\", canJump(nums, count) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean canJump(int[] nums) {\n\t\tboolean can = true;\n\t\tif (nums.length < 2) {\n\t\t\treturn can;\n\t\t}\n\t\tint n = nums.length;\n\t\tint stride = 1;\n\t\tfor (int i = n - 2; i >= 0; i--) {\n\t\t\tif (nums[i] < stride) {\n\t\t\t\tstride++;\n\t\t\t\tcan = false;\n\t\t\t} else {\n\t\t\t\tcan = true;\n\t\t\t\tstride = 1;\n\t\t\t}\n\t\t}\n\t\treturn can;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef canJump(self, nums):\n\t\t\"\"\"\n\t\t:type nums: List[int]\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tlength = len(nums)\n\t\tbegin = length - 1\n\t\tfor i in reversed(range(length - 1)):\n\t\t\tif i + nums[i] >= begin:\n\t\t\t\tbegin = i\n\t\treturn not begin\n# %%\ns = Solution()\nprint(s.canJump(nums = [2,3,1,1,4]))",
"status": 1,
"keywords": "贪心,数组,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/54/54_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/54/54_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/54/54_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 56,
"question_title": "合并区间",
"difficulty": "中等",
"question_content": "<p>以数组 <code>intervals</code> 表示若干个区间的集合,其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<strong><br />输出:</strong>[[1,6],[8,10],[15,18]]<strong><br />解释:</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>intervals = [[1,4],[4,5]]<strong><br />输出:</strong>[[1,5]]<strong><br />解释:</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= intervals.length <= 10<sup>4</sup></code></li>\t<li><code>intervals[i].length == 2</code></li>\t<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471018",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic int compare(const void *a, const void *b)\n{\n\treturn ((int *)a)[0] - ((int *)b)[0];\n}\nint **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes)\n{\n\tif (intervalsSize == 0)\n\t{\n\t\t*returnSize = 0;\n\t\treturn intervals;\n\t}\n\tint i, len = 0;\n\tint *tmp = malloc(intervalsSize * 2 * sizeof(int));\n\tfor (i = 0; i < intervalsSize; i++)\n\t{\n\t\ttmp[i * 2] = intervals[i][0];\n\t\ttmp[i * 2 + 1] = intervals[i][1];\n\t}\n\tqsort(tmp, intervalsSize, 2 * sizeof(int), compare);\n\tintervals[0][0] = tmp[0];\n\tintervals[0][1] = tmp[1];\n\tfor (i = 1; i < intervalsSize; i++)\n\t{\n\t\tif (tmp[i * 2] > intervals[len][1])\n\t\t{\n\t\t\tlen++;\n\t\t\tintervals[len][0] = tmp[i * 2];\n\t\t\tintervals[len][1] = tmp[i * 2 + 1];\n\t\t}\n\t\telse if (tmp[i * 2 + 1] > intervals[len][1])\n\t\t{\n\t\t\tintervals[len][1] = tmp[i * 2 + 1];\n\t\t}\n\t}\n\tlen += 1;\n\t*returnSize = len;\n\t*returnColumnSizes = malloc(len * sizeof(int));\n\tfor (i = 0; i < len; i++)\n\t{\n\t\t(*returnColumnSizes)[i] = 2;\n\t}\n\treturn intervals;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 1 || argc % 2 == 0)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test s0 e0 s1 e1...\");\n\t\texit(-1);\n\t}\n\tint i, count = 0;\n\tint *sizes = malloc((argc - 1) / 2 * sizeof(int));\n\tint **intervals = malloc((argc - 1) / 2 * sizeof(int *));\n\tfor (i = 0; i < (argc - 1) / 2; i++)\n\t{\n\t\tsizes[i] = 2;\n\t\tintervals[i] = malloc(2 * sizeof(int));\n\t\tintervals[i][0] = atoi(argv[i * 2 + 1]);\n\t\tintervals[i][1] = atoi(argv[i * 2 + 2]);\n\t}\n\tint *col_sizes;\n\tint **results = merge(intervals, (argc - 1) / 2, sizes, &count, &col_sizes);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"[%d,%d]\\n\", results[i][0], results[i][1]);\n\t}\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int[][] merge(int[][] intervals) {\n\t\tList<int[]> res = new ArrayList<>();\n\t\tif (intervals == null) {\n\t\t\treturn res.toArray(new int[0][]);\n\t\t}\n\t\tArrays.sort(intervals, (a, b) -> a[0] - b[0]);\n\t\tint i = 0;\n\t\tint left = 0;\n\t\tint right = 0;\n\t\twhile (i < intervals.length) {\n\t\t\tleft = intervals[i][0];\n\t\t\tright = intervals[i][1];\n\t\t\twhile (i < intervals.length - 1 && right >= intervals[i + 1][0]) {\n\t\t\t\ti++;\n\t\t\t\tright = Math.max(right, intervals[i][1]);\n\t\t\t}\n\t\t\tres.add(new int[] { left, right });\n\t\t\ti++;\n\t\t}\n\t\treturn res.toArray(new int[0][]);\n\t}\n}\n",
"js": "",
"python": "class Interval(object):\n\tdef __init__(self, s=0, e=0):\n\t\tself.start = s\n\t\tself.end = e\nclass Solution(object):\n\tdef list2interval(self, list_interval):\n\t\tret = []\n\t\tfor i in list_interval:\n\t\t\tinterval = Interval(i[0], i[1])\n\t\t\tret.append(interval)\n\t\treturn ret\n\tdef interval2list(self, interval):\n\t\tret = []\n\t\tx = [0,0]\n\t\tfor i in interval:\n\t\t\tx[0] = i.start\n\t\t\tx[1] = i.end\n\t\t\tret.append(x)\n\t\t\tx = [0,0]\n\t\treturn ret\n\tdef merge(self, intervals):\n\t\t\"\"\"\n\t\t:type intervals: List[Interval]\n\t\t:rtype: List[Interval]\n\t\t\"\"\"\n\t\tif intervals is None:\n\t\t\treturn\n\t\tls = len(intervals)\n\t\tif ls <= 1:\n\t\t\treturn intervals\n\t\tintervals = self.list2interval(intervals)\t\t\n\t\tintervals.sort(key=lambda x: x.start)\n\t\tpos = 0\n\t\twhile pos < len(intervals) - 1:\n\t\t\tif intervals[pos].end >= intervals[pos + 1].start:\n\t\t\t\tnext = intervals.pop(pos + 1)\n\t\t\t\tif next.end > intervals[pos].end:\n\t\t\t\t\tintervals[pos].end = next.end\n\t\t\telse:\n\t\t\t\tpos += 1\n\t\tintervals = self.interval2list(intervals)\n\t\treturn intervals\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.merge(intervals = [[1,4],[4,5]]))",
"status": 1,
"keywords": "数组,排序",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/55/55_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/55/55_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/55/55_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 57,
"question_title": "插入区间",
"difficulty": "中等",
"question_content": "<p>给你一个<strong> 无重叠的</strong><em> ,</em>按照区间起始端点排序的区间列表。</p>\n<p>在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。</p>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]<strong><br />输出:</strong>[[1,5],[6,9]]</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]<strong><br />输出:</strong>[[1,2],[3,10],[12,16]]<strong><br />解释:</strong>这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>intervals = [], newInterval = [5,7]<strong><br />输出:</strong>[[5,7]]</pre>\n<p><strong>示例 4:</strong></p>\n<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,3]<strong><br />输出:</strong>[[1,5]]</pre>\n<p><strong>示例 5:</strong></p>\n<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,7]<strong><br />输出:</strong>[[1,7]]</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>\n <li><code>intervals[i].length == 2</code></li>\n <li><code>0 <= intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>\n <li><code>intervals</code> 根据 <code>intervals[i][0]</code> 按 <strong>升序</strong> 排列</li>\n <li><code>newInterval.length == 2</code></li>\n <li><code>0 <= newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470920",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstatic int compare(const void *a, const void *b)\n{\n\treturn ((int *)a)[0] - ((int *)b)[0];\n}\nint **insert(int **intervals, int intervalsSize, int *intervalsColSize, int *newInterval,\n\t\t\t int newIntervalSize, int *returnSize, int **returnColumnSizes)\n{\n\tint i, len = 0;\n\tint *tmp = malloc((intervalsSize + 1) * 2 * sizeof(int));\n\tfor (i = 0; i < intervalsSize; i++)\n\t{\n\t\ttmp[i * 2] = intervals[i][0];\n\t\ttmp[i * 2 + 1] = intervals[i][1];\n\t}\n\ttmp[i * 2] = newInterval[0];\n\ttmp[i * 2 + 1] = newInterval[1];\n\tqsort(tmp, intervalsSize + 1, 2 * sizeof(int), compare);\n\tint **results = malloc((intervalsSize + 1) * sizeof(int *));\n\tresults[0] = malloc(2 * sizeof(int));\n\tresults[0][0] = tmp[0];\n\tresults[0][1] = tmp[1];\n\tfor (i = 1; i < intervalsSize + 1; i++)\n\t{\n\t\tresults[i] = malloc(2 * sizeof(int));\n\t\tif (tmp[i * 2] > results[len][1])\n\t\t{\n\t\t\tlen++;\n\t\t\tresults[len][0] = tmp[i * 2];\n\t\t\tresults[len][1] = tmp[i * 2 + 1];\n\t\t}\n\t\telse if (tmp[i * 2 + 1] > results[len][1])\n\t\t{\n\t\t\tresults[len][1] = tmp[i * 2 + 1];\n\t\t}\n\t}\n\tlen += 1;\n\t*returnSize = len;\n\t*returnColumnSizes = malloc(len * sizeof(int));\n\tfor (i = 0; i < len; i++)\n\t{\n\t\t(*returnColumnSizes)[i] = 2;\n\t}\n\treturn results;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3 || argc % 2 == 0)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test new_s new_e s0 e0 s1 e1...\");\n\t\texit(-1);\n\t}\n\tint new_interv[2];\n\tnew_interv[0] = atoi(argv[1]);\n\tnew_interv[1] = atoi(argv[2]);\n\tint i, count = 0;\n\tint *size = malloc((argc - 3) / 2 * sizeof(int));\n\tint **intervals = malloc((argc - 3) / 2 * sizeof(int *));\n\tfor (i = 0; i < (argc - 3) / 2; i++)\n\t{\n\t\tintervals[i] = malloc(2 * sizeof(int));\n\t\tintervals[i][0] = atoi(argv[i * 2 + 3]);\n\t\tintervals[i][1] = atoi(argv[i * 2 + 4]);\n\t}\n\tint *col_sizes;\n\tint **results = insert(intervals, (argc - 3) / 2, size, new_interv, 2, &count, &col_sizes);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"[%d,%d]\\n\", results[i][0], results[i][1]);\n\t}\n\treturn 0;\n}",
"java": "\npublic class Interval {\n\tint start;\n\tint end;\n\n\tInterval() {\n\t\tstart = 0;\n\t\tend = 0;\n\t}\n\n\tInterval(int s, int e) {\n\t\tstart = s;\n\t\tend = e;\n\t}\n}\n\nclass Solution {\n\tpublic int[][] insert(int[][] intervals, int[] newInterval) {\n\t\tint[][] newIntervals = new int[intervals.length + 1][];\n\t\tSystem.arraycopy(intervals, 0, newIntervals, 0, intervals.length);\n\t\tnewIntervals[intervals.length] = newInterval;\n\n\t\tArrays.sort(newIntervals, (a, b) -> a[0] - b[0]);\n\t\tStack<int[]> stack = new Stack<>();\n\t\tfor (int[] num : newIntervals) {\n\t\t\tif (stack.isEmpty()) {\n\t\t\t\tstack.push(num);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint[] arr = stack.peek();\n\t\t\tif (arr[1] >= num[0]) {\n\t\t\t\tint[] combine = { arr[0], Math.max(arr[1], num[1]) };\n\t\t\t\tstack.pop();\n\t\t\t\tstack.push(combine);\n\t\t\t} else {\n\t\t\t\tstack.push(num);\n\t\t\t}\n\t\t}\n\t\treturn stack.toArray(new int[0][]);\n\t}\n}\n",
"js": "",
"python": "class Interval(object):\n\tdef __init__(self, s=0, e=0):\n\t\tself.start = s\n\t\tself.end = e\nclass Solution(object):\n\tdef list2interval(self, list_interval):\n\t\tret = []\n\t\tfor i in list_interval:\n\t\t\tinterval = Interval(i[0], i[1])\n\t\t\tret.append(interval)\n\t\treturn ret\n\tdef interval2list(self, interval):\n\t\tret = []\n\t\tx = [0,0]\n\t\tfor i in interval:\n\t\t\tx[0] = i.start\n\t\t\tx[1] = i.end\n\t\t\tret.append(x)\n\t\t\tx = [0,0]\n\t\treturn ret\n\tdef insert(self, intervals, newInterval):\n\t\t\"\"\"\n\t\t:type intervals: List[Interval]\n\t\t:type newInterval: Interval\n\t\t:rtype: List[Interval]\n\t\t\"\"\"\n\t\tif intervals is None or len(intervals) == 0:\n\t\t\treturn [newInterval]\n\t\tintervals = self.list2interval(intervals)\n\t\tnewInterval = Interval(newInterval[0], newInterval[1])\n\t\tintervals.sort(key=lambda x:x.start)\n\t\tpos = 0\n\t\twhile pos < len(intervals):\n\t\t\tif newInterval.end < intervals[pos].start:\n\t\t\t\tintervals.insert(pos, newInterval)\n\t\t\t\tintervals = self.interval2list(intervals)\n\t\t\t\treturn intervals\n\t\t\tif self.check_overlap(intervals[pos], newInterval):\n\t\t\t\ttemp = intervals.pop(pos)\n\t\t\t\tnewInterval = self.merge_intervals(temp, newInterval)\n\t\t\telse:\n\t\t\t\tpos += 1\n\t\tif len(intervals) == 0 or pos == len(intervals):\n\t\t\tintervals.append(newInterval)\n\t\tintervals = self.interval2list(intervals)\n\t\treturn intervals\n\tdef check_overlap(self, curr_int, new_int):\n\t\tif curr_int.start <= new_int.start:\n\t\t if curr_int.end > new_int.start:\n\t\t\t return True\n\t\telse:\n\t\t\tif curr_int.start <= new_int.end:\n\t\t\t\treturn True\n\t\treturn False\n\tdef merge_intervals(self, int1, int2):\n\t\ttemp_int = Interval()\n\t\ttemp_int.start = min([int1.start, int2.start])\n\t\ttemp_int.end = max([int1.end, int2.end])\n\t\treturn temp_int\n# %%\ns = Solution()\nprint(s.insert(intervals = [[1,3],[6,9]], newInterval = [2,5]))",
"status": 1,
"keywords": "数组",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/56/56_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/56/56_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/56/56_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 58,
"question_title": "最后一个单词的长度",
"difficulty": "简单",
"question_content": "<p>给你一个字符串 <code>s</code>,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。</p><p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"Hello World\"<strong><br />输出:</strong>5</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \" \"<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length <= 10<sup>4</sup></code></li>\t<li><code>s</code> 仅有英文字母和空格 <code>' '</code> 组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470793",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint lengthOfLastWord(char *s)\n{\n\tint len = 0;\n\twhile (*s != '\\0')\n\t{\n\t\tif (s[-1] == ' ' && s[0] != ' ')\n\t\t{\n\t\t\tlen = 1;\n\t\t}\n\t\telse if (*s != ' ')\n\t\t{\n\t\t\tlen++;\n\t\t}\n\t\ts++;\n\t}\n\treturn len;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test word\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%d\\n\", lengthOfLastWord(argv[1]));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int lengthOfLastWord(String s) {\n\t\tint count = 0;\n\t\tfor (int i = s.length() - 1; i >= 0; i--) {\n\t\t\tif (s.charAt(i) != ' ') {\n\t\t\t\tcount++;\n\t\t\t} else if (count > 0) {\n\t\t\t\treturn count;\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef lengthOfLastWord(self, s):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tif len(s) == 0:\n\t\t\treturn 0\n\t\ttemp = s.split(' ')\n\t\ttemp = [t for t in temp if len(t) > 0]\n\t\tif len(temp) == 0:\n\t\t\treturn 0\n\t\telse:\n\t\t\treturn len(temp[-1])\n# %%\ns = Solution()\nprint(s.lengthOfLastWord(s = \"Hello World\"))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_44833195/article/details/106370752"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/57/57_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/57/57_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/57/57_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 59,
"question_title": "螺旋矩阵 II",
"difficulty": "中等",
"question_content": "<p>给你一个正整数 <code>n</code> ,生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素,且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code> 。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[[1]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= n <= 20</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470226",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> generateMatrix(int n)\n\t{\n\t\tvector<vector<int>> matrix(n, vector<int>(n));\n\t\tint direction = 0;\n\t\tint hor_top = 0;\n\t\tint hor_bottom = n - 1;\n\t\tint ver_left = 0;\n\t\tint ver_right = n - 1;\n\t\tint num = 0;\n\t\twhile (num < n * n)\n\t\t{\n\t\t\tswitch (direction)\n\t\t\t{\n\t\t\tcase 0:\n\t\t\t\tfor (int i = ver_left; i <= ver_right; i++)\n\t\t\t\t{\n\t\t\t\t\tmatrix[hor_top][i] = ++num;\n\t\t\t\t}\n\t\t\t\thor_top++;\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (int i = hor_top; i <= hor_bottom; i++)\n\t\t\t\t{\n\t\t\t\t\tmatrix[i][ver_right] = ++num;\n\t\t\t\t}\n\t\t\t\tver_right--;\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tfor (int i = ver_right; i >= ver_left; i--)\n\t\t\t\t{\n\t\t\t\t\tmatrix[hor_bottom][i] = ++num;\n\t\t\t\t}\n\t\t\t\thor_bottom--;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tfor (int i = hor_bottom; i >= hor_top; i--)\n\t\t\t\t{\n\t\t\t\t\tmatrix[i][ver_left] = ++num;\n\t\t\t\t}\n\t\t\t\tver_left++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdirection++;\n\t\t\tdirection %= 4;\n\t\t}\n\t\treturn matrix;\n\t}\n};",
"java": "package LeetCode;\n\npublic class GenerateMatrix {\n\tpublic int[][] generateMatrix(int n) {\n\t\tint[][] res = new int[n][n];\n\t\tif (n == 0) {\n\t\t\treturn res;\n\t\t}\n\t\tint left = 0;\n\t\tint right = n - 1;\n\t\tint up = 0;\n\t\tint down = n - 1;\n\t\tint i = 1;\n\t\twhile (i <= n * n) {\n\n\t\t\tfor (int col = left; col <= right; col++) {\n\t\t\t\tres[up][col] = i;\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t\tup++;\n\t\t\tif (i <= n * n) {\n\t\t\t\tfor (int j = up; j <= down; j++) {\n\t\t\t\t\tres[j][right] = i;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tright--;\n\t\t\t}\n\t\t\tif (i <= n * n) {\n\t\t\t\tfor (int j = right; j >= left; j--) {\n\t\t\t\t\tres[down][j] = i;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tdown--;\n\t\t\t}\n\t\t\tif (i <= n * n) {\n\t\t\t\tfor (int j = down; j >= up; j--) {\n\t\t\t\t\tres[j][left] = i;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tleft++;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\n\tpublic static void main(String[] args) {\n\t\tGenerateMatrix a = new GenerateMatrix();\n\t\ta.generateMatrix(3);\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef generateMatrix(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: List[List[int]]\n\t\t\"\"\"\n\t\tres = [[0] * n for _ in range(n)]\n\t\tpos = [0, 0]\n\t\tmove = (0, 1)\n\t\tfor index in range(1, n * n + 1):\n\t\t\tres[pos[0]][pos[1]] = index\n\t\t\tif res[(pos[0] + move[0]) % n][(pos[1] + move[1]) % n] > 0:\n\t\t\t\tmove = (move[1], -1 * move[0])\n\t\t\tpos[0] = pos[0] + move[0]\n\t\t\tpos[1] = pos[1] + move[1]\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.generateMatrix(2))",
"status": 1,
"keywords": "数组,矩阵,模拟",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/58/58_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/58/58_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/58/58_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 60,
"question_title": "排列序列",
"difficulty": "困难",
"question_content": "<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol>\t<li><code>\"123\"</code></li>\t<li><code>\"132\"</code></li>\t<li><code>\"213\"</code></li>\t<li><code>\"231\"</code></li>\t<li><code>\"312\"</code></li>\t<li><code>\"321\"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>\"213\"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>\"2314\"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>\"123\"</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= n <= 9</code></li>\t<li><code>1 <= k <= n!</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471016",
"cpp": "#include <stdbool.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic char *getPermutation(int n, int k)\n{\n\tint i;\n\tchar *result = malloc(n + 1);\n\tbool *used = malloc(n * sizeof(bool));\n\tmemset(used, false, n * sizeof(bool));\n\tint total = 1;\n\tfor (i = 1; i <= n; i++)\n\t{\n\t\ttotal *= i;\n\t}\n\tk = k - 1;\n\tfor (i = 0; i < n; i++)\n\t{\n\t\ttotal /= (n - i);\n\t\tint gid = k / total;\n\t\tk %= total;\n\t\tint x = -1;\n\t\tint count = 0;\n\t\twhile (count <= gid)\n\t\t{\n\t\t\tx = (x + 1) % n;\n\t\t\tif (!used[x])\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\t\tused[x] = true;\n\t\tresult[i] = x + 1 + '0';\n\t}\n\tresult[n] = '\\0';\n\treturn result;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n, k\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", getPermutation(atoi(argv[1]), atoi(argv[2])));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic String getPermutation(int n, int k) {\n\t\tStringBuilder sb = new StringBuilder();\n\t\tList<Integer> candidates = new ArrayList<>();\n\t\tint[] factorials = new int[n + 1];\n\t\tfactorials[0] = 1;\n\t\tint fact = 1;\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tcandidates.add(i);\n\t\t\tfact *= i;\n\t\t\tfactorials[i] = fact;\n\t\t}\n\t\tk -= 1;\n\t\tfor (int i = n - 1; i >= 0; --i) {\n\t\t\tint index = k / factorials[i];\n\t\t\tsb.append(candidates.remove(index));\n\t\t\tk -= index * factorials[i];\n\t\t}\n\t\treturn sb.toString();\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef getPermutation(self, n, k):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:type k: int\n\t\t:rtype: str\n\t\t\"\"\"\n\t\timport math\n\t\tres=[\"\"]\n\t\tdef generate(s,k):\n\t\t\tn=len(s)\t\n\t\t\tif n<=2:\n\t\t\t\tif k==2:\n\t\t\t\t\tres[0]+=s[::-1]\n\t\t\t\telse:\n\t\t\t\t\tres[0]+=s\n\t\t\t\treturn\n\t\t\tstep = math.factorial(n-1) \n\t\t\tyu=k%step\n\t\t\tif yu==0:\n\t\t\t\tyu=step\n\t\t\t\tc=k//step-1 \n\t\t\telse:\n\t\t\t\tc=k//step\n\t\t\tres[0]+=s[c]\n\t\t\tgenerate(s[:c]+s[c+1:],yu)\n\t\t\treturn \n\t\ts=\"\"\n\t\tfor i in range(1,n+1):\n\t\t\ts+=str(i) \n\t\tgenerate(s,k)\n\t\treturn res[0]\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.getPermutation(3, 2))",
"status": 1,
"keywords": "递归,数学",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104335405"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/59/59_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/59/59_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/59/59_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7,
"question_title": "整数反转",
"difficulty": "简单",
"question_content": "<p>给你一个 32 位的有符号整数 <code>x</code> ,返回将 <code>x</code> 中的数字部分反转后的结果。</p><p>如果反转后整数超过 32 位的有符号整数的范围 <code>[−2<sup>31</sup>,  2<sup>31 </sup>− 1]</code> ,就返回 0。</p><strong>假设环境不允许存储 64 位整数(有符号或无符号)。</strong><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 123<strong><br />输出:</strong>321</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = -123<strong><br />输出:</strong>-321</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 120<strong><br />输出:</strong>21</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>x = 0<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469815",
"cpp": "int reverse(int x)\n{\n\tlong long int r = 0;\n\twhile (x)\n\t{\n\t\tr = r * 10 + (x % 10);\n\t\tx /= 10;\n\t}\n\tif (r > 2147483647)\n\t\treturn 0;\n\tif (r < -2147483648)\n\t\treturn 0;\n\treturn (int)r;\n}",
"java": "class Solution {\n\tpublic int reverse(int x) {\n\t\tlong xx = x;\n\t\tlong r;\n\t\tlong y = 0;\n\t\tboolean sign = xx < 0;\n\t\twhile (xx != 0) {\n\t\t\tr = xx % 10;\n\t\t\ty = y * 10 + r;\n\t\t\tif (sign) {\n\t\t\t\txx = (long) Math.ceil(xx / 10);\n\t\t\t} else {\n\t\t\t\txx = (long) Math.floor(xx / 10);\n\t\t\t}\n\t\t}\n\t\treturn y > Integer.MAX_VALUE || y < Integer.MIN_VALUE ? 0 : (int) y;\n\t}\n}",
"js": "/**\n * @param {number} x\n * @return {number}\n */\nvar reverse = function(x) {\n \n let r;\n let y=0;\n let sign = x<0;\n while(x!=0){\n r = x%10;\n y=y*10+r;\n if(sign){\n x = Math.ceil(x/10);\n }else{\n x = Math.floor(x/10);\n }\n }\n \n return y > 0x7FFFFFFF || y< -0x7FFFFFFF ? 0 : y;\n};\n\nfunction main(){\n\n}\n\nmain();\n",
"python": "import math\nclass Solution:\n\tdef reverse(self, x: int) -> int:\n\t\tr = 0\n\t\ty = 0\n\t\tabs_x = abs(x)\n\t\tnegative = x < 0\n\t\twhile abs_x != 0:\n\t\t\tr = abs_x % 10\n\t\t\ty = y*10+r\n\t\t\tabs_x = int(math.floor(abs_x/10))\n\t\tif negative:\n\t\t\ty = -y\n\t\treturn 0 if (y > 2147483647 or y < -2147483648) else y\n# %%\ns = Solution()\nprint(s.reverse(x = 123))\nprint(s.reverse(x = -123))",
"status": 1,
"keywords": "数学",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/6/6_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/6/6_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/6/6_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 61,
"question_title": "旋转链表",
"difficulty": "中等",
"question_content": "<p>给你一个链表的头节点 <code>head</code> ,旋转链表,将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/rotate1.jpg\" style=\"width: 600px; height: 254px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[4,5,1,2,3]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/roate2.jpg\" style=\"width: 472px; height: 542px;\" /><pre><strong>输入:</strong>head = [0,1,2], k = 4<strong><br />输出:</strong>[2,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点的数目在范围 <code>[0, 500]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li><code>0 <= k <= 2 * 10<sup>9</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469922",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *rotateRight(ListNode *head, int k)\n\t{\n\t\tif (head == nullptr)\n\t\t{\n\t\t\treturn head;\n\t\t}\n\t\tint len = 0;\n\t\tListNode dummy;\n\t\tdummy.next = head;\n\t\tListNode *tail = &dummy;\n\t\twhile (tail->next != nullptr)\n\t\t{\n\t\t\tlen++;\n\t\t\ttail = tail->next;\n\t\t}\n\t\tListNode *prev = &dummy;\n\t\tListNode *p = head;\n\t\tk = k % len;\n\t\tfor (int i = 0; i < len - k; i++)\n\t\t{\n\t\t\tprev = p;\n\t\t\tp = p->next;\n\t\t}\n\t\tif (p != nullptr)\n\t\t{\n\t\t\tprev->next = tail->next;\n\t\t\ttail->next = head;\n\t\t\thead = p;\n\t\t}\n\t\treturn head;\n\t}\n};",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode rotateRight(ListNode head, int k) {\n\t\tif (head == null || k == 0) {\n\t\t\treturn head;\n\t\t}\n\t\tListNode cursor = head;\n\t\tListNode tail = null;\n\t\tint length = 1;\n\t\twhile (cursor.next != null) {\n\t\t\tcursor = cursor.next;\n\t\t\tlength++;\n\t\t}\n\t\tint loop = length - (k % length);\n\t\ttail = cursor;\n\t\tcursor.next = head;\n\t\tcursor = head;\n\t\tfor (int i = 0; i < loop; i++) {\n\t\t\tcursor = cursor.next;\n\t\t\ttail = tail.next;\n\t\t}\n\t\ttail.next = null;\n\t\treturn cursor;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef rotateRight(self, head, k):\n\t\t\"\"\"\n\t\t:type head: ListNode\n\t\t:type k: int\n\t\t:rtype: ListNode\n\t\t\"\"\"\n\t\tif not head or k == 0:\n\t\t\treturn head\n\t\tslow = fast = head\n\t\tlength = 1\n\t\twhile k and fast.next:\n\t\t\tfast = fast.next\n\t\t\tlength += 1\n\t\t\tk -= 1\n\t\tif k != 0:\n\t\t\tk = (k + length - 1) % length \n\t\t\treturn self.rotateRight(head, k)\n\t\telse:\n\t\t\twhile fast.next:\n\t\t\t\tfast = fast.next\n\t\t\t\tslow = slow.next\n\t\t\treturn self.rotate(head, fast, slow)\n\tdef rotate(self, head, fast, slow):\n\t\tfast.next = head\n\t\thead = slow.next\n\t\tslow.next = None\n\t\treturn head\n# %%\nl = LinkList()\nlist1 = [0,1,2]\nk = 4\nl1 = l.initList(list1)\ns = Solution()\nprint(l.convert_list(s.rotateRight(l1, k)))",
"status": 1,
"keywords": "链表,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104336132"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/60/60_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/60/60_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/60/60_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 62,
"question_title": "不同路径",
"difficulty": "中等",
"question_content": "<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>\n<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>\n<p>问总共有多少条不同的路径?</p>\n<p> </p>\n<p><strong>示例 1:</strong></p><img\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0062.Unique%20Paths/images/robot_maze.png\" />\n<pre><strong>输入:</strong>m = 3, n = 7<strong><br />输出:</strong>28</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>m = 3, n = 2<strong><br />输出:</strong>3<strong><br />解释:</strong>从左上角开始,总共有 3 条路径可以到达右下角。<br />1. 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右<br />3. 向下 -> 向右 -> 向下</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>m = 7, n = 3<strong><br />输出:</strong>28</pre>\n<p><strong>示例 4:</strong></p>\n<pre><strong>输入:</strong>m = 3, n = 3<strong><br />输出:</strong>6</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 <= m, n <= 100</code></li>\n <li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600469822",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstatic int uniquePaths(int m, int n)\n{\n\tint row, col;\n\tint *grids = malloc(m * n * sizeof(int));\n\tfor (col = 0; col < m; col++)\n\t{\n\t\tgrids[col] = 1;\n\t}\n\tfor (row = 0; row < n; row++)\n\t{\n\t\tgrids[row * m] = 1;\n\t}\n\tfor (row = 1; row < n; row++)\n\t{\n\t\tfor (col = 1; col < m; col++)\n\t\t{\n\t\t\tgrids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];\n\t\t}\n\t}\n\treturn grids[m * n - 1];\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test m n\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%d\\n\", uniquePaths(atoi(argv[1]), atoi(argv[2])));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int uniquePaths(int m, int n) {\n\t\tint[][] route = new int[m][n];\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (i == 0 || j == 0) {\n\t\t\t\t\troute[i][j] = 1;\n\t\t\t\t} else {\n\t\t\t\t\troute[i][j] = route[i - 1][j] + route[i][j - 1];\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn route[m - 1][n - 1];\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef uniquePaths(self, m, n):\n\t\t\"\"\"\n\t\t:type m: int\n\t\t:type n: int\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tdmap = [[0] * n for _ in range(m)]\n\t\tfor i in range(m):\n\t\t\tdmap[i][0] = 1\n\t\tfor j in range(n):\n\t\t\tdmap[0][j] = 1\n\t\tfor i in range(1, m):\n\t\t\tfor j in range(1, n):\n\t\t\t\tl = u = 0\n\t\t\t\tif i-1 >= 0:\n\t\t\t\t\tu = dmap[i-1][j]\n\t\t\t\tif j-1>= 0:\n\t\t\t\t\tl = dmap[i][j-1]\n\t\t\t\tdmap[i][j] = l + u\n\t\treturn dmap[m-1][n-1]\n# %%\ns = Solution()\nprint(s.uniquePaths(m = 3, n = 7))",
"status": 1,
"keywords": "数学,动态规划,组合数学",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/61/61_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/61/61_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/61/61_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 63,
"question_title": "不同路径 II",
"difficulty": "中等",
"question_content": "<p>一个机器人位于一个 <em>m x n </em>网格的左上角 (起始点在下图中标记为“Start” )。</p>\n<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。</p>\n<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>\n<p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot_maze.png\"\n style=\"height: 183px; width: 400px;\" /></p>\n<p>网格中的障碍物和空位置分别用 <code>1</code> 和 <code>0</code> 来表示。</p>\n<p> </p>\n<p><strong>示例 1:</strong></p><img alt=\"\"\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot1.jpg\"\n style=\"width: 242px; height: 242px;\" />\n<pre><strong>输入:</strong>obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]<strong><br />输出:</strong>2<strong><br />解释:</strong>3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:<br />1. 向右 -> 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右 -> 向右</pre>\n<p><strong>示例 2:</strong></p><img alt=\"\"\n src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot2.jpg\"\n style=\"width: 162px; height: 162px;\" />\n<pre><strong>输入:</strong>obstacleGrid = [[0,1],[0,0]]<strong><br />输出:</strong>1</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>m == obstacleGrid.length</code></li>\n <li><code>n == obstacleGrid[i].length</code></li>\n <li><code>1 <= m, n <= 100</code></li>\n <li><code>obstacleGrid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470123",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstatic int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize)\n{\n\tint row, col;\n\tint reset = 0;\n\tfor (row = 0; row < obstacleGridRowSize; row++)\n\t{\n\t\tif (reset)\n\t\t{\n\t\t\tobstacleGrid[row][0] = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (obstacleGrid[row][0] == 1)\n\t\t\t{\n\t\t\t\treset = 1;\n\t\t\t}\n\t\t}\n\t}\n\treset = 0;\n\tfor (col = 0; col < obstacleGridColSize; col++)\n\t{\n\t\tif (reset)\n\t\t{\n\t\t\tobstacleGrid[0][col] = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (obstacleGrid[0][col] == 1)\n\t\t\t{\n\t\t\t\treset = 1;\n\t\t\t}\n\t\t}\n\t}\n\tfor (row = 0; row < obstacleGridRowSize; row++)\n\t{\n\t\tint *line = obstacleGrid[row];\n\t\tfor (col = 0; col < obstacleGridColSize; col++)\n\t\t{\n\t\t\tline[col] ^= 1;\n\t\t}\n\t}\n\tfor (row = 1; row < obstacleGridRowSize; row++)\n\t{\n\t\tint *last_line = obstacleGrid[row - 1];\n\t\tint *line = obstacleGrid[row];\n\t\tfor (col = 1; col < obstacleGridColSize; col++)\n\t\t{\n\t\t\tif (line[col] != 0)\n\t\t\t{\n\t\t\t\tline[col] = line[col - 1] + last_line[col];\n\t\t\t}\n\t\t}\n\t}\n\treturn obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1];\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test m n\\n\");\n\t\texit(-1);\n\t}\n\tint i, j, k = 3;\n\tint row_size = atoi(argv[1]);\n\tint col_size = atoi(argv[2]);\n\tint **grids = malloc(row_size * sizeof(int *));\n\tfor (i = 0; i < row_size; i++)\n\t{\n\t\tgrids[i] = malloc(col_size * sizeof(int));\n\t\tint *line = grids[i];\n\t\tfor (j = 0; j < col_size; j++)\n\t\t{\n\t\t\tline[j] = atoi(argv[k++]);\n\t\t\tprintf(\"%d \", line[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tprintf(\"%d\\n\", uniquePathsWithObstacles(grids, row_size, col_size));\n\treturn 0;\n}",
"java": "public class Solution {\n\tpublic int uniquePathsWithObstacles(int[][] obstacleGrid) {\n\t\tint m = obstacleGrid.length;\n\t\tint n = obstacleGrid[0].length;\n\t\tif (obstacleGrid[0][0] == 1) {\n\t\t\treturn 0;\n\t\t} else if (m == 1 && n == 1) {\n\t\t\treturn 1;\n\t\t}\n\t\tint[][] paths = new int[m][n];\n\t\tfor (int i = 0; i < m; ++i) {\n\t\t\tif (obstacleGrid[i][0] == 1) {\n\t\t\t\twhile (i < m) {\n\t\t\t\t\tpaths[i][0] = 0;\n\t\t\t\t\t++i;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} else {\n\t\t\t\tpaths[i][0] = 1;\n\t\t\t}\n\t\t}\n\t\tfor (int j = 1; j < n; ++j) {\n\t\t\tif (obstacleGrid[0][j] == 1) {\n\t\t\t\twhile (j < n) {\n\t\t\t\t\tpaths[0][j] = 0;\n\t\t\t\t\t++j;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} else {\n\t\t\t\tpaths[0][j] = 1;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i < m; ++i)\n\t\t\tfor (int j = 1; j < n; ++j) {\n\t\t\t\tif (obstacleGrid[i][j] == 1) {\n\t\t\t\t\tpaths[i][j] = 0;\n\t\t\t\t} else {\n\t\t\t\t\tpaths[i][j] = paths[i][j - 1] + paths[i - 1][j];\n\t\t\t\t}\n\t\t\t}\n\t\treturn paths[m - 1][n - 1];\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef uniquePathsWithObstacles(self, obstacleGrid):\n\t\tm, n = len(obstacleGrid), len(obstacleGrid[0])\n\t\tif m == 0:\n\t\t\treturn 0\n\t\tdmap = [[0] * (n + 1) for _ in range(m + 1)]\n\t\tdmap[m - 1][n] = 1\n\t\tfor i in range(m - 1, -1, -1):\n\t\t\tfor j in range(n - 1, -1, -1):\n\t\t\t\tif obstacleGrid[i][j] == 1:\n\t\t\t\t\tdmap[i][j] = 0\n\t\t\t\telse:\n\t\t\t\t\tdmap[i][j] = dmap[i][j + 1] + dmap[i + 1][j]\n\t\treturn dmap[0][0]\n# %%\ns = Solution()\nprint(s.uniquePathsWithObstacles(obstacleGrid = [[0,1],[0,0]]))",
"status": 1,
"keywords": "数组,动态规划,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/lilong_dream/article/details/19931301"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/62/62_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/62/62_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/62/62_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 64,
"question_title": "最小路径和",
"difficulty": "中等",
"question_content": "<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == grid.length</code></li>\t<li><code>n == grid[i].length</code></li>\t<li><code>1 <= m, n <= 200</code></li>\t<li><code>0 <= grid[i][j] <= 100</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470227",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic inline int min(int a, int b)\n{\n\treturn a < b ? a : b;\n}\nint minPathSum(int **grid, int gridRowSize, int gridColSize)\n{\n\tint i, j;\n\tint **dp = malloc(gridRowSize * sizeof(int *));\n\tfor (i = 0; i < gridRowSize; i++)\n\t{\n\t\tdp[i] = malloc(gridColSize * sizeof(int));\n\t}\n\tdp[0][0] = grid[0][0];\n\tint sum = dp[0][0];\n\tfor (i = 1; i < gridRowSize; i++)\n\t{\n\t\tsum += grid[i][0];\n\t\tdp[i][0] = sum;\n\t}\n\tsum = dp[0][0];\n\tfor (i = 1; i < gridColSize; i++)\n\t{\n\t\tsum += grid[0][i];\n\t\tdp[0][i] = sum;\n\t}\n\tfor (i = 1; i < gridRowSize; i++)\n\t{\n\t\tfor (j = 1; j < gridColSize; j++)\n\t\t{\n\t\t\tdp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);\n\t\t}\n\t}\n\treturn dp[gridRowSize - 1][gridColSize - 1];\n}\nint main(int argc, char **argv)\n{\n\tint i, j;\n\tint row = argc - 1;\n\tint col = strlen(argv[1]);\n\tint **grid = malloc(row * sizeof(int *));\n\tfor (i = 0; i < row; i++)\n\t{\n\t\tgrid[i] = malloc(col * sizeof(int));\n\t\tfor (j = 0; j < col; j++)\n\t\t{\n\t\t\tgrid[i][j] = argv[i + 1][j] - '0';\n\t\t\tprintf(\"%d \", grid[i][j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tprintf(\"%d\\n\", minPathSum(grid, row, col));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int minPathSum(int[][] grid) {\n\t\tint m = grid.length;\n\t\tint n = grid[0].length;\n\n\t\tint sum = 0;\n\n\t\tif (m < 1 || n < 1)\n\t\t\treturn 0;\n\t\tif (m == 1) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tsum = sum + grid[0][i];\n\t\t\t}\n\t\t\treturn sum;\n\t\t}\n\t\tif (n == 1) {\n\t\t\tfor (int i = 0; i < m; i++) {\n\t\t\t\tsum = sum + grid[i][0];\n\t\t\t}\n\t\t\treturn sum;\n\t\t}\n\n\t\tint[][] dp = new int[m][n];\n\t\tdp[0][0] = grid[0][0];\n\n\t\tfor (int k = 1; k < m; k++) {\n\t\t\tdp[k][0] = grid[k][0] + dp[k - 1][0];\n\t\t}\n\n\t\tfor (int l = 1; l < n; l++) {\n\t\t\tdp[0][l] = grid[0][l] + dp[0][l - 1];\n\t\t}\n\n\t\tfor (int k = 1; k < m; k++) {\n\t\t\tfor (int l = 1; l < n; l++) {\n\t\t\t\tdp[k][l] = grid[k][l] + Math.min(dp[k - 1][l], dp[k][l - 1]);\n\t\t\t}\n\t\t}\n\t\treturn dp[m - 1][n - 1];\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef minPathSum(self, grid):\n\t\t\"\"\"\n\t\t:type grid: List[List[int]]\n\t\t:rtype: int\n\t\t\"\"\"\n\t\theight = len(grid)\n\t\tif height == 0:\n\t\t\treturn 0\n\t\twidth = len(grid[0])\n\t\tpathmap = []\n\t\tfor i in range(height):\n\t\t\tpathmap.append([100000000000] * width)\n\t\tpathmap[0][0] = grid[0][0]\n\t\tfor i in range(height):\n\t\t\tfor j in range(width):\n\t\t\t\tcompare = [pathmap[i][j]]\n\t\t\t\tif i - 1 >= 0:\n\t\t\t\t\tcompare.append(pathmap[i - 1][j] + grid[i][j])\n\t\t\t\tif j - 1 >= 0:\n\t\t\t\t\tcompare.append(pathmap[i][j - 1] + grid[i][j])\n\t\t\t\tpathmap[i][j] = min(compare)\n\t\treturn pathmap[-1][-1]\n# %%\ns = Solution()\nprint(s.minPathSum(grid = [[1,3,1],[1,5,1],[4,2,1]]))",
"status": 1,
"keywords": "数组,动态规划,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/63/63_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/63/63_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/63/63_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 65,
"question_title": "有效数字",
"difficulty": "困难",
"question_content": "<p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol>\t<li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li>\t<li>(可选)一个 <code>'e'</code> 或 <code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol>\t<li>(可选)一个符号字符(<code>'+'</code> 或 <code>'-'</code>)</li>\t<li>下述格式之一:\t<ol>\t\t<li>至少一位数字,后面跟着一个点 <code>'.'</code></li>\t\t<li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li>\t\t<li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li>\t</ol>\t</li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol>\t<li>(可选)一个符号字符(<code>'+'</code> 或 <code>'-'</code>)</li>\t<li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul>\t<li><code>[\"2\", \"0089\", \"-0.1\", \"+3.14\", \"4.\", \"-.9\", \"2e10\", \"-90E3\", \"3e+7\", \"+6e-1\", \"53.5e93\", \"-123.456e789\"]</code></li></ul><p>部分无效数字列举如下:</p><ul>\t<li><code>[\"abc\", \"1a\", \"1e\", \"e3\", \"99e2.5\", \"--6\", \"-+3\", \"95a54e53\"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code> 。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"0\"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"e\"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \".\"<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \".1\"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length <= 20</code></li>\t<li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code> 。</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470924",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <ctype.h>\nstatic bool isNumber(const char *s)\n{\n\twhile (*s == ' ')\n\t\t++s;\n\tbool if_find_num = false;\n\tif (*s == '-' || *s == '+')\n\t\t++s;\n\twhile (isdigit(*s))\n\t{\n\t\tif_find_num = true;\n\t\t++s;\n\t}\n\tif (*s == '.')\n\t\t++s;\n\twhile (isdigit(*s))\n\t{\n\t\tif_find_num = true;\n\t\t++s;\n\t}\n\tif (if_find_num == true && *s == 'e')\n\t{\n\t\t++s;\n\t\tif (*s == '+' || *s == '-')\n\t\t\t++s;\n\t\tif_find_num = false;\n\t\twhile (isdigit(*s))\n\t\t{\n\t\t\tif_find_num = true;\n\t\t\t++s;\n\t\t}\n\t}\n\twhile (*s == ' ')\n\t\t++s;\n\treturn *s == '\\0' && if_find_num == true;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test number\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", isNumber(argv[1]) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tchar[] chars;\n\tboolean point = false;\n\tboolean exponent = false;\n\tpublic boolean isNumber(String s) {\n\t\ts = s.trim();\n\t\tint length = s.length();\n\t\tif (length == 0) {\n\t\t\treturn false;\n\t\t}\n\t\tchars = s.toCharArray();\n\t\tString[] ss = s.split(\"e\");\n\t\tif (ss.length == 0) {\n\t\t\treturn false;\n\t\t}\n\t\tif (ss[0].length() == 0)\n\t\t\treturn false;\n\t\tif (ss[0].length() < length)\n\t\t\texponent = true;\n\t\tif (ss[0].length() == length - 1) {\n\t\t\treturn false;\n\t\t}\n\t\tString[] pre = ss[0].split(\"\\\\.\");\n\t\tif (pre.length == 0) {\n\t\t\treturn false;\n\t\t}\n\t\tif (pre[0].length() < ss[0].length())\n\t\t\tpoint = true;\n\t\tboolean result = pre(0, pre[0].length());\n\t\tresult = result && middle(pre[0].length() + 1, ss[0].length());\n\t\tif (exponent) {\n\t\t\tresult = result && is(ss[0].length() + 1, length);\n\t\t}\n\t\treturn result;\n\t}\n\tpublic boolean pre(int i, int length) {\n\t\tif (i >= length) {\n\t\t\treturn true;\n\t\t}\n\t\tif (chars[i] == '+' || chars[i] == '-') {\n\t\t\ti++;\n\t\t}\n\t\tif (i == length && !point) {\n\t\t\treturn false;\n\t\t}\n\t\tfor (; i < length; i++) {\n\t\t\tif (chars[i] < '0' || chars[i] > '9') {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\tpublic boolean middle(int i, int length) {\n\t\tif (i >= length && point) {\n\t\t\tif (chars[i - 2] >= '0' && chars[i - 2] <= '9') {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t\tfor (; i < length; i++) {\n\t\t\tif (chars[i] < '0' || chars[i] > '9') {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\tpublic boolean is(int i, int length) {\n\t\tif (i == 1) {\n\t\t\treturn false;\n\t\t}\n\t\tif (chars[i] == '+' || chars[i] == '-') {\n\t\t\ti++;\n\t\t}\n\t\tif (i == length) {\n\t\t\treturn false;\n\t\t}\n\t\tfor (; i < length; i++) {\n\t\t\tif (chars[i] < '0' || chars[i] > '9') {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef isNumber(self, s):\n\t\ts = s.strip()\n\t\tls, pos = len(s), 0\n\t\tif ls == 0:\n\t\t\treturn False\n\t\tif s[pos] == '+' or s[pos] == '-':\n\t\t\tpos += 1\n\t\tisNumeric = False\n\t\twhile pos < ls and s[pos].isdigit():\n\t\t\tpos += 1\n\t\t\tisNumeric = True\n\t\tif pos < ls and s[pos] == '.':\n\t\t\tpos += 1\n\t\t\twhile pos < ls and s[pos].isdigit():\n\t\t\t\tpos += 1\n\t\t\t\tisNumeric = True\n\t\telif pos < ls and s[pos] == 'e' and isNumeric:\n\t\t\tisNumeric = False\n\t\t\tpos += 1\n\t\t\tif pos < ls and (s[pos] == '+' or s[pos] == '-'):\n\t\t\t\tpos += 1\n\t\t\twhile pos < ls and s[pos].isdigit():\n\t\t\t\tpos += 1\n\t\t\t\tisNumeric = True\n\t\tif pos == ls and isNumeric:\n\t\t\treturn True\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isNumber(s = \"0\"))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104340910"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/64/64_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/64/64_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/64/64_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 66,
"question_title": "加一",
"difficulty": "简单",
"question_content": "<p>给定一个由 <strong>整数 </strong>组成的<strong> 非空</strong> 数组所表示的非负整数,在该数的基础上加一。</p><p>最高位数字存放在数组的首位, 数组中每个元素只存储<strong>单个</strong>数字。</p><p>你可以假设除了整数 0 之外,这个整数不会以零开头。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = [1,2,3]<strong><br />输出:</strong>[1,2,4]<strong><br />解释:</strong>输入数组表示数字 123。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = [4,3,2,1]<strong><br />输出:</strong>[4,3,2,2]<strong><br />解释:</strong>输入数组表示数字 4321。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = [0]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= digits.length <= 100</code></li>\t<li><code>0 <= digits[i] <= 9</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469823",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> plusOne(vector<int> &digits)\n\t{\n\t\tint carry = 1;\n\t\tvector<int> res;\n\t\tfor (int i = digits.size() - 1; i >= 0; i--)\n\t\t{\n\t\t\tint d = digits[i] + carry;\n\t\t\tres.push_back(d % 10);\n\t\t\tcarry = d / 10;\n\t\t}\n\t\tif (carry > 0)\n\t\t{\n\t\t\tres.push_back(carry);\n\t\t}\n\t\treverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n};",
"java": "class Solution {\n\tpublic int[] plusOne(int[] digits) {\n\t\tint i = digits.length - 1;\n\t\twhile (i >= 0 && (digits[i] = digits[i] + 1) == 10) {\n\t\t\tdigits[i] = 0;\n\t\t\ti--;\n\t\t}\n\t\tif (digits[0] == 0) {\n\t\t\tint[] temp = new int[digits.length + 1];\n\t\t\ttemp[0] = 1;\n\t\t\treturn temp;\n\t\t}\n\t\treturn digits;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef plusOne(self, digits):\n\t\tls = len(digits)\n\t\tfor index in reversed(range(ls)):\n\t\t\tif digits[index] < 9:\n\t\t\t\tdigits[index] += 1\n\t\t\t\treturn digits\n\t\t\telse:\n\t\t\t\tdigits[index] = 0\n\t\tdigits.insert(0, 1)\n\t\treturn digits\n# %%\ns = Solution()\nprint(s.plusOne(digits = [1,2,3]))",
"status": 1,
"keywords": "数组,数学",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/65/65_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 67,
"question_title": "二进制求和",
"difficulty": "简单",
"question_content": "<p>给你两个二进制字符串,返回它们的和(用二进制表示)。</p><p>输入为 <strong>非空 </strong>字符串且只包含数字&nbsp;<code>1</code>&nbsp;和&nbsp;<code>0</code>。</p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> a = &quot;11&quot;, b = &quot;1&quot;<strong><br />输出:</strong> &quot;100&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> a = &quot;1010&quot;, b = &quot;1011&quot;<strong><br />输出:</strong> &quot;10101&quot;</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>\t<li>每个字符串仅由字符 <code>&#39;0&#39;</code> 或 <code>&#39;1&#39;</code> 组成。</li>\t<li><code>1 &lt;= a.length, b.length &lt;= 10^4</code></li>\t<li>字符串如果不是 <code>&quot;0&quot;</code> ,就都不含前导零。</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471009",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tstring addBinary(string a, string b)\n\t{\n\t\tstring res;\n\t\tint carry = 0;\n\t\tint i = a.length() - 1;\n\t\tint j = b.length() - 1;\n\t\tfor (; i >= 0 && j >= 0; i--, j--)\n\t\t{\n\t\t\tif (a[i] == '1' && b[j] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t}\n\t\t\t\tcarry = 1;\n\t\t\t}\n\t\t\telse if (a[i] == '0' && b[j] == '0')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t}\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (i >= 0)\n\t\t{\n\t\t\tif (a[i--] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres.push_back(carry + '0');\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t}\n\t\twhile (j >= 0)\n\t\t{\n\t\t\tif (b[j--] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres.push_back(carry + '0');\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t}\n\t\tif (carry > 0)\n\t\t{\n\t\t\tres.push_back('1');\n\t\t}\n\t\treverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n};",
"java": "class Solution {\n\tpublic String addBinary(String a, String b) {\n\n\t\tStringBuffer s1 = new StringBuffer(a);\n\t\ts1.reverse();\n\t\tStringBuffer s2 = new StringBuffer(b);\n\t\ts2.reverse();\n\n\t\tif (s1.length() > s2.length()) {\n\t\t\tint n = s1.length() - s2.length();\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\ts2.append('0');\n\t\t\t}\n\t\t} else if (s1.length() < s2.length()) {\n\t\t\tint n = s2.length() - s1.length();\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\ts1.append('0');\n\t\t\t}\n\t\t}\n\n\t\tStringBuffer stringBuffer = new StringBuffer(\"\");\n\t\tint i = 0;\n\t\tchar flag = '0';\n\t\twhile (i < s1.length() && i < s2.length()) {\n\t\t\tif (flag == '0') {\n\t\t\t\tif (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t} else {\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {\n\t\t\t\t\tflag = '0';\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t} else {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tif (flag == '1') {\n\t\t\tstringBuffer.append(flag);\n\t\t}\n\t\tstringBuffer.reverse();\n\t\treturn stringBuffer.toString();\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef addBinary(self, a, b):\n\t\tres = ''\n\t\tlsa, lsb = len(a), len(b)\n\t\tpos, plus, curr = -1, 0, 0\n\t\twhile (lsa + pos) >= 0 or (lsb + pos) >= 0:\n\t\t\tif (lsa + pos) >= 0:\n\t\t\t\tcurr += int(a[pos])\n\t\t\tif (lsb + pos) >= 0:\n\t\t\t\tcurr += int(b[pos])\n\t\t\tres = str(curr % 2) + res\n\t\t\tcurr //= 2\n\t\t\tpos -= 1\n\t\tif curr == 1:\n\t\t\tres = '1' + res\n\t\treturn res\n# %%\ns = Solution()\nprint(s.addBinary(a = \"1010\", b = \"1011\"))",
"status": 1,
"keywords": "位运算,数学,字符串,模拟",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/66/66_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/66/66_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/66/66_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 68,
"question_title": "文本左右对齐",
"difficulty": "困难",
"question_content": "<div class=\"notranslate\">\n <p>给定一个单词数组和一个长度&nbsp;<em>maxWidth</em>,重新排版单词,使其成为每行恰好有&nbsp;<em>maxWidth</em>&nbsp;个字符,且左右两端对齐的文本。</p>\n\n <p>你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格&nbsp;<code>' '</code>&nbsp;填充,使得每行恰好有 <em>maxWidth</em>&nbsp;个字符。\n </p>\n\n <p>要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。</p>\n\n <p>文本的最后一行应为左对齐,且单词之间不插入<strong>额外的</strong>空格。</p>\n\n <p><strong>说明:</strong></p>\n\n <ul>\n <li>单词是指由非空格字符组成的字符序列。</li>\n <li>每个单词的长度大于 0,小于等于&nbsp;<em>maxWidth</em>。</li>\n <li>输入单词数组 <code>words</code>&nbsp;至少包含一个单词。</li>\n </ul>\n\n <p><strong>示例:</strong></p>\n\n <pre><strong>输入:</strong>\n words = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\n maxWidth = 16\n<strong>输出:</strong>\n [\n &nbsp; &nbsp;\"This &nbsp; &nbsp;is &nbsp; &nbsp;an\",\n &nbsp; &nbsp;\"example &nbsp;of text\",\n &nbsp; &nbsp;\"justification. &nbsp;\"\n ]\n </pre>\n\n <p><strong>示例&nbsp;2:</strong></p>\n\n <pre><strong>输入:</strong>\n words = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\n maxWidth = 16\n<strong>输出:</strong>\n [\n &nbsp; \"What &nbsp; must &nbsp; be\",\n &nbsp; \"acknowledgment &nbsp;\",\n &nbsp; \"shall be &nbsp; &nbsp; &nbsp; &nbsp;\"\n ]\n<strong>解释: </strong>注意最后一行的格式应为 \"shall be \" 而不是 \"shall be\"\n 因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。\n </pre>\n\n <p><strong>示例&nbsp;3:</strong></p>\n\n <pre><strong>输入:</strong>\n words = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n &nbsp; \"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\n maxWidth = 20\n<strong>输出:</strong>\n [\n &nbsp; \"Science &nbsp;is &nbsp;what we\",\n \"understand &nbsp; &nbsp; &nbsp;well\",\n &nbsp; \"enough to explain to\",\n &nbsp; \"a &nbsp;computer. &nbsp;Art is\",\n &nbsp; \"everything &nbsp;else &nbsp;we\",\n &nbsp; \"do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"\n ]\n </pre>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470798",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic void line_fill(char *line, int len, char **words, int *word_lens, int max_size,\n\t\t\t\t\t int even_spaces, int remain_spaces, int start, int end)\n{\n\tint i, j;\n\tchar *p = line;\n\tfor (i = start; i < end; i++)\n\t{\n\t\tmemcpy(p, words[i], word_lens[i]);\n\t\tp += word_lens[i];\n\t\tif (i < end - 1)\n\t\t{\n\t\t\tfor (j = 0; j < even_spaces; j++)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t\tif (remain_spaces > 0)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t\tremain_spaces--;\n\t\t\t}\n\t\t}\n\t}\n\twhile (p - line < max_size)\n\t{\n\t\t*p++ = ' ';\n\t}\n\t*p++ = '\\0';\n}\nstatic char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize)\n{\n\tint i, j, k, cap = 100, count = 0;\n\tchar **lines = malloc(cap * sizeof(char *));\n\tchar *buf = malloc(cap * (maxWidth + 1));\n\tfor (i = 0; i < cap; i++)\n\t{\n\t\tlines[i] = buf + i * (maxWidth + 1);\n\t}\n\tint *word_lens = malloc(wordsSize * sizeof(int));\n\tfor (i = 0; i < wordsSize; i++)\n\t{\n\t\tword_lens[i] = strlen(words[i]);\n\t}\n\tint wc = 0;\n\tint len = 0;\n\tint start = 0;\n\tint chars = 0;\n\tfor (i = 0, j = 0; i < wordsSize; i++)\n\t{\n\t\tif (len + word_lens[i] > maxWidth)\n\t\t{\n\t\t\tint even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);\n\t\t\tint remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);\n\t\t\tcount++;\n\t\t\twc = 1;\n\t\t\tlen = word_lens[i] + 1;\n\t\t\tchars = word_lens[i];\n\t\t\tstart = i;\n\t\t}\n\t\telse if (len + word_lens[i] == maxWidth)\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tint even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;\n\t\t\tint remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);\n\t\t\tcount++;\n\t\t\twc = 0;\n\t\t\tlen = 0;\n\t\t\tchars = 0;\n\t\t\tstart = i + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tlen += word_lens[i] + 1;\n\t\t\twc++;\n\t\t}\n\t}\n\tif (wc > 0)\n\t{\n\t\tchar *p = lines[count];\n\t\tfor (i = start; i < start + wc; i++)\n\t\t{\n\t\t\tmemcpy(p, words[i], word_lens[i]);\n\t\t\tp += word_lens[i];\n\t\t\tif (i < start + wc - 1)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t}\n\t\twhile (p - lines[count] < maxWidth)\n\t\t{\n\t\t\t*p++ = ' ';\n\t\t}\n\t\t*p++ = '\\0';\n\t\tcount++;\n\t}\n\t*returnSize = count;\n\treturn lines;\n}\nint main(int argc, char **argv)\n{\n\tif (argc <= 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test maxsize words...\\n\");\n\t\texit(-1);\n\t}\n\tint i, count;\n\tchar **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%s\\n\", lines[i]);\n\t}\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic List<String> fullJustify(String[] words, int maxWidth) {\n\t\tList<String> ret = new ArrayList<>();\n\t\tint index = 0;\n\t\twhile (index < words.length) {\n\t\t\tint cur = index, len = 0;\n\t\t\twhile (cur < words.length && len + words[cur].length() + cur - index <= maxWidth) {\n\t\t\t\tlen = len + words[cur++].length();\n\t\t\t}\n\t\t\tcur--;\n\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\tif (cur == words.length - 1) {\n\t\t\t\tfor (int i = index; i <= cur; i++) {\n\t\t\t\t\tsb.append(words[i]);\n\t\t\t\t\tif (i < cur) {\n\t\t\t\t\t\tsb.append(' ');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tint base = cur > index ? (maxWidth - len) / (cur - index) : (maxWidth - len);\n\t\t\t\tString baseStr = genSpace(base);\n\t\t\t\tint left = cur > index ? (maxWidth - len) % (cur - index) : 0;\n\t\t\t\tString leftStr = genSpace(base + 1);\n\t\t\t\tfor (int i = index; i <= cur; i++) {\n\t\t\t\t\tsb.append(words[i]);\n\t\t\t\t\tif (i < cur) {\n\t\t\t\t\t\tsb.append(left > 0 ? leftStr : baseStr);\n\t\t\t\t\t\tleft--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (sb.length() < maxWidth) {\n\t\t\t\tsb.append(genSpace(maxWidth - sb.length()));\n\t\t\t}\n\t\t\tret.add(sb.toString());\n\t\t\tindex = cur + 1;\n\t\t}\n\t\treturn ret;\n\t}\n\tprivate String genSpace(int n) {\n\t\tchar[] cs = new char[n];\n\t\tArrays.fill(cs, ' ');\n\t\treturn new String(cs);\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef fullJustify(self, words, maxWidth):\n\t\t\"\"\"\n\t\t:type words: List[str]\n\t\t:type maxWidth: int\n\t\t:rtype: List[str]\n\t\t\"\"\"\n\t\tres = []\n\t\tres_list = []\n\t\tcurr = []\n\t\tcount, pos = 0, 0\n\t\twhile pos < len(words):\n\t\t\tword = words[pos]\n\t\t\tif len(word) > maxWidth:\n\t\t\t\tpos += 1\n\t\t\tif len(word) + count + len(curr)<= maxWidth:\n\t\t\t\tcount += len(word)\n\t\t\t\tcurr.append(word)\n\t\t\t\tpos += 1\n\t\t\telse:\n\t\t\t\tres_list.append(curr)\n\t\t\t\tcurr = []\n\t\t\t\tcount = 0\n\t\tif len(curr) > 0:\n\t\t\tres_list.append(curr)\n\t\tfor index, curr in enumerate(res_list):\n\t\t\ttext = ''\n\t\t\tremain = sum([len(t) for t in curr])\n\t\t\tif len(curr) == 1:\n\t\t\t\ttext = curr[0] + ' ' * (maxWidth - remain)\n\t\t\telif index == len(res_list) - 1:\n\t\t\t\ttext = ' '.join(curr)\n\t\t\t\ttext += ' ' * (maxWidth - remain - len(curr) + 1)\n\t\t\telse:\n\t\t\t\tstep = (maxWidth - remain) / (len(curr) - 1 )\n\t\t\t\textra = (maxWidth - remain) % (len(curr) - 1 )\n\t\t\t\tfor index in range(len(curr) - 1):\n\t\t\t\t\ttext += curr[index] + ' ' * int(step)\n\t\t\t\t\tif extra > 0:\n\t\t\t\t\t\ttext += ' '\n\t\t\t\t\t\textra -= 1\n\t\t\t\ttext += curr[-1]\n\t\t\tres.append(text)\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.fullJustify([\"Don't\",\"go\",\"around\",\"saying\",\"the\",\"world\",\"owes\",\"you\",\"a\",\"living;\",\"the\",\"world\",\"owes\",\"you\",\"nothing;\",\"it\",\"was\",\"here\",\"first.\"],30))",
"status": 1,
"keywords": "字符串,模拟",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104341710"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 69,
"question_title": "x 的平方根",
"difficulty": "简单",
"question_content": "<p>实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>\n<p>计算并返回&nbsp;<em>x</em>&nbsp;的平方根,其中&nbsp;<em>x </em>是非负整数。</p>\n<p>由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。</p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong> 4<strong><br />输出:</strong> 2</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong> 8<strong><br />输出:</strong> 2<strong><br />说明:</strong> 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470124",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint mySqrt(int x)\n\t{\n\t\tif (x == 0)\n\t\t{\n\t\t\treturn 0;\n\t\t}\n\t\tunsigned int lo = 1, hi = x;\n\t\tunsigned int mid = (lo + hi) / 2;\n\t\tfor (;;)\n\t\t{\n\t\t\tif (mid > x / mid)\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (mid + 1 > x / (mid + 1))\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlo = mid;\n\t\t\t\t}\n\t\t\t}\n\t\t\tmid = (lo + hi) / 2;\n\t\t}\n\t\treturn mid;\n\t}\n};",
"java": "class Solution {\n\tpublic int mySqrt(int x) {\n\t\tint left = 0, right = 46340;\n\t\twhile (left < right) {\n\t\t\tint mid = (left + right) / 2;\n\t\t\tif (mid * mid < x)\n\t\t\t\tleft = mid + 1;\n\t\t\telse if (mid * mid > x)\n\t\t\t\tif ((mid - 1) * (mid - 1) <= x)\n\t\t\t\t\treturn mid - 1;\n\t\t\t\telse\n\t\t\t\t\tright = mid - 1;\n\t\t\telse\n\t\t\t\treturn mid;\n\t\t}\n\t\tif (left * left > x)\n\t\t\treturn left - 1;\n\t\treturn left;\n\t}\n}\n",
"js": "",
"python": "class Solution:\n\tdef mySqrt(self, x):\n\t\tif x == 0:\n\t\t\treturn 0\n\t\tif x < 4:\n\t\t\treturn 1\n\t\tres = 2 * self.mySqrt(x / 4)\n\t\tif (res + 1) * (res + 1) <= x and (res + 1) * (res + 1) >= 0:\n\t\t\treturn res + 1\n\t\treturn res\n# %%\ns = Solution()\nprint(s.mySqrt(4))",
"status": 1,
"keywords": "数学,二分查找",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/68/68_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/68/68_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/68/68_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 70,
"question_title": "爬楼梯",
"difficulty": "简单",
"question_content": "<p>假设你正在爬楼梯。需要 <em>n</em>&nbsp;阶你才能到达楼顶。</p><p>每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?</p><p><strong>注意:</strong>给定 <em>n</em> 是一个正整数。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> 2<strong><br />输出:</strong> 2<strong><br />解释:</strong> 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong> 3<strong><br />输出:</strong> 3<strong><br />解释:</strong> 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470125",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint climbStairs(int n)\n\t{\n\t\tint a = 1;\n\t\tint b = 2;\n\t\tint c = 0;\n\t\tfor (int i = 3; i <= n; i++)\n\t\t{\n\t\t\tc = a + b;\n\t\t\ta = b;\n\t\t\tb = c;\n\t\t}\n\t\treturn n == 1 ? a : (n == 2 ? b : c);\n\t}\n};",
"java": "class Solution {\n\tpublic int climbStairs(int n) {\n\t\tint[] num = new int[n + 1];\n\t\tnum[0] = 1;\n\t\tnum[1] = 1;\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tnum[i] = num[i - 1] + num[i - 2];\n\t\t}\n\t\treturn num[n];\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef climbStairs(self, n):\n\t\tif n <= 1:\n\t\t\treturn 1\n\t\tdp = [1] * 2\n\t\tfor i in range(2, n + 1):\n\t\t\tdp[1], dp[0] = dp[1] + dp[0], dp[1]\n\t\treturn dp[1]\n# %%\ns = Solution()\nprint(s.climbStairs(2))",
"status": 1,
"keywords": "记忆化搜索,数学,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/69/69_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/69/69_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/69/69_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 8,
"question_title": "字符串转换整数 (atoi)",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n\t<p>请你来实现一个&nbsp;<code>myAtoi(string s)</code>&nbsp;函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 <code>atoi</code> 函数)。</p>\n\n\t<p>函数&nbsp;<code>myAtoi(string s)</code> 的算法如下:</p>\n\n\t<ul>\n\t\t<li>读入字符串并丢弃无用的前导空格</li>\n\t\t<li>检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。</li>\n\t\t<li>读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。</li>\n\t\t<li>将前面步骤读入的这些数字转换为整数(即,\"123\" -&gt; 123, \"0032\" -&gt; 32)。如果没有读入数字,则整数为 <code>0</code> 。必要时更改符号(从步骤 2 开始)。</li>\n\t\t<li>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>− 1]</code>\n\t\t\t,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被固定为 <code>−2<sup>31</sup></code> ,大于\n\t\t\t<code>2<sup>31&nbsp;</sup>− 1</code> 的整数应该被固定为 <code>2<sup>31&nbsp;</sup>− 1</code> 。\n\t\t</li>\n\t\t<li>返回整数作为最终结果。</li>\n\t</ul>\n\n\t<p><strong>注意:</strong></p>\n\n\t<ul>\n\t\t<li>本题中的空白字符只包括空格字符 <code>' '</code> 。</li>\n\t\t<li>除前导空格或数字后的其余字符串外,<strong>请勿忽略</strong> 任何其他字符。</li>\n\t</ul>\n\n\t<p>&nbsp;</p>\n\n\t<p><strong>示例&nbsp;1:</strong></p>\n\n\t<pre><strong>输入:</strong>s = \"42\"\n<strong>输出:</strong>42\n<strong>解释:</strong>加粗的字符串为已经读入的字符,插入符号是当前读取的字符。\n第 1 步:\"42\"(当前没有读入字符,因为没有前导空格)\n\t\t ^\n第 2 步:\"42\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n\t\t ^\n第 3 步:\"<strong>42</strong>\"(读入 \"42\"\n\t\t ^\n解析得到整数 42 。\n由于 \"42\" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 42 。</pre>\n\n\t<p><strong>示例&nbsp;2:</strong></p>\n\n\t<pre><strong>输入:</strong>s = \" -42\"\n<strong>输出:</strong>-42\n<strong>解释:</strong>\n第 1 步:\" -42\"(读入前导空格,但忽视掉)\n\t\t\t^\n第 2 步:\" -42\"(读入 '-' 字符,所以结果应该是负数)\n\t\t\t ^\n第 3 步:\" -<strong>42</strong>\"(读入 \"42\"\n\t\t\t ^\n解析得到整数 -42 。\n由于 \"-42\" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 -42 。\n</pre>\n\n\t<p><strong>示例&nbsp;3:</strong></p>\n\n\t<pre><strong>输入:</strong>s = \"4193 with words\"\n<strong>输出:</strong>4193\n<strong>解释:</strong>\n第 1 步:\"4193 with words\"(当前没有读入字符,因为没有前导空格)\n\t\t ^\n第 2 步:\"4193 with words\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n\t\t ^\n第 3 步:\"<strong>4193</strong> with words\"(读入 \"4193\";由于下一个字符不是一个数字,所以读入停止)\n\t\t ^\n解析得到整数 4193 。\n由于 \"4193\" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 4193 。\n</pre>\n\n\t<p><strong>示例&nbsp;4:</strong></p>\n\n\t<pre><strong>输入:</strong>s = \"words and 987\"\n<strong>输出:</strong>0\n<strong>解释:</strong>\n第 1 步:\"words and 987\"(当前没有读入字符,因为没有前导空格)\n\t\t ^\n第 2 步:\"words and 987\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n\t\t ^\n第 3 步:\"words and 987\"(由于当前字符 'w' 不是一个数字,所以读入停止)\n\t\t ^\n解析得到整数 0 ,因为没有读入任何数字。\n由于 0 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 0 。</pre>\n\n\t<p><strong>示例&nbsp;5:</strong></p>\n\n\t<pre><strong>输入:</strong>s = \"-91283472332\"\n<strong>输出:</strong>-2147483648\n<strong>解释:</strong>\n第 1 步:\"-91283472332\"(当前没有读入字符,因为没有前导空格)\n\t\t ^\n第 2 步:\"-91283472332\"(读入 '-' 字符,所以结果应该是负数)\n\t\t ^\n第 3 步:\"-<strong>91283472332</strong>\"(读入 \"91283472332\"\n\t\t ^\n解析得到整数 -91283472332 。\n由于 -91283472332 小于范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 的下界,最终结果被截断为 -2<sup>31</sup> = -2147483648 。</pre>\n\n\t<p>&nbsp;</p>\n\n\t<p><strong>提示:</strong></p>\n\n\t<ul>\n\t\t<li><code>0 &lt;= s.length &lt;= 200</code></li>\n\t\t<li><code>s</code> 由英文字母(大写和小写)、数字(<code>0-9</code>)、<code>' '</code>、<code>'+'</code>、<code>'-'</code> 和\n\t\t\t<code>'.'</code> 组成\n\t\t</li>\n\t</ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470794",
"cpp": "int myAtoi(char *str)\n{\n\tint i = 0;\n\tint sign = 0;\n\twhile (str[i] && str[i] == ' ')\n\t\ti++;\n\tif (str[i] == NULL)\n\t\treturn 0;\n\tif (str[i] == '-')\n\t{\n\t\tsign = 1;\n\t\ti++;\n\t}\n\telse if (str[i] == '+')\n\t{\n\t\tsign = 0;\n\t\ti++;\n\t}\n\telse if (str[i] < '0')\n\t\treturn 0;\n\telse if (str[i] > '9')\n\t\treturn 0;\n\tlong long int r = 0;\n\twhile (str[i])\n\t{\n\t\tif (str[i] < '0')\n\t\t\tbreak;\n\t\telse if (str[i] > '9')\n\t\t\tbreak;\n\t\telse\n\t\t\tr = r * 10 + str[i++] - '0';\n\t\tif (r > INT_MAX)\n\t\t\tbreak;\n\t}\n\tr = sign ? -r : r;\n\tif (r < INT_MIN)\n\t\treturn INT_MIN;\n\tif (r > INT_MAX)\n\t\treturn INT_MAX;\n\treturn (int)r;\n}",
"java": "class Solution {\n\tpublic int myAtoi(String s) {\n\t\tlong y = 0;\n\t\tint i = 0;\n\t\tboolean w = false;\n\t\tboolean sign = false;\n\t\tint offset = 0;\n\t\tchar[] ints = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };\n\t\twhile (i < s.length()) {\n\t\t\tchar c = s.charAt(i);\n\t\t\tboolean isSign = false;\n\t\t\tif (w == false && c != ' ') {\n\t\t\t\tw = true;\n\t\t\t\tif (c == '-') {\n\t\t\t\t\tsign = true;\n\t\t\t\t\tisSign = true;\n\t\t\t\t}\n\t\t\t\tif (c == '+') {\n\t\t\t\t\tisSign = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (w && (!isSign)) {\n\t\t\t\tint v = Arrays.binarySearch(ints, c);\n\t\t\t\tif (v < 0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ty = y * 10 + v;\n\t\t\t\tif (y > 0x7FFFFFFF) {\n\t\t\t\t\ty = 0x7FFFFFFF;\n\t\t\t\t\toffset = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\treturn sign ? -(int) (y + offset) : (int) y;\n\t}\n}",
"js": "/**\n * @param {string} str\n * @return {number}\n */\nvar myAtoi = function(str) {\n let y=0;\n let i=0;\n let w=false;\n let sign=false;\n const ints=['0','1','2','3','4','5','6','7','8','9'];\n while(i<str.length){\n let c = str[i];\n let isSign=false;\n if(w===false&&c!==' '){\n w=true;\n if(c==='-'){\n sign=true;\n isSign=true;\n }\n if(c==='+'){\n isSign = true;\n }\n }\n \n if(w&&(!isSign)){\n const v = ints.indexOf(c);\n if(v<0){\n break;\n }\n y=y*10+v;\n }\n i++;\n }\n \n let offset=0;\n if(y>0x7FFFFFFF){\n \ty = 0x7FFFFFFF;\n offset = 1;\n }\n return sign ? -(y+offset) : y;\n};\n\nfunction main(){\n\n}\n\nmain();\n",
"python": "class Solution:\n\tdef myAtoi(self, s: str) -> int:\n\t\ty = 0\n\t\ti = 0\n\t\tw = False\n\t\tsign = False\n\t\tints = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n\t\twhile i < len(s):\n\t\t\tc = s[i]\n\t\t\tisSign = False\n\t\t\tif w == False and c != ' ':\n\t\t\t\tw = True\n\t\t\t\tif c == '-':\n\t\t\t\t\tsign = True\n\t\t\t\t\tisSign = True\n\t\t\t\tif c == '+':\n\t\t\t\t\tisSign = True\n\t\t\tif w and not isSign:\n\t\t\t\ttry:\n\t\t\t\t\tv = ints.index(c)\n\t\t\t\t\ty = y*10+v\n\t\t\t\texcept:\n\t\t\t\t\tbreak\n\t\t\ti += 1\n\t\toffset = 0\n\t\tif y > 2147483647:\n\t\t\ty = 2147483647\n\t\t\toffset = 1\n\t\treturn -(y+offset) if sign else y\n# %%\ns = Solution()\nprint(s.myAtoi(s = \"42\"))",
"status": 1,
"keywords": "字符串",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/7/7_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/7/7_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/7/7_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 71,
"question_title": "简化路径",
"difficulty": "中等",
"question_content": "<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p><p class=\"MachineTrans-lang-zh-CN\">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p><p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p><ul>\t<li>始终以斜杠 <code>'/'</code> 开头。</li>\t<li>两个目录名之间必须只有一个斜杠 <code>'/'</code> 。</li>\t<li>最后一个目录名(如果存在)<strong>不能 </strong>以 <code>'/'</code> 结尾。</li>\t<li>此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 <code>'.'</code> 或 <code>'..'</code>)。</li></ul><p>返回简化后得到的 <strong>规范路径</strong> 。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>path = \"/home/\"<strong><br />输出:</strong>\"/home\"<strong><br />解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>path = \"/../\"<strong><br />输出:</strong>\"/\"<strong><br />解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>path = \"/home//foo/\"<strong><br />输出:</strong>\"/home/foo\"<strong><br />解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>path = \"/a/./b/../../c/\"<strong><br />输出:</strong>\"/c\"</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= path.length <= 3000</code></li>\t<li><code>path</code> 由英文字母,数字,<code>'.'</code>,<code>'/'</code> 或 <code>'_'</code> 组成。</li>\t<li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470830",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic char *simplifyPath(char *path)\n{\n\tint len = strlen(path);\n\tif (len == 0)\n\t{\n\t\treturn path;\n\t}\n\tchar *p = path;\n\tint *indexes = malloc(len * sizeof(int));\n\tint depth = 0;\n\tint name_start = 1;\n\twhile (*p != '\\0')\n\t{\n\t\tif (*p == '/')\n\t\t{\n\t\t\tif (p > path && *(p - 1) != '/' && *(p - 1) != '.')\n\t\t\t{\n\t\t\t\tname_start = 1;\n\t\t\t}\n\t\t}\n\t\telse if (*p == '.')\n\t\t{\n\t\t\tif (*(p + 1) == '\\0' || *(p + 1) == '/')\n\t\t\t{\n\t\t\t\tp += 1;\n\t\t\t}\n\t\t\telse if (*(p + 1) == '.' && (*(p + 2) == '\\0' || *(p + 2) == '/'))\n\t\t\t{\n\t\t\t\tif (depth > 0)\n\t\t\t\t{\n\t\t\t\t\tdepth--;\n\t\t\t\t\tname_start = 1;\n\t\t\t\t}\n\t\t\t\tp += 2;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tindexes[depth++] = p - path;\n\t\t\t\twhile (*p != '/' && *p != '\\0')\n\t\t\t\t{\n\t\t\t\t\tp++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (*p == '\\0')\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (name_start && depth >= 0)\n\t\t\t{\n\t\t\t\tindexes[depth++] = p - path;\n\t\t\t\tname_start = 0;\n\t\t\t}\n\t\t}\n\t\tp++;\n\t}\n\tint i;\n\tchar *result = malloc(len + 1);\n\tchar *q = result;\n\tif (depth <= 0)\n\t{\n\t\t*q++ = '/';\n\t}\n\telse\n\t{\n\t\tfor (i = 0; i < depth; i++)\n\t\t{\n\t\t\tp = path + indexes[i];\n\t\t\t*q++ = '/';\n\t\t\twhile (*p != '/')\n\t\t\t{\n\t\t\t\t*q++ = *p++;\n\t\t\t}\n\t\t}\n\t}\n\t*q = '\\0';\n\treturn result;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test path\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", simplifyPath(argv[1]));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic String simplifyPath(String path) {\n\n\t\tDeque<String> stack = new ArrayDeque<>();\n\n\t\tfor (String str : path.split(\"/\")) {\n\n\t\t\tif (\"\".equals(str) || \".\".equals(str))\n\t\t\t\tcontinue;\n\t\t\tstack.push(str);\n\t\t}\n\n\t\tStringBuilder sb = new StringBuilder();\n\t\tint count = 0;\n\t\twhile (!stack.isEmpty()) {\n\t\t\tString str = stack.pop();\n\n\t\t\tif (\"..\".equals(str))\n\t\t\t\tcount++;\n\t\t\telse {\n\n\t\t\t\tif (count > 0) {\n\t\t\t\t\tcount--;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tsb.insert(0, str);\n\t\t\t\tsb.insert(0, \"/\");\n\t\t\t}\n\t\t}\n\n\t\tif (sb.length() == 0)\n\t\t\tsb.append(\"/\");\n\n\t\treturn sb.toString();\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef simplifyPath(self, path):\n\t\t\"\"\"\n\t\t:type path: str\n\t\t:rtype: str\n\t\t\"\"\"\n\t\tresult = []\n\t\tplist = path.split('/')\n\t\tfor pos in plist:\n\t\t\tif pos:\n\t\t\t\tif pos == '..':\n\t\t\t\t\ttry:\n\t\t\t\t\t\tresult.pop()\n\t\t\t\t\texcept:\n\t\t\t\t\t\tresult = []\n\t\t\t\telif pos != '.':\n\t\t\t\t\tresult.append(pos)\n\t\treturn '/'+'/'.join(result)\n# %%\ns = Solution()\nprint(s.simplifyPath(path = \"/home/\"))",
"status": 1,
"keywords": "栈,字符串",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/cmqwan/article/details/85017341"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/70/70_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/70/70_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/70/70_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 72,
"question_title": "编辑距离",
"difficulty": "困难",
"question_content": "<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul>\t<li>插入一个字符</li>\t<li>删除一个字符</li>\t<li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = \"horse\", word2 = \"ros\"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = \"intention\", word2 = \"execution\"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= word1.length, word2.length <= 500</code></li>\t<li><code>word1</code> 和 <code>word2</code> 由小写英文字母组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470829",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint minDistance(string word1, string word2)\n\t{\n\t\tint l1 = word1.length();\n\t\tint l2 = word2.length();\n\t\tvector<int> dp(l2 + 1);\n\t\tfor (int i = 0; i <= l2; i++)\n\t\t{\n\t\t\tdp[i] = i;\n\t\t}\n\t\tint up = 0;\n\t\tfor (int i = 1; i <= l1; i++)\n\t\t{\n\t\t\tint left_up = dp[0];\n\t\t\tdp[0] = i;\n\t\t\tfor (int j = 1; j <= l2; j++)\n\t\t\t{\n\t\t\t\tup = dp[j];\n\t\t\t\tif (word1[i - 1] == word2[j - 1])\n\t\t\t\t{\n\t\t\t\t\tdp[j] = left_up;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tdp[j] = 1 + min(left_up, min(up, dp[j - 1]));\n\t\t\t\t}\n\t\t\t\tleft_up = up;\n\t\t\t}\n\t\t}\n\t\treturn dp[l2];\n\t}\n};",
"java": "class Solution {\n\tpublic int minDistance(String word1, String word2) {\n\t\tint len1 = word1.length();\n\t\tint len2 = word2.length();\n\n\t\tif (len1 * len2 == 0)\n\t\t\treturn len1 + len2;\n\t\tString longerStr = len1 > len2 ? word1 : word2;\n\t\tString shorterStr = len1 > len2 ? word2 : word1;\n\t\tint shorterOne = Math.min(len1, len2);\n\n\t\tint[] dp = new int[shorterOne + 1];\n\n\t\tfor (int i = 0; i < shorterOne + 1; i++) {\n\t\t\tdp[i] = i;\n\t\t}\n\n\t\tfor (int j = 1; j <= longerStr.length(); j++) {\n\n\t\t\tint left = j;\n\n\t\t\tfor (int i = 1; i <= shorterStr.length(); i++) {\n\t\t\t\tint updateDown = dp[i] + 1;\n\t\t\t\tint updateLeft = left + 1;\n\t\t\t\tint updateLeftDown = dp[i - 1];\n\n\t\t\t\tif (longerStr.charAt(j - 1) != shorterStr.charAt(i - 1)) {\n\t\t\t\t\tupdateLeftDown++;\n\t\t\t\t}\n\n\t\t\t\tint min = Math.min(updateLeft, Math.min(updateDown, updateLeftDown));\n\n\t\t\t\tdp[i - 1] = left;\n\n\t\t\t\tif (i == dp.length - 1) {\n\t\t\t\t\tdp[i] = min;\n\t\t\t\t} else {\n\n\t\t\t\t\tleft = min;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dp[shorterOne];\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef minDistance(self, word1, word2):\n\t\tls_1, ls_2 = len(word1), len(word2)\n\t\tdp = list(range(ls_1 + 1))\n\t\tfor j in range(1, ls_2 + 1):\n\t\t\tpre = dp[0]\n\t\t\tdp[0] = j\n\t\t\tfor i in range(1, ls_1 + 1):\n\t\t\t\ttemp = dp[i]\n\t\t\t\tif word1[i - 1] == word2[j - 1]:\n\t\t\t\t\tdp[i] = pre\n\t\t\t\telse:\n\t\t\t\t\tdp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1)\n\t\t\t\tpre = temp\n\t\treturn dp[ls_1]\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.minDistance(\"horse\",\"ros\"))\t\t\n\tprint (s.minDistance(\"intention\",\"execution\"))\t\t",
"status": 1,
"keywords": "字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/71/71_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/71/71_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/71/71_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 73,
"question_title": "矩阵置零",
"difficulty": "中等",
"question_content": "<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">原地</a></strong> 算法<strong>。</strong></p><p><strong>进阶:</strong></p><ul>\t<li>一个直观的解决方案是使用  <code>O(<em>m</em><em>n</em>)</code> 的额外空间,但这并不是一个好的解决方案。</li>\t<li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li>\t<li>你能想出一个仅使用常量空间的解决方案吗?</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat1.jpg\" style=\"width: 450px; height: 169px;\" /><pre><strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat2.jpg\" style=\"width: 450px; height: 137px;\" /><pre><strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == matrix.length</code></li>\t<li><code>n == matrix[0].length</code></li>\t<li><code>1 <= m, n <= 200</code></li>\t<li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470228",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\npublic:\nvoid setZeroes(vector<vector<int>> &matrix)\n{\n\tbool bRow = false, bCol = false;\n\tfor (int row = 0; row < matrix.size(); row++)\n\t{\n\t\tfor (int col = 0; col < matrix[row].size(); col++)\n\t\t{\n\t\t\tif (matrix[row][col] == 0)\n\t\t\t{\n\t\t\t\tif (row == 0)\n\t\t\t\t{\n\t\t\t\t\tbRow = true;\n\t\t\t\t}\n\t\t\t\tif (col == 0)\n\t\t\t\t{\n\t\t\t\t\tbCol = true;\n\t\t\t\t}\n\t\t\t\tmatrix[0][col] = matrix[row][0] = 0;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int row = 1; row < matrix.size(); row++)\n\t{\n\t\tfor (int col = 1; col < matrix[row].size(); col++)\n\t\t{\n\t\t\tif (matrix[0][col] == 0 || matrix[row][0] == 0)\n\t\t\t{\n\t\t\t\tmatrix[row][col] = 0;\n\t\t\t}\n\t\t}\n\t}\n\tif (bRow)\n\t{\n\t\tfor (auto &m : matrix[0])\n\t\t{\n\t\t\tm = 0;\n\t\t}\n\t}\n\tif (bCol)\n\t{\n\t\tfor (int row = 0; row < matrix.size(); row++)\n\t\t{\n\t\t\tmatrix[row][0] = 0;\n\t\t}\n\t}\n}\n}\n;",
"java": "class Solution {\n\tpublic void setZeroes(int[][] matrix) {\n\t\tint[] xNum = new int[matrix[0].length];\n\t\tint[] yNum = new int[matrix.length];\n\t\tfor (int i = 0; i < matrix.length; i++) {\n\t\t\tfor (int j = 0; j < matrix[i].length; j++) {\n\t\t\t\tif (matrix[i][j] == 0) {\n\t\t\t\t\txNum[j] = 1;\n\t\t\t\t\tyNum[i] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < matrix.length; i++) {\n\t\t\tfor (int j = 0; j < matrix[i].length; j++) {\n\t\t\t\tif (xNum[j] == 1 || yNum[i] == 1) {\n\t\t\t\t\tmatrix[i][j] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef setZeroes(self, matrix):\n\t\t\"\"\"\n\t\t:type matrix: List[List[int]]\n\t\t:rtype: void Do not return anything, modify matrix in-place instead.\n\t\t\"\"\"\n\t\tif not matrix:\n\t\t\treturn\n\t\tm = len(matrix)\n\t\tif m == 0:\n\t\t\treturn\n\t\tr = []\n\t\tc = []\n\t\tn = len(matrix[0])\n\t\tfor i in range(m):\n\t\t\tfor j in range(n):\n\t\t\t\tif matrix[i][j] == 0:\n\t\t\t\t\tr.append(i)\n\t\t\t\t\tc.append(j)\n\t\tr = set(r)\n\t\tc = set(c)\n\t\tfor i in r:\n\t\t\tfor j in range(n):\n\t\t\t\tmatrix[i][j] = 0\n\t\tfor i in range(m):\n\t\t\tfor j in c:\n\t\t\t\tmatrix[i][j] = 0\n\t\treturn matrix\n# %%\ns = Solution()\nprint(s.setZeroes(matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]))",
"status": 1,
"keywords": "数组,哈希表,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104343739"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/72/72_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/72/72_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/72/72_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 74,
"question_title": "搜索二维矩阵",
"difficulty": "中等",
"question_content": "<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul>\t<li>每行中的整数从左到右按升序排列。</li>\t<li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == matrix.length</code></li>\t<li><code>n == matrix[i].length</code></li>\t<li><code>1 <= m, n <= 100</code></li>\t<li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469923",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\nstatic int binary_search(int *nums, int len, int target)\n{\n\tint low = -1;\n\tint high = len;\n\twhile (low + 1 < high)\n\t{\n\t\tint mid = low + (high - low) / 2;\n\t\tif (target > nums[mid])\n\t\t{\n\t\t\tlow = mid;\n\t\t}\n\t\telse\n\t\t{\n\t\t\thigh = mid;\n\t\t}\n\t}\n\tif (high == len || nums[high] != target)\n\t{\n\t\treturn -high - 1;\n\t}\n\telse\n\t{\n\t\treturn high;\n\t}\n}\nstatic bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target)\n{\n\tif (matrixRowSize == 0 || matrixColSize == 0)\n\t{\n\t\treturn false;\n\t}\n\tif (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1])\n\t{\n\t\treturn false;\n\t}\n\tint row = 0;\n\tint *nums = NULL;\n\tif (matrixRowSize > 0)\n\t{\n\t\tnums = malloc(matrixRowSize * sizeof(int));\n\t\tfor (row = 0; row < matrixRowSize; row++)\n\t\t{\n\t\t\tnums[row] = matrix[row][0];\n\t\t}\n\t\trow = binary_search(nums, matrixRowSize, target);\n\t\tif (row >= 0)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\trow = -row - 1;\n\t\t\tif (row == 0)\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\trow--;\n\t\t\t}\n\t\t}\n\t}\n\tint col = binary_search(matrix[row], matrixColSize, target);\n\treturn col >= 0;\n}\nint main(int argc, char **argv)\n{\n\tint row = 3;\n\tint col = 4;\n\tint **mat = malloc(row * sizeof(int *));\n\tmat[0] = malloc(col * sizeof(int));\n\tmat[0][0] = 1;\n\tmat[0][1] = 3;\n\tmat[0][2] = 5;\n\tmat[0][3] = 7;\n\tmat[1] = malloc(col * sizeof(int));\n\tmat[1][0] = 10;\n\tmat[1][1] = 11;\n\tmat[1][2] = 16;\n\tmat[1][3] = 20;\n\tmat[2] = malloc(col * sizeof(int));\n\tmat[2][0] = 23;\n\tmat[2][1] = 30;\n\tmat[2][2] = 34;\n\tmat[2][3] = 50;\n\tprintf(\"%s\\n\", searchMatrix(mat, row, col, atoi(argv[1])) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean searchMatrix(int[][] matrix, int target) {\n\t\tif (matrix.length == 0 || matrix[0].length == 0)\n\t\t\treturn false;\n\t\tint begin, mid, end;\n\t\tbegin = mid = 0;\n\t\tint len1 = matrix.length, len2 = matrix[0].length;\n\t\tend = len1 * len2 - 1;\n\t\twhile (begin < end) {\n\t\t\tmid = (begin + end) / 2;\n\t\t\tif (matrix[mid / len2][mid % len2] < target)\n\t\t\t\tbegin = mid + 1;\n\t\t\telse\n\t\t\t\tend = mid;\n\t\t}\n\t\treturn matrix[begin / len2][begin % len2] == target;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef searchMatrix(self, matrix, target):\n\t\t\"\"\"\n\t\t:type matrix: List[List[int]]\n\t\t:type target: int\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tif not matrix or not matrix[0]:\n\t\t\treturn False\n\t\trows = len(matrix)\n\t\tcols = len(matrix[0])\n\t\trow, col = 0, cols - 1\n\t\twhile True:\n\t\t\tif row < rows and col >= 0:\n\t\t\t\tif matrix[row][col] == target:\n\t\t\t\t\treturn True\n\t\t\t\telif matrix[row][col] < target:\n\t\t\t\t\trow += 1\n\t\t\t\telse:\n\t\t\t\t\tcol -= 1\n\t\t\telse:\n\t\t\t\treturn False\n# %%\ns = Solution()\nprint(s.searchMatrix(matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3))",
"status": 1,
"keywords": "数组,二分查找,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104343847"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/73/73_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/73/73_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/73/73_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 75,
"question_title": "颜色分类",
"difficulty": "中等",
"question_content": "<p>给定一个包含红色、白色和蓝色,一共 <code>n</code><em> </em>个元素的数组,<strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p><p>此题中,我们使用整数 <code>0</code>、 <code>1</code> 和 <code>2</code> 分别表示红色、白色和蓝色。</p><ul></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,0,2,1,1,0]<strong><br />输出:</strong>[0,0,1,1,2,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [2,0,1]<strong><br />输出:</strong>[0,1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>n == nums.length</code></li>\t<li><code>1 <= n <= 300</code></li>\t<li><code>nums[i]</code> 为 <code>0</code>、<code>1</code> 或 <code>2</code></li></ul><p> </p><p><strong>进阶:</strong></p><ul>\t<li>你可以不使用代码库中的排序函数来解决这道题吗?</li>\t<li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470126",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid sortColors(vector<int> &nums)\n\t{\n\t\tint i = 0, j = nums.size() - 1;\n\t\twhile (i < j)\n\t\t{\n\t\t\tif (nums[i] == 0)\n\t\t\t{\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (nums[j] != 0)\n\t\t\t{\n\t\t\t\tj--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tswap(nums[i], nums[j]);\n\t\t}\n\t\tj = nums.size() - 1;\n\t\twhile (i < j)\n\t\t{\n\t\t\tif (nums[i] == 1)\n\t\t\t{\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (nums[j] != 1)\n\t\t\t{\n\t\t\t\tj--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tswap(nums[i], nums[j]);\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic void sortColors(int[] nums) {\n\t\tint low = 0, high = nums.length - 1;\n\t\tint i = 0;\n\t\twhile (i <= high) {\n\t\t\tif (nums[i] == 0) {\n\t\t\t\tint tmp = nums[i];\n\t\t\t\tnums[i] = nums[low];\n\t\t\t\tnums[low] = tmp;\n\t\t\t\t++low;\n\t\t\t\t++i;\n\t\t\t} else if (nums[i] == 1) {\n\t\t\t\t++i;\n\t\t\t} else if (i <= high && nums[i] == 2) {\n\t\t\t\tint tmp = nums[i];\n\t\t\t\tnums[i] = nums[high];\n\t\t\t\tnums[high] = tmp;\n\t\t\t\t--high;\n\t\t\t}\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef sortColors(self, nums):\n\t\tlow, mid, high = 0, 0, len(nums) - 1\n\t\twhile mid <= high:\n\t\t\tif nums[mid] == 0:\n\t\t\t\tnums[low], nums[mid] = nums[mid], nums[low]\n\t\t\t\tlow += 1\n\t\t\t\tmid += 1\n\t\t\telif nums[mid] == 1:\n\t\t\t\tmid += 1\n\t\t\telse:\n\t\t\t\tnums[high], nums[mid] = nums[mid], nums[high]\n\t\t\t\thigh -= 1\n\t\treturn nums\n# %%\ns = Solution()\nprint(s.sortColors(nums = [2,0,2,1,1,0]))",
"status": 1,
"keywords": "数组,双指针,排序",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104343861"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/74/74_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/74/74_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/74/74_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 76,
"question_title": "最小覆盖子串",
"difficulty": "困难",
"question_content": "<p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>\"\"</code> 。</p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"ADOBECODEBANC\", t = \"ABC\"<strong><br />输出:</strong>\"BANC\"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"a\", t = \"a\"<strong><br />输出:</strong>\"a\"</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li>\t<li><code>s</code> 和 <code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗?",
"topic_link": "https://bbs.csdn.net/topics/600471015",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tstring minWindow(string s, string t)\n\t{\n\t\tvector<int> count(128);\n\t\tfor (char c : t)\n\t\t{\n\t\t\tcount[c]++;\n\t\t}\n\t\tint l = 0, r = 0;\n\t\tint need_to_meet = t.length();\n\t\tint start, min_len = INT_MAX;\n\t\twhile (r < s.length())\n\t\t{\n\t\t\tif (--count[s[r++]] >= 0)\n\t\t\t{\n\t\t\t\tneed_to_meet--;\n\t\t\t}\n\t\t\twhile (need_to_meet == 0)\n\t\t\t{\n\t\t\t\tif (r - l < min_len)\n\t\t\t\t{\n\t\t\t\t\tstart = l;\n\t\t\t\t\tmin_len = r - l;\n\t\t\t\t}\n\t\t\t\tif (++count[s[l++]] > 0)\n\t\t\t\t{\n\t\t\t\t\tneed_to_meet++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn min_len == INT_MAX ? \"\" : s.substr(start, min_len);\n\t}\n};",
"java": "public class Min_Win_Sub {\n\tpublic String minWindow(String s, String t) {\n\t\tint[] ta = new int[128];\n\t\tint[] sa = new int[128];\n\t\tint min = Integer.MAX_VALUE;\n\t\tString minwin = \"\";\n\t\tfor (int i = 0; i < t.length(); i++) {\n\t\t\tta[t.charAt(i)]++;\n\t\t}\n\t\tint count = 0;\n\t\tint end = 0;\n\t\tint start = 0;\n\t\twhile (end < s.length()) {\n\t\t\tif (ta[s.charAt(end)] != 0) {\n\t\t\t\tif (sa[s.charAt(end)] < ta[s.charAt(end)]) {\n\t\t\t\t\tcount++;\n\t\t\t\t}\n\t\t\t\tsa[s.charAt(end)]++;\n\t\t\t}\n\t\t\tif (count == t.length()) {\n\t\t\t\twhile (ta[s.charAt(start)] == 0 || sa[s.charAt(start)] > ta[s.charAt(start)]) {\n\t\t\t\t\tif (sa[s.charAt(start)] > ta[s.charAt(start)]) {\n\t\t\t\t\t\tsa[s.charAt(start)]--;\n\t\t\t\t\t}\n\t\t\t\t\tstart++;\n\t\t\t\t}\n\t\t\t\tif (end - start + 1 < min) {\n\t\t\t\t\tminwin = s.substring(start, end + 1);\n\t\t\t\t\tmin = end - start + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tend++;\n\t\t}\n\t\treturn minwin;\n\t}\n\n}",
"js": "",
"python": "class Solution(object):\n\tdef minWindow(self, s, t):\n\t\tls_s, ls_t = len(s), len(t)\n\t\tneed_to_find = [0] * 256\n\t\thas_found = [0] * 256\n\t\tmin_begin, min_end = 0, -1\n\t\tmin_window = 100000000000000\n\t\tfor index in range(ls_t):\n\t\t\tneed_to_find[ord(t[index])] += 1\n\t\tcount, begin = 0, 0\n\t\tfor end in range(ls_s):\n\t\t\tend_index = ord(s[end])\n\t\t\tif need_to_find[end_index] == 0:\n\t\t\t\tcontinue\n\t\t\thas_found[end_index] += 1\n\t\t\tif has_found[end_index] <= need_to_find[end_index]:\n\t\t\t\tcount += 1\n\t\t\tif count == ls_t:\n\t\t\t\tbegin_index = ord(s[begin])\n\t\t\t\twhile need_to_find[begin_index] == 0 or\\\n\t\t\t\t\thas_found[begin_index] > need_to_find[begin_index]:\n\t\t\t\t\tif has_found[begin_index] > need_to_find[begin_index]:\n\t\t\t\t\t\thas_found[begin_index] -= 1\n\t\t\t\t\tbegin += 1\n\t\t\t\t\tbegin_index = ord(s[begin])\n\t\t\t\twindow_ls = end - begin + 1\n\t\t\t\tif window_ls < min_window:\n\t\t\t\t\tmin_begin = begin\n\t\t\t\t\tmin_end = end\n\t\t\t\t\tmin_window = window_ls\n\t\tif count == ls_t:\n\t\t\treturn s[min_begin: min_end + 1]\n\t\telse:\n\t\t\treturn ''\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.minWindow('a', 'a'))",
"status": 1,
"keywords": "哈希表,字符串,滑动窗口",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/75/75_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/75/75_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/75/75_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 77,
"question_title": "组合",
"difficulty": "中等",
"question_content": "<p>给定两个整数 <em>n</em> 和 <em>k</em>,返回 1 ... <em>n </em>中所有可能的 <em>k</em> 个数的组合。</p>\n<p><strong>示例:</strong></p>\n<pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470921",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> combine(int n, int k)\n\t{\n\t\tvector<vector<int>> res;\n\t\tdfs(n, k, 1, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(int n, int k, int start, vector<vector<int>> &res)\n\t{\n\t\tif (stack.size() == k)\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = start; i <= n; i++)\n\t\t\t{\n\t\t\t\tstack.push_back(i);\n\t\t\t\tdfs(n, k, i + 1, res);\n\t\t\t\tstack.pop_back();\n\t\t\t}\n\t\t}\n\t}\n};",
"java": "\nimport java.util.*;\n\npublic class Solution77 {\n\tList<List<Integer>> output = new LinkedList<>();\n\tint n;\n\tint k;\n\n\tpublic void traceback(int first, LinkedList<Integer> current) {\n\t\tif (current.size() == k) {\n\n\t\t\toutput.add(new LinkedList<Integer>(current));\n\t\t\tSystem.out.println(output);\n\t\t\treturn;\n\t\t}\n\n\t\tfor (int i = first; i <= n; i++) {\n\n\t\t\tcurrent.add(i);\n\t\t\ttraceback(i + 1, current);\n\n\t\t\tcurrent.removeLast();\n\t\t}\n\t}\n\n\tpublic List<List<Integer>> combine(int n, int k) {\n\t\tthis.n = n;\n\t\tthis.k = k;\n\t\ttraceback(1, new LinkedList<>());\n\t\treturn output;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef combine(self, n, k):\n\t\tres = []\n\t\tself.get_combine(res, [], n, k, 1)\n\t\treturn res\n\tdef get_combine(self, res, prefix, n, k, start):\n\t\tif k == 0:\n\t\t\tres.append(list(prefix))\n\t\telif start <= n:\n\t\t\tprefix.append(start)\n\t\t\tself.get_combine(res, prefix,\n\t\t\t\t\t\t\t n, k - 1, start + 1)\n\t\t\tprefix.pop()\n\t\t\tself.get_combine(res, prefix,\n\t\t\t\t\t\t\t n, k, start + 1)\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.combine(4, 2))",
"status": 1,
"keywords": "数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/76/76_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/76/76_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/76/76_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 78,
"question_title": "子集",
"difficulty": "中等",
"question_content": "<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 10</code></li>\t<li><code>-10 <= nums[i] <= 10</code></li>\t<li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469824",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> subsets(vector<int> &nums)\n\t{\n\t\tvector<vector<int>> res;\n\t\tdfs(nums, 0, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &nums, int start, vector<vector<int>> &res)\n\t{\n\t\tres.push_back(stack);\n\t\tfor (int i = start; i < nums.size(); i++)\n\t\t{\n\t\t\tstack.push_back(nums[i]);\n\t\t\tdfs(nums, i + 1, res);\n\t\t\tstack.pop_back();\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> subsets(int[] nums) {\n\t\tList<List<Integer>> res = new ArrayList<List<Integer>>();\n\t\tList<Integer> tmp = new ArrayList<>();\n\t\tres.add(tmp);\n\t\tif (nums.length == 0)\n\t\t\treturn res;\n\t\thelper(nums, 0, tmp, res);\n\t\treturn res;\n\t}\n\n\tpublic void helper(int[] nums, int start, List<Integer> tmp, List<List<Integer>> res) {\n\t\tfor (int i = start; i < nums.length; i++) {\n\t\t\ttmp.add(nums[i]);\n\t\t\thelper(nums, i + 1, tmp, res);\n\t\t\tres.add(new ArrayList<Integer>(tmp));\n\t\t\ttmp.remove(tmp.size() - 1);\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef subsets(self, nums):\n\t\tnums.sort()\n\t\tres = [[]]\n\t\tfor index in range(len(nums)):\n\t\t\tsize = len(res)\n\t\t\tfor j in range(size):\n\t\t\t\tcurr = list(res[j])\n\t\t\t\tcurr.append(nums[index])\n\t\t\t\tres.append(curr)\n\t\treturn res\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.subsets([1,2,3]))",
"status": 1,
"keywords": "位运算,数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/77/77_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/77/77_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/77/77_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 79,
"question_title": "单词搜索",
"difficulty": "中等",
"question_content": "<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p><p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"SEE\"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg\" style=\"width: 322px; height: 242px;\" /><pre><strong>输入:</strong>board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCB\"<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == board.length</code></li>\t<li><code>n = board[i].length</code></li>\t<li><code>1 <= m, n <= 6</code></li>\t<li><code>1 <= word.length <= 15</code></li>\t<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li></ul><p> </p><p><strong>进阶:</strong>你可以使用搜索剪枝的技术来优化解决方案,使其在 <code>board</code> 更大的情况下可以更快解决问题?</p>",
"topic_link": "https://bbs.csdn.net/topics/600470927",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool dfs(char *word, char **board, bool *used,\n\t\t\t\tint row, int col, int row_size, int col_size)\n{\n\tif (board[row][col] != *word)\n\t{\n\t\treturn false;\n\t}\n\tused[row * col_size + col] = true;\n\tif (*(word + 1) == '\\0')\n\t{\n\t\treturn true;\n\t}\n\tbool result = false;\n\tif (row > 0 && !used[(row - 1) * col_size + col])\n\t{\n\t\tresult = dfs(word + 1, board, used, row - 1, col, row_size, col_size);\n\t}\n\tif (!result && row < row_size - 1 && !used[(row + 1) * col_size + col])\n\t{\n\t\tresult = dfs(word + 1, board, used, row + 1, col, row_size, col_size);\n\t}\n\tif (!result && col > 0 && !used[row * col_size + col - 1])\n\t{\n\t\tresult = dfs(word + 1, board, used, row, col - 1, row_size, col_size);\n\t}\n\tif (!result && col < col_size - 1 && !used[row * col_size + col + 1])\n\t{\n\t\tresult = dfs(word + 1, board, used, row, col + 1, row_size, col_size);\n\t}\n\tused[row * col_size + col] = false;\n\treturn result;\n}\nstatic bool exist(char **board, int boardRowSize, int boardColSize, char *word)\n{\n\tint i, j;\n\tint len = strlen(word);\n\tif (len > boardRowSize * boardColSize)\n\t{\n\t\treturn false;\n\t}\n\tbool *used = malloc(boardRowSize * boardColSize);\n\tfor (i = 0; i < boardRowSize; i++)\n\t{\n\t\tfor (j = 0; j < boardColSize; j++)\n\t\t{\n\t\t\tmemset(used, false, boardRowSize * boardColSize);\n\t\t\tif (dfs(word, board, used, i, j, boardRowSize, boardColSize))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test word row1 row2...\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean exist(char[][] board, String word) {\n\t\tint cl = board.length;\n\t\tint rl = board[0].length;\n\t\tboolean[][] flag = new boolean[cl][rl];\n\t\tfor (int i = 0; i < cl; i++) {\n\t\t\tfor (int j = 0; j < rl; j++) {\n\t\t\t\tif (find(board, word, flag, i, j, 0))\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tpublic boolean find(char[][] board, String word, boolean[][] flag, int i, int j, int index) {\n\t\tint cl = board.length;\n\t\tint rl = board[0].length;\n\t\tif (word.length() == index)\n\t\t\treturn true;\n\t\tif (i < 0 || i >= cl || j >= rl || j < 0)\n\t\t\treturn false;\n\t\tif (flag[i][j] || word.charAt(index) != board[i][j])\n\t\t\treturn false;\n\t\tflag[i][j] = true;\n\t\tboolean judge = find(board, word, flag, i - 1, j, index + 1) || find(board, word, flag, i + 1, j, index + 1)\n\t\t\t\t|| find(board, word, flag, i, j - 1, index + 1) || find(board, word, flag, i, j + 1, index + 1);\n\t\tflag[i][j] = false;\n\t\treturn judge;\n\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef exist(self, board, word):\n\t\t\"\"\"\n\t\t:type board: List[List[str]]\n\t\t:type word: str\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tcheck_board = [[True] * len(board[0]) for _ in range(len(board))]\n\t\tfor i in range(len(board)):\n\t\t\tfor j in range(len(board[0])):\n\t\t\t\tif board[i][j] == word[0] and check_board:\n\t\t\t\t\tcheck_board[i][j] = False\n\t\t\t\t\tres = self.check_exist(check_board, board, word, 1, len(word), i, j)\n\t\t\t\t\tif res:\n\t\t\t\t\t\treturn True\n\t\t\t\t\tcheck_board[i][j] = True\n\t\treturn False\n\tdef check_exist(self, check_board, board, word, index, ls, row, col):\n\t\tif index == ls:\n\t\t\treturn True\n\t\tfor temp in [(0, 1),(0, -1),(1, 0),(-1, 0)]:\n\t\t\tcurr_row = row + temp[0]\n\t\t\tcurr_col = col + temp[1]\n\t\t\tif curr_row >= 0 and curr_row < len(board) and curr_col >= 0 and curr_col < len(board[0]):\n\t\t\t\tif check_board[curr_row][curr_col] and board[curr_row][curr_col] == word[index]:\n\t\t\t\t\tcheck_board[curr_row][curr_col] = False\n\t\t\t\t\tres = self.check_exist(check_board, board, word, index + 1, len(word), curr_row, curr_col)\n\t\t\t\t\tif res:\n\t\t\t\t\t\treturn res\n\t\t\t\t\tcheck_board[curr_row][curr_col] = True\n\t\treturn False\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.exist(board = [[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]], word = \"ABCCED\"))",
"status": 1,
"keywords": "数组,回溯,矩阵",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/78/78_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 80,
"question_title": "删除有序数组中的重复项 II",
"difficulty": "中等",
"question_content": "<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\"\n target=\"_blank\"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>最多出现两次</strong> ,返回删除后数组的新长度。</p>\n<p>不要使用额外的数组空间,你必须在 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\"\n target=\"_blank\">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>\n<p> </p>\n<p><strong>说明:</strong></p>\n<p>为什么返回数值是整数,但输出的答案是数组呢?</p>\n<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>\n<p>你可以想象内部操作如下:</p>\n<pre>\n // <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝\n int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。\n // 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\n for (int i = 0; i < len; i++) {\n     print(nums[i]);\n }</pre>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>nums = [1,1,1,2,2,3]<strong><br />输出:</strong>5, nums = [1,1,2,2,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>5</strong>, 并且原数组的前五个元素被修改为 <strong>1, 1, 2, 2,</strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]<strong><br />输出:</strong>7, nums = [0,0,1,1,2,3,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>7</strong>, 并且原数组的前五个元素被修改为 <strong>0</strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>\n <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n <li><code>nums</code> 已按升序排列</li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600470127",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstatic int removeDuplicates(int *nums, int numsSize)\n{\n\tif (numsSize == 0)\n\t{\n\t\treturn 0;\n\t}\n\tint i;\n\tint len = 0;\n\tint count = 1;\n\tfor (i = 1; i < numsSize; i++)\n\t{\n\t\tif (nums[len] == nums[i])\n\t\t{\n\t\t\tif (count < 2)\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t\tnums[++len] = nums[i];\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcount = 1;\n\t\t\tnums[++len] = nums[i];\n\t\t}\n\t}\n\treturn len + 1;\n}\nint main(int argc, char **argv)\n{\n\tint i, count = argc - 1;\n\tint *nums = malloc(count * sizeof(int));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tnums[i] = atoi(argv[i + 1]);\n\t}\n\tcount = removeDuplicates(nums, count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%d \", nums[i]);\n\t}\n\tprintf(\"\\n\");\n}",
"java": "class Solution {\n\tpublic int removeDuplicates(int[] nums) {\n\t\tint i = 0;\n\t\tfor (int n : nums)\n\t\t\tif (i < 2 || n > nums[i - 2])\n\t\t\t\tnums[i++] = n;\n\t\treturn i;\n\t}\n}",
"js": "",
"python": "class Solution:\n\tdef removeDuplicates(self, nums):\n\t\t\"\"\"\n\t\t:type nums: List[int]\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tif nums is None:\n\t\t\treturn 0\n\t\tlength = len(nums)\n\t\tresult = 0\n\t\ti = j = 0\n\t\twhile i < length:\n\t\t\tj = i\n\t\t\twhile j < length:\n\t\t\t\tif nums[j] != nums[i]:\n\t\t\t\t\tbreak\n\t\t\t\tj += 1\n\t\t\tif j-i > 2:\n\t\t\t\tlength -= j-i-2\n\t\t\t\tfor k in range(j-i-2):\n\t\t\t\t\tdel nums[i]\n\t\t\t\tresult += 2\n\t\t\t\tj = i+2\n\t\t\telse:\n\t\t\t\tresult += (j-i)\n\t\t\ti = j\n\t\treturn result\n# %%\ns = Solution()\nprint(s.removeDuplicates(nums = [1,1,1,2,2,3]))",
"status": 1,
"keywords": "数组,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104349070"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/79/79_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/79/79_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/79/79_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 9,
"question_title": "回文数",
"difficulty": "简单",
"question_content": "<p>给你一个整数 <code>x</code> ,如果 <code>x</code> 是一个回文整数,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p><p>回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,<code>121</code> 是回文,而 <code>123</code> 不是。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 121<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = -121<strong><br />输出:</strong>false<strong><br />解释:</strong>从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 10<strong><br />输出:</strong>false<strong><br />解释:</strong>从右向左读, 为 01 。因此它不是一个回文数。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>x = -101<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li></ul><p> </p><p><strong>进阶:</strong>你能不将整数转为字符串来解决这个问题吗?</p>",
"topic_link": "https://bbs.csdn.net/topics/600471100",
"cpp": "bool isPalindrome(int x)\n{\n\tif (x < 0)\n\t\treturn false;\n\tchar r[11];\n\tint n = snprintf(r, 11, \"%d\", x);\n\tint i;\n\tfor (i = 0; i < n / 2; i++)\n\t{\n\t\tif (r[i] != r[n - i - 1])\n\t\t\treturn false;\n\t}\n\treturn true;\n}",
"java": "class Solution {\n\tpublic boolean isPalindrome(int x) {\n\t\tlong r;\n\t\tlong o = x;\n\t\tlong y = 0;\n\t\twhile (x > 0) {\n\t\t\tr = x % 10;\n\t\t\ty = y * 10 + r;\n\t\t\tx = (int) Math.floor(x / 10);\n\t\t}\n\t\treturn y == o;\n\t}\n}",
"js": "/**\n * @param {number} x\n * @return {boolean}\n */\nvar isPalindrome = function(x) {\n let r;\n let o=x;\n let y=0;\n while(x>0){\n r = x%10;\n y = y*10+r;\n x = Math.floor(x/10);\n }\n \n return y===o;\n};\n\nfunction main(){\n\n}\n\nmain();\n",
"python": "import math\nclass Solution:\n\tdef isPalindrome(self, x: int) -> bool:\n\t\to = x\n\t\ty = 0\n\t\twhile x > 0:\n\t\t\tr = x % 10\n\t\t\ty = y*10+r\n\t\t\tx = int(math.floor(x/10))\n\t\treturn y == o\n# %%\ns = Solution()\nprint(s.isPalindrome(x = 121))",
"status": 1,
"keywords": "数学",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/8/8_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/8/8_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/8/8_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 81,
"question_title": "搜索旋转排序数组 II",
"difficulty": "中等",
"question_content": "<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ,数组中的值不必互不相同。</p>\n<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 <= k < nums.length</code>)上进行了 <strong>旋转\n </strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0\n 开始</strong> 计数)。例如, <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为\n <code>[4,5,6,6,7,0,1,2,4,4]</code> 。\n</p>\n<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果\n <code>nums</code> 中存在这个目标值 <code>target</code> ,则返回 <code>true</code> ,否则返回 <code>false</code> 。\n</p>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 0<strong><br />输出:</strong>true</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 3<strong><br />输出:</strong>false</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 <= nums.length <= 5000</code></li>\n <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n <li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>\n <li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>\n</ul>\n<p> </p>\n<p><strong>进阶:</strong></p>\n<ul>\n <li>这是 <a\n href=\"https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/\">搜索旋转排序数组</a> 的延伸题目,本题中的 <code>nums</code> \n 可能包含重复元素。</li>\n <li>这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?</li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600469924",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\nstatic bool search(int *nums, int numsSize, int target)\n{\n\tint lo = 0;\n\tint hi = numsSize - 1;\n\twhile (lo <= hi)\n\t{\n\t\tint mid = lo + (hi - lo) / 2;\n\t\tif (nums[mid] == target)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\tif (nums[lo] == nums[mid] && nums[mid] == nums[hi])\n\t\t{\n\t\t\tlo++;\n\t\t\thi--;\n\t\t}\n\t\telse if (nums[lo] <= nums[mid])\n\t\t{\n\t\t\tif (nums[lo] <= target && target < nums[mid])\n\t\t\t{\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (nums[mid] < target && target <= nums[hi])\n\t\t\t{\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\nint main(int argc, char **argv)\n{\n\tint i;\n\tint target = atoi(argv[1]);\n\tint size = argc - 2;\n\tint *nums = malloc(size * sizeof(int));\n\tfor (i = 0; i < argc - 2; i++)\n\t{\n\t\tnums[i] = atoi(argv[i + 2]);\n\t}\n\tprintf(\"%d\\n\", search(nums, size, target));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean search(int[] nums, int target) {\n\t\tint low = 0;\n\t\tint high = nums.length - 1;\n\t\twhile (low <= high) {\n\t\t\twhile (low < high && nums[low] == nums[low + 1]) {\n\t\t\t\tlow++;\n\t\t\t}\n\t\t\twhile (low < high && nums[high] == nums[high - 1]) {\n\t\t\t\thigh--;\n\t\t\t}\n\t\t\tint mid = (low + high) / 2;\n\t\t\tif (nums[mid] == target) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (nums[mid] >= nums[0] && (target > nums[mid] || target < nums[0])) {\n\t\t\t\tlow = mid + 1;\n\t\t\t} else if (nums[mid] < nums[0] && target > nums[mid] && target < nums[0]) {\n\t\t\t\tlow = mid + 1;\n\t\t\t} else {\n\t\t\t\thigh = mid - 1;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef search(self, nums, target):\n\t\t\"\"\"\n\t\t:type nums: List[int]\n\t\t:type target: int\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tdef get(start, end):\n\t\t\tif start > end:\n\t\t\t\treturn False\n\t\t\tmid = (start + end) / 2\n\t\t\tmid = int(mid)\n\t\t\twhile mid < end and nums[mid + 1] == nums[mid]:\n\t\t\t\tmid += 1\n\t\t\twhile start < mid and nums[start + 1] == nums[start]:\n\t\t\t\tstart += 1\n\t\t\tif nums[mid] == target:\n\t\t\t\treturn True\n\t\t\telif mid == end:\n\t\t\t\treturn get(start, mid - 1)\n\t\t\telif start == mid:\n\t\t\t\treturn get(mid + 1, end)\n\t\t\telif nums[mid] >= nums[start]:\n\t\t\t\tif target >= nums[start] and target < nums[mid]:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\t\t\telse:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\telif nums[mid] <= nums[end]:\n\t\t\t\tif target > nums[mid] and target <= nums[end]:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\t\telse:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\treturn get(0, len(nums) - 1)\n# %%\ns = Solution()\nprint(s.search(nums = [2,5,6,0,0,1,2], target = 0))",
"status": 1,
"keywords": "数组,二分查找",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104349456"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/80/80_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/80/80_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/80/80_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 82,
"question_title": "删除排序链表中的重复元素 II",
"difficulty": "中等",
"question_content": "<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg\" style=\"width: 500px; height: 142px;\" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg\" style=\"width: 500px; height: 205px;\" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li>题目数据保证链表已经按升序排列</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470128",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct ListNode\n{\n\tint val;\n\tstruct ListNode *next;\n};\nstruct ListNode *deleteDuplicates(struct ListNode *head)\n{\n\tstruct ListNode dummy;\n\tstruct ListNode *p, *q, *prev;\n\tprev = &dummy;\n\tdummy.next = head;\n\tp = q = head;\n\twhile (p != NULL)\n\t{\n\t\twhile (q != NULL && q->val == p->val)\n\t\t{\n\t\t\tq = q->next;\n\t\t}\n\t\tif (p->next == q)\n\t\t{\n\t\t\tprev = p;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprev->next = q;\n\t\t}\n\t\tp = q;\n\t}\n\treturn dummy.next;\n}\nint main(int argc, char **argv)\n{\n\tint i;\n\tstruct ListNode *head = NULL;\n\tstruct ListNode *prev = NULL;\n\tstruct ListNode *p;\n\tfor (i = 0; i < argc - 1; i++)\n\t{\n\t\tp = malloc(sizeof(*p));\n\t\tp->val = atoi(argv[i + 1]);\n\t\tp->next = NULL;\n\t\tif (head == NULL)\n\t\t{\n\t\t\thead = p;\n\t\t\tprev = head;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprev->next = p;\n\t\t\tprev = p;\n\t\t}\n\t}\n\tp = deleteDuplicates(head);\n\twhile (p != NULL)\n\t{\n\t\tprintf(\"%d \", p->val);\n\t\tp = p->next;\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) { val = x; }\n}\nclass Solution {\n\tpublic ListNode deleteDuplicates(ListNode head) {\n\t\tif (head == null || head.next == null) {\n\t\t\treturn head;\n\t\t}\n\t\tListNode next = head.next;\n\t\tif (head.val == next.val) {\n\t\t\twhile (next != null && head.val == next.val) {\n\t\t\t\tnext = next.next;\n\t\t\t}\n\t\t\thead = deleteDuplicates(next);\n\t\t} else {\n\t\t\thead.next = deleteDuplicates(next);\n\t\t}\n\t\treturn head;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef deleteDuplicates(self, head):\n\t\t\"\"\"\n\t\t:type head: ListNode\n\t\t:rtype: ListNode\n\t\t\"\"\"\n\t\tnewnodehead = None\n\t\tnewnode = None\n\t\tnode = head\n\t\twhile node:\n\t\t\tlastval = node.val\n\t\t\tif node.next and node.next.val == lastval:\n\t\t\t\twhile node and node.val == lastval:\n\t\t\t\t\tnode=node.next\n\t\t\t\tcontinue\n\t\t\tif not newnodehead:\n\t\t\t\tnewnode=ListNode(node.val)\n\t\t\t\tnewnodehead=newnode\n\t\t\telse:\n\t\t\t\tnewnode.next=ListNode(node.val)\n\t\t\t\tnewnode=newnode.next\n\t\t\tnode = node.next\n\t\treturn newnodehead\n# %%\nl = LinkList()\nlist1 = [1,2,3,3,4,4,5]\nl1 = l.initList(list1)\ns = Solution()\nprint(l.convert_list(s.deleteDuplicates(l1)))",
"status": 1,
"keywords": "链表,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104349722"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/81/81_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/81/81_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/81/81_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 83,
"question_title": "删除排序链表中的重复元素",
"difficulty": "简单",
"question_content": "<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除所有重复的元素,使每个元素 <strong>只出现一次</strong> 。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list1.jpg\" style=\"width: 302px; height: 242px;\" /><pre><strong>输入:</strong>head = [1,1,2]<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list2.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,1,2,3,3]<strong><br />输出:</strong>[1,2,3]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li>题目数据保证链表已经按升序排列</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470129",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *deleteDuplicates(ListNode *head)\n\t{\n\t\tif (head == nullptr)\n\t\t{\n\t\t\treturn nullptr;\n\t\t}\n\t\tListNode *prev = head;\n\t\tListNode *p = prev->next;\n\t\twhile (p != nullptr)\n\t\t{\n\t\t\tif (p->val != prev->val)\n\t\t\t{\n\t\t\t\tprev->next = p;\n\t\t\t\tprev = p;\n\t\t\t}\n\t\t\tp = p->next;\n\t\t}\n\t\tprev->next = p;\n\t\treturn head;\n\t}\n};",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode deleteDuplicates(ListNode head) {\n\t\tif (head == null || head.next == null) {\n\t\t\treturn head;\n\t\t}\n\t\thead.next = deleteDuplicates(head.next);\n\t\tif (head.val == head.next.val)\n\t\t\thead = head.next;\n\t\treturn head;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef deleteDuplicates(self, head):\n\t\tif head is None:\n\t\t\treturn None\n\t\tpos = head\n\t\twhile pos is not None and pos.next is not None:\n\t\t\tif pos.val == pos.next.val:\n\t\t\t\tpos.next = pos.next.next\n\t\t\telse:\n\t\t\t\tpos = pos.next\n\t\treturn head\n# %%\nl = LinkList()\nlist1 = [1,1,2]\nl1 = l.initList(list1)\ns = Solution()\nprint(l.convert_list(s.deleteDuplicates(l1)))",
"status": 1,
"keywords": "链表",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_45176257/article/details/106487210"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/82/82_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 84,
"question_title": "柱状图中最大的矩形",
"difficulty": "困难",
"question_content": "<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png\"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png\"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>",
"topic_link": "https://bbs.csdn.net/topics/600471006",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstatic int largestRectangleArea(int *heights, int heightsSize)\n{\n\tint *indexes = malloc(heightsSize * sizeof(int));\n\tint *left = malloc(heightsSize * sizeof(int));\n\tint *right = malloc(heightsSize * sizeof(int));\n\tint i, pos = 0;\n\tfor (i = 0; i < heightsSize; i++)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\tleft[i] = pos == 0 ? -1 : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tpos = 0;\n\tfor (i = heightsSize - 1; i >= 0; i--)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\tright[i] = pos == 0 ? heightsSize : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tint max_area = 0;\n\tfor (i = 0; i < heightsSize; i++)\n\t{\n\t\tint area = heights[i] * (right[i] - left[i] - 1);\n\t\tmax_area = area > max_area ? area : max_area;\n\t}\n\treturn max_area;\n}\nint main(void)\n{\n\tint nums[] = {2, 1, 5, 6, 2, 3};\n\tint count = sizeof(nums) / sizeof(*nums);\n\tprintf(\"%d\\n\", largestRectangleArea(nums, count));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int largestRectangleArea(int[] heights) {\n\t\tint length = heights.length;\n\t\tif (length == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint maxSize = 0;\n\t\tfor (int i = 0; i < length; i++) {\n\t\t\tint nowHeight = heights[i];\n\t\t\tint nowWidth = 0;\n\t\t\tfor (int j = i; j < length; j++) {\n\t\t\t\tif (heights[j] < nowHeight) {\n\t\t\t\t\tnowHeight = heights[j];\n\t\t\t\t}\n\t\t\t\tnowWidth++;\n\t\t\t\tif (maxSize < nowHeight * nowWidth) {\n\t\t\t\t\tmaxSize = nowHeight * nowWidth;\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\treturn maxSize;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef largestRectangleArea(self, heights):\n\t\t\"\"\"\n\t\t:type heights: List[int]\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tlargest_rectangle = 0\n\t\tls = len(heights)\n\t\tstack = [-1]\n\t\ttop, pos = 0, 0\n\t\tfor pos in range(ls):\n\t\t\twhile top > 0 and heights[stack[top]] > heights[pos]:\n\t\t\t\tlargest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top - 1] - 1))\n\t\t\t\ttop -= 1\n\t\t\t\tstack.pop()\n\t\t\tstack.append(pos)\n\t\t\ttop += 1\n\t\twhile top > 0:\n\t\t\tlargest_rectangle = max(largest_rectangle, heights[stack[top]] * (ls - stack[top - 1] - 1))\n\t\t\ttop -= 1\n\t\treturn largest_rectangle\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.largestRectangleArea([2,1,5,6,2,3]))",
"status": 1,
"keywords": "栈,数组,单调栈",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/83/83_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/83/83_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/83/83_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 85,
"question_title": "最大矩形",
"difficulty": "困难",
"question_content": "<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg\" style=\"width: 402px; height: 322px;\" /><pre><strong>输入:</strong>matrix = [[\"1\",\"0\",\"1\",\"0\",\"0\"],[\"1\",\"0\",\"1\",\"1\",\"1\"],[\"1\",\"1\",\"1\",\"1\",\"1\"],[\"1\",\"0\",\"0\",\"1\",\"0\"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[\"0\"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[\"1\"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [[\"0\",\"0\"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>rows == matrix.length</code></li>\t<li><code>cols == matrix[0].length</code></li>\t<li><code>0 <= row, cols <= 200</code></li>\t<li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470828",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic inline int max(int a, int b)\n{\n\treturn a > b ? a : b;\n}\nstatic int area_calc(int *heights, int size)\n{\n\tint *indexes = malloc(size * sizeof(int));\n\tint *lhist = malloc(size * sizeof(int));\n\tint *rhist = malloc(size * sizeof(int));\n\tint i, pos = 0;\n\tfor (i = 0; i < size; i++)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\tlhist[i] = pos == 0 ? -1 : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tpos = 0;\n\tfor (i = size - 1; i >= 0; i--)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\trhist[i] = pos == 0 ? size : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tint max_area = 0;\n\tfor (i = 0; i < size; i++)\n\t{\n\t\tint area = heights[i] * (rhist[i] - lhist[i] - 1);\n\t\tmax_area = max(area, max_area);\n\t}\n\treturn max_area;\n}\nstatic int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize)\n{\n\tint i, j, max_area = 0;\n\tint *heights = malloc(matrixColSize * sizeof(int));\n\tmemset(heights, 0, matrixColSize * sizeof(int));\n\tfor (i = 0; i < matrixRowSize; i++)\n\t{\n\t\tfor (j = 0; j < matrixColSize; j++)\n\t\t{\n\t\t\theights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;\n\t\t}\n\t\tmax_area = max(max_area, area_calc(heights, matrixColSize));\n\t}\n\treturn max_area;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test row1 row2...\\n\");\n\t\texit(-1);\n\t}\n\tint i, j;\n\tint row_size = argc - 1;\n\tint col_size = strlen(argv[1]);\n\tfor (i = 0; i < row_size; i++)\n\t{\n\t\tprintf(\"%s\\n\", argv[i + 1]);\n\t}\n\tprintf(\"%d\\n\", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int maximalRectangle(char[][] matrix) {\n\t\tif (matrix == null || matrix.length == 0)\n\t\t\treturn 0;\n\t\tint m = matrix.length;\n\t\tint n = matrix[0].length;\n\t\tint[] left = new int[n];\n\t\tint[] right = new int[n];\n\t\tint[] height = new int[n];\n\t\tArrays.fill(right, n);\n\t\tint cur_left = 0;\n\t\tint cur_right = n;\n\t\tint res = 0;\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tcur_left = 0;\n\t\t\tcur_right = n;\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (matrix[i][j] == '1')\n\t\t\t\t\theight[j]++;\n\t\t\t\telse\n\t\t\t\t\theight[j] = 0;\n\t\t\t}\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (matrix[i][j] == '1') {\n\t\t\t\t\tleft[j] = Math.max(left[j], cur_left);\n\t\t\t\t} else {\n\t\t\t\t\tleft[j] = 0;\n\t\t\t\t\tcur_left = j + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = n - 1; j >= 0; j--) {\n\t\t\t\tif (matrix[i][j] == '1') {\n\t\t\t\t\tright[j] = Math.min(right[j], cur_right);\n\t\t\t\t} else {\n\t\t\t\t\tright[j] = n;\n\t\t\t\t\tcur_right = j;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\tres = Math.max(res, (right[j] - left[j]) * height[j]);\n\t\t}\n\t\treturn res;\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef maximalRectangle(self, matrix):\n\t\t\"\"\"\n\t\t:type matrix: List[List[str]]\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tif matrix is None or len(matrix) == 0:\n\t\t\treturn 0\n\t\tls_row, ls_col = len(matrix), len(matrix[0])\n\t\tleft, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col\n\t\tmaxA = 0\n\t\tfor i in range(ls_row):\n\t\t\tcurr_left, curr_right = 0, ls_col\n\t\t\tfor j in range(ls_col):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\theight[j] += 1\n\t\t\t\telse:\n\t\t\t\t\theight[j] = 0\n\t\t\tfor j in range(ls_col):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\tleft[j] = max(left[j], curr_left)\n\t\t\t\telse:\n\t\t\t\t\tleft[j], curr_left = 0, j + 1\n\t\t\tfor j in range(ls_col - 1, -1, -1):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\tright[j] = min(right[j], curr_right)\n\t\t\t\telse:\n\t\t\t\t\tright[j], curr_right = ls_col, j\n\t\t\tfor j in range(ls_col):\n\t\t\t\tmaxA = max(maxA, (right[j] - left[j]) * height[j])\n\t\treturn maxA\n# %%\ns = Solution()\nmatrix = [[\"1\",\"0\",\"1\",\"0\",\"0\"],[\"1\",\"0\",\"1\",\"1\",\"1\"],[\"1\",\"1\",\"1\",\"1\",\"1\"],[\"1\",\"0\",\"0\",\"1\",\"0\"]]\nprint(s.maximalRectangle(matrix))",
"status": 1,
"keywords": "栈,数组,动态规划,矩阵,单调栈",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 86,
"question_title": "分隔链表",
"difficulty": "中等",
"question_content": "<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg\" style=\"width: 662px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>:[1,2,2,4,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [2,1], x = 2<strong><br />输出</strong>:[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li><code>-200 <= x <= 200</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470229",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct ListNode\n{\n\tint val;\n\tstruct ListNode *next;\n};\nstruct ListNode *partition(struct ListNode *head, int x)\n{\n\tstruct ListNode dummy;\n\tstruct ListNode *prev1 = &dummy, *pivot;\n\tdummy.next = head;\n\tfor (pivot = head; pivot != NULL; pivot = pivot->next)\n\t{\n\t\tif (pivot->val >= x)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tprev1 = pivot;\n\t}\n\tstruct ListNode *p = pivot->next;\n\tstruct ListNode *prev2 = pivot;\n\twhile (p != NULL)\n\t{\n\t\tif (p->val < x)\n\t\t{\n\t\t\tprev2->next = p->next;\n\t\t\tp->next = prev1->next;\n\t\t\tprev1->next = p;\n\t\t\tprev1 = p;\n\t\t\tp = prev2->next;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprev2 = p;\n\t\t\tp = p->next;\n\t\t}\n\t}\n\treturn dummy.next;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test target n1 n2 n3...\\n\");\n\t\texit(-1);\n\t}\n\tint i, target = atoi(argv[1]);\n\tstruct ListNode *head = NULL;\n\tstruct ListNode *prev = NULL;\n\tstruct ListNode *p;\n\tfor (i = 0; i < argc - 2; i++)\n\t{\n\t\tp = malloc(sizeof(*p));\n\t\tp->val = atoi(argv[i + 2]);\n\t\tp->next = NULL;\n\t\tif (head == NULL)\n\t\t{\n\t\t\thead = p;\n\t\t\tprev = head;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tprev->next = p;\n\t\t\tprev = p;\n\t\t}\n\t}\n\tp = partition(head, target);\n\twhile (p != NULL)\n\t{\n\t\tprintf(\"%d \", p->val);\n\t\tp = p->next;\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode(int x) {\n\t\tval = x;\n\t}\n}\nclass Solution {\n\tpublic ListNode partition(ListNode head, int x) {\n\t\tListNode dummyHead1 = new ListNode(0);\n\t\tListNode dummyHead2 = new ListNode(0);\n\t\tListNode node1 = dummyHead1;\n\t\tListNode node2 = dummyHead2;\n\t\twhile (head != null) {\n\t\t\tif (head.val < x) {\n\t\t\t\tnode1.next = head;\n\t\t\t\thead = head.next;\n\t\t\t\tnode1 = node1.next;\n\t\t\t\tnode1.next = null;\n\t\t\t} else {\n\t\t\t\tnode2.next = head;\n\t\t\t\thead = head.next;\n\t\t\t\tnode2 = node2.next;\n\t\t\t\tnode2.next = null;\n\t\t\t}\n\t\t}\n\t\tnode1.next = dummyHead2.next;\n\t\treturn dummyHead1.next;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef partition(self, head, x):\n\t\t\"\"\"\n\t\t:type head: ListNode\n\t\t:type x: int\n\t\t:rtype: ListNode\n\t\t\"\"\"\n\t\tif head is None:\n\t\t\treturn None\n\t\tless = lesshead = None\n\t\tlast = pos = head\n\t\twhile pos is not None:\n\t\t\tif pos.val < x:\n\t\t\t\tif lesshead is None:\n\t\t\t\t\tlesshead = pos\n\t\t\t\telse:\n\t\t\t\t\tless.next = pos\n\t\t\t\tless = pos\n\t\t\t\tif head == pos:\n\t\t\t\t\tlast = head = pos.next\n\t\t\t\telse:\n\t\t\t\t\tlast.next = pos.next\n\t\t\telse:\n\t\t\t\tlast = pos\n\t\t\tpos = pos.next\n\t\tif lesshead is not None:\n\t\t\tless.next = head\n\t\telse:\n\t\t\tlesshead = head\n\t\treturn lesshead\n# %%\nl = LinkList()\nlist1 = [1,4,3,2,5,2]\nl1 = l.initList(list1)\nx = 3\ns = Solution()\nprint(l.convert_list(s.partition(l1, x)))",
"status": 1,
"keywords": "链表,双指针",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_45176257/article/details/106487204"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/85/85_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/85/85_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/85/85_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 87,
"question_title": "扰乱字符串",
"difficulty": "困难",
"question_content": "<div class=\"notranslate\">使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code> :\n <ol>\n <li>如果字符串的长度为 1 ,算法停止</li>\n <li>如果字符串的长度 &gt; 1 ,执行下述步骤:\n <ul>\n <li>在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 <code>s</code> ,则可以将其分成两个子字符串 <code>x</code> 和 <code>y</code>\n ,且满足 <code>s = x + y</code> 。</li>\n <li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,<code>s</code> 可能是\n <code>s = x + y</code> 或者 <code>s = y + x</code> 。\n </li>\n <li>在 <code>x</code> 和 <code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>\n </ul>\n </li>\n </ol>\n\n <p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em>\n </em>和&nbsp;<code>s2</code>,判断&nbsp;<code>s2</code><em>&nbsp;</em>是否是&nbsp;<code>s1</code><em>&nbsp;</em>的扰乱字符串。如果是,返回\n <code>true</code> ;否则,返回 <code>false</code> 。\n </p>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n\n <pre><strong>输入:</strong>s1 = \"great\", s2 = \"rgeat\"\n<strong>输出:</strong>true\n<strong>解释:</strong>s1 上可能发生的一种情形是:\n\"great\" --&gt; \"gr/eat\" // 在一个随机下标处分割得到两个子字符串\n\"gr/eat\" --&gt; \"gr/eat\" // 随机决定:「保持这两个子字符串的顺序不变」\n\"gr/eat\" --&gt; \"g/r / e/at\" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割\n\"g/r / e/at\" --&gt; \"r/g / e/at\" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」\n\"r/g / e/at\" --&gt; \"r/g / e/ a/t\" // 继续递归执行此算法,将 \"at\" 分割得到 \"a/t\"\n\"r/g / e/ a/t\" --&gt; \"r/g / e/ a/t\" // 随机决定:「保持这两个子字符串的顺序不变」\n算法终止,结果字符串和 s2 相同,都是 \"rgeat\"\n这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true\n</pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>s1 = \"abcde\", s2 = \"caebd\"\n<strong>输出:</strong>false\n</pre>\n\n <p><strong>示例 3:</strong></p>\n\n <pre><strong>输入:</strong>s1 = \"a\", s2 = \"a\"\n<strong>输出:</strong>true\n</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>s1.length == s2.length</code></li>\n <li><code>1 &lt;= s1.length &lt;= 30</code></li>\n <li><code>s1</code> 和 <code>s2</code> 由小写英文字母组成</li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600470832",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2)\n{\n\tif (high1 - low1 != high2 - low2)\n\t{\n\t\treturn false;\n\t}\n\telse if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1))\n\t{\n\t\treturn true;\n\t}\n\telse\n\t{\n\t\tint i, c1[128] = {0}, c2[128] = {0};\n\t\tfor (i = low1; i <= high1; i++)\n\t\t{\n\t\t\tc1[s1[i]]++;\n\t\t}\n\t\tfor (i = low2; i <= high2; i++)\n\t\t{\n\t\t\tc2[s2[i]]++;\n\t\t}\n\t\tif (memcmp(c1, c2, 128 * sizeof(int)))\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint len = high1 - low1 + 1;\n\t\t\tfor (i = 1; i < len; i++)\n\t\t\t{\n\t\t\t\tif (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) &&\n\t\t\t\t\tscramble(s1, low1 + i, high1, s2, low2 + i, high2))\n\t\t\t\t{\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) &&\n\t\t\t\t\tscramble(s1, low1 + i, high1, s2, low2, high2 - i))\n\t\t\t\t{\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t}\n}\nstatic bool isScramble(char *s1, char *s2)\n{\n\treturn scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1);\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test s1 s2\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", isScramble(argv[1], argv[2]) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean isScramble(String s1, String s2) {\n\t\tif (s1.length() == 0 && s2.length() == 0)\n\t\t\treturn true;\n\t\tif (s1.length() != s2.length())\n\t\t\treturn false;\n\t\treturn dfs(s1.toCharArray(), s2.toCharArray(), 0, 0, s1.length());\n\t}\n\tprivate boolean dfs(char[] s1, char[] s2, int start1, int start2, int len) {\n\t\tif (len == 1) {\n\t\t\treturn s1[start1] == s2[start2];\n\t\t}\n\t\tif (!equals(s1, s2, start1, start2, len)) {\n\t\t\treturn false;\n\t\t}\n\t\tfor (int i = 1; i < len; i++) {\n\t\t\tif (dfs(s1, s2, start1, start2, i) && dfs(s1, s2, start1 + i, start2 + i, len - i))\n\t\t\t\treturn true;\n\t\t\tif (dfs(s1, s2, start1, start2 + len - i, i) && dfs(s1, s2, start1 + i, start2, len - i))\n\t\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\tpublic boolean equals(char[] s1, char[] s2, int start1, int start2, int len) {\n\t\tint[] charArr = new int[26];\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tcharArr[s1[start1 + i] - 'a']++;\n\t\t\tcharArr[s2[start2 + i] - 'a']--;\n\t\t}\n\t\tfor (int item : charArr) {\n\t\t\tif (item != 0)\n\t\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef isScramble(self, s1, s2, memo={}):\n\t\tif len(s1) != len(s2) or sorted(s1) != sorted(s2):\n\t\t\treturn False\n\t\tif len(s1) <= len(s2) <= 1:\n\t\t\treturn s1 == s2\n\t\tif s1 == s2:\n\t\t\treturn True\n\t\tif (s1, s2) in memo:\n\t\t\treturn memo[s1, s2]\n\t\tn = len(s1)\n\t\tfor i in range(1, n):\n\t\t\ta = self.isScramble(s1[:i], s2[:i], memo) and self.isScramble(s1[i:], s2[i:], memo)\n\t\t\tif not a:\n\t\t\t\tb = self.isScramble(s1[:i], s2[-i:], memo) and self.isScramble(s1[i:], s2[:-i], memo)\n\t\t\tif a or b:\n\t\t\t\tmemo[s1, s2] = True\n\t\t\t\treturn True\n\t\tmemo[s1, s2] = False\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isScramble(s1 = \"great\", s2 = \"rgeat\"))",
"status": 1,
"keywords": "字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_44833195/article/details/106370719"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/86/86_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/86/86_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/86/86_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 88,
"question_title": "合并两个有序数组",
"difficulty": "简单",
"question_content": "<p>给你两个有序整数数组 <code>nums1</code><em> </em>和 <code>nums2</code>,请你将 <code>nums2</code><em> </em>合并到 <code>nums1</code><em> </em>中<em>,</em>使 <code>nums1</code><em> </em>成为一个有序数组。</p><p>初始化 <code>nums1</code> 和 <code>nums2</code> 的元素数量分别为 <code>m</code> 和 <code>n</code><em> </em>。你可以假设 <code>nums1</code><em> </em>的空间大小等于 <code>m + n</code>,这样它就有足够的空间保存来自 <code>nums2</code> 的元素。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<strong><br />输出:</strong>[1,2,2,3,5,6]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1], m = 1, nums2 = [], n = 0<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>nums1.length == m + n</code></li>\t<li><code>nums2.length == n</code></li>\t<li><code>0 <= m, n <= 200</code></li>\t<li><code>1 <= m + n <= 200</code></li>\t<li><code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470926",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid merge(vector<int> &nums1, int m, vector<int> &nums2, int n)\n\t{\n\t\tint i = m - 1;\n\t\tint j = n - 1;\n\t\tint k = nums1.size() - 1;\n\t\twhile (i >= 0 && j >= 0)\n\t\t{\n\t\t\tif (nums1[i] < nums2[j])\n\t\t\t{\n\t\t\t\tnums1[k--] = nums2[j--];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tnums1[k--] = nums1[i--];\n\t\t\t}\n\t\t}\n\t\twhile (j >= 0)\n\t\t{\n\t\t\tnums1[k--] = nums2[j--];\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic void merge(int[] nums1, int m, int[] nums2, int n) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tnums1[m] = nums2[i];\n\t\t\tm++;\n\t\t}\n\t\tint temp = 0;\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tfor (int j = i; j < m; j++) {\n\t\t\t\tif (nums1[i] > nums1[j]) {\n\t\t\t\t\ttemp = nums1[j];\n\t\t\t\t\tnums1[j] = nums1[i];\n\t\t\t\t\tnums1[i] = temp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef merge(self, nums1, m, nums2, n):\n\t\t\"\"\"\n\t\t:type nums1: List[int]\n\t\t:type m: int\n\t\t:type nums2: List[int]\n\t\t:type n: int\n\t\t:rtype: void Do not return anything, modify nums1 in-place instead.\n\t\t\"\"\"\n\t\tp1, p2 = m - 1, n - 1\n\t\tpos = m + n - 1\n\t\twhile p1 >= 0 and p2 >= 0:\n\t\t\tif nums1[p1] >= nums2[p2]:\n\t\t\t\tnums1[pos] = nums1[p1]\n\t\t\t\tp1 -= 1\n\t\t\telse:\n\t\t\t\tnums1[pos] = nums2[p2]\n\t\t\t\tp2 -= 1\n\t\t\tpos -= 1\n\t\twhile p2 >= 0:\n\t\t\tnums1[pos] = nums2[p2]\n\t\t\tp2 -= 1\n\t\t\tpos -= 1\n\t\treturn nums1\n# %%\ns = Solution()\nprint(s.merge(nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3))",
"status": 1,
"keywords": "数组,双指针,排序",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "ttps://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/87/87_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/87/87_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/87/87_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 89,
"question_title": "格雷编码",
"difficulty": "中等",
"question_content": "<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>\n<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>\n<p>格雷编码序列必须以 0 开头。</p>\n<p>&nbsp;</p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>&nbsp;2<strong><br />输出:</strong>&nbsp;[0,1,3,2]<strong><br />解释:</strong>00 - 001 - 111 - 310 - 2对于给定的&nbsp;<em>n</em>,其格雷编码序列并不唯一。例如,[0,2,3,1]&nbsp;也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1</pre>\n<p><strong>示例&nbsp;2:</strong></p>\n<pre><strong>输入:</strong>&nbsp;0<strong><br />输出:</strong>&nbsp;[0]<strong><br />解释:</strong> 我们定义格雷编码序列必须以 0 开头。给定编码总位数为 <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup>。当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</pre>",
"topic_link": "https://bbs.csdn.net/topics/600470797",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nint *grayCode(int n, int *returnSize)\n{\n\tif (n < 0)\n\t{\n\t\treturn NULL;\n\t}\n\tint i, count = 1 << n;\n\tint *codes = malloc(count * sizeof(int));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tcodes[i] = (i >> 1) ^ i;\n\t}\n\t*returnSize = 1 << n;\n\treturn codes;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n\\n\");\n\t\texit(-1);\n\t}\n\tint i, count;\n\tint *list = grayCode(atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%d \", list[i]);\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic List<Integer> grayCode(int n) {\n\t\tList<Integer> res = new ArrayList<>();\n\t\tres.add(0);\n\t\tint cur;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint change = 1 << i;\n\t\t\tcur = res.size() - 1;\n\t\t\twhile (cur >= 0) {\n\t\t\t\tres.add(res.get(cur) ^ change);\n\t\t\t\tcur--;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef grayCode(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: List[int]\n\t\t\"\"\"\n\t\tres = [0]\n\t\tfor i in range(n):\n\t\t\tfor j in reversed(range(len(res))):\n\t\t\t\tres.append(res[j] + (1 << i))\n\t\treturn res\nif __name__ == \"__main__\":\n\ts = Solution()\n\tprint (s.grayCode(2))",
"status": 1,
"keywords": "位运算,数学,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/qq_25406563/article/details/85065072"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/88/88_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/88/88_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/88/88_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 90,
"question_title": "子集 II",
"difficulty": "中等",
"question_content": "<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p><div class=\"original__bRMd\"><div><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,2]<strong><br />输出:</strong>[[],[1],[1,2],[1,2,2],[2],[2,2]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= nums.length <= 10</code></li>\t<li><code>-10 <= nums[i] <= 10</code></li></ul></div></div>",
"topic_link": "https://bbs.csdn.net/topics/600469825",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<vector<int>> subsetsWithDup(vector<int> &nums)\n\t{\n\t\tvector<vector<int>> res;\n\t\tsort(nums.begin(), nums.end());\n\t\tdfs(nums, 0, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector<int> stack;\n\tvoid dfs(vector<int> &nums, int start, vector<vector<int>> &res)\n\t{\n\t\tres.push_back(stack);\n\t\tint last = INT_MIN;\n\t\tfor (int i = start; i < nums.size(); i++)\n\t\t{\n\t\t\tif (last != nums[i])\n\t\t\t{\n\t\t\t\tstack.push_back(nums[i]);\n\t\t\t\tdfs(nums, i + 1, res);\n\t\t\t\tstack.pop_back();\n\t\t\t}\n\t\t\tlast = nums[i];\n\t\t}\n\t}\n};",
"java": "class Solution {\n\tpublic List<List<Integer>> subsetsWithDup(int[] nums) {\n\t\tList<List<Integer>> retList = new ArrayList<>();\n\t\tretList.add(new ArrayList<>());\n\t\tif (nums == null || nums.length == 0)\n\t\t\treturn retList;\n\t\tArrays.sort(nums);\n\t\tList<Integer> tmp = new ArrayList<>();\n\t\ttmp.add(nums[0]);\n\t\tretList.add(tmp);\n\t\tif (nums.length == 1)\n\t\t\treturn retList;\n\t\tint lastLen = 1;\n\t\tfor (int i = 1; i < nums.length; i++) {\n\t\t\tint size = retList.size();\n\t\t\tif (nums[i] != nums[i - 1]) {\n\t\t\t\tlastLen = size;\n\t\t\t}\n\t\t\tfor (int j = size - lastLen; j < size; j++) {\n\t\t\t\tList<Integer> inner = new ArrayList(retList.get(j));\n\t\t\t\tinner.add(nums[i]);\n\t\t\t\tretList.add(inner);\n\t\t\t}\n\t\t}\n\t\treturn retList;\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef subsetsWithDup(self, nums):\n\t\tnums.sort()\n\t\tres = [[]]\n\t\tbegin = 0\n\t\tfor index in range(len(nums)):\n\t\t\tif index == 0 or nums[index] != nums[index - 1]:\n\t\t\t\tbegin = 0\n\t\t\tsize = len(res)\n\t\t\tfor j in range(begin, size):\n\t\t\t\tcurr = list(res[j])\n\t\t\t\tcurr.append(nums[index])\n\t\t\t\tres.append(curr)\n\t\t\tbegin = size\n\t\treturn res\n# %%\ns = Solution()\nprint(s.subsetsWithDup(nums = [1,2,2]))",
"status": 1,
"keywords": "位运算,数组,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104361948"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/89/89_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/89/89_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/89/89_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 10,
"question_title": "正则表达式匹配",
"difficulty": "困难",
"question_content": "<p>给你一个字符串 <code>s</code> 和一个字符规律 <code>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul>\t<li><code>'.'</code> 匹配任意单个字符</li>\t<li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"aa\" p = \"a\"<strong><br />输出:</strong>false<strong><br />解释:</strong>\"a\" 无法匹配 \"aa\" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"aa\" p = \"a*\"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 \"aa\" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"ab\" p = \".*\"<strong><br />输出:</strong>true<strong><br />解释:</strong>\".*\" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \"aab\" p = \"c*a*b\"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 \"aab\"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = \"mississippi\" p = \"mis*is*p*.\"<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= s.length <= 20</code></li>\t<li><code>0 <= p.length <= 30</code></li>\t<li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li>\t<li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code>。</li>\t<li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470835",
"cpp": "bool isMatch(char *s, char *p)\n{\n\tif (!p || p[0] == NULL)\n\t\treturn (!s || s[0] == NULL);\n\tbool head_match = (s && s[0] && (s[0] == p[0] || p[0] == '.'));\n\tif (p[1] && p[1] == '*')\n\t{\n\t\treturn (head_match && isMatch(s + 1, p)) || isMatch(s, p + 2);\n\t}\n\telse\n\t{\n\t\treturn head_match && isMatch(s + 1, p + 1);\n\t}\n}",
"java": "class Solution {\n\tpublic boolean isMatch(String s, String p) {\n\t\tif (p.length() == 0)\n\t\t\treturn s.length() == 0;\n\t\tboolean head_match = s.length() > 0 && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');\n\t\tif (p.length() > 1 && p.charAt(1) == '*') {\n\t\t\treturn (head_match && isMatch(s.substring(1), p)) || isMatch(s, p.substring(2));\n\t\t} else {\n\t\t\treturn head_match && isMatch(s.substring(1), p.substring(1));\n\t\t}\n\t}\n}",
"js": "/**\n * @param {string} s\n * @param {string} p\n * @return {boolean}\n */\nvar isMatch = function(s, p) {\n \n let M = s.length;\n let N = p.length;\n let i = 0;\n let j = 0;\n\tlet k = 0;\n\t\n let ccid = 0;\n\n\tlet continues = [];\n\tlet preDotStarPos = {};\n let preCharStarPos = {};\n\n let c = null;\n\n const matchEmptyStar = (force)=>{\n \tlet hint = false;\n\n \t//...[\\m]\\n\n \t//...[\\w]*\n \tlet p1 = j+1<N && i<M && (s[i]!==p[j] && p[j]!=='.') && p[j+1]==='*';\n \tlet p2 = j+1<N && i<M && (force && p[j]!=='.') && p[j+1]==='*';\n\n \t//...[]\n \t//...[\\w]*\n \tlet p3 = j+1<N && i>=M && p[j+1]==='*';\n\n \tif(p1||p2||p3){\n \t\tj += 2;\n \t\thint = true;\n \t}\n\n \treturn hint;\n };\n\n const matchCharStar = ()=>{\n \tlet hint = false;\n \t\n \tlet p1 = i<M && j+1<N && p[j]!=='.' && p[j]===s[i] && p[j+1]==='*';\n\n \tif (p1) {\n \t\t\n \t\tif(c!=null&&c!=='.'&&p[j]!==c){\n \t\t\tk=0;\n \t\t}\n\n \t\ti += 1;\n \t\tc = p[j];\n \t\tk += 1;\n \t\thint = true;\n \t}\n \treturn hint;\n }\n\n const matchDotStar = ()=>{\n \tlet hint = false;\n \tlet p1 = i<M && j+1<=N && p[j]==='.' && p[j+1]==='*';\n\n \tif(p1){\n \t\tif(j+1<N-1){\n \t\t\tk += M-i;\t\n \t\t\ti = M;\n \t\t}else{\n \t\t\tk= 0;\n \t\t\ti = M;\n \t\t}\n \t\tc = p[j];\n \t\t\n \t\thint = true;\n \t}\n \treturn hint;\n }\n\n const matchCharContinue = ()=>{\n \tlet hint = false;\n \tlet p1 = j+1<N && (p[j]===c||p[j]==='.') && p[j+1]!=='*' && k>0;\n \tlet p2 = j+1===N && (p[j]===c||p[j]==='.') && k>0;\n\n \tif(p1||p2){\n \t\tk -= 1;\n \t\tj += 1;\n \t\thint = true;\n \t}\n \treturn hint;\n };\n\n const matchDotContinue = (start)=>{\n \tlet hint = false;\n \tlet p1 = c==='.' && k>0 && (p[j]!=='.'&&p[j]!==s[i]) && p[j+1]!=='*';\n \tif(p1 && (i-1)>start){\n \t\ti -= 1;\n \t\tk -= 1;\n \t\thint = true;\n \t}\n \treturn hint;\n };\n\n const matchChar = ()=>{\n \tlet hint = false;\n \tlet p1 = i<M && j<N && p[j]===s[i] && p[j+1]!=='*';\n \tlet p2 = i<M && j<N && p[j]==='.' && p[j+1]!=='*';\n \tif(p1||p2){\n \t\ti += 1;\n \t\tj += 1;\n \t\tc = s[i];\n \t\tk = 0;\n \t\thint = true;\n \t}\n \treturn hint;\n };\n\n const matchFinishState = ()=>{\n\t\tlet hint = false;\n \tlet p1 = i<M && j<N && (p[j]!=='.' && p[j+1]!=='*') && p[j]!==s[i];\n \tlet p2 = i<M && j<N && (p[j]==='.' && p[j+1]!=='*');\n\t\tlet p3 = i===M && j<N && (p[j+1]!=='*');\n\t\tlet p4 = i>M||j>=N;\n\n \tif(p1||p2||p3||p4){\n \t\thint = true;\n \t}\n \treturn hint;\n\t};\n\t\n\n\tconst preDotStar = ()=>{\n\t\tpreDotStarPos = {\n\t\t\ti,j,k,c\n\t\t};\n\t};\n\n const saveDotStar = ()=>{\n \tif(k>0){\n\t\t\tcontinues.push({\n\t\t\t\ttype: 0,\n\t\t\t\tccid,\n\t\t\t\tstart: preDotStarPos.i,\n\t\t\t\ti,j,k,c,\n\t\t\t});\n\t\t\tccid++;\n \t}\n };\n\n const preCharStar = ()=>{\n \tpreCharStarPos = {\n \t\ti,j,k,c\n \t};\n };\n\n const saveCharStar = ()=>{\n \tconst {i,j,k,c} = preCharStarPos;\n\t\tcontinues.push({\n\t\t\ttype: 1,\n\t\t\ti,j,k,c,\n\t\t\tccid\n\t\t});\n \tccid++;\n };\n\n const updateDotStar = ()=>{\n \tif(continues.length===0) {\n \t\treturn;\n \t}\n\n \tlet cc = continues[continues.length-1];\n \tif(cc.type===0){\n \t\tcc.k = k;\n\t \tcc.i = i;\n\t \tif(cc.k===0){\n\t \t\tcontinues.pop();\n\t \t}\n \t}\n }\n\n const load = ()=>{\n \tlet cc;\n \twhile(continues.length>0){\n \t\tlet ccc = continues[continues.length-1];\n \t\tif(ccc.type===0){\n \t\t\tif(ccc.k>0&&ccc.i>ccc.start){\n\t \t\t\tcc = ccc;\n\t \t\t\tbreak;\n\t \t\t}else{\n\t \t\t\tcontinues.pop();\n\t \t\t}\n \t\t}else{\n \t\t\tcc = ccc;\n \t\t\tcontinues.pop();\n \t\t\tbreak;\n \t\t}\n \t}\n\n \tif(cc==null){\n \t\treturn null;\n \t}\n \t\n \tif(cc.type===0){\n \t\ti = cc.i-1;\n\t \tj = cc.j;\n\t \tk = cc.k;\n\t \tc = cc.c;\t\n \t}else{\n \t\ti = cc.i;\n\t \tj = cc.j;\n\t \tk = cc.k;\n\t \tc = cc.c;\n \t}\n\n \treturn cc;\n\t}\n\t\n\tconst matchDotStars = ()=>{\n\t\twhile(matchEmptyStar()){}\n\t\tpreDotStar();\n\t\tif(matchDotStar()){\n\t\t\tj+=2;\n\t\t\twhile(matchEmptyStar()){}\t\n\t\t\twhile(matchDotContinue(preDotStarPos.i)) {\n\t\t\t\twhile(matchEmptyStar()){}\n\t\t\t}\n\t\t\tsaveDotStar();\n\t\t}\n\t\twhile(matchEmptyStar()){}\n\t}\n\n\tconst gotoDotStarBranch = (cc)=>{\n\t\twhile(matchDotContinue(cc.start)) {\n\t\t\twhile(matchEmptyStar()){}\n\t\t}\n\t\tupdateDotStar();\n\t}\n\n\tconst matchCharStars = ()=>{\n\t\tlet step = false;\n\t\tpreCharStar();\n\t\twhile(matchCharStar()){\n\t\t\tstep = true;\n\t\t\tsaveCharStar();\n\t\t\tpreCharStar();\n\t\t}\n\t\tif(step){\n\t\t\tj+=2;\n\t\t\twhile(true) {\n\t\t\t\twhile(matchEmptyStar()){}\n\t\t\t\tpreCharStar();\n\t\t\t\tif(!matchCharContinue()){\n\t\t\t\t\tbreak;\n\t\t\t\t}else{\n\t\t\t\t\tsaveCharStar();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile(matchEmptyStar()){}\n\t};\n\n\tconst gotoCharStarBranch = (cc)=>{\n\t\twhile(matchEmptyStar(true)){}\n\t}\n\n\tconst matchChars = ()=>{\n\t\twhile(matchChar()){\n\t\t\twhile(matchEmptyStar()){}\n\t\t}\n\t\twhile(matchEmptyStar()){}\n\t};\n\n const match = ()=>{\n \tlet count = 0;\n\n \twhile (true) {\n \t\t// .*\n \t\tmatchDotStars();\n\n\t\t\t// \\w*\n\t\t\tmatchCharStars();\n\n \t\t// \\w\\w\n \t\tmatchChars();\n\n \t\t// finish state\n \t\tif(matchFinishState()){\n\n\t\t\t\t// test real finish\n \t\t\tif(i===M&&j===N){\n \t\t\t\treturn true;\n \t\t\t}\n\n\t\t\t\t// else test if have continuation\n \t\t\tconst cc = load();\n \t\t\tif(cc==null){\n\t\t\t \treturn i===M && j===N;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// goto continuation and recover\n\t\t\t\tif(cc.type===0){\n\t\t\t\t\tgotoDotStarBranch(cc);\n\t\t\t\t}else{\n\t\t\t\t\tgotoCharStarBranch(cc);\n\t\t\t\t}\n \t\t}\n\t\t\twhile(matchEmptyStar()){}\n\n \t\t// count\n \t\tcount++;\n \t}\n };\n\n return match();\n}\n\nfunction main(){\n\tconst tests = [\n\t\t[\"aa\",\"a\",false],\n\t\t[\"aa\",\"a*\",true],\n\t\t[\"ab\",\".*\",true],\n\t\t[\"aab\",\"c*a*b\",true],\n\t\t[\"mississippi\",\"mis*is*ip*.\",true],\n\t\t[\"mississippi\",\"mis*is*p*.\",false],\n\t\t[\"a\",\"a*\",true],\n\t\t[\"aaaaaa\",\"a*a\",true],\n\t\t[\"aaaaaa\",\"a*aa\",true],\n\t\t[\"aaaaaa\",\"a*aaaaa\",true],\n\t\t[\"aaaaaa\",\"a*aaaaaa\",true],\n\t\t[\"aaaaaa\",\"a*aaaaaaa\",false],\n\t\t[\"a\",\"a*\",true],\n\t\t[\"a\",\"a*b\",false],\n\t\t[\"a\",\".*\",true],\n\t\t[\"aaa\",\"ab*a*c*a\", true],\n\t\t[\"aaa\",\"ab*ac*a\",true],\n\t\t[\"abcd\",\"d*\",false],\n\t\t[\"aaba\",\"ab*a*c*a\",false],\n\t\t[\"a\",\"ab*\",true],\n\t\t[\"bbbba\",\".*a*a\",true],\n\t\t[\"ab\",\".*..\",true],\n\t\t[\"\",\".*\",true],\n\t\t[\"\",\".*a*c*\",true],\n\t\t[\"\",\".*a*c*a\",false],\n\t\t[\"ab\",\".ab*a*c*\",false],\n\t\t[\"ab\",\"ab*a*c*\",true],\n\t\t[\"a\",\".*..a*\",false],\n\t\t[\"a\",\".*.aa*\",false],\n\t\t[\"a\",\".*.ba*\",false],\n\t\t[\"a\",\".*.a*\",true],\n\t\t[\"a\",\".*aa*\",true],\n\t\t[\"b\",\".*aa*\",false],\n\t\t[\"ababab\",\".*babab\",true],\n\t\t[\"ccccccccca\",\".*aa*\",true],\n\t\t[\"cccccccccaa\",\".*aa*\",true],\n\t\t[\"cccccccccaaa\",\".*aa*\",true],\n\t\t[\"cccccccccaaaa\",\".*aa*\",true],\n\t\t[\"cccccccccaaaaa\",\".*aa*\",true],\n\t\t[\"bbbbbbabbbbbbba\",\".*a\",true],\n\t\t[\"aasdfasdfasdfasdfas\",\"aasdf.*asdf.*asdf.*asdf.*s\",true],\n\t\t[\"ba\", \".*.\", true],\n\t\t[\"ba\", \".*..\", true],\n\t\t[\"ba\", \".*...\", false],\n\t\t[\"abbaaaabaabbcba\", \"a*.*ba.*c*..a*.a*.\", true],\n\t\t[\"ba\",\".a*.\",true],\n\t\t[\"bab\",\".a*.\",true],\n\t\t[\"ba\",\".a*a\",true],\n\t\t[\"baa\",\".a*a\",true],\n\t\t[\"baaa\",\".a*ac*a\",true],\n\t\t[\"bbcacbabbcbaaccabc\",\"b*a*a*.c*bb*b*.*.*\",true],\n\t\t[\"abacacccbbbcbcbb\", \".*.*.*ab*.*ab.*c*\", false],\n\t];\n\n\n\tfor(const t of tests){\n\t\tconst v = isMatch(t[0],t[1]);\n\t\tif(v===t[2]){\n\t\t\tconsole.log(`[OK] ${t[0]}, ${t[1]}, ${t[2]}===${v}`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR] ${t[0]}, ${t[1]}, ${t[2]}===${v}`);\n\t\t\tprocess.exit(0);\n\t\t}\n\t}\n}\n\nmain();\n",
"python": "class Solution:\n\tdef isMatch(self, s: str, p: str) -> bool:\n\t\tif len(p) == 0:\n\t\t\treturn len(s) == 0\n\t\thead_match = len(s) > 0 and (s[0] == p[0] or p[0] == '.')\n\t\tif len(p) > 1 and p[1] == '*':\n\t\t\tif head_match and self.isMatch(s[1:], p):\n\t\t\t\treturn True\n\t\t\treturn self.isMatch(s, p[2:])\n\t\telse:\n\t\t\tif not head_match:\n\t\t\t\treturn False\n\t\t\treturn self.isMatch(s[1:], p[1:])\n# %%\ns = Solution()\nprint(s.isMatch(s = \"aa\" , p = \"a\"))",
"status": 1,
"keywords": "递归,字符串,动态规划",
"license": {
"cpp": "csdn.net",
"python": "csdn.net",
"java": "csdn.net"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/9/9_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/9/9_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/9/9_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 91,
"question_title": "解码方法",
"difficulty": "中等",
"question_content": "<p>一条包含字母 <code>A-Z</code> 的消息通过以下映射进行了 <strong>编码</strong> :</p>\n<pre>'A' -> 1'B' -> 2...'Z' -> 26</pre>\n<p>要 <strong>解码</strong> 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,<code>\"11106\"</code> 可以映射为:</p>\n<ul>\n <li><code>\"AAJF\"</code> ,将消息分组为 <code>(1 1 10 6)</code></li>\n <li><code>\"KJF\"</code> ,将消息分组为 <code>(11 10 6)</code></li>\n</ul>\n<p>注意,消息不能分组为  <code>(1 11 06)</code> ,因为 <code>\"06\"</code> 不能映射为 <code>\"F\"</code> ,这是由于 <code>\"6\"</code> 和\n <code>\"06\"</code> 在映射中并不等价。\n</p>\n<p>给你一个只含数字的 <strong>非空 </strong>字符串 <code>s</code> ,请计算并返回 <strong>解码</strong> 方法的 <strong>总数</strong> 。</p>\n<p>题目数据保证答案肯定是一个 <strong>32 位</strong> 的整数。</p>\n<p> </p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong>s = \"12\"<strong><br />输出:</strong>2<strong><br />解释:</strong>它可以解码为 \"AB\"(1 2)或者 \"L\"(12)。</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong>s = \"226\"<strong><br />输出:</strong>3<strong><br />解释:</strong>它可以解码为 \"BZ\" (2 26), \"VF\" (22 6), 或者 \"BBF\" (2 2 6) 。</pre>\n<p><strong>示例 3:</strong></p>\n<pre><strong>输入:</strong>s = \"0\"<strong><br />输出:</strong>0<strong><br />解释:</strong>没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> \"10\" 和 'T'-> \"20\" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。</pre>\n<p><strong>示例 4:</strong></p>\n<pre><strong>输入:</strong>s = \"06\"<strong><br />输出:</strong>0<strong><br />解释:</strong>\"06\" 不能映射到 \"F\" ,因为字符串含有前导 0(\"6\"\"06\" 在映射中并不等价)。</pre>\n<p> </p>\n<p><strong>提示:</strong></p>\n<ul>\n <li><code>1 <= s.length <= 100</code></li>\n <li><code>s</code> 只包含数字,并且可能包含前导零。</li>\n</ul>",
"topic_link": "https://bbs.csdn.net/topics/600469925",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic int numDecodings(char *s)\n{\n\tint len = strlen(s);\n\tif (len == 0)\n\t{\n\t\treturn 0;\n\t}\n\tint a = 1;\n\tint b = s[0] == '0' ? 0 : a;\n\tint c = b;\n\tfor (int i = 2; i <= len; i++)\n\t{\n\t\tc = s[i - 1] == '0' ? 0 : b;\n\t\tint num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');\n\t\tif (num >= 10 && num <= 26)\n\t\t{\n\t\t\tc += a;\n\t\t}\n\t\ta = b;\n\t\tb = c;\n\t}\n\treturn c;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test number\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%d\\n\", numDecodings(argv[1]));\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic int numDecodings(String s) {\n\t\tif (s == null || s.length() == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint n = s.length();\n\t\tint[] dp = new int[n + 1];\n\t\tdp[0] = 1;\n\t\tdp[1] = (s.charAt(0) == '0' ? 0 : 1);\n\t\tfor (int i = 1; i < n; i++) {\n\t\t\tchar c = s.charAt(i);\n\t\t\tchar pre = s.charAt(i - 1);\n\t\t\tdp[i + 1] = c == '0' ? 0 : dp[i];\n\t\t\tif (pre == '1' || (pre == '2' && c <= '6')) {\n\t\t\t\tdp[i + 1] += dp[i - 1];\n\t\t\t}\n\t\t}\n\t\treturn dp[n];\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef numDecodings(self, s):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tls = len(s)\n\t\tif ls == 0:\n\t\t\treturn 0\n\t\tdp = [0] * ls\n\t\tfor index in range(ls):\n\t\t\tif index >= 1 and int(s[index - 1:index + 1]) < 27 and int(s[index - 1:index + 1]) >= 10:\n\t\t\t\tif index == 1:\n\t\t\t\t\tdp[index] = 1\n\t\t\t\telse:\n\t\t\t\t\tdp[index] += dp[index - 2]\n\t\t\tif int(s[index]) != 0:\n\t\t\t\tif index == 0:\n\t\t\t\t\tdp[index] = 1\n\t\t\t\telse:\n\t\t\t\t\tdp[index] += dp[index - 1]\n\t\treturn dp[ls - 1]\n# %%\ns = Solution()\nprint(s.numDecodings(s = \"12\"))",
"status": 1,
"keywords": "字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104364994"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 92,
"question_title": "反转链表 II",
"difficulty": "中等",
"question_content": "给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。<p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg\" style=\"width: 542px; height: 222px;\" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>链表中节点数目为 <code>n</code></li>\t<li><code>1 <= n <= 500</code></li>\t<li><code>-500 <= Node.val <= 500</code></li>\t<li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>",
"topic_link": "https://bbs.csdn.net/topics/600469826",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct ListNode\n{\n\tint val;\n\tstruct ListNode *next;\n};\nstatic struct ListNode *reverseBetween(struct ListNode *head, int m, int n)\n{\n\tint i;\n\tstruct ListNode dummy;\n\tstruct ListNode *prev = &dummy;\n\tprev->next = head;\n\tfor (i = 1; i < m; i++)\n\t{\n\t\tprev = prev->next;\n\t}\n\tstruct ListNode *p = prev->next;\n\tfor (i = m; i < n; i++)\n\t{\n\t\tstruct ListNode *q = p->next;\n\t\tp->next = q->next;\n\t\tq->next = prev->next;\n\t\tprev->next = q;\n\t}\n\treturn dummy.next;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 3)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test m n 1 2 3...\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = argc - 3;\n\tstruct ListNode dummy;\n\tstruct ListNode *prev = &dummy;\n\tstruct ListNode *p;\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tp = malloc(sizeof(*p));\n\t\tp->val = atoi(argv[i + 3]);\n\t\tp->next = NULL;\n\t\tprev->next = p;\n\t\tprev = p;\n\t}\n\tint m = atoi(argv[1]);\n\tint n = atoi(argv[2]);\n\tstruct ListNode *head = reverseBetween(dummy.next, m, n);\n\tfor (p = head; p != NULL; p = p->next)\n\t{\n\t\tprintf(\"%d \", p->val);\n\t}\n\tprintf(\"\\n\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic ListNode reverseBetween(ListNode head, int m, int n) {\n\t\tListNode dummy = new ListNode(0);\n\t\tdummy.next = head;\n\t\tListNode pre = dummy;\n\t\tfor (int i = 1; i < m; i++) {\n\t\t\tpre = pre.next;\n\t\t}\n\t\thead = pre.next;\n\t\tfor (int i = m; i < n; i++) {\n\t\t\tListNode nex = head.next;\n\t\t\thead.next = nex.next;\n\t\t\tnex.next = pre.next;\n\t\t\tpre.next = nex;\n\t\t}\n\t\treturn dummy.next;\n\t}\n}",
"js": "",
"python": "class ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef reverseBetween(self, head, m, n):\n\t\t\"\"\"\n\t\t:type head: ListNode\n\t\t:type m: int\n\t\t:type n: int\n\t\t:rtype: ListNode\n\t\t\"\"\"\n\t\tif m == n:\n\t\t\treturn head\n\t\tsplit_node, prev, curr = None, None, head\n\t\tcount = 1\n\t\twhile count <= m and curr is not None:\n\t\t\tif count == m:\n\t\t\t\tsplit_node = prev\n\t\t\tprev = curr\n\t\t\tcurr = curr.next\n\t\t\tcount += 1\n\t\ttail, next_node = prev, None\n\t\twhile curr is not None and count <= n:\n\t\t\tnext_temp = curr.next\n\t\t\tcurr.next = prev\n\t\t\tprev = curr\n\t\t\tcurr = next_temp\n\t\t\tcount += 1\n\t\tif split_node is not None:\n\t\t\tsplit_node.next = prev\n\t\tif tail is not None:\n\t\t\ttail.next = curr\n\t\tif m == 1:\n\t\t\treturn prev\n\t\treturn head\n# %%\nl = LinkList()\nlist1 = [1,2,3,4,5]\nl1 = l.initList(list1)\nleft = 2\nright = 4\ns = Solution()\nprint(l.convert_list(s.reverseBetween(l1, left, right)))",
"status": 1,
"keywords": "链表",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_44833195/article/details/106370716"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/91/91_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 93,
"question_title": "复原 IP 地址",
"difficulty": "中等",
"question_content": "<p>给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 <code>0</code>),整数之间用 <code>'.'</code> 分隔。</p><p>例如:\"0.1.2.201\"\"192.168.1.1\" 是 <strong>有效</strong> IP 地址,但是 \"0.011.255.245\"\"192.168.1.312\"\"192.168@1.1\" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"25525511135\"<strong><br />输出:</strong>[\"255.255.11.135\",\"255.255.111.35\"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"0000\"<strong><br />输出:</strong>[\"0.0.0.0\"]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"1111\"<strong><br />输出:</strong>[\"1.1.1.1\"]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \"010010\"<strong><br />输出:</strong>[\"0.10.0.10\",\"0.100.1.0\"]</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = \"101023\"<strong><br />输出:</strong>[\"1.0.10.23\",\"1.0.102.3\",\"10.1.0.23\",\"10.10.2.3\",\"101.0.2.3\"]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= s.length <= 3000</code></li>\t<li><code>s</code> 仅由数字组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470923",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool valid(char *ip, int len)\n{\n\tif (len > 1 && ip[0] == '0')\n\t{\n\t\treturn false;\n\t}\n\tif (len == 3)\n\t{\n\t\tint n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0');\n\t\tif (n > 255)\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n#define WIDTH 4\nstatic void dfs(char *s, int start, char *stack, int num, char **results, int *count)\n{\n\tint i, j;\n\tif (num == 4)\n\t{\n\t\tif (s[start] == '\\0')\n\t\t{\n\t\t\tresults[*count] = malloc(3 * 4 + 3 + 1);\n\t\t\tchar *p = results[*count];\n\t\t\tfor (j = 0; j < num; j++)\n\t\t\t{\n\t\t\t\tchar *q = stack + j * WIDTH;\n\t\t\t\twhile ((*p++ = *q++) != '\\0')\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t\tif (j != 3)\n\t\t\t\t{\n\t\t\t\t\t*(p - 1) = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t\t(*count)++;\n\t\t}\n\t}\n\telse\n\t{\n\t\tchar *p = stack + num * WIDTH;\n\t\tchar *q = p;\n\t\tfor (i = start; s[i] != '\\0' && i < start + 3; i++)\n\t\t{\n\t\t\t*q++ = s[i];\n\t\t\t*q = '\\0';\n\t\t\tif (!valid(p, q - p))\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdfs(s, i + 1, stack, num + 1, results, count);\n\t\t\tif (num + 1 < 4)\n\t\t\t{\n\t\t\t\tmemset(stack + (num + 1) * WIDTH, 0, WIDTH);\n\t\t\t}\n\t\t}\n\t}\n}\nstatic char **restoreIpAddresses(char *s, int *returnSize)\n{\n\tint count = 0;\n\tchar **results = malloc(100 * sizeof(char *));\n\tchar addr[16] = {'\\0'};\n\tdfs(s, 0, addr, 0, results, &count);\n\t*returnSize = count;\n\treturn results;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test num\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = 0;\n\tchar **list = restoreIpAddresses(argv[1], &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%s\\n\", list[i]);\n\t}\n}",
"java": "class Solution {\n\tprivate List<String> res = new ArrayList<>();\n\n\tpublic List<String> restoreIpAddresses(String s) {\n\t\tif (s.length() < 4)\n\t\t\treturn res;\n\t\tbacktrack(s, 0, new StringBuilder(), 0);\n\t\treturn res;\n\t}\n\n\tprivate void backtrack(String s, int start, StringBuilder sb, int pointNumOfSb) {\n\t\tif (pointNumOfSb > 4)\n\t\t\treturn;\n\t\tif (start == s.length() && pointNumOfSb == 4) {\n\t\t\tres.add(sb.toString().substring(1));\n\t\t\treturn;\n\t\t}\n\t\tfor (int i = start; i < s.length() && i - start < 3; i++) {\n\t\t\tString x = s.substring(start, i + 1);\n\t\t\tif (x.charAt(0) == '0' && x.length() > 1)\n\t\t\t\treturn;\n\t\t\tif (Integer.parseInt(x) <= 255) {\n\t\t\t\tsb.append(\".\" + x);\n\t\t\t\tbacktrack(s, i + 1, sb, pointNumOfSb + 1);\n\t\t\t\tsb.delete(sb.lastIndexOf(\".\"), sb.length());\n\t\t\t}\n\t\t}\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef restoreIpAddresses(self, s):\n\t\tls = len(s)\n\t\tif ls == 0 or ls > 12:\n\t\t\treturn []\n\t\tres = []\n\t\tfor i in range(1, 4):\n\t\t\tfor j in range(1, 4):\n\t\t\t\tfor k in range(1, 4):\n\t\t\t\t\tm = ls - i - j - k\n\t\t\t\t\tif m > 0 and m <= 3:\n\t\t\t\t\t\tadd1 = s[0:i]\n\t\t\t\t\t\tadd2 = s[i:i + j]\n\t\t\t\t\t\tadd3 = s[i + j:i + j + k]\n\t\t\t\t\t\tadd4 = s[i + j + k:]\n\t\t\t\t\t\tif self.isValid(add1) and self.isValid(add2) and \\\n\t\t\t\t\t\t\t\t\t\tself.isValid(add3) and self.isValid(add4):\n\t\t\t\t\t\t\tres.append(add1 + '.' + add2 + '.' + add3 + '.' + add4)\n\t\treturn res\n\tdef isValid(self, add):\n\t\tif len(add) == 1:\n\t\t\treturn True\n\t\tif add[0] == '0':\n\t\t\treturn False\n\t\tif int(add) <= 255:\n\t\t\treturn True\n\t\treturn False\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.restoreIpAddresses('25525511135'))",
"status": 1,
"keywords": "字符串,回溯",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 94,
"question_title": "二叉树的中序遍历",
"difficulty": "简单",
"question_content": "<p>给定一个二叉树的根节点 <code>root</code> ,返回它的 <strong>中序</strong> 遍历。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_1.jpg\" style=\"width: 202px; height: 324px;\" /><pre><strong>输入:</strong>root = [1,null,2,3]<strong><br />输出:</strong>[1,3,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>root = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>root = [1]<strong><br />输出:</strong>[1]</pre><p><strong>示例 4:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_5.jpg\" style=\"width: 202px; height: 202px;\" /><pre><strong>输入:</strong>root = [1,2]<strong><br />输出:</strong>[2,1]</pre><p><strong>示例 5:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_4.jpg\" style=\"width: 202px; height: 202px;\" /><pre><strong>输入:</strong>root = [1,null,2]<strong><br />输出:</strong>[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>树中节点数目在范围 <code>[0, 100]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong> 递归算法很简单,你可以通过迭代算法完成吗?</p>",
"topic_link": "https://bbs.csdn.net/topics/600471010",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct TreeNode\n{\n\tint val;\n\tstruct TreeNode *left;\n\tstruct TreeNode *right;\n};\nstatic void traverse(struct TreeNode *node, int *result, int *count)\n{\n\tif (node == NULL)\n\t{\n\t\treturn;\n\t}\n\ttraverse(node->left, result, count);\n\tresult[*count] = node->val;\n\t(*count)++;\n\ttraverse(node->right, result, count);\n}\nstatic int *inorderTraversal(struct TreeNode *root, int *returnSize)\n{\n\tif (root == NULL)\n\t{\n\t\t*returnSize = 0;\n\t\treturn NULL;\n\t}\n\tint count = 0;\n\tint *result = malloc(5000 * sizeof(int));\n\ttraverse(root, result, &count);\n\t*returnSize = count;\n\treturn result;\n}\nint main()\n{\n\tint count = 0;\n\tinorderTraversal(NULL, &count);\n\treturn 0;\n}",
"java": "\npublic class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode(int x) {\n\t\tval = x;\n\t}\n}\n\nclass Solution {\n\tpublic List<Integer> inorderTraversal(TreeNode root) {\n\t\tList<Integer> list = new ArrayList<>();\n\t\tStack<TreeNode> stack = new Stack<>();\n\t\tTreeNode cur = root;\n\t\twhile (cur != null || !stack.isEmpty()) {\n\t\t\tif (cur != null) {\n\t\t\t\tstack.push(cur);\n\t\t\t\tcur = cur.left;\n\t\t\t} else {\n\t\t\t\tcur = stack.pop();\n\t\t\t\tlist.add(cur.val);\n\t\t\t\tcur = cur.right;\n\t\t\t}\n\t\t}\n\t\treturn list;\n\t}\n}\n",
"js": "",
"python": "class TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\nclass List2Tree(object):\n\tdef __init__(self, nums: list):\n\t\tself.nums = nums\n\t\tself.queue = []\n\t\tif len(nums) == 1:\n\t\t\tself.root = TreeNode(self.nums.pop(0))\n\t\telse:\n\t\t\ta = self.nums.pop(0)\n\t\t\tb = self.nums.pop(0)\n\t\t\tc = self.nums.pop(0)\n\t\t\tself.root = TreeNode(a)\n\t\t\tif b is not None:\n\t\t\t\tself.root.left = TreeNode(b)\n\t\t\telse:\n\t\t\t\tself.root.left = b\n\t\t\tif c is not None:\n\t\t\t\tself.root.right = TreeNode(c)\n\t\t\telse:\n\t\t\t\tself.root.right = c\n\t\t\tself.queue.append(self.root.left)\n\t\t\tself.queue.append(self.root.right)\n\tdef convert(self):\n\t\twhile len(self.nums) > 0 and len(self.queue)> 0:\n\t\t\tnode = self.queue.pop(0)\n\t\t\tif node is not None:\n\t\t\t\tnum= self.nums.pop(0)\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.left = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.left = num\n\t\t\t\tif len(self.nums) > 0:\n\t\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tnum = None\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.right = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.right = num\n\t\t\t\tself.queue.append(node.left)\n\t\t\t\tself.queue.append(node.right)\n\t\treturn self.root\nclass Solution(object):\n\tdef inorderTraversal(self, root):\n\t\tif root is None:\n\t\t\treturn []\n\t\troot = List2Tree(root).convert()\n\t\tres = []\n\t\tstack = [root]\n\t\twhile len(stack) > 0:\n\t\t\tcurr = stack.pop()\n\t\t\tif not isinstance(curr, TreeNode):\n\t\t\t\tres.append(curr)\n\t\t\t\tcontinue\n\t\t\tif curr.right is not None:\n\t\t\t\tstack.append(curr.right)\n\t\t\tstack.append(curr.val)\n\t\t\tif curr.left is not None:\n\t\t\t\tstack.append(curr.left)\n\t\treturn res\n# %%\ns = Solution()\nprint(s.inorderTraversal(root = [1,None,2,3]))",
"status": 1,
"keywords": "栈,树,深度优先搜索,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/93/93_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/93/93_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/93/93_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 95,
"question_title": "不同的二叉搜索树 II",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>给你一个整数 <code>n</code> ,请你生成并返回所有由 <code>n</code> 个节点组成且节点值从 <code>1</code> 到 <code>n</code> 互不相同的不同\n <strong>二叉搜索树</strong><em> </em>。可以按 <strong>任意顺序</strong> 返回答案。\n </p>\n\n <p>&nbsp;</p>\n\n <div class=\"original__bRMd\">\n <div>\n <p><strong>示例 1:</strong></p>\n <img style=\"width: 600px; height: 148px;\"\n src=\"https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg\" alt=\"\">\n <pre><strong>输入:</strong>n = 3\n<strong>输出:</strong>[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n </pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>n = 1\n<strong>输出:</strong>[[1]]\n </pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= n &lt;= 8</code></li>\n </ul>\n </div>\n </div>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600471004",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\nstruct TreeNode\n{\n\tint val;\n\tstruct TreeNode *left;\n\tstruct TreeNode *right;\n};\nstatic struct TreeNode *dfs(int low, int high, int *count)\n{\n\tint i, j, k;\n\tif (low > high)\n\t{\n\t\t*count = 0;\n\t\treturn NULL;\n\t}\n\telse if (low == high)\n\t{\n\t\tstruct TreeNode *node = malloc(sizeof(*node));\n\t\tnode->val = low;\n\t\tnode->left = NULL;\n\t\tnode->right = NULL;\n\t\t*count = 1;\n\t\treturn node;\n\t}\n\telse\n\t{\n\t\t*count = 0;\n\t\tint capacity = 5;\n\t\tstruct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));\n\t\tfor (i = low; i <= high; i++)\n\t\t{\n\t\t\tint left_cnt, right_cnt;\n\t\t\tstruct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);\n\t\t\tstruct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);\n\t\t\tif (left_cnt == 0)\n\t\t\t\tleft_cnt = 1;\n\t\t\tif (right_cnt == 0)\n\t\t\t\tright_cnt = 1;\n\t\t\tif (*count + (left_cnt * right_cnt) >= capacity)\n\t\t\t{\n\t\t\t\tcapacity *= 2;\n\t\t\t\tcapacity += left_cnt * right_cnt;\n\t\t\t\troots = realloc(roots, capacity * sizeof(struct TreeNode));\n\t\t\t}\n\t\t\tfor (j = 0; j < left_cnt; j++)\n\t\t\t{\n\t\t\t\tfor (k = 0; k < right_cnt; k++)\n\t\t\t\t{\n\t\t\t\t\troots[*count].val = i;\n\t\t\t\t\troots[*count].left = left_subs == NULL ? NULL : &left_subs[j];\n\t\t\t\t\troots[*count].right = right_subs == NULL ? NULL : &right_subs[k];\n\t\t\t\t\t(*count)++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn roots;\n\t}\n}\nstatic struct TreeNode **generateTrees(int n, int *returnSize)\n{\n\tint i, count = 0;\n\tstruct TreeNode *roots = dfs(1, n, &count);\n\tstruct TreeNode **results = malloc(count * sizeof(struct TreeNode *));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tresults[i] = &roots[i];\n\t}\n\t*returnSize = count;\n\treturn results;\n}\nstatic void dump(struct TreeNode *node)\n{\n\tprintf(\"%d \", node->val);\n\tif (node->left != NULL)\n\t{\n\t\tdump(node->left);\n\t}\n\telse\n\t{\n\t\tprintf(\"# \");\n\t}\n\tif (node->right != NULL)\n\t{\n\t\tdump(node->right);\n\t}\n\telse\n\t{\n\t\tprintf(\"# \");\n\t}\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = 0;\n\tstruct TreeNode **results = generateTrees(atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tdump(results[i]);\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}",
"java": "class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode(int x) {\n\t\tval = x;\n\t}\n}\n\nclass Solution {\n\tpublic List<TreeNode> generateTrees(int n) {\n\n\t\tif (n == 0)\n\t\t\treturn new LinkedList<TreeNode>();\n\n\t\treturn generate_trees(1, n);\n\t}\n\n\tprivate LinkedList<TreeNode> generate_trees(int start, int end) {\n\t\tLinkedList<TreeNode> all_trees = new LinkedList<TreeNode>();\n\n\t\tif (start > end) {\n\t\t\tall_trees.add(null);\n\t\t\treturn all_trees;\n\t\t}\n\n\t\tfor (int i = start; i <= end; i++) {\n\t\t\tLinkedList<TreeNode> left_trees = generate_trees(start, i - 1);\n\t\t\tLinkedList<TreeNode> right_trees = generate_trees(i + 1, end);\n\n\t\t\tfor (TreeNode l : left_trees)\n\t\t\t\tfor (TreeNode r : right_trees) {\n\t\t\t\t\tTreeNode current_tree = new TreeNode(i);\n\t\t\t\t\tcurrent_tree.left = l;\n\t\t\t\t\tcurrent_tree.right = r;\n\t\t\t\t\tall_trees.add(current_tree);\n\t\t\t\t}\n\t\t}\n\n\t\treturn all_trees;\n\t}\n}",
"js": "",
"python": "class TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\n\tdef to_list(self, count):\n\t\tqueue = []\n\t\tqueue.append(self)\n\t\tresult = []\n\t\twhile len(queue) > 0:\n\t\t\tif count == 0:\n\t\t\t\tbreak\n\t\t\tnode = queue.pop(0)\n\t\t\tif node is None:\n\t\t\t\tresult.append('null')\n\t\t\telse:\n\t\t\t\tcount -= 1\n\t\t\t\tresult.append(node.val)\n\t\t\t\tqueue.append(node.left)\n\t\t\t\tqueue.append(node.right)\n\t\treturn result\nclass Solution(object):\n\tdef generateTrees(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: List[TreeNode]\n\t\t\"\"\"\n\t\tif n == 0:\n\t\t\treturn []\n\t\treturn self.get_trees(1, n)\n\tdef get_trees_impl(self, start, end):\n\t\ttrees = []\n\t\tif start > end:\n\t\t\ttrees.append(None)\n\t\t\treturn trees\n\t\tfor i in range(start, end + 1):\n\t\t\tlefts = self.get_trees_impl(start, i - 1)\n\t\t\trights = self.get_trees_impl(i + 1, end)\n\t\t\tfor j in range(len(lefts)):\n\t\t\t\tfor k in range(len(rights)):\n\t\t\t\t\troot = TreeNode(i)\n\t\t\t\t\troot.left = lefts[j]\n\t\t\t\t\troot.right = rights[k]\n\t\t\t\t\ttrees.append(root)\n\t\treturn trees\n\tdef get_trees(self, start, end):\n\t\ttrees = self.get_trees_impl(start, end)\n\t\tresults = []\n\t\tfor tree in trees:\n\t\t\tif tree is None:\n\t\t\t\tresults.append([])\n\t\t\telse:\n\t\t\t\tresults.append(tree.to_list(end))\n\t\treturn results\n# %%\ns = Solution()\nprint(s.generateTrees(n=3))",
"status": 1,
"keywords": "树,二叉搜索树,动态规划,回溯,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/94/94_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/94/94_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/94/94_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 96,
"question_title": "不同的二叉搜索树",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>给你一个整数 <code>n</code> ,求恰由 <code>n</code> 个节点组成且节点值从 <code>1</code> 到 <code>n</code> 互不相同的 <strong>二叉搜索树</strong>\n 有多少种?返回满足题意的二叉搜索树的种数。</p>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n <img style=\"width: 600px; height: 148px;\" src=\"https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg\"\n alt=\"\">\n <pre><strong>输入:</strong>n = 3\n<strong>输出:</strong>5\n </pre>\n\n <p><strong>示例 2:</strong></p>\n\n <pre><strong>输入:</strong>n = 1\n<strong>输出:</strong>1\n </pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li><code>1 &lt;= n &lt;= 19</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600469926",
"cpp": "#include <stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint numTrees(int n)\n\t{\n\t\tvector<int> sum(n + 1);\n\t\tsum[0] = 1;\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tfor (int j = 0; j < i; j++)\n\t\t\t{\n\t\t\t\tsum[i] += sum[j] * sum[i - j - 1];\n\t\t\t}\n\t\t}\n\t\treturn sum[n];\n\t}\n}",
"java": "class Solution {\n\tpublic int numTrees(int n) {\n\t\tif (n < 2) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tint[] count = new int[n + 1];\n\t\tcount[0] = 1;\n\t\tcount[1] = 1;\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tint sum = 0;\n\t\t\tfor (int root = 1; root <= i; root++) {\n\t\t\t\tsum = sum + count[root - 1] * count[i - root];\n\t\t\t}\n\t\t\tcount[i] = sum;\n\t\t}\n\n\t\treturn count[n];\n\t}\n}\n",
"js": "",
"python": "class Solution(object):\n\tdef numTrees(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tdp = [0] * (n + 1)\n\t\tdp[0] = 1\n\t\tdp[1] = 1\n\t\tfor level in range(2, n + 1):\n\t\t\tfor root in range(1, level + 1):\n\t\t\t\tdp[level] += dp[level - root] * dp[root - 1]\n\t\treturn dp[n]\n# %%\ns = Solution()\nprint(s.numTrees(n = 3))",
"status": 1,
"keywords": "树,二叉搜索树,数学,动态规划,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/95/95_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/95/95_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/95/95_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 97,
"question_title": "交错字符串",
"difficulty": "中等",
"question_content": "<p>给定三个字符串 <code>s1</code>、<code>s2</code>、<code>s3</code>,请你帮忙验证 <code>s3</code> 是否是由 <code>s1</code> 和 <code>s2</code><em> </em><strong>交错 </strong>组成的。</p><p>两个字符串 <code>s</code> 和 <code>t</code> <strong>交错</strong> 的定义与过程如下,其中每个字符串都会被分割成若干 <strong>非空</strong> 子字符串:</p><ul>\t<li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li>\t<li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li>\t<li><code>|n - m| <= 1</code></li>\t<li><strong>交错</strong> 是 <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> 或者 <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code></li></ul><p><strong>提示:</strong><code>a + b</code> 意味着字符串 <code>a</code> 和 <code>b</code> 连接。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0097.Interleaving%20String/images/interleave.jpg\" style=\"width: 561px; height: 203px;\" /><pre><strong>输入:</strong>s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbcbcac\"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbbaccc\"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s1 = \"\", s2 = \"\", s3 = \"\"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>0 <= s1.length, s2.length <= 100</code></li>\t<li><code>0 <= s3.length <= 200</code></li>\t<li><code>s1</code>、<code>s2</code>、和 <code>s3</code> 都由小写英文字母组成</li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600471101",
"cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool isInterleave(char *s1, char *s2, char *s3)\n{\n\tint i, j;\n\tint len1 = strlen(s1);\n\tint len2 = strlen(s2);\n\tint len3 = strlen(s3);\n\tif (len1 + len2 != len3)\n\t{\n\t\treturn false;\n\t}\n\tbool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool));\n\tbool **dp = malloc((len1 + 1) * sizeof(bool *));\n\tfor (i = 0; i < len1 + 1; i++)\n\t{\n\t\tdp[i] = &table[i * (len2 + 1)];\n\t}\n\tdp[0][0] = true;\n\tfor (i = 1; i < len1 + 1; i++)\n\t{\n\t\tdp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];\n\t}\n\tfor (i = 1; i < len2 + 1; i++)\n\t{\n\t\tdp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1];\n\t}\n\tfor (i = 1; i < len1 + 1; i++)\n\t{\n\t\tfor (j = 1; j < len2 + 1; j++)\n\t\t{\n\t\t\tbool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];\n\t\t\tbool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];\n\t\t\tdp[i][j] = up || left;\n\t\t}\n\t}\n\treturn dp[len1][len2];\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 4)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test s1 s2 s3\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%s\\n\", isInterleave(argv[1], argv[2], argv[3]) ? \"true\" : \"false\");\n\treturn 0;\n}",
"java": "class Solution {\n\tpublic boolean isInterleave(String s1, String s2, String s3) {\n\t\tif ((s1.length() + s2.length()) != s3.length())\n\t\t\treturn false;\n\t\tboolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];\n\t\tdp[0][0] = true;\n\t\tfor (int i = 1; i <= s1.length(); i++) {\n\t\t\tdp[0][i] = dp[0][i - 1] && s1.charAt(i - 1) == s3.charAt(i - 1) ? true : false;\n\t\t}\n\t\tfor (int i = 1; i <= s2.length(); i++) {\n\t\t\tdp[i][0] = dp[i - 1][0] && s2.charAt(i - 1) == s3.charAt(i - 1) ? true : false;\n\t\t}\n\t\tfor (int i = 1; i < dp.length; i++) {\n\t\t\tfor (int j = 1; j < dp[0].length; j++) {\n\t\t\t\tdp[i][j] = (dp[i][j - 1] && s1.charAt(j - 1) == s3.charAt(i + j - 1))\n\t\t\t\t\t\t|| (dp[i - 1][j] && s2.charAt(i - 1) == s3.charAt(i + j - 1));\n\t\t\t}\n\t\t}\n\t\treturn dp[s2.length()][s1.length()];\n\t}\n}",
"js": "",
"python": "class Solution(object):\n\tdef isInterleave(self, s1, s2, s3):\n\t\t\"\"\"\n\t\t:type s1: str\n\t\t:type s2: str\n\t\t:type s3: str\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tif len(s1) + len(s2) != len(s3):\n\t\t\treturn False\n\t\tqueue = [(0, 0), (-1, -1)]\n\t\tvisited = set()\n\t\tisSuccess = False\n\t\tindex = 0\n\t\twhile len(queue) != 1 or queue[0][0] != -1:\n\t\t\tp = queue.pop(0)\n\t\t\tif p[0] == len(s1) and p[1] == len(s2):\n\t\t\t\treturn True\n\t\t\tif p[0] == -1:\n\t\t\t\tqueue.append(p)\n\t\t\t\tindex += 1\n\t\t\t\tcontinue\n\t\t\tif p in visited:\n\t\t\t\tcontinue\n\t\t\tvisited.add(p)\n\t\t\tif p[0] < len(s1):\n\t\t\t\tif s1[p[0]] == s3[index]:\n\t\t\t\t\tqueue.append((p[0] + 1, p[1]))\n\t\t\tif p[1] < len(s2):\n\t\t\t\tif s2[p[1]] == s3[index]:\n\t\t\t\t\tqueue.append((p[0], p[1] + 1))\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isInterleave(s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbcbcac\"))",
"status": 1,
"keywords": "字符串,动态规划",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104374282"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/96/96_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/96/96_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/96/96_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 98,
"question_title": "验证二叉搜索树",
"difficulty": "中等",
"question_content": "<div class=\"notranslate\">\n <p>给你一个二叉树的根节点 <code>root</code> ,判断其是否是一个有效的二叉搜索树。</p>\n\n <p><strong>有效</strong> 二叉搜索树定义如下:</p>\n\n <ul>\n <li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>\n <li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>\n <li>所有左子树和右子树自身必须也是二叉搜索树。</li>\n </ul>\n\n <p>&nbsp;</p>\n\n <p><strong>示例 1:</strong></p>\n <img style=\"width: 302px; height: 182px;\" src=\"https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg\" alt=\"\">\n <pre><strong>输入:</strong>root = [2,1,3]\n<strong>输出:</strong>true\n</pre>\n\n <p><strong>示例 2:</strong></p>\n <img style=\"width: 422px; height: 292px;\" src=\"https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg\" alt=\"\">\n <pre><strong>输入:</strong>root = [5,1,4,null,null,3,6]\n<strong>输出:</strong>false\n<strong>解释:</strong>根节点的值是 5 ,但是右子节点的值是 4 。\n</pre>\n\n <p>&nbsp;</p>\n\n <p><strong>提示:</strong></p>\n\n <ul>\n <li>树中节点数目范围在<code>[1, 10<sup>4</sup>]</code> 内</li>\n <li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>\n </ul>\n</div>",
"topic_link": "https://bbs.csdn.net/topics/600469827",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct TreeNode\n{\n\tint val;\n\tTreeNode *left;\n\tTreeNode *right;\n\tTreeNode() : val(0), left(nullptr), right(nullptr) {}\n\tTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n\tTreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n};\nclass Solution\n{\npublic:\n\tbool isValidBST(TreeNode *root)\n\t{\n\t\tstack<TreeNode *> stk;\n\t\tint prev = INT_MIN;\n\t\tbool first = true;\n\t\twhile (!stk.empty() || root != nullptr)\n\t\t{\n\t\t\tif (root != nullptr)\n\t\t\t{\n\t\t\t\tstk.push(root);\n\t\t\t\troot = root->left;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\troot = stk.top();\n\t\t\t\tstk.pop();\n\t\t\t\tif (!first && prev >= root->val)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tfirst = false;\n\t\t\t\tprev = root->val;\n\t\t\t\troot = root->right;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n};",
"java": "\npublic class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode(int x) {\n\t\tval = x;\n\t}\n}\n\nclass Solution {\n\tpublic boolean isValidBST(TreeNode root) {\n\n\t\tif (root == null)\n\t\t\treturn true;\n\t\tif (root.left == null && root.right == null) {\n\t\t\treturn true;\n\t\t}\n\t\tif (root.left != null) {\n\t\t\tTreeNode cur = root.left;\n\t\t\twhile (cur.right != null) {\n\t\t\t\tcur = cur.right;\n\t\t\t}\n\t\t\tif (cur.val >= root.val) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\tif (root.right != null) {\n\t\t\tTreeNode cur = root.right;\n\t\t\twhile (cur.left != null) {\n\t\t\t\tcur = cur.left;\n\t\t\t}\n\t\t\tif (cur.val <= root.val) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\tboolean left = isValidBST(root.left);\n\t\tboolean right = isValidBST(root.right);\n\n\t\treturn left && right;\n\t}\n}",
"js": "",
"python": "import sys\nclass TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\nclass List2Tree(object):\n\tdef __init__(self, nums: list):\n\t\tself.nums = nums\n\t\tself.queue = []\n\t\tif len(nums) == 1:\n\t\t\tself.root = TreeNode(self.nums.pop(0))\n\t\telse:\n\t\t\ta = self.nums.pop(0)\n\t\t\tb = self.nums.pop(0)\n\t\t\tc = self.nums.pop(0)\n\t\t\tself.root = TreeNode(a)\n\t\t\tif b is not None:\n\t\t\t\tself.root.left = TreeNode(b)\n\t\t\telse:\n\t\t\t\tself.root.left = b\n\t\t\tif c is not None:\n\t\t\t\tself.root.right = TreeNode(c)\n\t\t\telse:\n\t\t\t\tself.root.right = c\n\t\t\tself.queue.append(self.root.left)\n\t\t\tself.queue.append(self.root.right)\n\tdef convert(self):\n\t\twhile len(self.nums) > 0 and len(self.queue)> 0:\n\t\t\tnode = self.queue.pop(0)\n\t\t\tif node is not None:\n\t\t\t\tnum= self.nums.pop(0)\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.left = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.left = num\n\t\t\t\tif len(self.nums) > 0:\n\t\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tnum = None\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.right = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.right = num\n\t\t\t\tself.queue.append(node.left)\n\t\t\t\tself.queue.append(node.right)\n\t\treturn self.root\nclass Solution(object):\n\tdef isValidBST(self, root):\n\t\troot = List2Tree(root).convert()\n\t\treturn self.isVaild_helper(root, -sys.maxsize - 1, sys.maxsize)\n\tdef isVaild_helper(self, root, minVal, maxVal):\n\t\tif root is None:\n\t\t\treturn True\n\t\tif root.val >= maxVal or root.val <= minVal:\n\t\t\treturn False\n\t\treturn self.isVaild_helper(root.left, minVal, root.val) and self.isVaild_helper(root.right, root.val, maxVal)\n# %%\ns = Solution()\nprint(s.isValidBST([5,1,4,None,None,3,6]))",
"status": 1,
"keywords": "树,深度优先搜索,二叉搜索树,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://github.com/zhangyu345293721/leetcode"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 99,
"question_title": "恢复二叉搜索树",
"difficulty": "中等",
"question_content": "<p>给你二叉搜索树的根节点 <code>root</code> ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。</p><p><strong>进阶:</strong>使用 O(<em>n</em>) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover1.jpg\" style=\"width: 422px; height: 302px;\" /><pre><strong>输入:</strong>root = [1,3,null,null,2]<strong><br />输出:</strong>[3,1,null,null,2]<strong><br />解释:</strong>3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover2.jpg\" style=\"width: 581px; height: 302px;\" /><pre><strong>输入:</strong>root = [3,1,4,null,null,2]<strong><br />输出:</strong>[2,1,4,null,null,3]<strong><br />解释:</strong>2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>树上节点的数目在范围 <code>[2, 1000]</code> 内</li>\t<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600470826",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct TreeNode\n{\n\tint val;\n\tTreeNode *left;\n\tTreeNode *right;\n\tTreeNode() : val(0), left(nullptr), right(nullptr) {}\n\tTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n\tTreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n};\nclass Solution\n{\npublic:\n\tvoid recoverTree(TreeNode *root)\n\t{\n\t\tdfs(root);\n\t\tint tmp = p0_->val;\n\t\tp0_->val = p1_->val;\n\t\tp1_->val = tmp;\n\t}\nprivate:\n\tint wrong_ = 0;\n\tTreeNode *prev_ = nullptr;\n\tTreeNode *p0_ = nullptr;\n\tTreeNode *p1_ = nullptr;\n\tvoid dfs(TreeNode *root)\n\t{\n\t\tif (root == nullptr || wrong_ == 2)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\tdfs(root->left);\n\t\tif (prev_ != nullptr && prev_->val > root->val)\n\t\t{\n\t\t\tif (++wrong_ == 1)\n\t\t\t{\n\t\t\t\tp0_ = prev_;\n\t\t\t\tp1_ = root;\n\t\t\t}\n\t\t\telse if (wrong_ == 2)\n\t\t\t{\n\t\t\t\tp1_ = root;\n\t\t\t}\n\t\t}\n\t\tprev_ = root;\n\t\tdfs(root->right);\n\t}\n};",
"java": "public class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode() {\n\t}\n\n\tTreeNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tTreeNode(int val, TreeNode left, TreeNode right) {\n\t\tthis.val = val;\n\t\tthis.left = left;\n\t\tthis.right = right;\n\t}\n}\n\nclass Solution {\n\tTreeNode t1, t2, pre;\n\n\tpublic void recoverTree(TreeNode root) {\n\t\tinorder(root);\n\t\tint temp = t1.val;\n\t\tt1.val = t2.val;\n\t\tt2.val = temp;\n\t}\n\n\tpublic void inorder(TreeNode root) {\n\t\tif (root == null)\n\t\t\treturn;\n\t\tinorder(root.left);\n\t\tif (pre != null && pre.val > root.val) {\n\t\t\tif (t1 == null)\n\t\t\t\tt1 = pre;\n\t\t\tt2 = root;\n\t\t}\n\t\tpre = root;\n\t\tinorder(root.right);\n\t}\n}",
"js": "",
"python": "import sys\nclass TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\n\tdef to_list(self, count):\n\t\tqueue = []\n\t\tqueue.append(self)\n\t\tresult = []\n\t\twhile len(queue) > 0:\n\t\t\tif count == 0:\n\t\t\t\tbreak\n\t\t\tnode = queue.pop(0)\n\t\t\tif node is None:\n\t\t\t\tresult.append('null')\n\t\t\telse:\n\t\t\t\tcount -= 1\n\t\t\t\tresult.append(node.val)\n\t\t\t\tqueue.append(node.left)\n\t\t\t\tqueue.append(node.right)\n\t\treturn result\nclass List2Tree(object):\n\tdef __init__(self, nums: list):\n\t\tself.nums = nums\n\t\tself.queue = []\n\t\tif len(nums) == 1:\n\t\t\tself.root = TreeNode(self.nums.pop(0))\n\t\telse:\n\t\t\ta = self.nums.pop(0)\n\t\t\tb = self.nums.pop(0)\n\t\t\tc = self.nums.pop(0)\n\t\t\tself.root = TreeNode(a)\n\t\t\tif b is not None:\n\t\t\t\tself.root.left = TreeNode(b)\n\t\t\telse:\n\t\t\t\tself.root.left = b\n\t\t\tif c is not None:\n\t\t\t\tself.root.right = TreeNode(c)\n\t\t\telse:\n\t\t\t\tself.root.right = c\n\t\t\tself.queue.append(self.root.left)\n\t\t\tself.queue.append(self.root.right)\n\tdef convert(self):\n\t\twhile len(self.nums) > 0 and len(self.queue) > 0:\n\t\t\tnode = self.queue.pop(0)\n\t\t\tif node is not None:\n\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.left = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.left = num\n\t\t\t\tif len(self.nums) > 0:\n\t\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tnum = None\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.right = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.right = num\n\t\t\t\tself.queue.append(node.left)\n\t\t\t\tself.queue.append(node.right)\n\t\treturn self.root\nclass Solution(object):\n\tdef __init__(self):\n\t\tself.first = self.second = None\n\t\tself.pre = TreeNode(-sys.maxsize - 1)\n\tdef recoverTree(self, root):\n\t\tlength = len(root)\n\t\troot = List2Tree(root).convert()\n\t\tself.traverse(root)\n\t\tself.first.val, self.second.val = self.second.val, self.first.val\n\t\treturn root.to_list(length)\n\tdef traverse(self, root):\n\t\tif root is None:\n\t\t\treturn\n\t\tself.traverse(root.left)\n\t\tif self.pre.val >= root.val:\n\t\t\tif self.first is None:\n\t\t\t\tself.first = self.pre\n\t\t\tif self.first is not None:\n\t\t\t\tself.second = root\n\t\tself.pre = root\n\t\tself.traverse(root.right)\n# %%\ns = Solution()\nprint(s.recoverTree(root=[1, 3, None, None, 2]))",
"status": 1,
"keywords": "树,深度优先搜索,二叉搜索树,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/a1439775520/article/details/104377203"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/98/98_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/98/98_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/98/98_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 100,
"question_title": "相同的树",
"difficulty": "简单",
"question_content": "<p>给你两棵二叉树的根节点 <code>p</code> 和 <code>q</code> ,编写一个函数来检验这两棵树是否相同。</p><p>如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex1.jpg\" style=\"width: 622px; height: 182px;\" /><pre><strong>输入:</strong>p = [1,2,3], q = [1,2,3]<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex2.jpg\" style=\"width: 382px; height: 182px;\" /><pre><strong>输入:</strong>p = [1,2], q = [1,null,2]<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex3.jpg\" style=\"width: 622px; height: 182px;\" /><pre><strong>输入:</strong>p = [1,2,1], q = [1,1,2]<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>两棵树上的节点数目都在范围 <code>[0, 100]</code> 内</li>\t<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li></ul>",
"topic_link": "https://bbs.csdn.net/topics/600469927",
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct TreeNode\n{\n\tint val;\n\tTreeNode *left;\n\tTreeNode *right;\n\tTreeNode() : val(0), left(nullptr), right(nullptr) {}\n\tTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n\tTreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n};\nclass Solution\n{\npublic:\n\tbool isSameTree(TreeNode *p, TreeNode *q)\n\t{\n\t\tif (p == nullptr && q == nullptr)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\tif (p == nullptr || q == nullptr)\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\tif (p->val != q->val)\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\treturn isSameTree(p->left, q->left) && isSameTree(p->right, q->right);\n\t}\n};",
"java": "public class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode() {\n\t}\n\n\tTreeNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tTreeNode(int val, TreeNode left, TreeNode right) {\n\t\tthis.val = val;\n\t\tthis.left = left;\n\t\tthis.right = right;\n\t}\n}\n\nclass Solution {\n\tpublic boolean isSameTree(TreeNode p, TreeNode q) {\n\t\tif (p == null && q == null) {\n\t\t\treturn true;\n\t\t}\n\t\tif (p != null && q != null && p.val == q.val) {\n\t\t\treturn isSameTree(p.left, q.left) && isSameTree(p.right, q.right);\n\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n}",
"js": "",
"python": "class TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\nclass Solution(object):\n\tdef isSameTree(self, p, q):\n\t\t\"\"\"\n\t\t:type p: TreeNode\n\t\t:type q: TreeNode\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tif p == q:\n\t\t\treturn True\n\t\ttry:\n\t\t\tleft = right = True\n\t\t\tif p.val == q.val:\n\t\t\t\tleft = self.isSameTree(p.left, q.left)\n\t\t\t\tright = self.isSameTree(p.right, q.right)\n\t\t\t\treturn (left and right)\n\t\texcept:\n\t\t\treturn False\n\t\treturn False\n# %%\ns = Solution()\nprint(s.isSameTree(p = [1,2,3], q = [1,2,3]))",
"status": 1,
"keywords": "树,深度优先搜索,广度优先搜索,二叉树",
"license": {
"cpp": "https://github.com/begeekmyfriend/leetcode",
"python": "https://github.com/qiyuangong/leetcode",
"java": "https://blog.csdn.net/weixin_45176257/article/details/106487181"
},
"notebook": {
"cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/99/99_cpp.ipynb?type=file",
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/99/99_python.ipynb?type=file",
"java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/99/99_java.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7434731,
"question_title": "按要求实现程序功能",
"question_content": "<p>&#xff08;1&#xff09;定义一个函数prime判断某个整数是否为素数&#xff1b;\n&#xff08;2&#xff09;然后从键盘输入一行字符串&#xff0c;将其中的连续数字依次提取出来形成一个列表。例如&#xff0c;字符串“ab12cd34fg67”按要求提取后形成列表[12,34,67]&#xff1b;\n&#xff08;3&#xff09;将列表中的所有非素数改为0&#xff08;要求用prime函数判断列表中的元素是否为素 数&#xff09;&#xff1b;\n&#xff08;4&#xff09;输出原始字符串及修改前、修改后的列表。\n提示&#xff1a;可以用s.isdigit()判断s是否为数字字符&#xff08;串&#xff09;</p>",
"difficulty": "简单",
"answer_id": 53405170,
"answer_content": "<pre>\n<code class=\"language-python\">import math,re\n\n\n# 判断素数函数\ndef prime(num):\n\tflag &#61; False\n\tif num &gt; 1:\n\t\tfor i in range(2, math.floor(math.sqrt(num))):\n\t\t\tif (num % i) &#61;&#61; 0:\n\t\t\t\tflag &#61; True\n\t\t\t\tbreak\n\n\tif flag:\n\t\tprint(num, &#34;不是素数&#34;)\n\telse:\n\t\tprint(num, &#34;是素数&#34;)\n\treturn flag\n# 键盘输入字符串\ns &#61; input(&#34;请输入字符串&#xff1a;&#34;)\n\n# 提取数字字符串 列表\nsList &#61; re.findall(r&#39;(\\d&#43;)&#39;, s)\nsNum &#61; [int(x) for x in sList]\n# 非素数重置为0\ny &#61; lambda x: 0 if prime(x) else x\nsNew &#61; [y(x) for x in sNum]\n# 输出\nprint(sNum) # 输出原列表\nprint(sNew) # 输出修改后列表</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "import math,re\ndef prime(num):\n\tflag = False\n\tif num > 1:\n\t\tfor i in range(2, math.floor(math.sqrt(num))):\n\t\t\tif (num % i) == 0:\n\t\t\t\tflag = True\n\t\t\t\tbreak\n\tif flag:\n\t\tprint(num, \"不是素数\")\n\telse:\n\t\tprint(num, \"是素数\")\n\treturn flag\ns = input(\"请输入字符串:\")\nsList = re.findall(r'(\\d+)', s)\nsNum = [int(x) for x in sList]\ny = lambda x: 0 if prime(x) else x\nsNew = [y(x) for x in sNum]\nprint(sNum) \nprint(sNew) ",
"topic_link": "https://bbs.csdn.net/topics/600470198",
"status": 1,
"keywords": "散列表,算法中阶,数据结构,散列函数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/0.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7450424,
"question_title": "输出整数的全排列",
"question_content": "输入整数n(3<=n<=7),编写程序输出1,2,....,n整数的全排列,按字典序输出。\n输入样例:\n输入:3\n输出:123 132 213 231 312 321",
"difficulty": "简单",
"answer_id": 53431109,
"answer_content": "<pre>\n<code>import random\nn &#61; int(input())\nt &#61; list()\nt1 &#61; set()\nfor i in range(1,n&#43;1):\n t.append(str(i))\nwhile True:\n sum &#61; 1\n for i in range(1, n &#43; 1):\n sum *&#61; i\n if len(t1) &gt;&#61; sum:\n break\n random.shuffle(t)\n t1.add(&#34;&#34;.join(t))\ns &#61; sorted(t1)\nfor i in s:\n print(i,end&#61;&#34; &#34;)</code></pre>\n\n<p><img alt=\"\" height=\"150\" src=\"https://img-ask.csdnimg.cn/upload/1623810777842.png\" width=\"567\" /></p>\n\n<p>记得点个采纳&#xff0c;谢谢 </p>\n",
"tag_name": "python",
"python": "import random\nn = int(input())\nt = list()\nt1 = set()\nfor i in range(1,n+1):\n\tt.append(str(i))\nwhile True:\n\tsum = 1\n\tfor i in range(1, n + 1):\n\t\tsum *= i\n\tif len(t1) >= sum:\n\t\tbreak\n\trandom.shuffle(t)\n\tt1.add(\"\".join(t))\ns = sorted(t1)\nfor i in s:\n\tprint(i,end=\" \")",
"topic_link": "https://bbs.csdn.net/topics/600470295",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/1.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7457826,
"question_title": "求最大公约数和最小公倍数",
"question_content": "<p>输入两个数x 和y&#xff0c;如果x 或y 小于等于0&#xff0c;提示请输入正整数&#xff0c;求这两个数的最大公约数和最小公倍数。\n注意&#xff1a;可以采用欧几里得辗转相除算法来求最大公约数。最小公倍数的计算方法是两数的乘积除以两数最大公约数的结果。</p>",
"difficulty": "简单",
"answer_id": 53442305,
"answer_content": "<pre>\n<code class=\"language-python\">x &#61; int(input(&#34;输入x:&#34;))\ny &#61; int(input(&#34;输入y:&#34;))\nif x &lt;&#61; 0 or x &lt;&#61; 0:\n print(&#34;请输入正整数&#34;)\nif x &lt; y:\n x,y&#61;y,x\n v1 &#61; x*y\n v2 &#61; x%y\n while v2 !&#61; 0:\n x&#61;y\n y &#61; v2\n v2 &#61; x % y\n v1 &#61;v1/ y\n print(&#34;最大公约数为:%d&#34; % y) \n print(&#34;最小公倍数为:%d&#34; % v1) \n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "x = int(input(\"输入x:\"))\ny = int(input(\"输入y:\"))\nif x <= 0 or y <= 0:\n\tprint(\"请输入正整数\")\nif x < y:\n\tx,y=y,x\n\tv1 = x*y\n\tv2 = x%y\n\twhile v2 != 0:\n\t\tx=y\n\t\ty = v2\n\t\tv2 = x % y\n\tv1 =v1/ y\n\tprint(\"最大公约数为:%d\" % y) \n\tprint(\"最小公倍数为:%d\" % v1) ",
"topic_link": "https://bbs.csdn.net/topics/600469983",
"status": 1,
"keywords": "算法初阶,基础知识,算法基础,设计算法,分析分治算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/10.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7423742,
"question_title": "实现保留3位有效数字(四舍六入五成双规则)",
"question_content": "输入:1234 输出:1234\n 12 12.0\n 4 4.00\n 0.2 0.200\n 0.32 0.320\n 1.3 1.30\n 1.235 1.24\n 1.245 1.24\n 1.2451 1.25",
"difficulty": "中等",
"answer_id": 53383866,
"answer_content": "<pre>\n<code class=\"language-python\">a &#61; input()\nif &#39;.&#39; in a:\n a &#61; float(a)\n if a*1000%10!&#61;5:\n a &#61; &#39;%.2f&#39;%(a)\n else:\n if len(str(a).split(&#34;.&#34;)[1])&gt;3:\n a &#61; &#39;%.2f&#39;%(a)\n else:\n if int(a*100%10%2)&#61;&#61;1:\n a &#61; float(&#39;%.2f&#39;%float(int(a*100)/100))&#43;0.01\n else:\n a &#61; &#39;%.2f&#39;%float(int(a*100)/100)\n print(a)\nelse:\n a &#61; int(a)\n if a&gt;99:\n print(a)\n else:\n print(float(a))</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "a = input()\nif '.' in a:\n\ta = float(a)\n\tif a*1000%10!=5:\n\t\ta = '%.2f'%(a)\n\telse:\n\t\tif len(str(a).split(\".\")[1])>3:\n\t\t\ta = '%.2f'%(a)\n\t\telse:\n\t\t\tif int(a*100%10%2)==1:\n\t\t\t\ta = float('%.2f'%float(int(a*100)/100))+0.01\n\t\t\telse:\n\t\t\t\ta = '%.2f'%float(int(a*100)/100)\n\tprint(a)\nelse:\n\ta = int(a)\n\tif a>99:\n\t\tprint(a)\n\telse:\n\t\tif 0 < a < 10: \n\t\t\tprint('%.2f'%a)\n\t\telse:\n\t\t\tprint(float(a))",
"topic_link": "https://bbs.csdn.net/topics/600469884",
"status": 1,
"keywords": "数学,算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/11.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1005354,
"question_title": "随机生成车牌号",
"question_content": "<p>某市随机生成车辆号牌的规则是:号牌字头为\"某A-\",\"某B-\"等(字母为除了C以外的A~H范围内的大写字母),字头后面由5位字符组成,第1位必须是数字;第2、3、4、5位可以是任意数字或不含字母\"O\"的大写英文字母。\n程序功能为:调用自己设计的函数license_plate(),随机生成5个车辆号牌,等待输入一个心仪号码的序号选择号牌,并将其打印输出。\n程序运行结果如下图所示:\n![图片说明](https://img-ask.csdn.net/upload/201912/08/1575761967_651946.jpg)</p>",
"difficulty": "简单",
"answer_id": 1186938,
"answer_content": "\n```\nimport random\ndef genrndchar(metachar):\n\treturn metachar[int(random.random() * len(metachar))]\ndef license_plate():\n\ts = \"\"\n\ts = s + genrndchar(['A', 'B', 'C', 'D', 'E', 'H'])\n\ts = s + '-'\n\ts = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])\n\tfor i in range(4):\n\t\ts = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])\n\treturn s\n\nlst = []\nfor i in range(5):\n\tplate = license_plate()\n\tlst.append(plate)\n\tprint(str(i + 1) + \":\" + plate)\nx = int(input(\"请输入您心仪的号牌序号:\")) - 1\nprint(\"您选中的号牌为:\" + lst[x])\n```",
"tag_name": "python",
"python": "import random\ndef genrndchar(metachar):\n\treturn metachar[int(random.random() * len(metachar))]\ndef license_plate():\n\ts = \"\"\n\ts = s + genrndchar(['A', 'B', 'C', 'D', 'E', 'H'])\n\ts = s + '-'\n\ts = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])\n\tfor i in range(4):\n\t\ts = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])\n\treturn s\nlst = []\nfor i in range(5):\n\tplate = license_plate()\n\tlst.append(plate)\n\tprint(str(i + 1) + \":\" + plate)\nx = int(input(\"请输入您心仪的号牌序号:\")) - 1\nprint(\"您选中的号牌为:\" + lst[x])",
"topic_link": "https://bbs.csdn.net/topics/600470298",
"status": 1,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/12.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7441321,
"question_title": "学生信息读写",
"question_content": "<p>包含方法&#xff1a;动态输入学生信息&#xff0c;展示信息。<br />\n2.设计功能1&#xff0c;能将学生对象的信息写入二进制文件&#xff0c;<br />\n3.设计功能2&#xff0c;能从二进制文件中读出学生信息&#xff0c;展示出来。<br />\n4.设计功能3&#xff0c;能从二进制文件中读出学生信息&#xff0c;转换成csv格式并保存到文件<br />\n5.设计功能4&#xff0c;能从二进制文件中读出学生信息&#xff0c;转换成json格式并保存到文件<br />\n6.设计功能5&#xff0c;能从csv文件中读出学生信息&#xff0c;展示&#xff0c;并保存到二进制文件<br />\n7.设计功能6&#xff0c;能从json文件中读出学生信息&#xff0c;展示&#xff0c;并保存到二进制文件<br />\n8.以上功能可以由多个代码文件实现</p>",
"difficulty": "中等",
"answer_id": 53417409,
"answer_content": "<pre>\n<code>class stu:\n def __init__(self, id&#61;1, name&#61;&#39;Tom&#39;, grades&#61;&#39;class1&#39;, result&#61;100, address&#61;&#39;CN&#39;, phone&#61;&#39;110&#39;):\n self.id &#61; id\n self.name &#61; name\n self.grades &#61; grades\n self.result &#61; result\n self.address &#61; address\n self.phone &#61; phone\n self.data &#61; dict(id&#61;id, name&#61;name, grades&#61;grades, result&#61;result, address&#61;address, phone&#61;phone)\n\n def write_file(self):\n &#34;&#34;&#34;功能1&#34;&#34;&#34;\n f &#61; open(&#39;data.txt&#39;, &#39;w&#39;, encoding&#61;&#39;utf-8&#39;)\n f.write(str(self.data))\n f.close()\n\n def read_file(self):\n &#34;&#34;&#34;功能2&#34;&#34;&#34;\n f &#61; open(&#39;data.txt&#39;, &#39;r&#39;, encoding&#61;&#39;utf-8&#39;)\n self.data &#61; eval(f.read())\n f.close()\n print(self.data)\n\n\ns &#61; stu()\ns.write_file()\ns.read_file()</code></pre>\n\n<p>参考一下&#xff1a;如果对你有帮助&#xff0c;可以点击我这个回答右上方的【采纳】按钮&#xff0c;给我个采纳吗&#xff0c;谢谢</p>\n",
"tag_name": "python",
"python": "class stu:\n def __init__(self, id=1, name='Tom', grades='class1', result=100, address='CN', phone='110'):\n self.id = id\n self.name = name\n self.grades = grades\n self.result = result\n self.address = address\n self.phone = phone\n self.data = dict(id=id, name=name, grades=grades, result=result, address=address, phone=phone)\n def write_file(self):\n \"\"\"功能1\"\"\"\n f = open('data.txt', 'w', encoding='utf-8')\n f.write(str(self.data))\n f.close()\n def read_file(self):\n \"\"\"功能2\"\"\"\n f = open('data.txt', 'r', encoding='utf-8')\n self.data = eval(f.read())\n f.close()\n print(self.data)\ns = stu()\ns.write_file()\ns.read_file()\n",
"topic_link": "https://bbs.csdn.net/topics/600470403",
"status": 0,
"keywords": "算法中阶,动态规划,动态规划原理,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/13.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7445764,
"question_title": "判断成绩是否及格",
"question_content": "<pre>\n# 键盘输入一个成绩:\n# 如果成绩&gt;&#61;60并且&lt;80输出及格\n# 成绩&gt;&#61;80并且&lt;&#61;100输出 优\n# 成绩&gt;&#61;0并且&lt;60 输出 输入成绩有误&#xff0c;请检查\n# 输入其他字符 输出你输入的字符不合法</pre>",
"difficulty": "简单",
"answer_id": 53424594,
"answer_content": "<pre>\n<code class=\"language-python\"># 如果成绩&gt;&#61;60并且&lt;80输出及格\n# 成绩&gt;&#61;80并且&lt;&#61;100输出 优\n# 成绩&gt;&#61;0并且&lt;60 输出 输入成绩有误&#xff0c;请检查\n# 输入其他字符 输出你输入的字符不合法\n\nscore &#61; input(&#34;请输入学生的成绩:&#34;)\n\nif score.isdigit():\n score &#61; int(score)\n if 0 &lt;&#61; score &lt; 60:\n print(&#39;输入成绩有误&#xff0c;请检查&#39;)\n if 60 &lt;&#61; score &lt; 80:\n print(&#39;及格&#39;)\n if 80 &lt;&#61; score &lt;&#61; 100:\n print(&#39;优&#39;)\nelse:\n print(&#39;你输入的字符不合法&#39;)</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "\nscore = input(\"请输入学生的成绩:\")\nif score.isdigit():\n score = int(score)\n if 0 <= score < 60:\n print('输入成绩有误,请检查')\n if 60 <= score < 80:\n print('及格')\n if 80 <= score <= 100:\n print('优')\nelse:\n print('你输入的字符不合法')\n",
"topic_link": "https://bbs.csdn.net/topics/600470299",
"status": 0,
"keywords": "比较大小",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/14.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7446240,
"question_title": "利用numpy和Matplotlib在同一张纸上绘制0-1间的x**2和x**3曲线",
"question_content": "<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623285669066.jpeg\" /></p>\n\n<p> </p>",
"difficulty": "简单",
"answer_id": 53425485,
"answer_content": "<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623291663218.png\" /></p>\n\n<pre>\n<code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\ndata &#61; np.linspace(0,1,1000)\nplt.title(&#39;lines&#39;)\nplt.xlabel(&#39;x&#39;)\nplt.ylabel(&#39;y&#39;)\nplt.xlim((0,1))\nplt.ylim((0,1))\nb &#61; np.arange(0,1,0.2)\nplt.xticks(b)\nplt.yticks([0,0.5,1])\nplt.plot(data,data**2,ls&#61;&#39;--&#39;)\nplt.plot(data,data**3,color&#61;&#39;b&#39;)\nplt.legend([&#39;y&#61;x^2&#39;,&#39;y^3&#39;])\nplt.savefig(&#39;jp.png&#39;)\nplt.show()</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "import numpy as np\nimport matplotlib.pyplot as plt\ndata = np.linspace(0,1,1000)\nplt.title('lines')\nplt.xlabel('x')\nplt.ylabel('y')\nplt.xlim((0,1))\nplt.ylim((0,1))\nb = np.arange(0,1,0.2)\nplt.xticks(b)\nplt.yticks([0,0.5,1])\nplt.plot(data,data**2,ls='--')\nplt.plot(data,data**3,color='b')\nplt.legend(['y=x^2','y^3'])\nplt.savefig('jp.png')\nplt.show()\n",
"topic_link": "https://bbs.csdn.net/topics/600469984",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/15.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7442242,
"question_title": "python复利计算利息",
"question_content": "<p>. 编写一个程序计算10万元本金在银行以4.5%的年利息存5年后得到的收益&#xff0c;按复利计算利息。</p>",
"difficulty": "简单",
"answer_id": 53419050,
"answer_content": "<pre>\n<code class=\"language-python\"># encoding: utf-8\n&#34;&#34;&#34;\n&#64;version: 1.0\n&#64;author: AusKa_T\n&#64;file: 2\n&#64;time: 2021/6/4 0004 17:30\n&#34;&#34;&#34;\n&#34;&#34;&#34;\n计算复利公式\n&#34;&#34;&#34;\nimport matplotlib.pyplot as plt\n# 加载字体\nimport matplotlib as mpl\n\nmpl.rcParams[&#34;font.sans-serif&#34;] &#61; [&#34;KaiTi&#34;]\nmpl.rcParams[&#34;font.serif&#34;] &#61; [&#34;KaiTi&#34;]\n\na &#61; 100000 # 这里是本金\nr1 &#61; 0.045\nx &#61; []\ny1 &#61; []\ny2 &#61; []\ny3 &#61; []\ny4 &#61; []\ny5 &#61; []\ny6 &#61; []\n# 填入数据\nfor i in range(1, 6):\n x.append(i)\n y1.append(a * (1 &#43; r1) ** i)\n\nfilename1 &#61; &#34;利率&#34; &#43; str(r1) &#43; &#34;.txt&#34;\n\nwith open(filename1, &#34;w&#34;, encoding&#61;&#34;utf-8&#34;) as f1:\n for i in range(1, 6):\n f1.write(\n &#34;第&#34; &#43; str(i) &#43; &#34;年的本息和为&#xff1a;&#34; &#43; str(y1[i - 1]).split(&#39;.&#39;)[0] &#43; &#39;.&#39; &#43; str(y1[i - 1]).split(&#39;.&#39;)[1][:2] &#43; &#34;\\n&#34;)\n\n\n\n# 绘图\ntitle &#61; &#34;本金为&#34; &#43; str(a) &#43; &#34;的时候的复利金额&#34;\nplt.title(title)\nplt.plot(x, y1, &#34;b^-&#34;, label&#61;&#34;利率为&#34; &#43; str(r1))\nplt.xlabel(&#34;存储年限&#34;)\nplt.ylabel(&#34;本息和&#34;)\n\n# 显示标签\nplt.legend()\n# 显示格子\nplt.grid()\n# 保存图片\nplt.savefig(&#34;复利图.PNG&#34;)\nplt.show()</code></pre>\n\n<p> </p>\n\n<p><img alt=\"\" height=\"1048\" src=\"https://img-ask.csdnimg.cn/upload/1622799225422.png\" width=\"1928\" /></p>\n",
"tag_name": "python",
"python": "import matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams[\"font.sans-serif\"] = [\"KaiTi\"]\nmpl.rcParams[\"font.serif\"] = [\"KaiTi\"]\na = 100000 \nr1 = 0.045\nx = []\ny1 = []\ny2 = []\ny3 = []\ny4 = []\ny5 = []\ny6 = []\nfor i in range(1, 6):\n\tx.append(i)\n\ty1.append(a * (1 + r1) ** i)\nfilename1 = \"利率\" + str(r1) + \".txt\"\nwith open(filename1, \"w\", encoding=\"utf-8\") as f1:\n\tfor i in range(1, 6):\n\t\tf1.write(\n\t\t\t\"\" + str(i) + \"年的本息和为:\" + str(y1[i - 1]).split('.')[0] + '.' + str(y1[i - 1]).split('.')[1][:2] + \"\\n\")\ntitle = \"本金为\" + str(a) + \"的时候的复利金额\"\nplt.title(title)\nplt.plot(x, y1, \"b^-\", label=\"利率为\" + str(r1))\nplt.xlabel(\"存储年限\")\nplt.ylabel(\"本息和\")\nplt.legend()\nplt.grid()\nplt.savefig(\"复利图.PNG\")\nplt.show()",
"topic_link": "https://bbs.csdn.net/topics/600470500",
"status": 0,
"keywords": "算法高阶,计算几何学,算法问题选编",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/16.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 1048722,
"question_title": "Python编写程序,对模拟打字游戏并进行成绩判定",
"question_content": "1、编写一个程序,要求该程序运行后,生成一个名为“new.py”的文件,在每一行后面添加行号,且行号以\"#\"开头,即行号为注释信息,不影响程序的正常执行。同时要求所有行的\"#\"号垂直对齐。",
"difficulty": "简单",
"answer_id": 1246232,
"answer_content": "\n```\nimport random\nimport string\n\n\nclass Game:\n def __init__(self):\n self.total_count = 0\n self.right_count = 0\n self.len = 15\n\n def play(self):\n n = int(input('请输入打字次数:'))\n while n:\n right = 0\n print('游戏次数剩余{}次'.format(n))\n word = ''.join(random.sample(string.ascii_letters + string.digits + ' ', random.randint(1, self.len)))\n print(word)\n res = input()\n if len(res) != len(word):\n print('输入的字符串超长,无效')\n self.total_count += len(word)\n n -= 1\n continue\n for i in zip(word, res):\n if i[0] == i[1]:\n right += 1\n self.right_count += right\n self.total_count += len(word)\n print('本次的正确率为{:.2f}%\\n'.format(right * 100 / len(word)))\n n -= 1\n print('整体的正确率为{:.2f}%'.format(self.right_count * 100 / self.total_count))\n\n\nif __name__ == '__main__':\n Game().play()\n\n```",
"tag_name": "python",
"python": "import random\nimport string\nclass Game:\n def __init__(self):\n self.total_count = 0\n self.right_count = 0\n self.len = 15\n def play(self):\n n = int(input('请输入打字次数:'))\n while n:\n right = 0\n print('游戏次数剩余{}次'.format(n))\n word = ''.join(random.sample(string.ascii_letters + string.digits + ' ', random.randint(1, self.len)))\n print(word)\n res = input()\n if len(res) != len(word):\n print('输入的字符串超长,无效')\n self.total_count += len(word)\n n -= 1\n",
"topic_link": "https://bbs.csdn.net/topics/600469985",
"status": 0,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/17.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7462191,
"question_title": "python 编程 猜数字",
"question_content": "<p>游戏&#xff08;猜数字&#xff09;&#xff1a;随机生成一个数字&#xff08;取值范围&#xff3b;1&#xff0c;100&#xff3d;&#xff09;。让用户猜数字&#xff0c;并给出相应的提示&#xff1a;\n如果用户输入比答案大&#xff0c;提示‘Too big&#xff0c; try again’&#xff1b;\n反之&#xff0c;提示‘Too small, try again’&#xff1b;\n如果猜中了&#xff0c;提示‘Congratulations!’。\n最后&#xff0c;要给出反馈&#xff08;答案&#xff0c;猜的次数&#xff0c;猜的历史&#xff09;。\n </p>",
"difficulty": "简单",
"answer_id": 53447630,
"answer_content": "<p><img alt=\"\" height=\"415\" src=\"https://img-ask.csdnimg.cn/upload/1624885147252.png\" width=\"494\" /></p>\n\n<pre>\n<code>import random\n\nv&#61;random.randint(1,100)\n\ncount&#61;0\nhis&#61;[]\nwhile 1:\n vi&#61;input(&#34;输入猜测值&#xff1a;&#34;)\n count&#43;&#61;1\n his.append(vi)\n if int(vi)&gt;v:\n print(&#34;Too big,try again&#34;)\n if int(vi)&lt;v:\n print(&#34;Too small,try again&#34;)\n if int(vi)&#61;&#61;v:\n print(&#39;Congratulations!&#39;)\n break\n\nprint(&#34;答案是&#xff1a;%d&#34; % v)\nprint(&#34;猜测次数&#xff1a;%d&#34; % count)\nst&#61;&#39;&#39;\nfor i in his:\n st&#43;&#61;i\n st&#43;&#61;&#39;,&#39;\nst&#61;st[:-1]\nprint(&#34;猜测历史&#xff1a;%s&#34; % st)\n\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "import random\nv=random.randint(1,100)\ncount=0\nhis=[]\nwhile 1:\n\tvi=input(\"输入猜测值:\")\n\tcount+=1\n\this.append(vi)\n\tif int(vi)>v:\n\t\tprint(\"Too big,try again\")\n\tif int(vi)<v:\n\t\tprint(\"Too small,try again\")\n\tif int(vi)==v:\n\t\tprint('Congratulations!')\n\t\tbreak\nprint(\"答案是:%d\" % v)\nprint(\"猜测次数:%d\" % count)\nst=''\nfor i in his:\n\tst+=i\n\tst+=','\nst=st[:-1]\nprint(\"猜测历史:%s\" % st)",
"topic_link": "https://bbs.csdn.net/topics/600469885",
"status": 1,
"keywords": "猜大小",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/18.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7424772,
"question_title": "用Python来创造一个提示用户输入数字的乘法表?",
"question_content": "<p>如果用户选择菜单选项1&#xff0c;提示用户输入1到10之间的整数&#xff0c;并打印一个乘法表&#xff0c;显示整数1与输入整数相乘的结果&#xff0c;如下面的示例所示。注意:不需要检查输入的数字是否在1到10之间。如果用户选择菜单选项2&#xff0c;退出程序。如果用户在菜单选择中输入了1或2以外的任何内容&#xff0c;输出信息“菜单选择错误&#xff0c;请重试并继续程序。”\n结果应该如下&#xff1a;\n1)创建乘法表\n2)退出程序\n请从以上菜单中选择一个选项:1\n输入一个介于1到10之间的整数:3</p>\n<p style=\"margin-left:0.5in; margin-right:0in\">1  2  3 </p>\n<p style=\"margin-left:0.5in; margin-right:0in\">2  4  6 </p>\n<p style=\"margin-left:0.5in; margin-right:0in\">3  6  9 </p>\n<p style=\"margin-left:0.5in; margin-right:0in\"\n1)创建乘法表\n2)退出程序\n请从以上菜单中选择一个选项:4\n菜单选择错误&#xff0c;请重试\n \n1)创建乘法表\n2)退出程序\n请从以上菜单中选择一个选项&#xff1a;2\n谢谢你使用乘法表\n \n </p>\n<p style=\"margin-left:0.5in; margin-right:0in\"> </p>",
"difficulty": "简单",
"answer_id": 53385987,
"answer_content": "<pre>\n<code class=\"language-python\">while True:\n print(&#39;1)创建乘法表&#39;)\n print(&#39;2)退出程序&#39;)\n n &#61; input(&#39;请从以上菜单中选择一个选项:&#39;).strip()\n if n&#61;&#61;&#39;1&#39;:\n x &#61; int(input(&#39;输入一个介于1到10之间的整数:&#39;))\n for i in range(1,x&#43;1):\n for j in range(1,x&#43;1):\n print(&#34;{:&gt;4}&#34;.format(i*j),end&#61;&#34;&#34;)\n print()\n elif n&#61;&#61;&#39;2&#39;:\n print(&#39;谢谢你使用乘法表&#39;)\n break\n else:\n print(&#39;菜单选择错误&#xff0c;请重试&#39;)\n print(&#39;-----------------------------&#39;)\n</code></pre>\n\n<p><img alt=\"\" height=\"392\" src=\"https://img-ask.csdnimg.cn/upload/1619860246564.png\" width=\"334\" /></p>",
"tag_name": "python",
"python": "while True:\n\tprint('1)创建乘法表')\n\tprint('2)退出程序')\n\tn = input('请从以上菜单中选择一个选项:').strip()\n\tif n=='1':\n\t\tx = int(input('输入一个介于1到10之间的整数:'))\n\t\tfor i in range(1,x+1):\n\t\t\tfor j in range(1,x+1):\n\t\t\t\tprint(\"{:>4}\".format(i*j),end=\"\")\n\t\t\tprint()\n\telif n=='2':\n\t\tprint('谢谢你使用乘法表')\n\t\tbreak\n\telse:\n\t\tprint('菜单选择错误,请重试')\n\tprint('-----------------------------')",
"topic_link": "https://bbs.csdn.net/topics/600469886",
"status": 1,
"keywords": "算法中阶,贪心算法,活动选择问题,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/19.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7455330,
"question_title": "Python团队出行财务系统管理",
"question_content": "<p>假设有N个人出行&#xff0c;期间会产生交通费&#xff08;A&#xff09;、餐费&#xff08;B&#xff09;、住宿费&#xff08;C&#xff09;三种类型的费用&#xff0c;设计并实现团体出行期间的财务管理系统&#xff0c;能够根据每笔账单的信息&#xff0c;最终计算出在此次出行中&#xff0c;每个人的花销。</p>\n\n<p>系统运行时&#xff0c;先输入出行人数N的值。随后给出2个菜单&#xff0c;菜单1代表输入账单&#xff0c;菜单2代表计算每个人的花销。1.输入的账单信息时&#xff0c;先输入该笔账单参与的人数M&#xff0c;再输入涉及到的M个成员的代号&#xff08;从1-N&#xff09;&#xff0c;账单类别&#xff08;A、B、C三者之一&#xff09;&#xff0c;及金额。2.计算每个人最终花销时&#xff0c;根据账单采用AA制的方式。</p>",
"difficulty": "中等",
"answer_id": 53438235,
"answer_content": "<pre>\n<code>p &#61; int(input(&#39;出行人数&#xff1a;&#39;))\nprint(&#39;成员代号为&#xff1a;&#39;, [i for i in range(1, p&#43;1)])\nd &#61; {}\nr &#61; []\nm &#61; input(&#39;账单记录输入1&#xff0c;计算花销输入2&#39;)\nif m &#61;&#61; &#39;1&#39;:\n n &#61; int(input(&#39;参与人数&#xff1a;&#39;))\n pl &#61; &#39;&#39;\n for i in range(n):\n pl &#43;&#61; input(&#39;成员代号&#xff1a;&#39;)\n d[&#39;pl&#39;] &#61; pl\n d[&#39;style&#39;] &#61; input(&#39;账单类别&#xff0c;输入a、b、c&#xff1a;&#39;)\n d[&#39;money&#39;] &#61; int(input(&#39;账单金额&#xff1a;&#39;))\n r.append(d)\nelif m &#61;&#61; &#39;2&#39;:\n for i in r:\n avg &#61; d[&#39;money&#39;] / len(d[&#39;pl&#39;])\n print(f&#34;成员代号:{len(d[&#39;pl&#39;])}&#xff0c;每人平均费用&#xff1a;{avg}&#xff0c;账单类型&#xff1a;{d[&#39;style&#39;]}&#34;)\n</code></pre>\n\n<p>如果对你有帮助&#xff0c;可以点击我这个回答右上方的【采纳】按钮&#xff0c;给我个采纳吗&#xff0c;谢谢</p>\n",
"tag_name": "python",
"python": "p = int(input('出行人数:'))\nprint('成员代号为:', [i for i in range(1, p+1)])\nd = {}\nr = []\nm = input('账单记录输入1,计算花销输入2')\nif m == '1':\n n = int(input('参与人数:'))\n pl = ''\n for i in range(n):\n pl += input('成员代号:')\n d['pl'] = pl\n d['style'] = input('账单类别,输入a、b、c:')\n d['money'] = int(input('账单金额:'))\n r.append(d)\nelif m == '2':\n for i in r:\n avg = d['money'] / len(d['pl'])\n print(f\"成员代号:{len(d['pl'])},每人平均费用:{avg},账单类型:{d['style']}\")\n",
"topic_link": "https://bbs.csdn.net/topics/600469883",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/2.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7457794,
"question_title": "找出素数对",
"question_content": "任意输入一个大于10的偶数编程找出所有和等于该偶数的素数对",
"difficulty": "简单",
"answer_id": 53442307,
"answer_content": "<pre>\n<code class=\"language-python\">h &#61; 0\n\ndef a(h):\n x &#61; 0\n for j in range(2, h):\n if h % j &#61;&#61; 0:\n x &#61; 1\n break\n\n if x &#61;&#61; 0:\n return 1\n\nn &#61; int(input(&#34;输入任意大于10的偶数&#xff1a;&#34;))\nfor i in range(n,n&#43;1):\n h &#61; 0\n if i % 2 &#61;&#61; 0:\n for k in range(2, i):\n if a(k) &#61;&#61; 1 and a(i - k) &#61;&#61; 1:\n h &#61; 1\n if h &#61;&#61; 0:\n print(&#34;%d can&#39;t&#34; % i)\n break\n else:\n print(&#34;%d&#61;%d&#43;%d&#34; % (i, k, i - k))\n break\n</code></pre>\n\n<p>参考这个&#xff1a;<a href=\"https://blog.csdn.net/edxuanlen/article/details/78336429?utm_source&#61;blogxgwz5&amp;utm_medium&#61;distribute.pc_relevant.none-task-blog-baidujs_title-1&amp;spm&#61;1001.2101.3001.4242\">https://blog.csdn.net/edxuanlen/article/details/78336429?utm_source&#61;blogxgwz5&amp;utm_medium&#61;distribute.pc_relevant.none-task-blog-baidujs_title-1&amp;spm&#61;1001.2101.3001.4242</a></p>\n",
"tag_name": "python",
"python": "h = 0\ndef a(h):\n\tx = 0\n\tfor j in range(2, h):\n\t\tif h % j == 0:\n\t\t\tx = 1\n\t\t\tbreak\n\tif x == 0:\n\t\treturn 1\nn = int(input(\"输入任意大于10的偶数:\"))\nfor i in range(n,n+1):\n\th = 0\n\tif i % 2 == 0:\n\t\tfor k in range(2, i):\n\t\t\tif a(k) == 1 and a(i - k) == 1:\n\t\t\t\th = 1\n\t\t\t\tif h == 0:\n\t\t\t\t\tprint(\"%d can't\" % i)\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tprint(\"%d=%d+%d\" % (i, k, i - k))\n\t\t\t\t\tbreak",
"topic_link": "https://bbs.csdn.net/topics/600470404",
"status": 1,
"keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/20.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1089095,
"question_title": "将一组数尽可能均匀地分成两堆,使两个堆中的数的和尽可能相等",
"question_content": "<p>麦克叔叔去世了,他在遗嘱中给他的两个孙子阿贝和鲍勃留下了一堆珍贵的口袋妖怪卡片。遗嘱中唯一的方向是“尽可能均匀地分配纸牌的价值”。作为Mike遗嘱的执行人,你已经为每一张口袋妖怪卡片定价,以获得准确的货币价值。你要决定如何将口袋妖怪卡片分成两堆,以尽量减少每一堆卡片的价值总和的差异。\n例如,你有下列n=8 个口袋妖怪卡片:\n![图片说明](https://img-ask.csdn.net/upload/202006/27/1593264517_917706.png)\n经过大量的工作,你发现你可以用下面的方法来划分卡片:\n![图片说明](https://img-ask.csdn.net/upload/202006/27/1593264556_309804.png)\n这给了安倍10美元的牌给了鲍勃11美元的牌。这是最好的除法吗?\n你要做的是解决n张牌的问题其中每张牌ci都有一个正整数值vi.你的解决方法是计算牌应该如何被分割以及每摞牌的价值。\n输入输出示例如下:\n![图片说明](https://img-ask.csdn.net/upload/202006/27/1593264585_763892.png)\n1.通过检查所有可能的桩以蛮力解决此问题。 对这种蛮力算法的时间复杂度进行分析,并通过实施和实验验证您的分析结果(既写出来算法的设计思路等),并用python算法实现编程\n2.通过动态编程开发更有效的算法。 您应该首先通过动态编程的思想来分析此问题,并编写相应的递归属性。 对这种算法的时间复杂度进行分析,并通过实施和实验验证您的分析结果。并用python代码实现动态编程</p>",
"difficulty": "中等",
"answer_id": 1295967,
"answer_content": "\n```\ndef deal(data,flag):\n a=[]\n for i in data:\n if i>=flag:\n return [i]\n elif a==[]:\n a.append([i])\n else:\n a=a+[k+[i] for k in a if sum(k)+i<=flag]\n a.append([i])\n #return sorted(a,key=sum)[-1]\n target=sum(max(a,key=sum))\n return list(filter(lambda x:sum(x)==target,a))\nif __name__=='__main__':\n c=[2,1,3,1,5,2,3,4]\n flag=sum(c)//2\n res=deal(c,flag)\n print(res)\n\n```\n\n算法复杂度最差是N的3次方",
"tag_name": "python",
"python": "def deal(data,flag):\n\ta=[]\n\tfor i in data:\n\t\tif i>=flag:\n\t\t\treturn [i]\n\t\telif a==[]:\n\t\t\ta.append([i])\n\t\telse:\n\t\t\ta=a+[k+[i] for k in a if sum(k)+i<=flag]\n\t\t\ta.append([i])\n\ttarget=sum(max(a,key=sum))\n\treturn list(filter(lambda x:sum(x)==target,a))\nif __name__=='__main__':\n\tc=[2,1,3,1,5,2,3,4]\n\tflag=sum(c)//2\n\tres=deal(c,flag)\n\tprint(res)",
"topic_link": "https://bbs.csdn.net/topics/600469887",
"status": 1,
"keywords": "算法初阶,基础知识,算法基础,设计算法,分析分治算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/21.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7425360,
"question_title": "任意多行字符串拆分数值求和",
"question_content": "<pre><p>编写程序&#xff0c;统计每行字符串中若干整数的和。每行字符串中整数间的分隔符可能有逗号“,”、分号“ ;”和空格&#xff0c;有多少行就求多少行。\n输入格式:\n任意输入若干行由整数构成的字符串&#xff08;回车换行&#xff09;&#xff0c;整数间以逗号或空格或分号分隔。测试数确保至少有一行数据&#xff0c;字符串中的整数数据均合法有效。最后以一个回车结束输入。\n输出格式:\n对应输出原输入串&#xff08;一行中的字符序列&#xff09;&#xff0c;冒号后输出各个整数之和。\n</pre><p>输入样例:</p></p>\n<pre>\n<code>1; 2 ,3\n2 3; 4\n10,20 30; 40\n 9</code></pre>\n<p>输出样例:</p>\n<pre>\n<code>1; 2 ,3:6\n2 3; 4:9\n10,20 30; 40:100</code></pre>",
"difficulty": "简单",
"answer_id": 53386858,
"answer_content": "<pre>\n<code class=\"language-python\"># s &#61; &#34;&#34;&#34;1; 2 ,3\n# 2 3; 4\n# 10,20 30; 40\n# 9&#34;&#34;&#34;\n\nstopword &#61; &#39;&#39; # 修改输入终止符为空行\n\ns &#61; &#39;&#39;\n\nprint(&#39;请输入内容,输入空行回车结束输入&#xff1a;&#39;)\n\nfor line in iter(input, stopword):\n s &#61; s &#43; line &#43; &#34;\\n&#34;\n\nprint(s)\n\nl &#61; s.splitlines()\nfor num in range(0, len(l)):\n one &#61; l[num].replace(&#39;,&#39;, &#39; &#39;).replace(&#39;;&#39;, &#39; &#39;).split()\n onesum &#61; 0\n for i in range(0, len(one)):\n onesum &#61; onesum &#43; int(one[i])\n print(l[num] &#43; &#34;:&#34; &#43; str(onesum))\n</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "stopword = '' \ns = ''\nprint('请输入内容,输入空行回车结束输入:')\nfor line in iter(input, stopword):\n\ts = s + line + \"\\n\"\nprint(s)\nl = s.splitlines()\nfor num in range(0, len(l)):\n\tone = l[num].replace(',', ' ').replace(';', ' ').split()\n\tonesum = 0\n\tfor i in range(0, len(one)):\n\t\tonesum = onesum + int(one[i])\n\tprint(l[num] + \":\" + str(onesum))",
"topic_link": "https://bbs.csdn.net/topics/600470501",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/22.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7456975,
"question_title": "五星好评点评器",
"question_content": "运用Python制作五星好评点评器",
"difficulty": "简单",
"answer_id": 53441583,
"answer_content": "<p> </p>\n\n<p> </p>\n\n<p>大概吧# 也不知道你要的结果是什么。</p>\n\n<p> </p>\n\n<p> </p>\n\n<pre>\n<code class=\"language-python\">import sys\nfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QGroupBox\nclass Windows(QWidget):\n def __init__(self):\n super().__init__()\n self.resize(500, 400)\n self.setWindowTitle(&#39;好评点评器&#39;)\n self.butt()\n\n def butt(self):\n self.button &#61; QPushButton(self)\n self.button.setText(&#39;python&#39;)\n self.button.resize(100, 70)\n self.button1 &#61; QPushButton(self)\n self.button1.setText(&#39;C&#39;)\n self.button1.resize(100, 70)\n self.button1.move(100, 0)\n self.button2 &#61; QPushButton(self)\n self.button2.setText(&#39;C&#43;&#43;&#39;)\n self.button2.resize(100, 70)\n self.button2.move(200, 0)\n self.button3 &#61; QPushButton(self)\n self.button3.setText(&#39;C#&#39;)\n self.button3.resize(100, 70)\n self.button3.move(300, 0)\n self.button4 &#61; QPushButton(self)\n self.button4.setText(&#39;JAVA&#39;)\n self.button4.resize(100, 70)\n self.button4.move(400, 0)\n self.box &#61; QGroupBox(&#39;评论&#39;, self) # 分组线\n self.box.move(10, 80)\n self.box.resize(200, 100)\n self.box1 &#61; QGroupBox(&#39;我的评论&#39;, self) # 分组线\n self.box1.move(220, 80)\n self.box1.resize(200, 100)\n self.qlbel &#61; QLabel(self.box)\n self.qlbel.setText(&#39;非常好&#xff0c;简单易学&#39;)\n self.qlbel.move(20, 30)\n self.qlbel1 &#61; QLabel(self.box)\n self.qlbel1.setText(&#39;★★★★☆&#39;)\n self.qlbel1.move(20, 70)\n self.qlbel1.setStyleSheet(&#39; color: #FF4500;&#39;)\n\n\n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n app &#61; QApplication(sys.argv)\n windows &#61; Windows()\n windows.show()\n sys.exit(app.exec_())\n</code></pre>\n",
"tag_name": "python",
"python": "import sys\nfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QGroupBox\nclass Windows(QWidget):\n def __init__(self):\n super().__init__()\n self.resize(500, 400)\n self.setWindowTitle('好评点评器')\n self.butt()\n def butt(self):\n self.button = QPushButton(self)\n self.button.setText('python')\n self.button.resize(100, 70)\n self.button1 = QPushButton(self)\n self.button1.setText('C')\n self.button1.resize(100, 70)\n self.button1.move(100, 0)\n self.button2 = QPushButton(self)\n self.button2.setText('C++')\n self.button2.resize(100, 70)\n self.button2.move(200, 0)\n self.button3 = QPushButton(self)\n self.button3.setText('C#')\n self.button3.resize(100, 70)\n self.button3.move(300, 0)\n self.button4 = QPushButton(self)\n self.button4.setText('JAVA')\n self.button4.resize(100, 70)\n self.button4.move(400, 0)\n self.box = QGroupBox('评论', self) # 分组线\n self.box.move(10, 80)\n self.box.resize(200, 100)\n self.box1 = QGroupBox('我的评论', self) # 分组线\n self.box1.move(220, 80)\n self.box1.resize(200, 100)\n self.qlbel = QLabel(self.box)\n self.qlbel.setText('非常好,简单易学')\n self.qlbel.move(20, 30)\n self.qlbel1 = QLabel(self.box)\n self.qlbel1.setText('★★★★☆')\n self.qlbel1.move(20, 70)\n self.qlbel1.setStyleSheet(' color: #FF4500;')\nif __name__ == '__main__':\n app = QApplication(sys.argv)\n windows = Windows()\n windows.show()\n sys.exit(app.exec_())\n",
"topic_link": "https://bbs.csdn.net/topics/600469888",
"status": 0,
"keywords": "点评器",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/23.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7447154,
"question_title": "文件检索",
"question_content": "<p>1、从键盘输入字符串至str&#xff0c;输入路径至findpath&#xff0c;检索并列出findpath中所有含有str的文件名及所在路径&#xff1b;</p>\n\n<p>2、将检索到的前5个文件复制到某个指定的目录中。</p>",
"difficulty": "简单",
"answer_id": 53426675,
"answer_content": "<pre>\n<code class=\"language-python\">import os\nimport shutil\n\nstr &#61; input(&#39;输入字符串&#xff1a;&#39;)\nfindpath &#61; r&#39;e:\\xxx&#39;\n\ndef listdir(path, list_name): #传入存储的list\n for file in os.listdir(path):\n file_path &#61; os.path.join(path, file)\n if os.path.isdir(file_path):\n listdir(file_path, list_name)\n else:\n list_name.append(file_path)\n\nflist &#61; []\nlistdir(findpath, flist)\nflist &#61; [x for x in flist if x.find(str)!&#61;-1]\nprint(*flist,sep&#61;&#39;\\n&#39;)\npath2 &#61; r&#39;e:\\xxx2&#39;\nfor x in flist[:5]:\n shutil.copy(x,path2)\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "import os\nimport shutil\nstr = input('输入字符串:')\nfindpath = r'e:\\xxx'\ndef listdir(path, list_name): \n for file in os.listdir(path):\n file_path = os.path.join(path, file)\n if os.path.isdir(file_path):\n listdir(file_path, list_name)\n else:\n list_name.append(file_path)\nflist = []\nlistdir(findpath, flist)\nflist = [x for x in flist if x.find(str)!=-1]\nprint(*flist,sep='\\n')\npath2 = r'e:\\xxx2'\nfor x in flist[:5]:\n shutil.copy(x,path2)\n",
"topic_link": "https://bbs.csdn.net/topics/600469889",
"status": 0,
"keywords": "图算法,算法高阶,单源最短路径,差分约束和最短路径",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/24.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7439730,
"question_title": "冒泡排序",
"question_content": "<p>用冒泡排序编写一个函数&#xff0c;允许接受多个数字的输入&#xff0c;不使用sort方法&#xff0c;给数字从小到大排序&#xff0c;最终输出从小到大的列表。</p>",
"difficulty": "简单",
"answer_id": 53414504,
"answer_content": "<p> 你要的python版本&#xff0c;它来了</p>\n\n<pre>\n<code class=\"language-python\">#!/usr/bin/python3\n\n\ndef bubbleSort(arr):\n n &#61; len(arr)\n \n # 遍历所有数组元素\n for i in range(n):\n \n # Last i elements are already in place\n for j in range(0, n-i-1):\n \n if arr[j] &gt; arr[j&#43;1] :\n arr[j], arr[j&#43;1] &#61; arr[j&#43;1], arr[j]\n\n\nx&#61;input(&#34;请输入数字,空格分隔&#xff1a;&#34;)\nxlist&#61;x.split(&#34; &#34;)\narr &#61; [int(xlist[i]) for i in range(len(xlist))]\nbubbleSort(arr)\n \nprint (&#34;排序后的数组:&#34;)\nfor i in range(len(arr)):\n print (&#34;%d&#34; %arr[i]),</code></pre>\n\n<p><img alt=\"\" height=\"118\" src=\"https://img-ask.csdnimg.cn/upload/1622516700637.png\" width=\"310\" /></p>\n",
"tag_name": "python",
"python": "#!/usr/bin/python3\ndef bubbleSort(arr):\n\tn = len(arr)\n\tfor i in range(n):\n\t\tfor j in range(0, n-i-1):\n\t\t\tif arr[j] > arr[j+1] :\n\t\t\t\tarr[j], arr[j+1] = arr[j+1], arr[j]\nx=input(\"请输入数字,空格分隔:\")\nxlist=x.split(\" \")\narr = [int(xlist[i]) for i in range(len(xlist))]\nbubbleSort(arr)\nprint (\"排序后的数组:\")\nfor i in range(len(arr)):\n\tprint (\"%d\" %arr[i]),",
"topic_link": "https://bbs.csdn.net/topics/600469986",
"status": 1,
"keywords": "桶排序,算法初阶,线性时间排序,排序和顺序统计量",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/25.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7461537,
"question_title": "线性回归",
"question_content": "<p>把以下数据制作成csv数据文件,并命名为LR_data_exam.csv。 <br />\n对该文件内的数据进行线性回归<br />\n画出数据的散点图<br />\n建立线性回归模型并拟合<br />\n打印拟合结果(参数) <br />\n画出数据和拟合直线的图<br />\n用model.predict预测x为10,20,30时的结果<br />\n用拟合参数预测x为40,50,60时的结果<br />\n   battery,KM<br />\n   2,12<br />\n   5,31<br />\n   8,45<br />\n   8,52<br />\n   13,79<br />\n   15,85<br />\n   17,115<br />\n   19,119<br />\n   21,135<br />\n   24,145</p>",
"difficulty": "简单",
"answer_id": 53446955,
"answer_content": "<pre>\n<code class=\"language-python\">import numpy as np\nimport csv\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LinearRegression\nimport matplotlib\nmatplotlib.rcParams[&#39;font.sans-serif&#39;] &#61; [&#39;SimHei&#39;]\nmatplotlib.rcParams[&#39;font.family&#39;]&#61;[&#39;sans-serif&#39;]\n\ndata &#61; &#39;&#39;&#39;battery,KM\n 2,12\n 5,31\n 8,45\n 8,52\n 13,79\n 15,85\n 17,115\n 19,119\n 21,135\n 24,145&#39;&#39;&#39;.split(&#39;\\n&#39;)\nfor i,j in enumerate(data):\n data[i] &#61; j.replace(&#39; &#39;, &#39;&#39;).split(&#39;,&#39;)\n\nwith open(&#39;LR_data_exam.csv&#39;,&#39;w&#39;) as csvFile: #一行一行地写入到csv文件中\n writer &#61; csv.writer(csvFile)\n #先写columns_name\n writer.writerow(data[0])\n #写入多行用writerows\n writer.writerows(data[1:])</code></pre>\n\n<p> LR_data_exam.csv&#xff1a;</p>\n\n<p><img alt=\"\" height=\"310\" src=\"https://img-ask.csdnimg.cn/upload/1624861372289.png\" width=\"402\" /></p>\n\n<pre>\n<code class=\"language-python\">dataframe &#61; pd.read_csv(&#34;LR_data_exam.csv&#34;, sep&#61;&#34;,&#34;)\nx &#61; dataframe[&#39;battery&#39;].tolist()\ny &#61; dataframe[&#39;KM&#39;].tolist()\n\n# 画出数据的散点图\nfig &#61; plt.figure()\nfig.set_size_inches(10, 4) # 整个绘图区域的宽度10和高度4\nax &#61; fig.add_subplot(1, 2, 1) # 整个绘图区分成一行两列&#xff0c;当前图是第一个。\n# 画出原始数据的散点图。\nax.set_title(&#34;散点图&#34;)\nax.set_xlabel(&#34;battery&#34;)\nax.set_ylabel(&#34;KM&#34;)\nax.scatter(x, y)\nplt.show()</code></pre>\n\n<p><img alt=\"\" height=\"269\" src=\"https://img-ask.csdnimg.cn/upload/1624861411134.png\" width=\"309\" /></p>\n\n<pre>\n<code class=\"language-python\">regressor &#61; LinearRegression()\nregressor &#61; regressor.fit(np.reshape(x,(-1, 1)),np.reshape(y,(-1, 1)))\nprint(regressor.coef_, regressor.intercept_) # 打印拟合结果(参数)</code></pre>\n\n<pre>\n[[6.28763667]] [-1.19680404]</pre>\n\n<pre>\n<code class=\"language-python\"># 画出数据和拟合直线的图\nplt.scatter(x, y)\nplt.plot(np.reshape(x,(-1,1)), regressor.predict(np.reshape(x,(-1,1))))\nplt.xlabel(&#34;battery&#34;) \nplt.ylabel(&#34;KM&#34;) \nplt.title(&#34;拟合结果&#34;)\nplt.show()</code></pre>\n\n<p><img alt=\"\" height=\"275\" src=\"https://img-ask.csdnimg.cn/upload/1624861449046.png\" width=\"399\" /></p>\n\n<pre>\n<code class=\"language-python\">print(regressor.predict([[10], [20], [30]])) # model.predict预测x为10,20,30时的结果\nfor i in [40,50,60]:\n print(regressor.coef_*i &#43; regressor.intercept_) #用拟合参数预测x为40,50,60时的结果</code></pre>\n\n<pre>\n[[ 61.67956266]\n [124.55592935]\n [187.43229605]]\n[[250.30866274]]\n[[313.18502944]]\n[[376.06139613]]</pre>\n",
"tag_name": "python",
"python": "import numpy as np\nimport csv\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LinearRegression\nimport matplotlib\nmatplotlib.rcParams['font.sans-serif'] = ['SimHei']\nmatplotlib.rcParams['font.family']=['sans-serif']\ndata = '''battery,KM\n 2,12\n 5,31\n 8,45\n 8,52\n 13,79\n 15,85\n 17,115\n 19,119\n 21,135\n 24,145'''.split('\\n')\nfor i,j in enumerate(data):\n data[i] = j.replace(' ', '').split(',')\nwith open('LR_data_exam.csv','w') as csvFile: \n writer = csv.writer(csvFile)\n writer.writerow(data[0])\n writer.writerows(data[1:])\n",
"topic_link": "https://bbs.csdn.net/topics/600470502",
"status": 0,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/26.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7458855,
"question_title": "统计成绩分布",
"question_content": "<p>随机产生10个[0&#xff0c;100]之间的整数&#xff0c;模拟5名学生的考试成绩。\n要求:输出成绩&#xff0c;计算平均分&#xff0c;统计高于平均分的人数</p>",
"difficulty": "简单",
"answer_id": 53443612,
"answer_content": "<p>用random函数就行&#xff0c;然后再遍历&#xff0c;输出&#xff0c;求平均分</p>\n\n<pre>\n<code>import random\na&#61;[]\nsum&#61;0\ncnt&#61;0\nfor i in range(10):\n x&#61;random.randint(10,100)\n sum&#43;&#61;x\n print(x,end&#61;&#34; &#34;)\n a.append(x)\nave&#61;sum/10\nprint()\nprint(ave)\nfor i in range(10):\n if a[i]&gt;ave:\n cnt&#43;&#61;1\nprint(cnt)\n</code></pre>\n\n<p><img alt=\"\" height=\"111\" src=\"https://img-ask.csdnimg.cn/upload/1624535692509.png\" width=\"459\" /></p>\n\n<p>如果有帮助请点一下<strong>我回答右上方的采纳</strong>&#xff0c;谢谢&#xff01;以后有什么问题可以互相交流。 </p>\n",
"tag_name": "python",
"python": "import random\na=[]\nsum=0\ncnt=0\nfor i in range(10):\n\tx=random.randint(0,100)\n\tsum+=x\n\tprint(x,end=\" \")\n\ta.append(x)\nave=sum/10\nprint()\nprint(ave)\nfor i in range(10):\n\tif a[i]>ave:\n\t\tcnt+=1\nprint(cnt)",
"topic_link": "https://bbs.csdn.net/topics/600470405",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/27.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7440964,
"question_title": "按要求实现程序功能",
"question_content": " 第一题:\n \n \n 给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。\n \n \n 输入格式:\n \n \n 输入首先给出正整数N(≤10​5​​),即员工总人数;随后给出N个整数,即每个员工的工龄,范围在[0, 50]。\n \n \n 输出格式:\n \n \n 按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”。每项占一行。如果人数为0则不输出该项。\n \n \n 输入样例:\n \n \n 8\n10 2 0 5 7 2 5 2 \n \n 输出样例:\n \n \n 0:1\n2:3\n5:2\n7:1\n10:1 \n \n 第二题:\n \n \n 编写程序,统计每行字符串中若干整数的和。每行字符串中整数间的分隔符可能有逗号“,”、分号“ ;”和空格,有多少行就求多少行。\n \n \n 输入格式:\n \n \n 任意输入若干行由整数构成的字符串(回车换行),整数间以逗号或空格或分号分隔。测试数确保至少有一行数据,字符串中的整数数据均合法有效。最后以一个回车结束输入。\n \n \n 输出格式:\n \n \n 对应输出原输入串(一行中的字符序列),冒号后输出各个整数之和。\n \n \n 输入样例:\n \n \n 1; 2 ,3\n2 3; 4\n10,20 30; 40\n 9 \n \n 输出样例:\n \n \n 1; 2 ,3:6\n2 3; 4:9\n10,20 30; 40:100\n 9:9 ",
"difficulty": "中等",
"answer_id": 53416676,
"answer_content": "<p>这样修改即可&#xff1a;</p>\n\n<pre>\n<code class=\"language-python\">第一题&#xff1a;\na&#61;int(input())\nb&#61;map(int,input().split())\nc&#61;{}\nfor i in b:\n if i in c:\n c[i]&#43;&#61;1 #键存在就加1\n else:\n c[i]&#61;1 #而键不存在就c[i]&#61;1\n\nfor i in sorted(c.items(),key&#61;lambda x:x[0]): #字典排序&#xff08;按工龄的递增顺序输出每个工龄的员工个数&#xff09;\n print(&#39;{}:{}&#39;.format(i[0],i[1]))\n\n#第二题\n\nnum&#61;[]\nns&#61;[]\nwhile True:\n n &#61; input() \n if n &#61;&#61; &#39;&#39;:\n break\n else:\n num .append(n.replace(&#39;;&#39;, &#39; &#39;).replace(&#39;,&#39;, &#39; &#39;).split())\n ns.append(n)\ntry:\n for a,b in zip(num,ns):\n result&#61;0\n for i in a:\n result &#43;&#61; int(i)\n print(b&#43;f&#39;:{result}&#39;)\n\nexcept:\n print(&#39;divided by zero&#39;)</code></pre>\n\n<p>如满意解答&#xff0c;请点采纳。 </p>\n",
"tag_name": "python",
"python": "a=int(input())\nb=map(int,input().split())\nc={}\nfor i in b:\n\tif i in c:\n\t\tc[i]+=1 \n\telse:\n\t\tc[i]=1 \nfor i in sorted(c.items(),key=lambda x:x[0]): \n\tprint('{}:{}'.format(i[0],i[1]))\nnum=[]\nns=[]\nwhile True:\n\tn = input() \n\tif n == '':\n\t\tbreak\n\telse:\n\t\tnum .append(n.replace(';', ' ').replace(',', ' ').split())\n\t\tns.append(n)\ntry:\n\tfor a,b in zip(num,ns):\n\t\tresult=0\n\t\tfor i in a:\n\t\t\tresult += int(i)\n\t\tprint(b+f':{result}')\nexcept:\n\tprint('divided by zero')",
"topic_link": "https://bbs.csdn.net/topics/600470503",
"status": 0,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/28.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7447625,
"question_title": "根据指定值从一个列表中查找所有匹配元素的位置,要求使用列表中的index方法进行查找。",
"question_content": "<p><strong>题目内容&#xff1a;</strong>\n编写程序实现以下功能&#xff1a;根据指定值从一个列表中查找所有匹配元素的位置&#xff0c;要求使用列表中的index方法进行查找。\n \n<strong>输入格式:</strong>\n先输入待查找元素的值。\n再输入一个整数&#xff0c;表示列表中的元素个数。\n最后依次输入列表中的元素。\n \n<strong>输出格式&#xff1a;</strong>\n输出一个列表&#xff0c;各元素值为匹配元素的位置。如果没有匹配元素&#xff0c;则输出一个空列表。\n \n<strong>输入样例&#xff1a;</strong>\n10\n5\n5\n10\n15\n10\n20\n \n<strong>输出样例&#xff1a;</strong>\n[1, 3]\n \n<strong>输入样例&#xff1a;</strong>\n30\n5\n5\n10\n15\n10\n20\n \n<strong>输出样例&#xff1a;</strong>\n[]</p>",
"difficulty": "简单",
"answer_id": 53427546,
"answer_content": "<pre>\n<code class=\"language-python\">check &#61; input()\nnum &#61; int(input())\nlist1 &#61; []\nfor i in range(num):\n x &#61; input()\n list1.append(x)\nindices &#61; []\nstart &#61; 0\nwhile 1:\n try:\n x &#61; list1.index(check,start)\n indices.append(x)\n start &#61; x&#43;1\n except:\n break\nprint(indices)</code></pre>\n\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623404307590.png\" /></p>\n\n<p> </p>\n",
"tag_name": "python",
"python": "check = input()\nnum = int(input())\nlist1 = []\nfor i in range(num):\n\tx = input()\n\tlist1.append(x)\nindices = []\nstart = 0\nwhile 1:\n\ttry:\n\t\tx = list1.index(check,start)\n\t\tindices.append(x)\n\t\tstart = x+1\n\texcept:\n\t\tbreak\nprint(indices)",
"topic_link": "https://bbs.csdn.net/topics/600469987",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/29.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7431923,
"question_title": "运气最佳游戏",
"question_content": "<p>运气最佳游戏:\n一群人排成一圈&#xff0c;按“1&#xff0c;2&#xff0c;......n”依次编号。然后从第1个人开始数&#xff0c;数到m把 踢出圈&#xff0c;其后的人再从1开始数&#xff0c;数到m&#xff0c;再把它踢出去......\n如此不停地进行下去直到最后只剩下一个人为止,那个人就是运气最佳。\n具体需求如下所示。\n编写函数模拟该游戏。\n根据用户输入的m和n&#xff0c;指定人的总数n和踢出第m个人。\n最后输出运气最佳的人的编号。</p>",
"difficulty": "简单",
"answer_id": 53400791,
"answer_content": "<pre>\n<code class=\"language-python\">n &#61; eval(input(&#34;请输入总人数&#xff1a;&#34;))\nm &#61; eval(input(&#34;请输踢出第几个人&#xff1a;&#34;))\n\nlist1 &#61; []\nfor i in range(1, n&#43;1):\n list1.append(i)\n\nwhile len(list1) !&#61; 1:\n if len(list1) &gt;&#61; m:\n list1 &#61; list1[m:] &#43; list1[0:m-1]\n print(list1)\n elif len(list1) &lt; m:\n while len(list1) !&#61; 1:\n if m%len(list1) !&#61; 0:\n list1 &#61; list1[m%len(list1):] &#43; list1[0:m%len(list1)-1]\n else:\n list1 &#61; list1[0:-2]\n\nprint(&#34;最佳运气人编号为&#xff1a;%d号&#xff01;&#34;%list1[0])</code></pre>\n\n<p>这个可以吗&#xff1f;</p>\n",
"tag_name": "python",
"python": "n = eval(input(\"请输入总人数:\"))\nm = eval(input(\"请输踢出第几个人:\"))\nlist1 = []\nfor i in range(1, n+1):\n list1.append(i)\nwhile len(list1) != 1:\n if len(list1) >= m:\n list1 = list1[m:] + list1[0:m-1]\n print(list1)\n elif len(list1) < m:\n while len(list1) != 1:\n if m%len(list1) != 0:\n list1 = list1[m%len(list1):] + list1[0:m%len(list1)-1]\n else:\n list1 = list1[0:-2]\nprint(\"最佳运气人编号为:%d号!\"%list1[0])\n",
"topic_link": "https://bbs.csdn.net/topics/600470296",
"status": 0,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/3.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7456701,
"question_title": "要求编写函数fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和",
"question_content": "<p>要求编写函数fn(a,n) 求a&#43;aa&#43;aaa&#43;&#43;⋯&#43;aa⋯aa(n个a&#xff09;之和&#xff0c;fn须返回的是数列和。\n从控制台输入正整数a和n的值&#xff08;两个值都不超过9&#xff09;&#xff0c;并输出fn(a,n)的结果值。</p>",
"difficulty": "简单",
"answer_id": 53441357,
"answer_content": "<pre>\n<code>def fun(a,n):\n s &#61; 1\n sum &#61; 1\n for i in range(1,n):\n s &#61; 1 &#43; s*10\n sum&#43;&#61;s\n y &#61; a *sum\n print(y)\n\ndef main():\n while(1):\n a &#61; int(input(&#39;请输入a:&#39;))\n if a&gt;9 or a&lt;0: \n print(&#39;a的值输入错误&#xff0c;请重新输入&#xff1a;&#39;)\n else:\n break\n while(1):\n n &#61; int(input(&#39;请输入n:&#39;))\n if n&gt;9 or n&lt;0: \n print(&#39;n的值输入错误&#xff0c;请重新输入&#xff1a;&#39;)\n else:\n break\n fun(a,n)\n \nif __name__ &#61;&#61; &#39;__main__&#39;:\n main()\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "def fun(a,n):\n\ts = 1\n\tsum = 1\n\tfor i in range(1,n):\n\t\ts = 1 + s*10\n\t\tsum+=s\n\ty = a *sum\n\tprint(y)\ndef main():\n\twhile(1):\n\t\ta = int(input('请输入a:'))\n\t\tif a>9 or a<0: \n\t\t\tprint('a的值输入错误,请重新输入:')\n\t\telse:\n\t\t\tbreak\n\twhile(1):\n\t\tn = int(input('请输入n:'))\n\t\tif n>9 or n<0: \n\t\t\tprint('n的值输入错误,请重新输入:')\n\t\telse:\n\t\t\tbreak\n\tfun(a,n)\nif __name__ == '__main__':\n\tmain()",
"topic_link": "https://bbs.csdn.net/topics/600469890",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/30.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7425433,
"question_title": "求两个给定正整数的最大公约数和最小公倍数",
"question_content": "<pre><p>本题要求两个给定正整数的最大公约数和最小公倍数。\n输入格式:\n输入在两行中分别输入正整数x和y。\n输出格式:\n在一行中输出最大公约数和最小公倍数的值。\n</pre><pre>输入样例1:\n在这里给出一组输入。\n例如&#xff1a;</p><pre><code>100\n1520</code></pre><p>输出样例1:\n在这里给出相应的输出。\n例如&#xff1a;</p><pre><code>20 7600</code></pre></pre>",
"difficulty": "中等",
"answer_id": 53386909,
"answer_content": "<pre>\n<code class=\"language-python\">&#34;&#34;&#34;该函数返回两个数的最大公约数&#34;&#34;&#34; \ndef hcf(x, y):\n # 获取最小值\n if x &gt; y:\n smaller &#61; y\n else:\n smaller &#61; x \n for i in range(1,smaller &#43; 1):\n if((x % i &#61;&#61; 0) and (y % i &#61;&#61; 0)):\n hcf &#61; i \n return hcf\n&#34;&#34;&#34;该函数返回两个数的最小公倍数&#34;&#34;&#34; \ndef lcm(x, y): \n # 获取最大的数\n if x &gt; y:\n greater &#61; x\n else:\n greater &#61; y\n \n while(True):\n if((greater % x &#61;&#61; 0) and (greater % y &#61;&#61; 0)):\n lcm &#61; greater\n break\n greater &#43;&#61; 1\n \n return lcm\n \n# 用户输入两个数字\nnum1 &#61; int(input(&#34;输入第一个数字: &#34;))\nnum2 &#61; int(input(&#34;输入第二个数字: &#34;))\n \nprint(&#34;最大公约数为&#34;,hcf(num1, num2),&#34;最小公倍数为&#34;,lcm(num1,num2))</code></pre>\n\n<p>运行结果&#xff1a;</p>\n\n<p><img alt=\"\" height=\"85\" src=\"https://img-ask.csdnimg.cn/upload/1620135200306.png\" width=\"262\" /></p>",
"tag_name": "python",
"python": "def hcf(x, y):\n if x > y:\n\t smaller = y\n else:\n\t smaller = x \n for i in range(1,smaller + 1):\n\t if((x % i == 0) and (y % i == 0)):\n\t\t hcf = i \n return hcf\ndef lcm(x, y): \n if x > y:\n\t greater = x\n else:\n\t greater = y\n while(True):\n\t if((greater % x == 0) and (greater % y == 0)):\n\t\t lcm = greater\n\t\t break\n\t greater += 1\n return lcm\nnum1 = int(input(\"输入第一个数字: \"))\nnum2 = int(input(\"输入第二个数字: \"))\nprint(\"最大公约数为\",hcf(num1, num2),\"最小公倍数为\",lcm(num1,num2))",
"topic_link": "https://bbs.csdn.net/topics/600469988",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/31.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 759934,
"question_title": "将一个列表中字典字段相同的元素合并并且值相加",
"question_content": "如下两个列表,需要将oldList转化为newList,去掉相同字段的字典,并且去掉的参数里面的值要相加\noldList = [{'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1972},\n {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1450},\n {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1334}]\nnewList = [{'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 4756},\n {'3-3': 406, '3-2': 0, '3-1': 0, '3-0': 0}]\n",
"difficulty": "简单",
"answer_id": 816448,
"answer_content": "你好,我写了一种方式供你参考,希望可以帮到你。\n\n```\nimport operator\n\noldList = [{'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1972},\n {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1450},\n {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1334}]\n\nnewList = []\n\n# 先把oldList第一个元素给newList,防止下面for循环中newList为空\nnewList.append(oldList[0])\n\n# 每给newList添加一个元素时都和前面的比较,若keys()相同,则合并\nfor t in range(1,len(oldList)):\n for li in newList:\n if operator.eq(li.keys(), oldList[t].keys()):\n for key in li.keys():\n li[key] += oldList[t][key]\n break\n # 若keys()都不相同,则把新元素添加到newList\n elif operator.eq(li,newList[-1]):\n newList.append(oldList[t])\n break\n\nprint(newList)\n\n```",
"tag_name": "python",
"python": "import operator\noldList = [{'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1972},\n\t\t {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n\t\t {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1450},\n\t\t {'3-3': 203, '3-2': 0, '3-1': 0, '3-0': 0},\n\t\t {'0-0': 0, '0-1': 0, '0-2': 0, '0-3': 1334}]\nnewList = []\nnewList.append(oldList[0])\nfor t in range(1,len(oldList)):\n\tfor li in newList:\n\t\tif operator.eq(li.keys(), oldList[t].keys()):\n\t\t\tfor key in li.keys():\n\t\t\t\tli[key] += oldList[t][key]\n\t\t\tbreak\n\t\telif operator.eq(li,newList[-1]):\n\t\t\tnewList.append(oldList[t])\n\t\t\tbreak\nprint(newList)",
"topic_link": "https://bbs.csdn.net/topics/600469989",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/32.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7443246,
"question_title": "字符串统计",
"question_content": "<p>从键盘输入一个包含有英文字母、数字、空格和其它字符的字符串&#xff0c;并分别实现下面的功能&#xff1a;统计字符串中出现2次的英文字母&#xff08;区分大小写&#xff09;\n统计字符串中出现n次的数字&#xff0c;n从键盘输入</p>",
"difficulty": "简单",
"answer_id": 53420309,
"answer_content": "<pre>\n<code>#第一题\ns&#61;input(&#39;input a string:\\n&#39;)\ndict1&#61;{}\nfor c in s:\n if c.isalpha():\n if w not in dict1.keys():\n dict1[w] &#61; 1\n else:\n dict1[w] &#43;&#61; 1\nfor key in dict1.keys():\n if dict1[key]&#61;&#61;2:\n print(key)\n\n\n\n#第二题\ns&#61;input(&#39;input a string:\\n&#39;)\nn&#61;int(input(&#39;input a n:\\n&#39;))\ndict2&#61;{}\nfor c in s:\n if c.isdigit():\n if m not in dict2.keys():\n dict2[m] &#61; 1\n else:\n dict2[m] &#43;&#61; 1\nfor key in dict2.keys():\n if dict2[key]&#61;&#61;n:\n print(key)\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "#第一题\ns=input('input a string:\\n')\ndict1={}\nfor c in s:\n\tif c.isalpha():\n\t\tif c not in dict1.keys():\n\t\t\tdict1[c] = 1\n\t\telse:\n\t\t\tdict1[c] += 1\nfor key in dict1.keys():\n\tif dict1[key]==2:\n\t\tprint(key)\n#第二题\ns=input('input a string:\\n')\nn=int(input('input a n:\\n'))\ndict2={}\nfor c in s:\n\tif c.isdigit():\n\t\tif c not in dict2.keys():\n\t\t\tdict2[c] = 1\n\t\telse:\n\t\t\tdict2[c] += 1\nfor key in dict2.keys():\n\tif dict2[key]==n:\n\t\tprint(key)",
"topic_link": "https://bbs.csdn.net/topics/600470406",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/33.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 1098045,
"question_title": "knn算法",
"question_content": "k近邻法 的算法\n编写一个代码(其输入参数包含下面内容)\n输入2个类别的数据(都是二维的坐标点),可设输入每一类约10个点\n输入一个坐标点的坐标\n输出:使用K近邻法判断该坐标点属于哪个类别",
"difficulty": "中等",
"answer_id": 1312397,
"answer_content": "```python\n#!/usr/bin/python\n# coding=utf-8\n#########################################\n# kNN: k Nearest Neighbors\n\n# 输入: newInput: (1xN)的待分类向量\n# dataSet: (NxM)的训练数据集\n# labels: 训练数据集的类别标签向量\n# k: 近邻数\n\n# 输出: 可能性最大的分类标签\n#########################################\n\nfrom numpy import *\nimport operator\n\n# 创建一个数据集,包含2个类别共4个样本\ndef createDataSet():\n # 生成一个矩阵,每行表示一个样本\n group = array([[1.0, 0.9], [1.0, 1.0], [0.1, 0.2], [0.0, 0.1]])\n # 4个样本分别所属的类别\n labels = ['A', 'A', 'B', 'B']\n return group, labels\n\n# KNN分类算法函数定义\ndef kNNClassify(newInput, dataSet, labels, k):\n numSamples = dataSet.shape[0] # shape[0]表示行数\n\n # # step 1: 计算距离[\n # 假如:\n # Newinput:[1,0,2]\n # Dataset:\n # [1,0,1]\n # [2,1,3]\n # [1,0,2]\n # 计算过程即为:\n # 1、求差\n # [1,0,1] [1,0,2]\n # [2,1,3] -- [1,0,2]\n # [1,0,2] [1,0,2]\n # =\n # [0,0,-1]\n # [1,1,1]\n # [0,0,-1]\n # 2、对差值平方\n # [0,0,1]\n # [1,1,1]\n # [0,0,1]\n # 3、将平方后的差值累加\n # [1]\n # [3]\n # [1]\n # 4、将上一步骤的值求开方,即得距离\n # [1]\n # [1.73]\n # [1]\n #\n # ]\n # tile(A, reps): 构造一个矩阵,通过A重复reps次得到\n # the following copy numSamples rows for dataSet\n diff = tile(newInput, (numSamples, 1)) - dataSet # 按元素求差值\n squaredDiff = diff ** 2 # 将差值平方\n squaredDist = sum(squaredDiff, axis = 1) # 按行累加\n distance = squaredDist ** 0.5 # 将差值平方和求开方,即得距离\n\n # # step 2: 对距离排序\n # argsort() 返回排序后的索引值\n sortedDistIndices = argsort(distance)\n classCount = {} # define a dictionary (can be append element)\n for i in xrange(k):\n # # step 3: 选择k个最近邻\n voteLabel = labels[sortedDistIndices[i]]\n\n # # step 4: 计算k个最近邻中各类别出现的次数\n # when the key voteLabel is not in dictionary classCount, get()\n # will return 0\n classCount[voteLabel] = classCount.get(voteLabel, 0) + 1\n\n # # step 5: 返回出现次数最多的类别标签\n maxCount = 0\n for key, value in classCount.items():\n if value > maxCount:\n maxCount = value\n maxIndex = key\n\n return maxIndex\n```\n```python\n#!/usr/bin/python\n# coding=utf-8\nimport KNN\nfrom numpy import *\n# 生成数据集和类别标签\ndataSet, labels = KNN.createDataSet()\n# 定义一个未知类别的数据\ntestX = array([1.2, 1.0])\nk = 3\n# 调用分类函数对未知数据分类\noutputLabel = KNN.kNNClassify(testX, dataSet, labels, 3)\nprint \"Your input is:\", testX, \"and classified to class: \", outputLabel\n\ntestX = array([0.1, 0.3])\noutputLabel = KNN.kNNClassify(testX, dataSet, labels, 3)\nprint \"Your input is:\", testX, \"and classified to class: \", outputLabel\n```",
"tag_name": "python",
"python": "from numpy import *\nimport operator\ndef createDataSet():\n\tgroup = array([[1.0, 0.9], [1.0, 1.0], [0.1, 0.2], [0.0, 0.1]])\n\tlabels = ['A', 'A', 'B', 'B']\n\treturn group, labels\ndef kNNClassify(newInput, dataSet, labels, k):\n\tnumSamples = dataSet.shape[0] \n\tdiff = tile(newInput, (numSamples, 1)) - dataSet \n\tsquaredDiff = diff ** 2 \n\tsquaredDist = sum(squaredDiff, axis = 1) \n\tdistance = squaredDist ** 0.5 \n\tsortedDistIndices = argsort(distance)\n\tclassCount = {} \n\tfor i in range(k):\n\t\tvoteLabel = labels[sortedDistIndices[i]]\n\t\tclassCount[voteLabel] = classCount.get(voteLabel, 0) + 1\n\tmaxCount = 0\n\tfor key, value in classCount.items():\n\t\tif value > maxCount:\n\t\t\tmaxCount = value\n\t\t\tmaxIndex = key\n\treturn maxIndex",
"topic_link": "https://bbs.csdn.net/topics/600470407",
"status": 0,
"keywords": "分治法,算法初阶,基础知识,算法基础,设计算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/34.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7418328,
"question_title": "求列表(整数列表)的平衡点",
"question_content": "对于一个整数列表,如果有一个切分位置使其前面的元素之和等于后面的元素之和,就称该位置是平衡点\n请编写程序求列表(整数列表)的平衡点,不存在时给出提示。\n思路:\n1.确定一个位置\n2.求在这个位置前面的元素之和,与在这个位置后面的元素之和,做比较,判断是否相等\n 2.1 怎么求前面元素之和:\n 假设这个位置是[i],那么前面元素就是[0]~[i-1], 后面元素为[i+1]~[len(list)-1]\n 进行求和:累加求和\n 2.2判断是否相等:相等,给出这个平衡位置;不相等,输出“该列表不存在平衡点”",
"difficulty": "中等",
"answer_id": 53374253,
"answer_content": "<pre>\n<code class=\"language-python\">a&#61;input(&#39;请输入一个整数列表:&#39;).split(&#34;,&#34;)\nflag&#61;0\nfor i in range(0,len(a)-1):\n s1&#61;0\n for m in range(0,i):\n s1&#43;&#61;int(a[m])\n s2&#61;0\n for n in range(i&#43;1,len(a)):\n s2&#43;&#61;int(a[n])\n if s1&#61;&#61;s2:\n print(&#39;该整数列表存在平衡点,且平衡点为&#xff1a;&#39;,a[i])\n flag&#61;1\n break\nif flag&#61;&#61;0:\n print(&#39;该整数列表不存在平衡点&#39;)6</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "a=input('请输入一个整数列表:').split(\",\")\nflag=0\nfor i in range(0,len(a)-1):\n\ts1=0\n\tfor m in range(0,i):\n\t\ts1+=int(a[m])\n\ts2=0\n\tfor n in range(i+1,len(a)):\n\t\ts2+=int(a[n])\n\tif s1==s2:\n\t\tprint('该整数列表存在平衡点,且平衡点为:',a[i])\n\t\tflag=1\n\t\tbreak\nif flag==0:\n\tprint('该整数列表不存在平衡点')",
"topic_link": "https://bbs.csdn.net/topics/600470504",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/35.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7436880,
"question_title": "计算出因子里面4和7的个数",
"question_content": "<p>输入一个正数n&#xff0c;计算出因子里面分别有几个4和7&#xff0c;输出因子中4和7的个位数</p>",
"difficulty": "简单",
"answer_id": 53409217,
"answer_content": "<pre>\n<code class=\"language-python\">n &#61; int(input(&#34;输入数字&#xff1a;&#34;))\nfactor &#61; [n] # 包括n&#xff1f;\nnum &#61; 1\nwhile num &lt;&#61; n/2&#43;1:\n if n % num &#61;&#61; 0:\n factor.append(num)\n num &#61; num &#43; 1\nprint(factor)\nm &#61; [str(i) for i in factor]\ncount4 &#61; 0\ncount7 &#61; 0\nfor i in m:\n if &#39;4&#39; in i:\n count4 &#43;&#61; 1\n print(&#39;以4结尾的因子的个位数&#xff1a;&#39;, int(i)%10)\n if &#39;7&#39; in i:\n count7 &#43;&#61; 1\n print(&#39;以7结尾的因子的个位数&#xff1a;&#39;, int(i)%10)\nprint(&#39;因子里面分别有{0}个4和{1}个7&#39;.format(count4,count7))</code></pre>\n\n<p>这里的因子包括了数字本身&#xff0c;如果不想包括可以将factor 初始化为[]</p>\n\n<p>输入&#xff1a;140</p>\n\n<p>输出&#xff1a;</p>\n\n<pre>\n[140, 1, 2, 4, 5, 7, 10, 14, 20, 28, 35, 70]\n以4结尾的因子的个位数&#xff1a; 0\n以4结尾的因子的个位数&#xff1a; 4\n以7结尾的因子的个位数&#xff1a; 7\n以4结尾的因子的个位数&#xff1a; 4\n以7结尾的因子的个位数&#xff1a; 0\n因子里面分别有3个4和2个7</pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "n = int(input(\"输入数字:\"))\nfactor = [n] \nnum = 1\nwhile num <= n/2+1:\n\tif n % num == 0:\n\t\tfactor.append(num)\n\tnum = num + 1\nprint(factor)\nm = [str(i) for i in factor]\ncount4 = 0\ncount7 = 0\nfor i in m:\n\tif '4' in i:\n\t\tcount4 += 1\n\t\tprint('以4结尾的因子的个位数:', int(i)%10)\n\tif '7' in i:\n\t\tcount7 += 1\n\t\tprint('以7结尾的因子的个位数:', int(i)%10)\nprint('因子里面分别有{0}个4和{1}个7'.format(count4,count7))",
"topic_link": "https://bbs.csdn.net/topics/600470505",
"status": 1,
"keywords": "算法高阶,数论算法,算法问题选编,整数的因子分解",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/36.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7433771,
"question_title": "检查密码强度",
"question_content": "<p>定义一个名为“isStrongPassword”的函数&#xff0c;该函数将字符串作为参数。功能然后将检查所提供的字符串是否满足以下条件&#xff0c;以检查是否为强 \n密码&#xff1a;\n1.必须至少包含1个大写和小写字母的组合\n2.必须至少包含3位数字\n3.必须至少包含3个特殊字符&#xff08;包括空格&#xff09;\n4.密码长度必须至少12个字符\n该函数将返回一个布尔值&#xff0c;即如果满足所有条件则返回True或返回False\n确保使用可能返回False值的每个可能的输入来测试函数也一样</p>",
"difficulty": "简单",
"answer_id": 53404077,
"answer_content": "<pre>\n<code>\ndef isStrongPassword(pwd):\n chars &#61; list(pwd)\n upper &#61; [c for c in chars if &#39;A&#39; &lt;&#61; c and c &lt;&#61; &#39;Z&#39;]\n lower &#61; [c for c in chars if &#39;a&#39; &lt;&#61; c and c &lt;&#61; &#39;z&#39;]\n digit &#61; [c for c in chars if &#39;0&#39; &lt;&#61; c and c &lt;&#61; &#39;9&#39; ]\n symbol &#61; [c for c in chars if not (&#39;A&#39; &lt;&#61; c and c &lt;&#61; &#39;Z&#39; or &#39;a&#39; &lt;&#61; c and c &lt;&#61; &#39;z&#39; or &#39;0&#39; &lt;&#61; c and c &lt;&#61; &#39;9&#39;)] \n strong &#61; len(upper) &gt;&#61; 1 and len(lower) &gt;&#61; 1 and len(digit) &gt;&#61; 3 and len(symbol) &gt;&#61; 3 and len(pwd) &gt;&#61; 12\n return strong\n \n \n \nprint(isStrongPassword(&#34;Str0n9P&#64;$$w0rd&#34;))\nprint(isStrongPassword(&#34;StrongPassword&#34;))\nprint(isStrongPassword(&#34;Stron9P&#64;$$0rd&#34;))\nprint(isStrongPassword(&#34;Str0n9Pass0rd&#34;))\nprint(isStrongPassword(&#34;str0n9p&#64;$$0rd&#34;))\nprint(isStrongPassword(&#34;Str0n9P&#64;$$&#34;))\nprint(isStrongPassword(&#34;12345678&#34;))\nprint(isStrongPassword(&#34;~!&#64;#$%^&amp;*()_&#43;&#34;))\nprint(isStrongPassword(&#34;STRONGPASSWORD&#34;))\n\n\n# output\nTrue\nFalse\nFalse\nFalse\nFalse\nFalse\nFalse\nFalse\nFalse</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "def isStrongPassword(pwd):\n\tchars = list(pwd)\n\tupper = [c for c in chars if 'A' <= c and c <= 'Z']\n\tlower = [c for c in chars if 'a' <= c and c <= 'z']\n\tdigit = [c for c in chars if '0' <= c and c <= '9' ]\n\tsymbol = [c for c in chars if not ('A' <= c and c <= 'Z' or 'a' <= c and c <= 'z' or '0' <= c and c <= '9')] \n\tstrong = len(upper) >= 1 and len(lower) >= 1 and len(digit) >= 3 and len(symbol) >= 3 and len(pwd) >= 12\n\treturn strong\nprint(isStrongPassword(\"Str0n9P@$$w0rd\"))\nprint(isStrongPassword(\"StrongPassword\"))\nprint(isStrongPassword(\"Stron9P@$$0rd\"))\nprint(isStrongPassword(\"Str0n9Pass0rd\"))\nprint(isStrongPassword(\"str0n9p@$$0rd\"))\nprint(isStrongPassword(\"Str0n9P@$$\"))\nprint(isStrongPassword(\"12345678\"))\nprint(isStrongPassword(\"~!@#$%^&*()_+\"))\nprint(isStrongPassword(\"STRONGPASSWORD\"))",
"topic_link": "https://bbs.csdn.net/topics/600469891",
"status": 1,
"keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/37.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7450710,
"question_title": "Python 文件 分数统计",
"question_content": "<p>【问题描述】</p>\n\n<p> </p>\n\n<p>从一个文本文件内读入任意多个学生的分数&#xff0c;求出最高分&#xff0c;最低分和平均分存入文件result.txt内。</p>\n\n<p> </p>\n\n<p>【输入形式】</p>\n\n<p> </p>\n\n<p>一个文件&#xff0c;文件中分数之间由换行隔开&#xff0c;输入的文件名为grade.txt。输入的分数都是整数。</p>\n\n<p> </p>\n\n<p>【输出形式】</p>\n\n<p> </p>\n\n<p>计算出grade.txt中所有分数的最高分&#xff0c;最低分和平均分并分3行存入result.txt的文件内。平均分保留1位小数。</p>\n\n<p> </p>\n\n<p>【样例输入】</p>\n\n<p> </p>\n\n<p>60</p>\n\n<p> </p>\n\n<p>70</p>\n\n<p> </p>\n\n<p>80</p>\n\n<p> </p>\n\n<p>【样例输出】</p>\n\n<p> </p>\n\n<p>80</p>\n\n<p> </p>\n\n<p>60</p>\n\n<p> </p>\n\n<p>70</p>\n\n<p> </p>",
"difficulty": "简单",
"answer_id": 53431675,
"answer_content": "<pre>\n<code class=\"language-python\">grade &#61; input()\nwhile grade !&#61; &#34;&#34;:\n with open(&#34;../Down/Setting/zgtest.txt&#34;,&#39;a&#39;) as f:\n f.write(grade &#43; &#39;\\n&#39;)\n grade &#61; input()\nwith open(&#34;grade.txt&#34;,&#39;r&#39;) as f: #读出分数到列表\n keys &#61; list(set(f.readlines()))\nindex &#61; 0\nwhile(index &lt; len(keys)): #处理读出来的分数为整数\n keys[index] &#61; int(keys[index].replace(&#39;\\n&#39;,&#39;&#39;))\n index &#61; index &#43; 1\nwith open(&#34;result.txt&#34;,&#39;a&#39;) as r: #写入结果\n r.write(str(max(keys)) &#43; &#39;\\n&#39;)\n r.write(str(min(keys)) &#43; &#39;\\n&#39;)\n avg &#61; round(sum(keys) / len(keys),1)\n r.write(str(avg) &#43; &#39;\\n&#39;)\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "grade = input()\nwhile grade != \"\":\n\twith open(\"../Down/Setting/zgtest.txt\",'a') as f:\n\t\tf.write(grade + '\\n')\n\tgrade = input()\nwith open(\"grade.txt\",'r') as f: \n\tkeys = list(set(f.readlines()))\nindex = 0\nwhile(index < len(keys)): \n\tkeys[index] = int(keys[index].replace('\\n',''))\n\tindex = index + 1\nwith open(\"result.txt\",'a') as r: \n\tr.write(str(max(keys)) + '\\n')\n\tr.write(str(min(keys)) + '\\n')\n\tavg = round(sum(keys) / len(keys),1)\n\tr.write(str(avg) + '\\n')",
"topic_link": "https://bbs.csdn.net/topics/600470408",
"status": 0,
"keywords": "算法高阶,计算几何学,算法问题选编,确定任意一对线段是否相交",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/38.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 6750637,
"question_title": "Python编程 随机生成一个具有 20 个元素的元素值在 1-10 之间的列表",
"question_content": "<p>随机生成一个具有 20 个元素的元素值在 1-10 之间的列表&#xff0c;输出连续最长数的个数。</p>",
"difficulty": "简单",
"answer_id": 51592169,
"answer_content": "<pre>\n<code>import random\na &#61; [random.randint(1,10) for i in range(20)]\nprint(a)\nl &#61; rl &#61; 1\nn &#61; rn &#61; a[0]\nfor v in a[1:]:\n if v&#61;&#61;n:\n l &#43;&#61; 1\n if l&gt;rl:\n rl &#61; l\n rn &#61; v\n else:\n l &#61; 1\n n &#61; v\nprint(f&#39;连续最长的数是{rn},连续了{rl}次&#39;)\n</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "import random\na = [random.randint(1,10) for i in range(20)]\nprint(a)\nl = rl = 1\nn = rn = a[0]\nfor v in a[1:]:\n\tif v==n:\n\t\tl += 1\n\t\tif l>rl:\n\t\t\trl = l\n\t\t\trn = v\n\telse:\n\t\tl = 1\n\t\tn = v\nprint(f'连续最长的数是{rn},连续了{rl}次')",
"topic_link": "https://bbs.csdn.net/topics/600469892",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/39.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7411744,
"question_title": "编写Python程序实现素数处理的功能",
"question_content": "<p style=\"margin-left:0cm; margin-right:0cm\">编写Python程序实现素数处理的功能&#xff0c;要求如下&#xff1a;</p><p style=\"margin-left:0cm; margin-right:0cm\">&#xff08;1&#xff09;从键盘输入一个整数X&#xff0c;编写一个函数Find&#xff08;x&#61;100&#xff09;&#xff0c;找出1—X之间的所有的素数&#xff08;即质数&#xff09;&#xff0c;并将这些素数按照升序存放在列表 prime_list []中。</p><p style=\"margin-left:0cm; margin-right:0cm\">&#xff08;2&#xff09;在程序中编写一个函数delete_seven( prime_list)&#xff0c;将 prime_list []中个位含有7的素数删除&#xff1b;</p><p style=\"margin-left:0cm; margin-right:0cm\"><strong>【程序要求&#xff1a;】</strong></p><p style=\"margin-left:0cm; margin-right:0cm\">&#xff08;1&#xff09;有引导用户键盘输入的提示信息&#xff0c;言语要恰当&#xff1b;</p><p style=\"margin-left:0cm; margin-right:0cm\">&#xff08;2&#xff09;函数Find&#xff08;x&#61;100&#xff09;实现要正确&#xff0c;实现列表 prime_list []中查询到的素数的输出&#xff1b;</p><p style=\"margin-left:0cm; margin-right:0cm\">&#xff08;3&#xff09;函数delete_seven( prime_list)实现正确&#xff0c;输出删除指定素数后的相关数据信息。</p>",
"difficulty": "简单",
"answer_id": 53363059,
"answer_content": "<pre>\n<code>def find(x&#61;100):\n pl &#61; []\n for m in range(2,x&#43;1):\n for k in pl:\n if m % k &#61;&#61; 0:\n break\n else:\n pl.append(m)\n return pl\n\ndef delete_seven(pl):\n return [x for x in pl if x%10!&#61;7]\n\nx &#61; int(input(&#39;输入一个整数:&#39;))\nprime_list &#61; find(x)\nprint(prime_list)\nprint(delete_seven(prime_list))\n\n</code></pre>\n\n<p><img alt=\"\" height=\"90\" src=\"https://img-ask.csdnimg.cn/upload/1617100202885.png\" width=\"421\" /></p>",
"tag_name": "python",
"python": "def find(x=100):\n\tpl = []\n\tfor m in range(2,x+1):\n\t\tfor k in pl:\n\t\t\tif m % k == 0:\n\t\t\t\tbreak\n\t\telse:\n\t\t\tpl.append(m)\n\treturn pl\ndef delete_seven(pl):\n\treturn [x for x in pl if x%10!=7]\nx = int(input('输入一个整数:'))\nprime_list = find(x)\nprint(prime_list)\nprint(delete_seven(prime_list))",
"topic_link": "https://bbs.csdn.net/topics/600470199",
"status": 1,
"keywords": "散列表,算法中阶,数据结构,散列函数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/4.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 864358,
"question_title": "请问如何用python做两个数组逐位判断?",
"question_content": "比如有以下数组:\na1: 1,0,0,1,0,0,0,1\na2: 0,0,0,0,1,1,1,1\na3: 0,1,0,1,0,1,0,0\na4: 1,0,1,1,1,1,0,0\na5: .......\n抓取三个数组进行判断,\n if ((a1第一位or a2第一位 or a3第一位=1 )and (a1第二位 or a2 第二位 or a3第二位=1)and....\n直到判断完所有位数为止,所有位都有了1的话就输出当前这三个数组,已输出的数组不参与之后的判断。",
"difficulty": "简单",
"answer_id": 993346,
"answer_content": "不知道抓取3个数组是任意3个,比如123 124 125 134 135 235...,还是顺序联系的3个,123 456 789这样抓。\n我是按照前者写的,如果不是,请说明\n```\n# -*- coding: UTF-8 -*-\nfrom itertools import combinations\n\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\n\nal = list(combinations(a,3))\nfor i in al:\n flag = True\n for j in range(len(i[0])):\n if (i[0][j] + i[1][j] + i[2][j] == 0):\n flag = False\n break\n if flag:\n print(i) \n\t\t\n```\n([1, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n\n----------------------------\n只使用1次\n\n```\n# -*- coding: UTF-8 -*-\nfrom itertools import combinations\n\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\nal = list(combinations(list(i for i in range(0, 6)),3))\nused = []\nfor i in al:\n\tflag = False\n\tif not(i[0] in used or i[1] in used or i[2] in used):\n\t\tflag = True\n\t\tfor j in range(7):\n\t\t\tif (a[i[0]][j] + a[i[1]][j] + a[i[2]][j] == 0):\n\t\t\t\tflag = False\n\t\t\t\tbreak\n\tif flag:\n\t\tused.append(i[0])\n\t\tused.append(i[1])\n\t\tused.append(i[2])\n\t\tprint(' '.join(map(str, a[i[0]])) + \"; \" + ' '.join(map(str, a[i[1]])) + \"; \" + ' '.join(map(str, a[i[2]])))\n\t\t\n```\n1 0 0 1 0 0 0 1; 0 0 0 0 1 1 1 1; 1 1 1 1 1 1 1 0\n",
"tag_name": "python",
"python": "# -*- coding: UTF-8 -*-\nfrom itertools import combinations\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\nal = list(combinations(a,3))\nfor i in al:\n\tflag = True\n\tfor j in range(len(i[0])):\n\t\tif (i[0][j] + i[1][j] + i[2][j] == 0):\n\t\t\tflag = False\n\t\t\tbreak\n\tif flag:\n\t\tprint(i)\t",
"topic_link": "https://bbs.csdn.net/topics/600469990",
"status": 1,
"keywords": "算法初阶,基础知识,分治策略,最大子数组问题",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/40.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7427183,
"question_title": "python 编写函数计算圆周率",
"question_content": "<p>计算圆周率。存在圆心在直角坐标系原点且半径为 1 的圆及其外切正方形。为计算方便&#xff0c;仅考虑位于第一象限的四分之一正方形和四分之一圆。随机生成该四分之一正方形中一系列点&#xff0c;散布于四分之一圆内比例即为圆周率四分之一。散步点越多&#xff0c;结果越精确&#xff0c;耗时也越长。</p>",
"difficulty": "简单",
"answer_id": 53391236,
"answer_content": "<pre>\n<code class=\"language-python\">from random import random\nfrom math import sqrt\n\nN&#61;eval(input(&#34;请输入次数&#xff1a;&#34;))\n#散在园内部点为0\nK&#61;0 \n#循环撒点的过程看成是遍历循环的过程&#xff0c;用for in range对所有点进行抛洒\nfor i in range(1,N&#43;1):\n# 定义x&#xff0c;y当作两个随机数的坐标值&#xff0c;这个坐标就是圆的抛点\n x,y&#61;random(),random()\n# 如何判断这个坐标就在圆内呢&#xff1f;判断这个点到圆心的距离是否等于1\n dist &#61;sqrt(x**2&#43;y**2)\n if dist&lt;&#61;1.0:\n K&#61;K&#43;1\n#用这个值比上整个区域值\npi&#61;4*(K/N) \nprint(&#34;圆周率值&#xff1a;{}&#34;.format(pi))</code></pre>\n\n<p><strong>运行结果</strong></p>\n\n<pre>\n<code>请输入次数&#xff1a;666666\n圆周率值&#xff1a;3.1404271404271404</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "from random import random\nfrom math import sqrt\nN=eval(input(\"请输入次数:\"))\nK=0 \nfor i in range(1,N+1):\n\tx,y=random(),random()\n\tdist =sqrt(x**2+y**2)\n\tif dist<=1.0:\n\t\tK=K+1\npi=4*(K/N)\t\nprint(\"圆周率值:{}\".format(pi))",
"topic_link": "https://bbs.csdn.net/topics/600469893",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/41.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7456509,
"question_title": "随机分组",
"question_content": "用python实现随机分组 要求3~4人一组",
"difficulty": "简单",
"answer_id": 53440302,
"answer_content": "<pre>\n<code>import random\n \n \n# 构建分身数据源&#xff08;25个分身对象&#xff09;\ndef get_source_data():\n data &#61; []\n for i in range(4):\n data.append({&#34;name&#34;:&#34;组长&#34;, &#34;role&#34;:i}) # role表示分身ID\n for i in range(7):\n for j in range(3):\n data.append({&#34;name&#34;: &#34;组员&#34; &#43; str(i), &#34;role&#34;: j})\n return data\n \n \n# 进行5次选择&#xff0c;选出5组\ndef group_by_random():\n des_list &#61; []\n data_list &#61; get_source_data()\n for i in range(5): # 分5组\n one_list &#61; []\n has_selected &#61; {} # 标记某个人是否已经被添加进当前组中\n while(len(one_list) &lt; 5): # 直到每组中有5个不同的人&#xff08;组中最多有同一个人的1个分身&#xff09;\n index &#61; random.randint(0, len(data_list)-1)\n person &#61; data_list[index]\n # 如果这个人&#xff08;此处不区分“分身”&#xff0c;只区分“人”&#xff09;没有被添加到当前组中&#xff0c;就添加该分身到组中&#xff0c;同时删除原数据列表中的这个分身对象\n if not has_selected.__contains__(person[&#34;name&#34;]): \n one_list.append(person[&#34;name&#34;])\n data_list.pop(index)\n has_selected[person[&#34;name&#34;]] &#61; True\n des_list.append(one_list)\n return des_list\n \n \nif __name__ &#61;&#61; &#39;__main__&#39;:\n for group in group_by_random():\n print(&#34;分组&#xff1a;&#34;, group)</code></pre>\n\n<p><img alt=\"\" height=\"154\" src=\"https://img-ask.csdnimg.cn/upload/1624348367919.png\" width=\"422\" /></p>\n\n<p>如果对你有帮助&#xff0c;可以点击我这个回答右上方的【采纳】按钮&#xff0c;给我个采纳吗&#xff0c;谢谢<br />\n </p>\n",
"tag_name": "python",
"python": "import random\ndef get_source_data():\n\tdata = []\n\tfor i in range(4):\n\t\tdata.append({\"name\":\"组长\", \"role\":i}) \n\tfor i in range(7):\n\t\tfor j in range(3):\n\t\t\tdata.append({\"name\": \"组员\" + str(i), \"role\": j})\n\treturn data\ndef group_by_random():\n\tdes_list = []\n\tdata_list = get_source_data()\n\tfor i in range(5): \n\t\tone_list = []\n\t\thas_selected = {} \n\t\twhile(len(one_list) < 5): \n\t\t\tindex = random.randint(0, len(data_list)-1)\n\t\t\tperson = data_list[index]",
"topic_link": "https://bbs.csdn.net/topics/600470409",
"status": 0,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/42.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7456124,
"question_title": "新浪微博热门话题",
"question_content": "<p>新浪微博可以在发言中嵌入“话题”&#xff0c;即将发言中的话题文字写在一对“#”之间&#xff0c;就可以生成话题链接&#xff0c;点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表&#xff0c;并将最热门的话题放在醒目的位置推荐大家关注。\n \n本题目要求实现一个简化的热门话题推荐功能&#xff0c;从大量英文&#xff08;因为中文分词处理比较麻烦&#xff09;微博中解析出话题&#xff0c;找出被最多条微博提到的话题。\n \n输入格式:\n输入说明&#xff1a;输入首先给出一个正整数N&#xff08;≤10​5​​&#xff09;&#xff0c;随后N行&#xff0c;每行给出一条英文微博&#xff0c;其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题&#xff0c;输入保证#成对出现。\n输出格式:\n第一行输出被最多条微博提到的话题&#xff0c;第二行输出其被提到的微博条数。如果这样的话题不唯一&#xff0c;则输出按字母序最小的话题&#xff0c;并在第三行输出And k more ...&#xff0c;其中k是另外几条热门话题的条数。输入保证至少存在一条话题。\n注意&#xff1a;两条话题被认为是相同的&#xff0c;如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后&#xff0c;它们是相同的字符串&#xff1b;同时它们有完全相同的分词。输出时除首字母大写外&#xff0c;只保留小写英文字母和数字&#xff0c;并用一个空格分隔原文中的单词。\n输入样例:\n4\nThis is a #test of topic#.\nAnother #Test of topic.#\nThis is a #Hot# #Hot# topic\nAnother #hot!# #Hot# topic\n输出样例:\nHot\n2\nAnd 1 more</p>",
"difficulty": "简单",
"answer_id": 53439642,
"answer_content": "<pre>\n<code class=\"language-python\">import re\n\na &#61; int(input(&#39;输入微博数量&#xff08;小于等于105的正整数&#xff09;&#xff1a;&#39;))\nb &#61; []\nc &#61; []\nwhile len(b)&lt;a:\n x &#61; input(&#39;请输入微博内容&#xff0c;小于140字&#xff1a;&#39;)\n if len(x)&lt;140:\n b.append(x)\n else:\n print(&#39;信息超出140字限制&#xff0c;请从新输入。&#39;)\n c &#43;&#61; re.findall(&#39;#[^#]&#43;#&#39;,x)\nd &#61; [{&#39;n&#39;:n,&#39;c&#39;:len(c)-len(re.findall(&#39;#[^#]&#43;#&#39;,re.sub(n,&#39;&#39;,&#39;&#39;.join(c.copy()))))} for n in set(c)]\ne &#61; sorted(d,key&#61;lambda x:x[&#39;c&#39;],reverse&#61;True)\nprint(e[0][&#39;n&#39;].title())\nprint(e[0][&#39;c&#39;])</code></pre>\n\n<p>做了个大概&#xff0c;同一微博之中&#xff0c;话题重复的我没有排重&#xff0c;排重需要去掉所有非英文字母和数字的符号、并忽略大小写区别&#xff0c;你自己实现以下&#xff0c;在c&#43;&#61;那一行&#xff0c;自己追加&#xff0c;排序没有实现同样数量的按字母再次排序&#xff0c;也自己去实现一下</p>\n",
"tag_name": "python",
"python": "import re\na = int(input('输入微博数量(小于等于105的正整数):'))\nb = []\nc = []\nwhile len(b)<a:\n\tx = input('请输入微博内容,小于140字:')\n\tif len(x)<140:\n\t\tb.append(x)\n\telse:\n\t\tprint('信息超出140字限制,请从新输入。')\n\tc += re.findall('#[^#]+#',x)\nd = [{'n':n,'c':len(c)-len(re.findall('#[^#]+#',re.sub(n,'',''.join(c.copy()))))} for n in set(c)]\ne = sorted(d,key=lambda x:x['c'],reverse=True)\nprint(e[0]['n'].title())\nprint(e[0]['c'])",
"topic_link": "https://bbs.csdn.net/topics/600470506",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/43.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7429009,
"question_title": "实现通讯录系统",
"question_content": "<p>(2)编写程序,完成以下功能:( a )设计一个空字典,用于存放用户的通信录(包括姓名和电话号码)。( b )程序运行后,显示用户选项:1新増联系人2查询联系人3删除联系人4退出程序( c )根据用户的选择,进入下一步,执行相应的功能,完成通信录的增加、查询、删除以及退出系统的功能。</p>",
"difficulty": "简单",
"answer_id": 53395410,
"answer_content": "<pre>\n<code>d &#61; {}\nwhile 1:\n s &#61; input(&#34;显示用户选项:1新増联系人2查询联系人3删除联系人4退出程序&#34;)\n if s &#61;&#61; &#39;1&#39;:\n name &#61; input(&#34;输入新增姓名:&#34;)\n number &#61; input(&#34;输入电话号码:&#34;)\n d[name] &#61; number\n if s &#61;&#61; &#39;2&#39;:\n name &#61; input(&#34;输入查询姓名:&#34;)\n print(f&#39;查询结果&#xff1a;{d.get(name, &#34;&#34;)}&#39;)\n if s &#61;&#61; &#39;3&#39;:\n name &#61; input(&#34;输入删除的姓名:&#34;)\n del d[name]\n if s &#61;&#61; &#39;4&#39;:\n break</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "d = {}\nwhile 1:\n\ts = input(\"显示用户选项:1新増联系人2查询联系人3删除联系人4退出程序\")\n\tif s == '1':\n\t\tname = input(\"输入新增姓名:\")\n\t\tnumber = input(\"输入电话号码:\")\n\t\td[name] = number\n\tif s == '2':\n\t\tname = input(\"输入查询姓名:\")\n\t\tprint(f'查询结果:{d.get(name, \"\")}')\n\tif s == '3':\n\t\tname = input(\"输入删除的姓名:\")\n\t\tdel d[name]\n\tif s == '4':\n\t\tbreak",
"topic_link": "https://bbs.csdn.net/topics/600469991",
"status": 0,
"keywords": "算法中阶,贪心算法,活动选择问题,高级设计和分析技术",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/44.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 827445,
"question_title": "循环随机取数组直到得出指定数字?",
"question_content": "举个例子:\n随机数字范围:0~100\n每组数字量:6(s1,s2,s3,s4,s5,s6)\n第二轮开始随机数字范围:新s1和新s2取值为旧s1和s2之间,新s3和新s4取值为旧s3和s4之间,新s5和新s6取值为旧s5和s6之间。\n跳出循环条件:任意数字=37\n如因s1=s2!=37&&s3=s4!=37&&s5=s6!=37使数组进入无意义无限循环,则重新取0~100六个数字并开始如上述第二轮随机的随机取值。",
"difficulty": "简单",
"answer_id": 924449,
"answer_content": "\n```python\n#!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport random\n\ndef random_test():\n # 初始化一个列表,随机0-100的6个数字\n rst_list = [random.randint(0,100) for i in range(0, 6)]\n print(rst_list)\n while 1:\n # 新建临时数据,用来存储rst_list按规则处理后的数据\n temp = []\n for k,v in enumerate(rst_list):\n if k%2==0:\n temp.append(random.randint(min([rst_list[k],rst_list[k+1]]),max([rst_list[k],rst_list[k+1]])))\n else:\n temp.append(random.randint(min(rst_list[k-1], rst_list[k]),max(rst_list[k-1], rst_list[k])))\n # 将结果temp列表,写入结果列表 rst_list\n rst_list = temp\n print(rst_list)\n # 判断37在结果列表中,则返回结果列表 rst_list\n if 37 in rst_list:\n print('rst_list:',rst_list)\n return rst_list\n else:\n # 37没在列表中,则按要求判断死循环情况(需求里的 s1=s2!=37&s3=s4!=37&s5=s6!=37 )\n # 如果进入死循环,则重新初始化结果列表 rst_list\n if rst_list[0]==rst_list[1] and rst_list[2]==rst_list[3] and rst_list[4]==rst_list[5]:\n rst_list = [random.randint(0, 100) for i in range(0, 6)]\n\ndef main():\n random_test()\n\n\nif __name__ == '__main__':\n main()\n\n```\n\n\n根据需求描述,按照最简单和容易理解的方式,逐步实现需求。注释和打印较多,实际代码篇幅并不多",
"tag_name": "python",
"python": "import random\ndef random_test():\n\trst_list = [random.randint(0,100) for i in range(0, 6)]\n\tprint(rst_list)\n\twhile 1:\n\t\ttemp = []\n\t\tfor k,v in enumerate(rst_list):\n\t\t\tif k%2==0:\n\t\t\t\ttemp.append(random.randint(min([rst_list[k],rst_list[k+1]]),max([rst_list[k],rst_list[k+1]])))\n\t\t\telse:\n\t\t\t\ttemp.append(random.randint(min(rst_list[k-1], rst_list[k]),max(rst_list[k-1], rst_list[k])))\n\t\trst_list = temp\n\t\tprint(rst_list)\n\t\tif 37 in rst_list:\n\t\t\tprint('rst_list:',rst_list)\n\t\t\treturn rst_list\n\t\telse:\n\t\t\tif rst_list[0]==rst_list[1] and rst_list[2]==rst_list[3] and rst_list[4]==rst_list[5]:\n\t\t\t\trst_list = [random.randint(0, 100) for i in range(0, 6)]\ndef main():\n\trandom_test()\nif __name__ == '__main__':\n\tmain()",
"topic_link": "https://bbs.csdn.net/topics/600469992",
"status": 1,
"keywords": "算法初阶,基础知识,随机算法,概率分析和随机算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/45.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7405895,
"question_title": "输入起始和结束的正整数,求其两个正整数之间的偶数和。",
"question_content": "<p>输入起始和结束的正整数&#xff0c;求其两个正整数之间的偶数和。</p>",
"difficulty": "简单",
"answer_id": 53355299,
"answer_content": "<pre>\n<code class=\"language-python\"># coding:utf-8\n\nx1 &#61; input(&#34;请输入起始数&#xff1a;&#34;)\nx2 &#61; input(&#34;请输入结束数&#xff1a;&#34;)\na &#61; int(x1)\nb &#61; int(x2)\n\n# 若a,b均为偶数时均算在内\nsum1 &#61; 0\nfor i in range(a, b&#43;1):\n if i % 2 &#61;&#61; 0:\n sum1 &#43;&#61; i\n i &#43;&#61; 2\n else:\n i &#43;&#61; 1\n\n# 若a,b均为偶数时&#xff0c;不算a和b\nsum2 &#61; 0\nfor i in range(a&#43;1, b):\n if i % 2 &#61;&#61; 0:\n sum2 &#43;&#61; i\n i &#43;&#61; 2\n else:\n i &#43;&#61; 1\n\nprint(str(a)&#43;&#34;到&#34;&#43;str(b)&#43;&#34;之间的偶数和(边界是偶数时算着两个边界)是&#xff1a;&#34;&#43;str(sum1))\nprint(str(a)&#43;&#34;到&#34;&#43;str(b)&#43;&#34;之间的偶数和(边界是偶数时不算两个边界)是&#xff1a;&#34;&#43;str(sum2))</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "x1 = input(\"请输入起始数:\")\nx2 = input(\"请输入结束数:\")\na = int(x1)\nb = int(x2)\nsum1 = 0\nfor i in range(a, b+1):\n\tif i % 2 == 0:\n\t\tsum1 += i\n\t\ti += 2\n\telse:\n\t\ti += 1\nsum2 = 0\nfor i in range(a+1, b):\n\tif i % 2 == 0:\n\t\tsum2 += i\n\t\ti += 2\n\telse:\n\t\ti += 1\nprint(str(a)+\"\"+str(b)+\"之间的偶数和(边界是偶数时算这两个边界)是:\"+str(sum1))\nprint(str(a)+\"\"+str(b)+\"之间的偶数和(边界是偶数时不算两个边界)是:\"+str(sum2))",
"topic_link": "https://bbs.csdn.net/topics/600470507",
"status": 1,
"keywords": "数学,奇偶数",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/46.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7451178,
"question_title": "编程通过键盘输入每一位运动员",
"question_content": "<p>体操比赛成绩统计。多名运动员&#xff0c;多个评委打分&#xff0c;去掉一个最高分和去掉一个最低分&#xff0c;对其余分数求平均分作为一个运动员成绩。\n编程通过键盘输入每位运动员编号和每个评委的成绩&#xff0c;求出运动员的最终成绩&#xff0c;并将运动员编号和最终成绩保存在一个字典中&#xff0c;形如{编号1:最终成绩1,学号2:最终成绩2.....&#xff0c;并将结果输出。</p>",
"difficulty": "简单",
"answer_id": 53432974,
"answer_content": "<pre>\n<code class=\"language-python\">t &#61; int(input(&#39;请输入评委人数&#xff08;不得少于3人&#xff09;&#xff1a;&#39;))\ns &#61; int(input(&#39;请输入学生人数&#xff08;不得少于1人&#xff09;&#xff1a;&#39;))\nstus &#61; []\nfor i in range(s):\n stu &#61; {&#39;score&#39;:[]}\n stu.update({&#39;sn&#39;:str(input(&#39;----\\n请输入学生学号&#xff1a;&#39;))})\n for j in range(t):\n stu[&#39;score&#39;].append(input(&#39;请输入评委&#39;&#43;str(j&#43;1)&#43;&#39;的评分&#xff1a;&#39;))\n stu[&#39;score&#39;].sort()\n stu.update({&#39;min&#39;:stu[&#39;score&#39;].pop(0)})\n stu.update({&#39;max&#39;:stu[&#39;score&#39;].pop()})\n stu.update({&#39;avg&#39;:eval(&#39;&#43;&#39;.join(stu[&#39;score&#39;]))/len(stu[&#39;score&#39;])})\n stus.append(stu)\nr &#61; {n[&#39;sn&#39;]:n[&#39;avg&#39;] for n in stus}\nprint(r)\n</code></pre>\n\n<p>使用列表stus存储所有学生成绩&#xff0c;每个学生的成绩信息为循环中stu词典&#xff0c;stu[&#39;sn&#39;]记录学号&#xff0c;stu[&#39;score&#39;]记录所有评委评分</p>\n\n<p>使用列表删除元素方式移除最高分和最低分并记录到min和max里&#xff0c;用剩余成绩通过字符串拼接成一个算式并运算记录到avg里</p>\n\n<p>最终组成输出格式信息词典&#xff0c;并打印</p>\n",
"tag_name": "python",
"python": "t = int(input('请输入评委人数(不得少于3人):'))\ns = int(input('请输入学生人数(不得少于1人):'))\nstus = []\nfor i in range(s):\n\tstu = {'score':[]}\n\tstu.update({'sn':str(input('----\\n请输入学生学号:'))})\n\tfor j in range(t):\n\t\tstu['score'].append(input('请输入评委'+str(j+1)+'的评分:'))\n\tstu['score'].sort()\n\tstu.update({'min':stu['score'].pop(0)})\n\tstu.update({'max':stu['score'].pop()})\n\tstu.update({'avg':eval('+'.join(stu['score']))/len(stu['score'])})\n\tstus.append(stu)\nr = {n['sn']:n['avg'] for n in stus}\nprint(r)",
"topic_link": "https://bbs.csdn.net/topics/600469993",
"status": 1,
"keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/47.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7439132,
"question_title": "以特殊格式处理连续增加的数字",
"question_content": "给出一串数字, 程序要把数字按照这样的格式输出,把连续增加的数字用 [x-y] 的形式表示,只显示这一组顺序数字的首位两个数字,不连续增加的数字单独列出。 例如:\n输入:1, 2, 3, 4, 5, 8, 10, 11, 12, 13, 20, 21, 22; \n输出:[1-5] [8] [10-13] [20-22]。",
"difficulty": "简单",
"answer_id": 53412990,
"answer_content": "<pre>\n<code class=\"language-python\"># -*- coding: utf-8 -*-\n# Python3\n# 输入格式&#xff1a;1, 2, 3, 4, 5, 8, 10, 11, 12, 13, 20, 21, 22\nseq &#61; list(map(int, input().split(&#39;,&#39;)))\ntmp &#61; [seq[0]]\nall_list &#61; []\nfor n in range(len(seq)):\n if n &#61;&#61; len(seq) - 1:\n all_list.append(tmp)\n break\n if seq[n &#43; 1] - seq[n] &#61;&#61; 1:\n tmp.append(seq[n &#43; 1])\n else:\n all_list.append(tmp)\n tmp &#61; [seq[n &#43; 1]]\nfor a in all_list:\n if len(a) &gt; 1:\n print(&#39;[%s-%s]&#39; % (a[0], a[-1]))\n else:\n print(&#39;[%s]&#39; % a[0])\n</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "seq = list(map(int, input().split(',')))\ntmp = [seq[0]]\nall_list = []\nfor n in range(len(seq)):\n\tif n == len(seq) - 1:\n\t\tall_list.append(tmp)\n\t\tbreak\n\tif seq[n + 1] - seq[n] == 1:\n\t\ttmp.append(seq[n + 1])\n\telse:\n\t\tall_list.append(tmp)\n\t\ttmp = [seq[n + 1]]\nfor a in all_list:\n\tif len(a) > 1:\n\t\tprint('[%s-%s]' % (a[0], a[-1]))\n\telse:\n\t\tprint('[%s]' % a[0])",
"topic_link": "https://bbs.csdn.net/topics/600469994",
"status": 1,
"keywords": "排序",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/48.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7423091,
"question_title": "python 输出不重复的字符",
"question_content": "<pre><p>输入一个字符串&#xff0c;把最左边的10个不重复的字符&#xff08;大小写算不同字符&#xff09;挑选出来。 如不重复的字符不到10个&#xff0c;则按实际数目输出。\n输入格式:\n输入一个字符串s。\n输出格式:\n输出一个字符串&#xff0c;包含字符串s最左边10个不重复的字符。不到10个按实际输出。</pre>\n输入样例1:</p>\n<code>Hello world, hello python</code>\n<p>输出样例1:</p>\n<pre>\n<code>Helo wrd,h</code></pre>\n<p>输入样例2:</p>\n<pre>\n<code>succeed</code></pre>\n<p>输出样例2:</p>\n<pre>\n<code>suced</code></pre>",
"difficulty": "简单",
"answer_id": 53382491,
"answer_content": "<pre>\n<code class=\"language-python\">def unique(s):\n set1 &#61; set([])\n out &#61; &#34;&#34;\n for i in range(len(s)):\n c &#61; s[i]\n if c not in set1:\n out &#61; out &#43; c\n set1.add(c)\n\n return out[0:10]\n\n\ndef main():\n strin &#61; &#34;Hello world, hello python&#34;\n ret &#61; unique(strin)\n print(ret)\n\n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n main()\n</code></pre>\n\n<p> </p>",
"tag_name": "python",
"python": "def unique(s):\n\tset1 = set([])\n\tout = \"\"\n\tfor i in range(len(s)):\n\t\tc = s[i]\n\t\tif c not in set1:\n\t\t\tout = out + c\n\t\t\tset1.add(c)\n\treturn out[0:10]\ndef main():\n\tstrin = \"Hello world, hello python\"\n\tret = unique(strin)\n\tprint(ret)\nif __name__ == '__main__':\n\tmain()",
"topic_link": "https://bbs.csdn.net/topics/600470410",
"status": 1,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/49.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 919782,
"question_title": "二次方程式",
"question_content": "为二次方程式ax**2 + bx + c = 0 设计一个名为Equation的类,这个类包括:\n1、一个参数为a、b、c的构造方法\n2、一个名为getDeta() 的方法返回判别式的值\n3、 一个名为getRoot1()和getRoot2()的方法返回方程式的两个根,如果判别式为负,则返回0。",
"difficulty": "简单",
"answer_id": 1057528,
"answer_content": "\n```\nclass XEqu(object):\n\tdef __init__(self,a,b,c):\n\t\tself.a = a\n\t\tself.b = b\n\t\tself.c = c\n\t\n\tdef getDeta(self):\n\t\treturn pow(self.b,2)-4*self.a*self.c\n\t\t\n\tdef _is_middle(self):\n\t\tif getDeta() < 0:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n\t\n\tdef getRoot1(self):\n\t\tif self._is_middle():\n\t\t\treturn (-self.b+pow(getDeta(),0.5))/(2*self.a)\n\t\telse:\n\t\t\treturn 0\n\t\t\n\tdef getRoot2(self):\n\t\tif self._is_middle():\n\t\t\treturn (-self.b-pow(getDeta(),0.5))/(2*self.a)\n\t\telse:\n\t\t\treturn 0\n```\n\n大概是这样子的吧",
"tag_name": "python",
"python": "class XEqu(object):\n\tdef __init__(self,a,b,c):\n\t\tself.a = a\n\t\tself.b = b\n\t\tself.c = c\n\tdef getDeta(self):\n\t\treturn pow(self.b,2)-4*self.a*self.c\n\tdef _is_middle(self):\n\t\tif self.getDeta() < 0:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n\tdef getRoot1(self):\n\t\tif self._is_middle():\n\t\t\treturn (-self.b+pow(self.getDeta(),0.5))/(2*self.a)\n\t\telse:\n\t\t\treturn 0\n\tdef getRoot2(self):\n\t\tif self._is_middle():\n\t\t\treturn (-self.b-pow(self.getDeta(),0.5))/(2*self.a)\n\t\telse:\n\t\t\treturn 0\n",
"topic_link": "https://bbs.csdn.net/topics/600469982",
"status": 0,
"keywords": "B树,算法高阶,高级数据结构,B树上的基本操作",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/5.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7448359,
"question_title": "按要求实现程序功能",
"question_content": "<ol><li>\n\t<ol><li>自定义函数<strong>ispalindrome(str)</strong>&#xff1a;实现对参数str的判断&#xff1a;若是回文返回<strong>True</strong>&#xff0c;否则返回<strong>False</strong></li><li>自定义函数<strong>formpalindrome(str)</strong>&#xff1a;利用参数<strong>str</strong>生成回文&#xff1a;以<strong>str</strong>最后一个字符为中心&#xff0c;生成回文&#xff0c;比如“<strong>12</strong><strong>3</strong>”-&gt;“<strong>12</strong><strong>3</strong><strong>21</strong>”&#xff1b;“<strong>Acd</strong><strong>c</strong>”-&gt;“<strong>Acd</strong><strong>c</strong><strong>dcA</strong>”</li><li>程序执行时&#xff1a;先使用<strong>input()</strong>函数接收一串字符&#xff0c;放在一个变量&#xff08;比如<strong>s</strong>&#xff09;中</li><li>调用自定义函数<strong>ispalindromic()</strong>&#xff0c;判断字符串<strong>s</strong>&#xff0c;根据返回的结果判断\n\t\t<ol><li>如果是回文&#xff1a;打印“您输入的‘********’是回文&#xff01;”</li><li>若不是回文&#xff1a;打印“您输入的‘***’不是回文&#xff01;”&#xff0c;并调用函数<strong>formpalindromic()</strong>&#xff0c;生成该字符串对应的回文&#xff08;见要求b&#xff09;&#xff0c;另起一行打印&#xff1a;“将其转变为回文的效果为&#xff1a;‘********’”</li></ol>\n\t\t</li></ol>\n\t</li><li>本次判断输出完毕&#xff0c;提示用户是否继续再来一次&#xff08;Y/N&#xff09;</li><li>根据用户的选择(Y/N)&#xff1a;\n\t<ol><li>不是’n’或’N’&#xff1a;通过外层while循环继续实现多次上述操作</li><li>‘n’或’N’    &#xff1a;输出 “下次再见&#xff01;”&#xff0c;程序执行完毕</li></ol>\n\t</li></ol>\n<p> </p>\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623549185739.png\" />\n </p>",
"difficulty": "简单",
"answer_id": 53428518,
"answer_content": "<pre>\n<code>def is_palindrome(str):\n low &#61; 0\n high &#61; len(str) - 1\n\n while low &lt; high:\n if str[low] !&#61; str[high]:\n return False\n low &#43;&#61; 1\n high -&#61; 1\n return True\ndef formpalindrome(str):\n print(&#39;将其转变为回文的效果为&#39;)\n for i in range(len(str) - 1):\n print(str[i], end&#61;&#39;&#39;)\n print(str[::-1])\ndef ispalindromic(str):\n if is_palindrome(str):\n print(&#34;您输入的&#39;&#34;&#43;str&#43;&#34;&#39;是回文&#xff01;&#34;)\n else:\n print(&#34;您输入的&#39;&#34; &#43; str &#43; &#34;&#39;不是回文&#xff01;&#34;)\n formpalindrome(str)\n\nwhile(1):\n print(&#34;请输入一串文字:&#34;)\n s&#61;input()\n ispalindromic(s)\n print(&#39;再来一次&#xff08;Y/N&#xff09;&#39;)\n c&#61;input()\n if c&#61;&#61;str(&#39;n&#39;):\n print(&#34;下次再见&#xff01;&#34;)\n break;</code></pre>\n\n<p><img alt=\"\" height=\"217\" src=\"https://img-ask.csdnimg.cn/upload/1623550739293.png\" width=\"481\" /></p>\n\n<p> </p>\n",
"tag_name": "python",
"python": "def is_palindrome(str):\n\tlow = 0\n\thigh = len(str) - 1\n\twhile low < high:\n\t\tif str[low] != str[high]:\n\t\t\treturn False\n\t\tlow += 1\n\t\thigh -= 1\n\treturn True\ndef formpalindrome(str):\n\tprint('将其转变为回文的效果为')\n\tfor i in range(len(str) - 1):\n\t\t print(str[i], end='')\n\tprint(str[::-1])\ndef ispalindromic(str):\n\tif is_palindrome(str):\n\t\tprint(\"您输入的'\"+str+\"'是回文!\")\n\telse:\n\t\tprint(\"您输入的'\" + str + \"'不是回文!\")\n\t\tformpalindrome(str)\nwhile(1):\n\tprint(\"请输入一串文字:\")\n\ts=input()\n\tispalindromic(s)\n\tprint('再来一次(Y/N)')\n\tc=input()\n\tif c==str('n'):\n\t\tprint(\"下次再见!\")\n\t\tbreak",
"topic_link": "https://bbs.csdn.net/topics/600470508",
"status": 0,
"keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/50.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7432048,
"question_title": "按照要求实现程序功能",
"question_content": "<p>1. 用循环结构编写harmonic(n)函数&#xff0c;计算并返回第n阶调和数(1&#43;1/2&#43;1/3 &#43;…&#43;1/n)。定义主函数main()&#xff0c;打印输出前m个调和数。&#xff08;m的值在main()函数中通过键盘输入&#xff0c;文件名&#xff1a;Harmonic.py &#xff09;\n2. 用递归思想编写harmonic(n)函数&#xff0c;计算并返回第n阶调和数(1&#43;1/2&#43;1/3 &#43;…&#43;1/n)。定义主函数main()&#xff0c;打印输出前m个调和数。&#xff08;m的值在main()函数中通过键盘输入&#xff0c;文件名&#xff1a;Harmonic_Recursion.py &#xff09;\n3. 利用可变参数编写函数aver_nums(*b)&#xff0c;求任意个数的数值平均值&#xff0c;定义主函数main()&#xff0c;在main()函数通过键盘输入若干学生的成绩&#xff0c;调用aver_nums(*b)函数求出成绩的平均分&#xff0c;并打印输出。 (文件名&#xff1a; AverScore.py &#xff09;</p>",
"difficulty": "简单",
"answer_id": 53400931,
"answer_content": "<pre>\n<code class=\"language-python\"># Harmonic.py\ndef harmonic(n):\n result &#61; 0\n for i in range(1, n&#43;1):\n result &#43;&#61; 1/i\n return result\n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n m &#61; int(input())\n for i in range(1, m&#43;1):\n print(harmonic(i))\n\n# Harmonic_Recursion.py\ndef harmonic(n):\n if n&#61;&#61;1:\n return 1\n return harmonic(n-1)&#43;1/n\n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n m &#61; int(input())\n for i in range(1, m&#43;1):\n print(harmonic(i))\n\n# AverScore.py \ndef aver_nums(*b):\n n &#61; len(b)\n return sum(b)/n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n score &#61; input() # 输入成绩&#xff0c;之间用空格隔开\n b &#61; [int(i) for i in score.split(&#39; &#39;)]\n print(aver_nums(*b))</code></pre>\n",
"tag_name": "python",
"python": "def harmonic(n):\n\tresult = 0\n\tfor i in range(1, n+1):\n\t\tresult += 1/i\n\treturn result\nif __name__ == '__main__':\n\tm = int(input())\n\tfor i in range(1, m+1):\n\t\tprint(harmonic(i))\ndef harmonic(n):\n\tif n==1:\n\t\treturn 1\n\treturn harmonic(n-1)+1/n\nif __name__ == '__main__':\n\tm = int(input())\n\tfor i in range(1, m+1):\n\t\tprint(harmonic(i))\ndef aver_nums(*b):\n\tn = len(b)\n\treturn sum(b)/n\nif __name__ == '__main__':\n\tscore = input() \n\tb = [int(i) for i in score.split(' ')]\n\tprint(aver_nums(*b))",
"topic_link": "https://bbs.csdn.net/topics/600470400",
"status": 0,
"keywords": "算法初阶,基础知识,分治策略,用主方法求解递归式",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/6.ipynb?type=file"
}
}
\ No newline at end of file
{
"question_id": 7455269,
"question_title": "列表奇偶拆分",
"question_content": "<p>【问题描述】\n输入一个列表&#xff0c;包含若干个整数&#xff08;允许为空&#xff09;&#xff0c;然后将其中的奇数和偶数单独放置在一个列表中&#xff0c;保持原有顺序\n【输入形式】\n【输出形式】\n分两行输出&#xff0c;第一行输出偶数序列&#xff0c;第二行输出奇数序列\n【样例输入1】\n[48,82,47,54,55,57,27,73,86,14]\n【样例输出1】\n48, 82, 54, 86, 14\n47, 55, 57, 27, 73\n【样例输入2】\n[10, 22, 40]\n【样例输出2】\n10, 22, 40\nNONE\n【样例说明】\n如果奇偶拆分后&#xff0c;奇数列表&#xff0c;或者偶数列表为空&#xff0c;请直接输出NONE表示\n </p>",
"difficulty": "简单",
"answer_id": 53438879,
"answer_content": "<pre>\n<code class=\"language-python\">x &#61; input()\nx1 &#61; x.strip(&#39;[]&#39;) # 去除输入列表字符串的[]\nx2 &#61; x1.split(&#34;,&#34;) # 将字符串变为列表\na &#61; []\nb &#61; []\nfor i in x2:\n if int(i) % 2 &#61;&#61; 0:\n a.append(i)\n else:\n b.append(i)\nif a &#61;&#61; []:\n print(&#34;NONE&#34;)\nelse:\n print(a)\nif b &#61;&#61; []:\n print(&#34;NONE&#34;)\nelse:\n print(b)</code></pre>\n\n<p> </p>\n\n<p><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1624271862835.png\" style=\"float:left\" /></p>\n\n<p> </p>\n\n<p> </p>\n\n<p> </p>\n\n<p> </p>\n\n<p> </p>\n\n<p><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1624271893188.png\" style=\"float:left\" /></p>\n",
"tag_name": "python",
"python": "x = input()\nx1 = x.strip('[]') \nx2 = x1.split(\",\") \na = []\nb = []\nfor i in x2:\n\tif int(i) % 2 == 0:\n\t\ta.append(i)\n\telse:\n\t\tb.append(i)\nif a == []:\n\tprint(\"NONE\")\nelse:\n\tprint(a)\nif b == []:\n\tprint(\"NONE\")\nelse:\n\tprint(b)",
"topic_link": "https://bbs.csdn.net/topics/600470297",
"status": 1,
"keywords": "散列表,散列表,算法中阶,数据结构",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/7.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7450673,
"question_title": "生成100个2位随机正整数",
"question_content": "<p>生成100个2位随机正整数&#xff0c;按每行十个输出&#xff0c;并求出个位数字分别为0,1,2,3,4,5,6,7,8,9的正整数的个数</p>",
"difficulty": "简单",
"answer_id": 53431696,
"answer_content": "<pre>\n<code class=\"language-python\">#!/usr/bin/python\n# -*- coding: UTF-8 -*-\n&#34;&#34;&#34;\n&#64;author: YangPC\n&#64;time:2021/06/16\n&#64;QQ:327844761\n&#64;微信公众号&#xff1a;ewbang\n&#34;&#34;&#34;\nimport random\n\n&#39;&#39;&#39;\n用Python生成100个2位随机正整数&#xff0c;按每行十个输出&#xff0c;并求出各位数字分别为0,1,2,3,4,5,6,7,8,9的正整数的个数\n&#39;&#39;&#39;\n\ndef fun():\n random_list &#61; [random.randint(10, 100) for n in range(100)]\n statistics &#61; {n: 0 for n in range(10)}\n for index, x in enumerate(random_list):\n print(x, end&#61;&#39; &#39;)\n statistics[int(x % 10)] &#61; statistics[int(x % 10)] &#43; 1\n if ((index &#43; 1) % 10 &#61;&#61; 0):\n print()\n print(statistics)\n\n\nif __name__ &#61;&#61; &#39;__main__&#39;:\n fun()\n</code></pre>\n\n<p>输出结果图 </p>\n\n<p><img alt=\"\" height=\"336\" src=\"https://img-ask.csdnimg.cn/upload/1623830471389.png\" width=\"886\" /></p>\n",
"tag_name": "python",
"python": "import random\ndef fun():\n\trandom_list = [random.randint(10, 99) for n in range(100)]\n\tstatistics = {n: 0 for n in range(10)}\n\tfor index, x in enumerate(random_list):\n\t\tprint(x, end=' ')\n\t\tstatistics[int(x % 10)] = statistics[int(x % 10)] + 1\n\t\tif ((index + 1) % 10 == 0):\n\t\t\tprint()\n\tprint(statistics)\nif __name__ == '__main__':\n\tfun()",
"topic_link": "https://bbs.csdn.net/topics/600470401",
"status": 1,
"keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/8.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
{
"question_id": 7434772,
"question_title": "迷宫问题,需要用递归",
"question_content": "<p>问题描述&#xff1a;一只老鼠在一个n×n迷宫的入口处&#xff0c;它想要吃迷宫出口处放着奶酪&#xff0c;问这只老鼠能否吃到奶酪&#xff1f;如果可以吃到&#xff0c;请给出一条从入口到奶酪的路径。\n思考&#xff1a;解决问题之前&#xff0c;我们首先要做的就是仔细研究问题&#xff0c;找出问题的已知条件和要得到的是什么。和解数学问题、物理问题一样要先弄懂问题。那么&#xff0c;老鼠走迷宫问题的已知条件有什么呢&#xff1f;\n数学模型重新定义问题&#xff1a;\n问题&#xff1a;问老鼠能否吃到奶酪就是问能否找到一条从迷宫入口到出口的路径。如果不能找到&#xff0c;那么老鼠就吃不到奶酪&#xff1b;如果能够找到&#xff0c;那么就给出这条路径。\n观察10×10的迷宫。这个迷宫其实是由10×10&#61;100个格子组成的&#xff0c;其中绿色格子代表墙&#xff0c;白色格子代表路&#xff0c;如图&#xff08;1&#xff09;所示。“绿色格子代表墙&#xff0c;白色格子代表路”是用语言形式描述的&#xff0c;需要转换成数学的形式。用1和0分别定义绿色格子和白色格子&#xff0c;可以得到如图&#xff08;2&#xff09;的迷宫。\n将上面10×10的迷宫定义为如下的二维数组&#xff0c;即\n m[10][10]&#61;[1,1,1,0,1,1,1,1,1,1,\n 1,0,0,0,0,0,0,0,1,1,\n 1,0,1,1,1,1,1,0,0,1,\n 1,0,1,0,0,0,0,1,0,1,\n 1,0,1,0,1,1,0,0,0,1,\n 1,0,0,1,1,0,1,0,1,1,\n 1,1,1,1,0,0,0,0,1,1,\n 1,0,0,0,0,1,1,1,0,0,\n 1,0,1,1,0,0,0,0,0,1,\n 1,1,1,1,1,1,1,1,1,1]\n有了对迷宫的数学定义&#xff0c;就可以很简单的定义迷宫的入口和出口了。迷宫的入口是m[0][3]&#xff0c;出口是m[7][9]。老鼠走迷宫问题就是要找一条从入口到出口的路径&#xff0c;如果存在就返回这条路径&#xff1b;如果不存在&#xff0c;就返回不存在这种路径。也就是说&#xff0c;要在二维数组m中找一条从m[0][3]到m[7][9]全部为0的路径。\n请使用递归解决迷宫路径查找问题。\n \n</p>",
"difficulty": "困难",
"answer_id": 53405437,
"answer_content": "<pre>\n<code class=\"language-python\">def maze(m, n, route, pos, export):\n &#34;&#34;&#34;走迷宫\n \n m - 迷宫数组&#xff0c;列表\n n - 迷宫阶数\n route - 可能的路线&#xff0c;列表\n pos - 当前位置&#xff0c;元组\n export - 出口位置&#xff0c;元组\n &#34;&#34;&#34;\n \n route.append(pos)\n if pos &#61;&#61; export:\n print(route)\n \n if pos[0] &gt; 0 and m[pos[0]-1][pos[1]] &#61;&#61; 0 and (pos[0]-1,pos[1]) not in route:\n maze(m, n, route[:], (pos[0]-1,pos[1]), export)\n \n if pos[0] &lt; n-1 and m[pos[0]&#43;1][pos[1]] &#61;&#61; 0 and (pos[0]&#43;1,pos[1]) not in route:\n maze(m, n, route[:], (pos[0]&#43;1,pos[1]), export)\n \n if pos[1] &gt; 0 and m[pos[0]][pos[1]-1] &#61;&#61; 0 and (pos[0],pos[1]-1) not in route:\n maze(m, n, route[:], (pos[0],pos[1]-1), export)\n \n if pos[1] &lt; n-1 and m[pos[0]][pos[1]&#43;1] &#61;&#61; 0 and (pos[0],pos[1]&#43;1) not in route:\n maze(m, n, route[:], (pos[0],pos[1]&#43;1), export)\n \n #print(&#39;此路不通&#39;)\n \nm &#61; [\n [1,1,1,0,1,1,1,1,1,1], \n [1,0,0,0,0,0,0,0,1,1], \n [1,0,1,1,1,1,1,0,0,1], \n [1,0,1,0,0,0,0,1,0,1], \n [1,0,1,0,1,1,0,0,0,1], \n [1,0,0,1,1,0,1,0,1,1], \n [1,1,1,1,0,0,0,0,1,1], \n [1,0,0,0,0,1,1,1,0,0], \n [1,0,1,1,0,0,0,0,0,1], \n [1,1,1,1,1,1,1,1,1,1] \n]\n\nmaze(m, len(m), list(), (0,3), (7,9))</code></pre>\n\n<p>运行结果&#xff1a;</p>\n\n<pre>\n<code class=\"language-bash\">[(0, 3), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (2, 7), (2, 8), (3, 8), (4, 8), (4, 7), (5, 7), (6, 7), (6, 6), (6, 5), (6, 4), (7, 4), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (7, 8), (7, 9)]</code></pre>\n\n<p> </p>\n",
"tag_name": "python",
"python": "def maze(m, n, route, pos, export):\n\t\"\"\"走迷宫\n\tm\t - 迷宫数组,列表\n\tn\t - 迷宫阶数\n\troute - 可能的路线,列表\n\tpos\t - 当前位置,元组\n\texport - 出口位置,元组\n\t\"\"\"\n\troute.append(pos)\n\tif pos == export:\n\t\tprint(route)\n\tif pos[0] > 0 and m[pos[0]-1][pos[1]] == 0 and (pos[0]-1,pos[1]) not in route:\n\t\tmaze(m, n, route[:], (pos[0]-1,pos[1]), export)\n\tif pos[0] < n-1 and m[pos[0]+1][pos[1]] == 0 and (pos[0]+1,pos[1]) not in route:\n\t\tmaze(m, n, route[:], (pos[0]+1,pos[1]), export)\n\tif pos[1] > 0 and m[pos[0]][pos[1]-1] == 0 and (pos[0],pos[1]-1) not in route:\n\t\tmaze(m, n, route[:], (pos[0],pos[1]-1), export)\n\tif pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route:\n\t\tmaze(m, n, route[:], (pos[0],pos[1]+1), export)\nm = [\n\t[1,1,1,0,1,1,1,1,1,1], \n\t[1,0,0,0,0,0,0,0,1,1], \n\t[1,0,1,1,1,1,1,0,0,1], \n\t[1,0,1,0,0,0,0,1,0,1], \n\t[1,0,1,0,1,1,0,0,0,1], \n\t[1,0,0,1,1,0,1,0,1,1], \n\t[1,1,1,1,0,0,0,0,1,1], \n\t[1,0,0,0,0,1,1,1,0,0], \n\t[1,0,1,1,0,0,0,0,0,1], \n\t[1,1,1,1,1,1,1,1,1,1] \n]\nmaze(m, len(m), list(), (0,3), (7,9))",
"topic_link": "https://bbs.csdn.net/topics/600470402",
"status": 1,
"keywords": "图算法,算法高阶,图的表示,基本的图算法",
"license": "csdn.net",
"notebook": {
"python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/9.ipynb?type=file"
},
"notebook_enable": 1
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册