LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails

LeetCode-Algorithms #007 Reverse Integer

给定一个32位整数, 将其各位反转并返回, 如果结果超出取值范围就返回0

 1 class Solution {
 2     public int reverse(int x) {
 3         //对原数取绝对值
 4         int y = Math.abs(x);
 5         //将原数转换为字符串
 6         String s1 = Integer.toString(y);
 7         //将字符串转换为字符数组
 8         char[] arr = s1.toCharArray();
 9         //反转数组
10         for (int i = 0; i < arr.length / 2; i++) {
11             char temp = arr[i];
12             arr[i] = arr[arr.length - 1 - i];
13             arr[arr.length - 1 - i] = temp;
14         }
15         //将反转后的字符数组拼接为字符串
16         StringBuilder sb = new StringBuilder();
17         for (char c : arr) {
18             sb.append(c);
19         }
20         String s2 = sb.toString();
21         //将新获得的字符串转换为整数
22         int res;
23         try {
24             res = Integer.parseInt(s2);
25         } catch (NumberFormatException e) {
26             return 0; //如果值超出了要求的范围,就返回0
27         }
28         //依据原数的正负值返回结果
29         return x < 0 ? res * -1 : res;
30     }
31 }

我这里的方法显然是取巧了, 先转换成字符串, 再反转字符串, 最后再转换回整数

结果还可以, 但是这种野路子方法不值得提倡, 重新考虑一下正经路数:

 1 public int reverse(int x) {
 2     //创建一个整数变量存储结果
 3     int ans = 0;
 4     //当x不等于0时循环
 5     while(x != 0) {
 6         //获取x的最末尾
 7         int i = x % 10;
 8         //去掉最末尾
 9         x /= 10;
10         //把获取到的最末位添加到结果中
11         ans = ans * 10 + i;
12     }
13     return ans;
14 }

核心是这样的, 但是光这样写没有办法避免超出取值范围时的问题, 因此再在循环中加一个选择条件

 1 class Solution {
 2     public int reverse(int x) {
 3         //创建一个整数变量存储结果
 4         int ans = 0;
 5         //当x不等于0时循环
 6         while(x != 0) {
 7             //获取x的最末尾
 8             int i = x % 10;
 9             //去掉最末尾
10             x /= 10;
11             //如果ans已经大于取值范围的十分之一, 直接使ans再增加一位会超出取值范围, 直接返回0
12             if(ans > Integer.MAX_VALUE / 10 || ans < Integer.MIN_VALUE / 10)return 0;
13             //否则就把获取到的最末位添加到结果中
14             ans = ans * 10 + i;
15         }
16         return ans;
17     }
18 }

这种写法其实和第一种速度相差不大, 但是比较接近题目本身的意思. 原本以为可能会有个别临界值会绕过选择条件, 后来琢磨了一下应该是不存在的, 当然, 为了健壮性也可以写成官方答案这样:

 1 class Solution {
 2     public int reverse(int x) {
 3         int rev = 0;
 4         while (x != 0) {
 5             int pop = x % 10;
 6             x /= 10;
 7             if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
 8             if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
 9             rev = rev * 10 + pop;
10         }
11         return rev;
12     }
13 }

LeetCode-Database #182 Duplicate Emails

从给出的Person表中找出重复的Email

前几道数据库的题目基本都在用自联结解决, 习惯了之后总是第一时间想着用自联结:

SELECT DISTINCT p1.Email
FROM Person p1, Person p2
WHERE p1.Email = p2.Email
AND p1.Id <> p2.Id
;

但是这样实在是太慢了, 换一个思路, 用COUNT来做, 就快很多:

SELECT Email
FROM (
SELECT Email, COUNT(Email) AS num
FROM PERSON
GROUP BY Email
) AS res
WHERE num > 1
;

再看看别人写的:

SELECT Email
FROM Person
GROUP BY Email
HAVING COUNT(Email) > 1
;

为什么我感觉自己都没见过HAVING这个关键字??

原文地址:https://www.cnblogs.com/chang4/p/9755289.html

时间: 2024-10-08 13:58:47

LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails的相关文章

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

leetcode 182. Duplicate Emails

传送门 182. Duplicate Emails My Submissions Question Total Accepted: 14498 Total Submissions: 38364 Difficulty: Easy Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email pr

【LeetCode】007 Reverse Interger

题目:LeetCode 007 Reverse Interger 题意:将一个整数的数字反转.保留正负符号. 思路:先将整数变成字符串,然后判断是否为负数,或是否含有’+’,然后从字符串末尾开始累计得到新整数即可. 但是还会有特殊情况,即正向为Int范围内,但反转之后会溢出,因此要进行特判. 代码如下: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int len, flag = 1, i = 0; 5 long long ans =

Leetcode练习题 7. Reverse Integer

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 o

[LeetCode] [SQL]: 182: Duplicate Emails

题目: Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+---------+ For example, your query should

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

Java for LeetCode 007 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决. 题目不难: JAVA实现如下: public class Solution { static public int reverse(int x) { if(x=

LeetCode 007 Reverse Integer - Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的

LeetCode记录之——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 反转数字的整数. 示例1:x = 123,返回321示例2:x = -