L500 键盘行
GitHub 账户:LuvnJoae 欢迎关注! https://github.com/LuvnJoae
GitHub 代码链接:https://github.com/LuvnJoae/Java_leetcode
思路与结果
代码思路1
package Day17_7_9.L500;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 思路1
1. 通过HashMap 键值形式存储,以行为值,字母为键
2. 对每个单词的字母进行遍历,如果每个字母的值不一样,就跳过,下一个,一样的话就添加到返回的字符串数组内
反思
1. 要有非空判断,即边界判定。
2. 其实也可以不用HashMap,因为就三行,所以直接用下标来判断也是可以的。
*/
public class Solution {
public String[] findWords(String[] words) {
if (words == null){
return null;
}
String[] str = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
HashMap<Character, Integer> map= new HashMap<>();
for (int i = 1; i < 4; i++) {
for (int j = 0; j < str[i-1].length(); j++) {
map.put(str[i-1].toCharArray()[j], i);
}
}
int temp;
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
String word = words[i].toLowerCase();
temp = map.get(word.toCharArray()[0]);
for (int i1 = 0; i1 < word.length(); i1++) {
if (map.get(word.toCharArray()[i1]) == temp)
if (i1 == word.length()-1){
list.add(words[i]);
}else
continue;
else
break;
}
}
return list.toArray(new String[list.size()]);
}
public static void main(String[] args) {
String[] str = {"Hello","Alaska","Dad","Peace"};
System.out.println(new Solution().findWords(str));
}
}
L344 反转字符串
思路与结果
代码思路1
package Day17_7_9.L344;
/**
* 思路1
1. 不能用额外的数组空间,只能用O(1)的额外空间,那就直接首尾互换呗,用一个temp字符当中转。
反思
1. 速度太慢了,但是为什么啊?
思路2
1. 看了别人的题解,原来 不需要输出,系统会自动输出,速度慢就慢在了这个输出上····
2. 去掉输出后,时间很快了
反思
其实还有一种异或的神仙解法,但没必要,效率差不多。
*/
public class Solution {
public void reverseString(char[] s) {
char temp;
for (int i = 0; i < (s.length / 2); i++) {
temp = s[i];
s[i] = s[s.length - i -1];
s[s.length - i - 1] = temp;
}
System.out.println(s);
}
public static void main(String[] args) {
char[] s = {'a','b','c','d','e'};
new Solution().reverseString(s);
}
}