Compare commits
2 Commits
3535bcfec3
...
d7f450756b
Author | SHA1 | Date | |
---|---|---|---|
d7f450756b | |||
175d1fa921 |
112
src/main/java/leetcode/editor/cn/RedundantConnection.java
Normal file
112
src/main/java/leetcode/editor/cn/RedundantConnection.java
Normal file
@ -0,0 +1,112 @@
|
||||
//<p>树可以看成是一个连通且 <strong>无环 </strong>的 <strong>无向 </strong>图。</p>
|
||||
//
|
||||
//<p>给定往一棵 <code>n</code> 个节点 (节点值 <code>1~n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code> 到 <code>n</code> 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code> ,<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示图中在 <code>ai</code> 和 <code>bi</code> 之间存在一条边。</p>
|
||||
//
|
||||
//<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组 <code>edges</code> 中最后出现的边。</p>
|
||||
//
|
||||
//<p> </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> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>n == edges.length</code></li>
|
||||
// <li><code>3 <= n <= 1000</code></li>
|
||||
// <li><code>edges[i].length == 2</code></li>
|
||||
// <li><code>1 <= ai < bi <= edges.length</code></li>
|
||||
// <li><code>ai != bi</code></li>
|
||||
// <li><code>edges</code> 中无重复元素</li>
|
||||
// <li>给定的图是连通的 </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)
|
||||
|
||||
}
|
84
src/main/java/leetcode/editor/cn/ReverseWordsInAString.java
Normal file
84
src/main/java/leetcode/editor/cn/ReverseWordsInAString.java
Normal 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> </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 = " hello world "
|
||||
//<strong>输出:</strong>"world hello"
|
||||
//<strong>解释:</strong>反转后的字符串中不能存在前导空格和尾随空格。
|
||||
//</pre>
|
||||
//
|
||||
//<p><strong>示例 3:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong>s = "a good example"
|
||||
//<strong>输出:</strong>"example good a"
|
||||
//<strong>解释:</strong>如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
|
||||
//</pre>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
// <li><code>s</code> 包含英文大小写字母、数字和空格 <code>' '</code></li>
|
||||
// <li><code>s</code> 中 <strong>至少存在一个</strong> 单词</li>
|
||||
//</ul>
|
||||
//
|
||||
//<ul>
|
||||
//</ul>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>进阶:</strong>如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 <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)
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<p>树可以看成是一个连通且 <strong>无环 </strong>的 <strong>无向 </strong>图。</p>
|
||||
|
||||
<p>给定往一棵 <code>n</code> 个节点 (节点值 <code>1~n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code> 到 <code>n</code> 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code> ,<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示图中在 <code>ai</code> 和 <code>bi</code> 之间存在一条边。</p>
|
||||
|
||||
<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组 <code>edges</code> 中最后出现的边。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= ai < bi <= edges.length</code></li>
|
||||
<li><code>ai != bi</code></li>
|
||||
<li><code>edges</code> 中无重复元素</li>
|
||||
<li>给定的图是连通的 </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>
|
@ -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> </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 = " hello world "
|
||||
<strong>输出:</strong>"world hello"
|
||||
<strong>解释:</strong>反转后的字符串中不能存在前导空格和尾随空格。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "a good example"
|
||||
<strong>输出:</strong>"example good a"
|
||||
<strong>解释:</strong>如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 包含英文大小写字母、数字和空格 <code>' '</code></li>
|
||||
<li><code>s</code> 中 <strong>至少存在一个</strong> 单词</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 <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>
|
Loading…
Reference in New Issue
Block a user