Skip to content

Commit a24b804

Browse files
author
wave-gbt
committed
add post
1 parent 0582296 commit a24b804

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

Database/177. Nth Highest Salary.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Write a SQL query to get the nth highest salary from the Employee table.
2+
3+
```
4+
+----+--------+
5+
| Id | Salary |
6+
+----+--------+
7+
| 1 | 100 |
8+
| 2 | 200 |
9+
| 3 | 300 |
10+
+----+--------+
11+
```
12+
For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
13+
14+
```
15+
+------------------------+
16+
| getNthHighestSalary(2) |
17+
+------------------------+
18+
| 200 |
19+
+------------------------+
20+
```
21+
**SQL**
22+
```mysql
23+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
24+
BEGIN
25+
set N=N-1;
26+
RETURN (
27+
# Write your MySQL query statement below.
28+
select distinct Salary from Employee order by Salary desc limit N,1
29+
);
30+
END
31+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Write a function to find the longest common prefix string amongst an array of strings.
2+
3+
**code**
4+
5+
```java
6+
class Solution {
7+
public String longestCommonPrefix(String[] strs) {
8+
9+
if(strs.length==0)
10+
return "";
11+
String prefix =strs[0];
12+
for(int i = 1;i<strs.length;i++){
13+
while(strs[i].indexOf(prefix) != 0){
14+
prefix = prefix.substring(0,prefix.length()-1);
15+
16+
}
17+
}
18+
return prefix;
19+
}
20+
}
21+
```

algorithm/20. Valid Parentheses.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
2+
3+
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
4+
5+
**code**
6+
```java
7+
class Solution {
8+
public boolean isValid(String s) {
9+
Stack<Integer> p = new Stack<>();
10+
for(int i = 0; i < s.length(); i++) {
11+
int q = "(){}[]".indexOf(s.substring(i, i + 1));
12+
if(q % 2 == 1) {
13+
if(p.isEmpty() || p.pop() != q - 1) return false;
14+
} else p.push(q);
15+
}
16+
return p.isEmpty();
17+
}
18+
}
19+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
2+
3+
**Example:**
4+
5+
```
6+
Input: 1->2->4, 1->3->4
7+
Output: 1->1->2->3->4->4
8+
```
9+
```
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
20+
if(l1 == null) {
21+
return l2;
22+
}
23+
if(l2 == null){
24+
return l1;
25+
}
26+
if(l1.val < l2.val){
27+
l1.next = mergeTwoLists(l1.next, l2);
28+
return l1;
29+
} else{
30+
l2.next = mergeTwoLists(l1, l2.next);
31+
return l2;
32+
}
33+
34+
}
35+
}
36+
```

algorithm/27. Remove Element.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Given an array and a value, remove all instances of that value in-place and return the new length.
2+
3+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
4+
5+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
6+
7+
**Example:**
8+
```
9+
Given nums = [3,2,2,3], val = 3,
10+
11+
Your function should return length = 2, with the first two elements of nums being 2.
12+
13+
```
14+
15+
**code**
16+
```java
17+
class Solution {
18+
public int removeElement(int[] nums, int val) {
19+
int i = 0;
20+
int n = nums.length;
21+
while (i < n) {
22+
if (nums[i] == val) {
23+
nums[i] = nums[n - 1];
24+
// reduce array size by one
25+
n--;
26+
} else {
27+
i++;
28+
}
29+
}
30+
return n;
31+
}
32+
}
33+
34+
```

leetcode-practice.iml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$" />
6+
<orderEntry type="inheritedJdk" />
7+
<orderEntry type="sourceFolder" forTests="false" />
8+
</component>
9+
</module>

0 commit comments

Comments
 (0)