ARTS-第30周

ARTS (第30周)

早就是优势,不论是发掘市场,还是开拓视野。

Algorithm 算法

翻转字符串里的单词

Leetcode151

https://leetcode-cn.com/problems/reverse-words-in-a-string/

给定一个字符串,逐个翻转字符串中的每个单词。

示例 1:

输入: “the sky is blue”
输出: “blue is sky the”
示例 2:

输入: “ hello world! “
输出: “world! hello”
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
示例 3:

输入: “a good example”
输出: “example good a”
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

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

解法1

核心思路就是将所有的单词逆序保存起来后进行拼接

做完这个发现其实可以不需要LinkedList额外保存,直接进行拼接就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 执行用时:4 ms,在所有java提交中击败了88.05%的用户
// 内存消耗:37.7 MB,在所有java提交中击败了94.71%的用户
public String reverseWords(String s) {
if (s != null && s.length() == 0) {
return "";
}
int idx = s.length();
// 动态容器 保存每一个字符串
LinkedList<String> l = new LinkedList<String>();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
l.add(s.substring(i + 1, idx).trim());
idx = i;
} else if (i == 0) {
l.add(s.substring(0, idx).trim());
}
}

// 拼接
StringBuilder sb = new StringBuilder();
// 逆序迭代器
Iterator<String> iterator = l.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
//System.out.println("_>"+next+";");
if (next != null && next.length()>0) {

sb.append(next);
if(iterator.hasNext())
sb.append(' ');
}
}
return sb.toString().trim();
}

解法2

在解法1的基础上做了点优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//执行用时: 4 ms,在所有java提交中击败了88.05 % 的用户
//内存消耗: 37.5 MB,在所有java提交中击败了95.04 % 的用户
public String reverseWords_(String s) {
if (s != null && s.length() == 0) {
return "";
}
int idx = s.length();
// 拼接
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
String str = s.substring(i + 1, idx).trim();
if (str != null && str.length() > 0) {
sb.append(str);
sb.append(' ');
}
idx = i;
} else if (i == 0) {
String str = s.substring(0, idx).trim();
if (str != null && str.length() > 0) {
sb.append(str);
sb.append(' ');
}
}
}

return sb.toString().trim();
}

反转字符串

Leetcode 344

https://leetcode-cn.com/problems/reverse-string/)

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 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”]

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

解法

这种解法应该叫做双指针。然后用二进制计算进行交换值。

1
2
3
4
5
6
7
8
9
10
11
// 执行用时:1 ms,在所有java提交中击败了100.00%的用户
// 内存消耗:51.5 MB,在所有java提交中击败了80.57%的用户
public void reverseString(char[] s) {
int m = s.length - 1;
int l = s.length / 2;
for (int i = 0; i < l; i++) {
s[i] ^= s[m - i];
s[m - i] ^= s[i];
s[i] ^= s[m - i];
}
}

Review 英文文章

https://spring.io/guides/gs/scheduling-tasks/

如何使用定时任务

Tip 技巧

java泛型:

泛型在实现的时候是使用的类型擦除,即把所有的泛型类型转成Object类型。

因此java的泛型有的人也称之为假泛型。

并且某个类无法同时继承/实现多个相同的接口或者类(即使泛型不同)

但如果需要获取泛型参数 可以通过反射这样获取:

Class类

public TypeVariable>[] getTypeParameters();

Field类

public Type getGenericType()

Method类

public Type getGenericReturnType()
public Type[] getGenericParameterTypes()
public Type[] getGenericExceptionTypes()

Constructor类

public Type[] getGenericParameterTypes()

java各种容器类的实现

例如几个常见的:

ArrayList是动态扩容的数组

Linkedlist就是双端链表、双端队列的实现

HashMap就是哈希表和红黑树的结合的实现

HashSet的实现就是底层有一个hashmap,各类操作都通关hashmap进行。

TreeMap的实现是红黑树。

TreeSet的实现就是底层有一个TreeMap,各类操作都通过TreeMap进行。

PriorityQueue 优先级队列的结构就是堆。

等等.

Share 分享

https://blog.csdn.net/singwhatiwanna/article/details/102549659 出名要趁早!

https://blog.csdn.net/singwhatiwanna/article/details/101442840 一个人生必做的选择