LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters

对于给定的字符串, 找出其每个字符都不重复的子串中最长的那个, 并返回该子串的长度:

想法还是遍历:

 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         // 如果s是null或空串, 就直接返回0
 4         if (s == null || "".equals(s))
 5             return 0;
 6         // 定义结果max
 7         int max = 1;
 8         // 定义遍历中用到的变量,表示要找的子串开始和结束的index
 9         int start = 0, end = 1;
10         // 把s转为字符数组
11         char[] arr = s.toCharArray();
12         // 遍历数组,子串的开始从原数组的第一个字符到倒数第二个字符
13         for (start = 0; start < arr.length - 1; start++) {
14             // end从start的下一个开始,到最后一个结束
15             // 创建一个HashSet用于储存和检验子串
16             HashSet<Character> sub = new HashSet<>();
17             // 把start位置的字符添加进HashSet
18             sub.add(arr[start]);
19             // 从start后面一个字符开始检验
20             for (end = start + 1; end < arr.length; end++) {
21                 // 如果end成功添加,证明没有重复,继续遍历,反之终止循环,继续下一个start
22                 if (sub.add(arr[end])) {
23                 } else {
24                     break;
25                 }
26             }
27             //如果本处开始的子串比max大就更新max
28             max = max>sub.size()? max : sub.size();
29         }
30         return max;
31     }
32 }

一看就知道效率很低, 等于是把所有不重复的子串都找出来比了一下, 好在一次通过:

当然成绩也是惨不忍睹??

看一看21ms的答案

 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         // 显然结果为0
 4         int ans = 0;
 5         // 取出s的长度
 6         int len = s.length();
 7         // 建立长度为128的整数数组,对于ASCII码中的字符
 8         int[] indexArr = new int[128];
 9
10         // 这里循环中用的是一种滑动窗口的思路
11         // i在前面走, j在后面跟,如果i和j中间没有重复,窗口就拉大
12         // 一旦出现了重复, j就跟到i的位置的字符上一次出现的位置后面一位
13         for (int i = 0, j = 0; i < len; i++) {
14             // 通过数组检验i位置的字符上一次出现的位置是在j之前还是j之后
15             // 如果是之前就维持j的位置不变,如果是之后就把j的位置推至上一次出现的位置之后一位
16             j = Math.max(indexArr[s.charAt(i)], j);
17             // 更新ans
18             ans = Math.max(ans, i - j + 1);
19             // 在整数数组对应字符的位置存入索引i+1,如原串中第三个字符是a,遍历到这里就在indexArr[97]存入3
20             // 实际上最后数组中存储的就是到目前为止,每个字符最后一次出现的位置
21             indexArr[s.charAt(i)] = i + 1;
22         }
23         //返回ans
24         return ans;
25     }
26 }

分析都写在注释里面了, 看了看比较快的一部分答案, 基本都是这个思路, 建立一个数组存储每一个出现过的字符最后一次出现的位置, 用滑动窗口检验子串

LeetCode-Database #177 Nth Highest Salary

写个函数返回第N高的工资

事实证明我是真的不会写SQL函数, 当时学的时候就没怎么练??

连滚带爬的完成了:

 1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
 2 BEGIN
 3   DECLARE Nth INT DEFAULT N - 1;
 4   RETURN (
 5       # Write your MySQL query statement below.
 6       SELECT DISTINCT Employee.Salary
 7       FROM Employee
 8       ORDER BY Employee.Salary DESC
 9       LIMIT Nth,1
10   );
11 END

也是大家写出来都一样的东西,贴两个比较顺眼的

1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
2 BEGIN
3   SET N = N - 1;
4   RETURN (
5       SELECT DISTINCT Salary FROM Employee GROUP BY Salary
6       ORDER BY Salary DESC LIMIT 1 OFFSET N
7   );
8 END
 1 CREATECREATE  FUNCTIONFUNCTION getNthHighestSalary(N INT) RETURNS INT
 2  getNthHighestSalary(N I BEGIN
 3   DECLARE offer_num INT DEFAULT N-1;
 4   RETURN (
 5       # Write your MySQL query statement below.
 6       SELECT IFNULL(
 7           (
 8               SELECT DISTINCT salary
 9               FROM Employee
10               ORDER BY Salary DESC
11               LIMIT 1 OFFSET offer_num
12           ) , NULL
13       )AS "getNthHighestSalary"
14  );
15 END

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

时间: 2024-10-25 05:11:41

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary的相关文章

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

leetcode -- Algorithms -- 3_ Longest Substring Without Repeating Characters

from itertools import permutations class Solution(object): def lengthOfLongestSubstring(self, s): temp = 0 new_list = [] new_string = [] all_sub = [] l = '' # get the new string without duplicate for i in range(len(s)): if s[i] not in new_list: new_l

Java for LeetCode 003 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode No.3 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【LeetCode】3. Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of

LeetCode 03: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

leetcode笔记:Longest Substring Without Repeating Characters

一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the lon

[Leetcode 3, Medium] Longest Substring Without Repeating Characters

Problem: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the long