2020 年 04 月 - 9 文章

LeedCode-SQL-连续出现的数字

  |   0 评论   |   0 浏览

题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字。 +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ 例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。 +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/consecutive-numbers 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解法 思路就是多表连接,根据id自增的关系进行判断 select distinct a.Num as ConsecutiveNums from Logs a left join Logs b on a.id+1=b.id left....

LeedCode-SQL-分数排名

  |   0 评论   |   0 浏览

题目描述 编写一个 SQL 查询来实现分数排名。 如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。 +----+-------+ | Id | Score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ 例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列): +-------+------+ | Score | Rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+ 解法 这个问题分为两部分来看 所有的分数按由大到小的顺序排序 针对排好的分数,给出排名 那第一部分,写出来很轻松 `select Score from Sco....

LeedCode-有效的括号

  |   0 评论   |   0 浏览

题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。 示例 1: 输入: "()" 输出: true 示例 2: 输入: "()[]{}" 输出: true 示例 3: 输入: "(]" 输出: false 示例 4: 输入: "([)]" 输出: false 示例 5: 输入: "{[]}" 输出: true 解法 1. 采用Java提供的工具类 利用Java提供的工具类String.contains() 判断是否存在 {}, [], (),如果存在,利用String.replace() 替换为空字符串 代码如下: public boolean isValid(String s) { if (s.length() <= 0) { return true; } while (s.contains("{}") || s.contains("[]") || s.contains("()")) { s = s.r....

Java 14 新特性

  |   0 评论   |   0 浏览

发布了名为Java 14的Java新版本,其中包括许多新功能,工具,安全性,调试和更新的文档方面的改进。 但是,Oracle还向您提供Java的较旧版本,因为它具有向后兼容性,因此您以前的代码仍可以在较旧的版本上运行,并且Java 14的语法与Java 8或9并不是完全不同的,它只是新版本 对前一个进行了一些改进 1. switch表达式优化 Switch Expression (JEP 361) 一直以来,Java都是使用类C++、C的switch表达式,在Java 12,13中对switch表达式做了一些优化,作为预览版本引入,直到Java14正式引入进来。 让我们来看一下,在Java 14之前如何写switch表达式。 tags.switch (day) { case 1: System.out.println("Let's meet!"); break; case 2: break; case 3: System.out.println("Let's meet!"); break; case 4: break; case 5: System.out.println("Let's....

LeedCode-longest common prefix

  |   0 评论   |   0 浏览

1.题目描述 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note: All given inputs are in lowercase letters a-z. 题目的意思就是求最长公共前缀。 2.解法 思路就是用第一个字符串(定义为 first)跟其他字符串去比较。是否存在最长公共前缀,开始先假定公共字符串是first,判断是否是其他字符串里的前缀,不匹配时,first减少一个字符,继续比较,当f....

LeedCode-Roman To Integer

  |   0 评论   |   0 浏览

题目描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV.....

LeedCode-Reverse Integer

  |   0 评论   |   0 浏览

1.题目描述 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 2. 解法 很基础的做法,通过除法,取余找到每位的数字进行反转 public int reverse(int x) { long n = 0; while(x != 0) { n = n * 10 + x % 10; x = x / 10; } return (int)n==n? (int)n:0; } 运行结果:

LeetCode-Palindrome Number

  |   0 评论   |   0 浏览
  1. 题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 2. 解题思路 2.1 先反转数字,再判断是否相等 负数是不可能回文的,直接返回false 把数字反转,跟原来的值比较是否相等 class So....

Git 初始化仓库

  |   0 评论   |   0 浏览

If you already have code ready to be pushed to this repository then run this in your terminal. cd existing-project git init git add --all git commit -m "Initial Commit" git remote add origin ssh://git@git.xxx.com/cyhy/xxxx.git git push origin master If your code is already tracked by Git then set this repository as your origin to push to. cd existing-project git remote set-url origin ssh://git@git.xxx.com/cyhy/xxxx.git git push origin master All done with the commands?