Compare commits

..

2 Commits

Author SHA1 Message Date
d7f450756b 684:冗余连接 2022-12-01 17:07:21 +08:00
175d1fa921 151:反转字符串中的单词 2022-11-30 17:36:01 +08:00
4 changed files with 288 additions and 0 deletions

View File

@ -0,0 +1,112 @@
//<p>树可以看成是一个连通且 <strong>无环&nbsp;</strong>&nbsp;<strong>无向&nbsp;</strong></p>
//
//<p>给定往一棵&nbsp;<code>n</code> 个节点 (节点值&nbsp;<code>1n</code>) 的树中添加一条边后的图添加的边的两个顶点包含在 <code>1</code> <code>n</code>&nbsp;中间且这条附加的边不属于树中已存在的边图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code>&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示图中在 <code>ai</code> <code>bi</code> 之间存在一条边</p>
//
//<p>请找出一条可以删去的边删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树如果有多个答案则返回数组&nbsp;<code>edges</code>&nbsp;中最后出现的边</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1</strong></p>
//
//<p><img alt="" src="https://pic.leetcode-cn.com/1626676174-hOEVUL-image.png" style="width: 152px; " /></p>
//
//<pre>
//<strong>输入:</strong> edges = [[1,2], [1,3], [2,3]]
//<strong>输出:</strong> [2,3]
//</pre>
//
//<p><strong>示例 2</strong></p>
//
//<p><img alt="" src="https://pic.leetcode-cn.com/1626676179-kGxcmu-image.png" style="width: 250px; " /></p>
//
//<pre>
//<strong>输入:</strong> edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
//<strong>输出:</strong> [1,4]
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示:</strong></p>
//
//<ul>
// <li><code>n == edges.length</code></li>
// <li><code>3 &lt;= n &lt;= 1000</code></li>
// <li><code>edges[i].length == 2</code></li>
// <li><code>1 &lt;= ai&nbsp;&lt; bi&nbsp;&lt;= edges.length</code></li>
// <li><code>ai != bi</code></li>
// <li><code>edges</code> 中无重复元素</li>
// <li>给定的图是连通的&nbsp;</li>
//</ul>
//
//<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li></li></div></div><br><div><li>👍 523</li><li>👎 0</li></div>
package leetcode.editor.cn;
import java.util.*;
// 684:冗余连接
public class RedundantConnection {
public static void main(String[] args) {
Solution solution = new RedundantConnection().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] findRedundantConnection(int[][] edges) {
List<List<Integer>> list = new ArrayList<>();
for (int[] edge : edges) {
if (list.size() == 0) {
list.add(new ArrayList<>(Arrays.asList(edge[0], edge[1])));
} else {
int index0 = -1;
int index1 = -1;
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i).contains(edge[0])) {
index0 = i;
if (index1 > -1) {
break;
}
}
if (list.get(i).contains(edge[1])) {
index1 = i;
if (index0 > -1) {
break;
}
}
}
if (index0 > -1 && index0 == index1) {
return edge;
}
Set<Integer> tmp = new HashSet<>();
if (index0 > -1) {
tmp.addAll(list.get(index0));
} else {
tmp.add(edge[0]);
}
if (index1 > -1) {
tmp.addAll(list.get(index1));
} else {
tmp.add(edge[1]);
}
if (index0 > -1 && index1 > -1) {
if (index0 > index1) {
list.remove(index0);
list.remove(index1);
} else {
list.remove(index1);
list.remove(index0);
}
} else if (index0 > -1) {
list.remove(index0);
} else if (index1 > -1) {
list.remove(index1);
}
list.add(new ArrayList<>(tmp));
}
}
return null;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,84 @@
//<p>给你一个字符串 <code>s</code> 请你反转字符串中 <strong>单词</strong> 的顺序</p>
//
//<p><strong>单词</strong> 是由非空格字符组成的字符串<code>s</code> 中使用至少一个空格将字符串中的 <strong>单词</strong> 分隔开</p>
//
//<p>返回 <strong>单词</strong> 顺序颠倒且 <strong>单词</strong> 之间用单个空格连接的结果字符串</p>
//
//<p><strong>注意</strong>输入字符串 <code>s</code>中可能会存在前导空格尾随空格或者单词间的多个空格返回的结果字符串中单词间应当仅用单个空格分隔且不包含任何额外的空格</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1</strong></p>
//
//<pre>
//<strong>输入</strong>s = "<span><code>the sky is blue</code></span>"
//<strong>输出</strong>"<span><code>blue is sky the</code></span>"
//</pre>
//
//<p><strong>示例 2</strong></p>
//
//<pre>
//<strong>输入</strong>s = " &nbsp;hello world &nbsp;"
//<strong>输出</strong>"world hello"
//<strong>解释</strong>反转后的字符串中不能存在前导空格和尾随空格
//</pre>
//
//<p><strong>示例 3</strong></p>
//
//<pre>
//<strong>输入</strong>s = "a good &nbsp; example"
//<strong>输出</strong>"example good a"
//<strong>解释</strong>如果两个单词间有多余的空格反转后的字符串需要将单词间的空格减少到仅有一个
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示</strong></p>
//
//<ul>
// <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
// <li><code>s</code> 包含英文大小写字母数字和空格 <code>' '</code></li>
// <li><code>s</code> <strong>至少存在一个</strong> 单词</li>
//</ul>
//
//<ul>
//</ul>
//
//<p>&nbsp;</p>
//
//<p><strong>进阶</strong>如果字符串在你使用的编程语言中是一种可变数据类型请尝试使用&nbsp;<code>O(1)</code> 额外空间复杂度的 <strong>原地</strong> 解法</p>
//
//<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div><br><div><li>👍 703</li><li>👎 0</li></div>
package leetcode.editor.cn;
// 151:反转字符串中的单词
public class ReverseWordsInAString {
public static void main(String[] args) {
Solution solution = new ReverseWordsInAString().new Solution();
// TO TEST
System.out.println(solution.reverseWords(" hello world "));
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseWords(String s) {
while (s.contains(" ")) {
s = s.replace(" ", " ");
}
if (s.charAt(0) == ' ') {
s = s.substring(1);
}
if (s.charAt(s.length() - 1) == ' ') {
s = s.substring(0, s.length() - 1);
}
String[] strs = s.split(" ");
s = "";
for (int i = strs.length - 1; i >= 0; i--) {
s += strs[i] + " ";
}
return s.substring(0, s.length() - 1);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,41 @@
<p>树可以看成是一个连通且 <strong>无环&nbsp;</strong>&nbsp;<strong>无向&nbsp;</strong>图。</p>
<p>给定往一棵&nbsp;<code>n</code> 个节点 (节点值&nbsp;<code>1n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code><code>n</code>&nbsp;中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code>&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示图中在 <code>ai</code><code>bi</code> 之间存在一条边。</p>
<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组&nbsp;<code>edges</code>&nbsp;中最后出现的边。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://pic.leetcode-cn.com/1626676174-hOEVUL-image.png" style="width: 152px; " /></p>
<pre>
<strong>输入:</strong> edges = [[1,2], [1,3], [2,3]]
<strong>输出:</strong> [2,3]
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://pic.leetcode-cn.com/1626676179-kGxcmu-image.png" style="width: 250px; " /></p>
<pre>
<strong>输入:</strong> edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
<strong>输出:</strong> [1,4]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == edges.length</code></li>
<li><code>3 &lt;= n &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 &lt;= ai&nbsp;&lt; bi&nbsp;&lt;= edges.length</code></li>
<li><code>ai != bi</code></li>
<li><code>edges</code> 中无重复元素</li>
<li>给定的图是连通的&nbsp;</li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li></li></div></div><br><div><li>👍 523</li><li>👎 0</li></div>

View File

@ -0,0 +1,51 @@
<p>给你一个字符串 <code>s</code> ,请你反转字符串中 <strong>单词</strong> 的顺序。</p>
<p><strong>单词</strong> 是由非空格字符组成的字符串。<code>s</code> 中使用至少一个空格将字符串中的 <strong>单词</strong> 分隔开。</p>
<p>返回 <strong>单词</strong> 顺序颠倒且 <strong>单词</strong> 之间用单个空格连接的结果字符串。</p>
<p><strong>注意:</strong>输入字符串 <code>s</code>中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "<span><code>the sky is blue</code></span>"
<strong>输出:</strong>"<span><code>blue is sky the</code></span>"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = " &nbsp;hello world &nbsp;"
<strong>输出:</strong>"world hello"
<strong>解释:</strong>反转后的字符串中不能存在前导空格和尾随空格。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "a good &nbsp; example"
<strong>输出:</strong>"example good a"
<strong>解释:</strong>如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
<li><code>s</code> 包含英文大小写字母、数字和空格 <code>' '</code></li>
<li><code>s</code><strong>至少存在一个</strong> 单词</li>
</ul>
<ul>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong>如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用&nbsp;<code>O(1)</code> 额外空间复杂度的 <strong>原地</strong> 解法。</p>
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div><br><div><li>👍 703</li><li>👎 0</li></div>