Merge remote-tracking branch 'origin/master'

This commit is contained in:
huangge1199@hotmail.com 2021-07-11 00:06:22 +08:00
commit d9be54d957
16 changed files with 627 additions and 0 deletions

View File

@ -0,0 +1,62 @@
//数组中占比超过一半的元素称之为主要元素给你一个 整数 数组找出其中的主要元素若没有返回 -1 请设计时间复杂度为 O(N) 空间复杂度为 O(1
//) 的解决方案
//
//
//
// 示例 1
//
//
//输入[1,2,5,9,5,9,5,5,5]
//输出5
//
// 示例 2
//
//
//输入[3,2]
//输出-1
//
// 示例 3
//
//
//输入[2,2,1,1,1,2,2]
//输出2
// Related Topics 数组 计数
// 👍 134 👎 0
package leetcode.editor.cn;
//面试题 17.10:主要元素
public class FindMajorityElementLcci {
public static void main(String[] args) {
//测试代码
Solution solution = new FindMajorityElementLcci().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int majorityElement(int[] nums) {
int nu = -1;
int count = 0;
for (int num : nums) {
if (count == 0) {
nu = num;
}
if (nu == num) {
count++;
} else {
count--;
}
}
count = 0;
for (int num : nums) {
if (nu == num) {
count++;
}
}
return count * 2 > nums.length ? nu : -1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,22 @@
<p>数组中占比超过一半的元素称之为主要元素。给你一个<strong> 整数 </strong>数组,找出其中的主要元素。若没有,返回 <code>-1</code> 。请设计时间复杂度为 <code>O(N)</code> 、空间复杂度为 <code>O(1)</code> 的解决方案。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>[1,2,5,9,5,9,5,5,5]
<strong>输出:</strong>5</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>[3,2]
<strong>输出:</strong>-1</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>[2,2,1,1,1,2,2]
<strong>输出:</strong>2</pre>
<div><div>Related Topics</div><div><li>数组</li><li>计数</li></div></div>\n<div><li>👍 134</li><li>👎 0</li></div>

View File

@ -0,0 +1,71 @@
//给定两个字符串 s t判断它们是否是同构的
//
// 如果 s 中的字符可以按某种映射关系替换得到 t 那么这两个字符串是同构的
//
// 每个出现的字符都应当映射到另一个字符同时不改变字符的顺序不同字符不能映射到同一个字符上相同字符只能映射到同一个字符上字符可以映射到自己本身
//
//
//
// 示例 1:
//
//
//输入s = "egg", t = "add"
//输出true
//
//
// 示例 2
//
//
//输入s = "foo", t = "bar"
//输出false
//
// 示例 3
//
//
//输入s = "paper", t = "title"
//输出true
//
//
//
// 提示
//
//
// 可以假设 s t 长度相同
//
// Related Topics 哈希表 字符串
// 👍 366 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
//205:同构字符串
public class IsomorphicStrings{
public static void main(String[] args) {
//测试代码
Solution solution = new IsomorphicStrings().new Solution();
solution.isIsomorphic("badc","baba");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character,Character> sMap = new HashMap<>();
Map<Character,Character> tMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char sch = s.charAt(i);
char tch = t.charAt(i);
if((sMap.containsKey(sch)&&sMap.get(sch)!=tch)||(tMap.containsKey(tch)&&tMap.get(tch)!=sch)){
return false;
}
sMap.put(sch,tch);
tMap.put(tch,sch);
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<p>给定两个字符串 <em><strong>s </strong></em>和 <strong><em>t</em></strong>,判断它们是否是同构的。</p>
<p>如果 <em><strong>s </strong></em>中的字符可以按某种映射关系替换得到 <strong><em>t </em></strong>,那么这两个字符串是同构的。</p>
<p>每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"egg", </code><strong><em>t = </em></strong><code>"add"</code>
<strong>输出:</strong>true
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"foo", </code><strong><em>t = </em></strong><code>"bar"</code>
<strong>输出:</strong>false</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong><strong><em>s</em></strong> = <code>"paper", </code><strong><em>t = </em></strong><code>"title"</code>
<strong>输出:</strong>true</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>可以假设 <em><strong>s </strong></em><strong><em>t </em></strong>长度相同。</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li></div></div>\n<div><li>👍 366</li><li>👎 0</li></div>

View File

@ -0,0 +1,72 @@
//假设Andy和Doris想在晚餐时选择一家餐厅并且他们都有一个表示最喜爱餐厅的列表每个餐厅的名字用字符串表示
//
// 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅 如果答案不止一个则输出所有答案并且不考虑顺序 你可以假设总是存在一个答案
//
// 示例 1:
//
// 输入:
//["Shogun", "Tapioca Express", "Burger King", "KFC"]
//["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
//输出: ["Shogun"]
//解释: 他们唯一共同喜爱的餐厅是Shogun
//
//
// 示例 2:
//
// 输入:
//["Shogun", "Tapioca Express", "Burger King", "KFC"]
//["KFC", "Shogun", "Burger King"]
//输出: ["Shogun"]
//解释: 他们共同喜爱且具有最小索引和的餐厅是Shogun它有最小的索引和1(0+1)
//
//
// 提示:
//
//
// 两个列表的长度范围都在 [1, 1000]
// 两个列表中的字符串的长度将在[130]的范围内
// 下标从0开始到列表的长度减1
// 两个列表都没有重复的元素
//
// Related Topics 数组 哈希表 字符串
// 👍 112 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
//599:两个列表的最小索引总和
public class MinimumIndexSumOfTwoLists{
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumIndexSumOfTwoLists().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
HashMap< Integer, List < String >> map = new HashMap < > ();
for (int i = 0; i < list1.length; i++) {
for (int j = 0; j < list2.length; j++) {
if (list1[i].equals(list2[j])) {
if (!map.containsKey(i + j)) {
map.put(i + j, new ArrayList < String > ());
}
map.get(i + j).add(list1[i]);
}
}
}
int min_index_sum = Integer.MAX_VALUE;
for (int key: map.keySet()) {
min_index_sum = Math.min(min_index_sum, key);
}
String[] res = new String[map.get(min_index_sum).size()];
return map.get(min_index_sum).toArray(res);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,31 @@
<p>假设Andy和Doris想在晚餐时选择一家餐厅并且他们都有一个表示最喜爱餐厅的列表每个餐厅的名字用字符串表示。</p>
<p>你需要帮助他们用<strong>最少的索引和</strong>找出他们<strong>共同喜爱的餐厅</strong>。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>
[&quot;Shogun&quot;, &quot;Tapioca Express&quot;, &quot;Burger King&quot;, &quot;KFC&quot;]
[&quot;Piatti&quot;, &quot;The Grill at Torrey Pines&quot;, &quot;Hungry Hunter Steakhouse&quot;, &quot;Shogun&quot;]
<strong>输出:</strong> [&quot;Shogun&quot;]
<strong>解释:</strong> 他们唯一共同喜爱的餐厅是&ldquo;Shogun&rdquo;
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>
[&quot;Shogun&quot;, &quot;Tapioca Express&quot;, &quot;Burger King&quot;, &quot;KFC&quot;]
[&quot;KFC&quot;, &quot;Shogun&quot;, &quot;Burger King&quot;]
<strong>输出:</strong> [&quot;Shogun&quot;]
<strong>解释:</strong> 他们共同喜爱且具有最小索引和的餐厅是&ldquo;Shogun&rdquo;它有最小的索引和1(0+1)。
</pre>
<p><strong>提示:</strong></p>
<ol>
<li>两个列表的长度范围都在&nbsp;[1, 1000]内。</li>
<li>两个列表中的字符串的长度将在[130]的范围内。</li>
<li>下标从0开始到列表的长度减1。</li>
<li>两个列表都没有重复的元素。</li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>字符串</li></div></div>\n<div><li>👍 112</li><li>👎 0</li></div>

View File

@ -0,0 +1,59 @@
//给定一个非负整数 numRows生成杨辉三角的前 numRows
//
//
//
// 在杨辉三角中每个数是它左上方和右上方的数的和
//
// 示例:
//
// 输入: 5
//输出:
//[
// [1],
// [1,1],
// [1,2,1],
// [1,3,3,1],
// [1,4,6,4,1]
//]
// Related Topics 数组 动态规划
// 👍 525 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
//118:杨辉三角
public class PascalsTriangle{
public static void main(String[] args) {
//测试代码
Solution solution = new PascalsTriangle().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> list = new ArrayList<>();
if(i==0){
list.add(1);
result.add(list);
continue;
}
List<Integer> bef = result.get(i-1);
for (int j = 0; j <= i; j++) {
if(j==0||j==i){
list.add(1);
}else{
list.add(bef.get(j-1)+ bef.get(j));
}
}
result.add(list);
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,18 @@
<p>给定一个非负整数&nbsp;<em>numRows</em>生成杨辉三角的前&nbsp;<em>numRows&nbsp;</em>行。</p>
<p><img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif"></p>
<p><small>在杨辉三角中,每个数是它左上方和右上方的数的和。</small></p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> 5
<strong>输出:</strong>
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]</pre>
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 525</li><li>👎 0</li></div>

View File

@ -0,0 +1,78 @@
//在MATLAB中有一个非常有用的函数 reshape它可以将一个矩阵重塑为另一个大小不同的新矩阵但保留其原始数据
//
// 给出一个由二维数组表示的矩阵以及两个正整数r和c分别表示想要的重构的矩阵的行数和列数
//
// 重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充
//
// 如果具有给定参数的reshape操作是可行且合理的则输出新的重塑矩阵否则输出原始矩阵
//
// 示例 1:
//
//
//输入:
//nums =
//[[1,2],
// [3,4]]
//r = 1, c = 4
//输出:
//[[1,2,3,4]]
//解释:
//行遍历nums的结果是 [1,2,3,4]新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵
//
//
// 示例 2:
//
//
//输入:
//nums =
//[[1,2],
// [3,4]]
//r = 2, c = 4
//输出:
//[[1,2],
// [3,4]]
//解释:
//没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵 所以输出原矩阵
//
//
// 注意
//
//
// 给定矩阵的宽和高范围在 [1, 100]
// 给定的 r c 都是正数
//
// Related Topics 数组 矩阵 模拟
// 👍 216 👎 0
package leetcode.editor.cn;
//566:重塑矩阵
public class ReshapeTheMatrix {
public static void main(String[] args) {
//测试代码
Solution solution = new ReshapeTheMatrix().new Solution();
solution.matrixReshape(new int[][]{{1,2},{3,4}},1,4);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int x = mat.length;
int y = mat[0].length;
if (x * y != r * c) {
return mat;
}
int[][] arr = new int[r][c];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
int num = i * y + j;
arr[num / c][num % c] = mat[i][j];
}
}
return arr;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,44 @@
<p>在MATLAB中有一个非常有用的函数 <code>reshape</code>,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。</p>
<p>给出一个由二维数组表示的矩阵,以及两个正整数<code>r</code><code>c</code>,分别表示想要的重构的矩阵的行数和列数。</p>
<p>重构后的矩阵需要将原始矩阵的所有元素以相同的<strong>行遍历顺序</strong>填充。</p>
<p>如果具有给定参数的<code>reshape</code>操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>
nums =
[[1,2],
[3,4]]
r = 1, c = 4
<strong>输出:</strong>
[[1,2,3,4]]
<strong>解释:</strong>
行遍历nums的结果是 [1,2,3,4]。新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>
nums =
[[1,2],
[3,4]]
r = 2, c = 4
<strong>输出:</strong>
[[1,2],
[3,4]]
<strong>解释:</strong>
没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵。 所以输出原矩阵。
</pre>
<p><strong>注意:</strong></p>
<ol>
<li>给定矩阵的宽和高范围在 [1, 100]。</li>
<li>给定的 r 和 c 都是正数。</li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>矩阵</li><li>模拟</li></div></div>\n<div><li>👍 216</li><li>👎 0</li></div>

View File

@ -0,0 +1,46 @@
//编写一个函数其作用是将输入的字符串反转过来输入字符串以字符数组 char[] 的形式给出
//
// 不要给另外的数组分配额外的空间你必须原地修改输入数组使用 O(1) 的额外空间解决这一问题
//
// 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符
//
//
//
// 示例 1
//
// 输入["h","e","l","l","o"]
//输出["o","l","l","e","h"]
//
//
// 示例 2
//
// 输入["H","a","n","n","a","h"]
//输出["h","a","n","n","a","H"]
// Related Topics 递归 双指针 字符串
// 👍 422 👎 0
package leetcode.editor.cn;
//344:反转字符串
public class ReverseString {
public static void main(String[] args) {
//测试代码
Solution solution = new ReverseString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void reverseString(char[] s) {
int length = s.length;
int size = length % 2 == 0 ? length / 2 : length / 2 + 1;
for (int i = 0; i < size; i++) {
char temp = s[i];
s[i] = s[length - 1 - i];
s[length - 1 - i] = temp;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,19 @@
<p>编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 <code>char[]</code> 的形式给出。</p>
<p>不要给另外的数组分配额外的空间,你必须<strong><a href="https://baike.baidu.com/item/原地算法" target="_blank">原地</a>修改输入数组</strong>、使用 O(1) 的额外空间解决这一问题。</p>
<p>你可以假设数组中的所有字符都是 <a href="https://baike.baidu.com/item/ASCII" target="_blank">ASCII</a> 码表中的可打印字符。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>[&quot;h&quot;,&quot;e&quot;,&quot;l&quot;,&quot;l&quot;,&quot;o&quot;]
<strong>输出:</strong>[&quot;o&quot;,&quot;l&quot;,&quot;l&quot;,&quot;e&quot;,&quot;h&quot;]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>[&quot;H&quot;,&quot;a&quot;,&quot;n&quot;,&quot;n&quot;,&quot;a&quot;,&quot;h&quot;]
<strong>输出:</strong>[&quot;h&quot;,&quot;a&quot;,&quot;n&quot;,&quot;n&quot;,&quot;a&quot;,&quot;H&quot;]</pre>
<div><div>Related Topics</div><div><li>递归</li><li>双指针</li><li>字符串</li></div></div>\n<div><li>👍 422</li><li>👎 0</li></div>

View File

@ -0,0 +1,50 @@
//给定一个字符串你需要反转字符串中每个单词的字符顺序同时仍保留空格和单词的初始顺序
//
//
//
// 示例
//
// 输入"Let's take LeetCode contest"
//输出"s'teL ekat edoCteeL tsetnoc"
//
//
//
//
// 提示
//
//
// 在字符串中每个单词由单个空格分隔并且字符串中不会有任何额外的空格
//
// Related Topics 双指针 字符串
// 👍 300 👎 0
package leetcode.editor.cn;
//557:反转字符串中的单词 III
public class ReverseWordsInAStringIii {
public static void main(String[] args) {
//测试代码
Solution solution = new ReverseWordsInAStringIii().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseWords(String s) {
StringBuilder str = new StringBuilder();
StringBuilder ss = new StringBuilder();
for (char ch : s.toCharArray()) {
if (ch != ' ') {
str.append(ch);
} else {
ss.append(str.reverse()).append(" ");
str = new StringBuilder();
}
}
ss.append(str.reverse());
return ss.toString();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,18 @@
<p>给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&quot;Let&#39;s take LeetCode contest&quot;
<strong>输出:</strong>&quot;s&#39;teL ekat edoCteeL tsetnoc&quot;
</pre>
<p>&nbsp;</p>
<p><strong><strong><strong><strong>提示:</strong></strong></strong></strong></p>
<ul>
<li>在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。</li>
</ul>
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div>\n<div><li>👍 300</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long