-
Parent, Child and Tree
SELECT
DISTINCT t1.Id AS Id,
CASE
WHEN t1.P_id is NULL THEN 'Root'
WHEN t2.P_id is NULL THEN 'Leaf'
ELSE 'Inner'
END AS Type
FROM Tree t1 LEFT JOIN Tree t2
ON t1.Id = t2.P_id
ORDER BY t1.Id
-
Orders (未测试)
SELECT COUNT(orderNumber) as temp FROM ORDERS
GROUP BY customerNumber
ORDER BY temp DESC
LIMIT 1;
-
Investments in 2012
SELECT SUM(TIV_2016) as TIV_2016
FROM
(SELECT TIV_2016, TIV_2015
FROM Insurance
group by LAT, LON
having count(*) = 1) as t
WHERE TIV_2015
IN (SELECT TIV_2015 FROM Insurance GROUP BY TIV_2015 HAVING COUNT(*)>1)
-
tweet recommendation
public class Solution {
// a list of tweet ids in sorted order
public static String[] getRecommendedTweets(int n, String[] values) {
String[] res = new String[values.length];
for (int i = 0; i < n; i++) {
Stack<Character> stack = new Stack<>();
String string = values[i];
if (string.length() % 2 != 0) {
res[i] = "NO";
continue;
}
for (int j = 0; j < string.length(); j++) {
if (string.charAt(j) == '(') stack.push(')');
else if (string.charAt(j) == '{') stack.push('}');
else if (string.charAt(j) == '[') stack.push(']');
else if (stack.isEmpty() || stack.pop() != string.charAt(j)) {
res[i] = "NO";
break;
}
}
if (res[i] != null) continue;
if (stack.isEmpty()) res[i] = "YES";
else res[i] = "NO";
}
return res;
}
public static void main(String[] args) {
String[] ss = new String[3];
String s1 = "{}{[]}";
String s2 = "{{";
String s3 = "{}[";
ss[0] = s1;
ss[1] = s2;
ss[2] = s3;
String[] res = getRecommendedTweets(3, ss);
for (String s : res) {
System.out.println(s);
}
}
}
public class Solution {
// a list of tweet ids in sorted order
public static String electionWinner(int n, String[] names) {
TreeMap<String, Integer> map = new TreeMap<>();
for (String s : names) {
map.put(s, map.getOrDefault(s, 0) + 1);
}
int max = 0;
String res = "";
for (String s : map.keySet()) {
if (map.get(s) > max) max = map.get(s);
res = s;
}
return res;
}
public static void main(String[] args) {
String s1 = "Mary";
String s2 = "Joy";
String[] ss = new String[4];
ss[0] = s1;
ss[1] = s1;
ss[2] = s2;
ss[3] = s2;
System.out.println(electionWinner(4, ss));
}
}
- find the winner
public class Solution {
// a list of tweet ids in sorted order
public static String winner(int[] andrea, int[] maria, String s) {
if (andrea == null || maria == null || andrea.length == 0 || maria.length == 0)
return "Tie";
int len = andrea.length;
int an = 0, ma = 0, i;
if (s.equals("EVEN")) i = 0;
else i = 1;
for (; i < len; i += 2) {
int num = andrea[i] - maria[i];
an += num;
ma += -num;
}
return an > ma ? "Andrea" : (an < ma ? "Maria" : "Tie");
}
public static void main(String[] args) {
int[] andrea = new int[] {1, 2, 3};
int[] maria = new int[] {2, 1, 3};
System.out.println(winner(andrea, maria, "ODD"));
}
}
- find the substring
public class Solution {
public static int winner(String s, String x) {
char[] ss = s.toCharArray();
char[] xx = x.toCharArray();
int xlen = xx.length;
int dif = ss.length - xlen;
for (int i = 0; i <= dif; i++) {
if (ss[i] == xx[0]) {
for (int j = 0; j < xlen; j++) {
if (xx[j] != '*' && ss[i + j] != xx[j]) break;
else if (j == xlen - 1) {
return i;
}
}
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(winner("sdfsfddacjd", "c*d"));
}
}
网友评论