1、编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
解题思路:
#使用group by分组,查询最小的id,删除表中不包括这些id的项
delete from person where id not in (select t.id from (select min(id) id from person group by email)t)
2、给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
解题思路:
#使用栈结构先进先出的特点,push进一个左括号,根据题目得知下一个括号必定为右括号,所以我们可以push进当前相反的括号,判断下一个括号是否为刚push进入栈的符号即可
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c:s.toCharArray()){
if (c == '('){
stack.push(')');
}else if (c == '['){
stack.push(']');
}else if (c == '{'){
stack.push('}');
}else if (stack.isEmpty() || c != stack.pop()){
return false;
}
}
return stack.isEmpty();
}
}
3、给你一个字符串 s 和一个 长度相同 的整数数组 indices 。请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置。返回重新排列后的字符串。
# 示例1
输入:s = "codeleet", indices = [4,5,6,7,0,2,1,3]
输出:"leetcode"
解释:如图所示,"codeleet" 重新排列后变为 "leetcode" 。
# 示例2
输入:s = "aiohn", indices = [3,1,4,2,0]
输出:"nihao"
解题思路:
public static void restoreString(String s, int[] indices) {
char[] chars = s.toCharArray();
char[] s1 = new char[indices.length];
int j = 0;
for (int i:indices){
s1[i] = chars[j];
j++;
}
System.out.println(new String(s1));
}