leetcode算法笔记1

1. leetcode 1 Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

利用hashmap,可降低时间复杂度为 O(n)

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}

一次循环,将数据存入 求target的补是否在Hashmap中.

2. leetcode 7 Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321
Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
溢出是关键考虑的问题.

int reverse(int x) {
        int ans = 0;
        while (x != 0) {
            int temp = ans * 10 + x % 10;
            if (temp / 10 != ans)  //如果不等的话说明temp已经溢出了
                return 0;
            ans = temp;
            x /= 10;
        }
        return ans;
        }
3. leetcode 176 Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

记得去重!!!

select (select Salary from Employee group by Salary order by Salary desc limit 1,1)as
SecondHighestSalary
4. leetcode 193 Valid Phone Numbers

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Example:

Assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

一行命令即可,注意用-P

grep -P '^(\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4})$' file.txt
5. leetcode 859 Buddy Strings

Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.

Example 1:

Input: A = "ab", B = "ba"
Output: true
Example 2:

Input: A = "ab", B = "ab"
Output: false
Example 3:

Input: A = "aa", B = "aa"
Output: true
Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:

Input: A = "", B = "aa"
Output: false

Note:

0 <= A.length <= 20000
0 <= B.length <= 20000
A and B consist only of lowercase letters.

最显然是长度不相等时不用比较。
然后,
1)字符串相等的话。只要有大于等于两个字母相同就可返回true。
2)字符串不相等的话。只能有两个字母不同。

 public boolean buddyStrings(String A, String B) {
        int count = 0,flag=0;
        Map<Integer, Integer> map = new HashMap<>();
        if (A.length() != B.length()){
            return false;
        } 
        for (int i = 0; i < A.length(); i++){
             if (A.charAt(i) != B.charAt(i)) {
                 count++;
                 if (count > 2) {
                     return false;
                 }
                 if (map.get(B.charAt(i) - 'a') != null){//存储不相同位置字符的ascii值,key-value为A[i]->B[i]形式,下一次不同时只要查找B[j] = A[i](key), A[j] = B[i](value)即可
                      if ((A.charAt(i) - 'a') == map.get(B.charAt(i) - 'a')){
                               flag = 1;
                 }
                 }
                 map.put(A.charAt(i)-'a', B.charAt(i)-'a');  
             } 

        } 
        if (count == 2 && flag == 1) {
            return true;
        }
        if(count == 0 && A.length()!= 0 && B.length()!= 0) {
            for(int j = 0; j < A.length()-1; j++){
                if(A.indexOf(A.charAt(j), j+1) != -1){
                    return true;
                }
            }
        }
        return false;
       
    }
196. Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+

Id is the primary key column for this table.
For example, after running your query, the above Person table should have the following rows:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

主要需注意的点是,在同一语句中,不能先select出同一表中的某些值,再update这个表。所以要加一个中间表

delete from 
Person
where id not in (select t.Id from (select min(Id) as Id from Person group by Email) t );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值