From a4a647e427739a5452c02bb1965c5d296c649e8e Mon Sep 17 00:00:00 2001 From: yjiewei <59330974+yjiewei@users.noreply.github.com> Date: Mon, 20 Jan 2020 22:34:24 +0800 Subject: [PATCH] Add files via upload --- ...3\345\222\214-\347\256\200\345\215\225.md" | 85 +++++++++++++ ...4\345\217\267-\347\256\200\345\215\225.md" | 112 ++++++++++++++++++ ...1\345\272\246-\347\256\200\345\215\225.md" | 26 ++++ 3 files changed, 223 insertions(+) create mode 100644 "yjiewei/10-\344\270\244\346\225\260\344\271\213\345\222\214-\347\256\200\345\215\225.md" create mode 100644 "yjiewei/11-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-\347\256\200\345\215\225.md" create mode 100644 "yjiewei/9-\344\270\212\345\215\207\347\232\204\346\270\251\345\272\246-\347\256\200\345\215\225.md" diff --git "a/yjiewei/10-\344\270\244\346\225\260\344\271\213\345\222\214-\347\256\200\345\215\225.md" "b/yjiewei/10-\344\270\244\346\225\260\344\271\213\345\222\214-\347\256\200\345\215\225.md" new file mode 100644 index 0000000..50330f2 --- /dev/null +++ "b/yjiewei/10-\344\270\244\346\225\260\344\271\213\345\222\214-\347\256\200\345\215\225.md" @@ -0,0 +1,85 @@ +## 1. 两数之和 + +### 题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 + +### 示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] + + 方法一:两遍哈希 + class Solution { + public int[] twoSum(int[] nums, int target) { + Map map = new HashMap<>(); + for (int i = 0;i map = new HashMap<>(); + for (int i = 0;i List[int]: + hashmap = {} + for index,value in enumerate(nums): + hashmap[value] = index + for i,v in enumerate(nums): + if hashmap.get(target - v)!=None and hashmap.get(target - v)!=i: + return [i,hashmap.get(target - v)] + +### 复杂度和方法一一样。 + + diff --git "a/yjiewei/11-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-\347\256\200\345\215\225.md" "b/yjiewei/11-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-\347\256\200\345\215\225.md" new file mode 100644 index 0000000..705da69 --- /dev/null +++ "b/yjiewei/11-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-\347\256\200\345\215\225.md" @@ -0,0 +1,112 @@ +## 20. 有效的括号 +### 题目:给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 + +### 有效字符串需满足: +* 左括号必须用相同类型的右括号闭合。 +* 左括号必须以正确的顺序闭合。注意空字符串可被认为是有效字符串。 + + + 示例 1: + + 输入: "()" + 输出: true + 示例 2: + + 输入: "()[]{}" + 输出: true + 示例 3: + + 输入: "(]" + 输出: false + 示例 4: + + 输入: "([)]" + 输出: false + 示例 5: + + 输入: "{[]}" + 输出: true + +--- + + 方法一:Java栈数据结构 + class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for(char c : s.toCharArray()){ + if(c == '('){ + stack.push(')'); + continue; + } + if(c == '{'){ + stack.push('}'); + continue; + } + if(c == '['){ + stack.push(']'); + continue; + } + if(stack.empty() || stack.pop()!=c){ + return false; + } + } + return stack.empty(); + } + } + + +### 解题方法来自力扣用户:时光 +### 解题思路:1.每个右括号必须与最近出现的左括号匹配 2.遍历是从左往右的,最先出现的左括号肯定是与最后遍历得到的相配对,用栈数据结构来处理最合适不过 3.将遍历到的左括号对应的右括号放入栈中可以避免配对判断,只需要判断是否相同即可。 + + 方法二:愚蠢暴力法 + class Solution { + public boolean isValid(String s) { + if(s.equals("")) + return true; + while(true){ + int length = s.length(); + s = s.replace("()",""); + s = s.replace("{}",""); + s = s.replace("[]",""); + if(length == s.length()){ + break; + } + } + //return s.isEmpty(); + return s.length() == 0; + } + } + + + + + class Solution { + public boolean isValid(String s) { + String t = s; + if(s.equals("")) + return true; + for(int i =0;i bool: + dic = {'{': '}', '[': ']', '(': ')', '?': '?'} + stack = ['?'] + for c in s: + if c in dic: stack.append(c) + elif dic[stack.pop()] != c: return False + return len(stack) == 1 + +### 复杂度分析 + +* 时间复杂度 O(N):正确的括号组合需要遍历 1 遍 s; +* 空间复杂度 O(N):哈希表和栈使用线性的空间大小。 \ No newline at end of file diff --git "a/yjiewei/9-\344\270\212\345\215\207\347\232\204\346\270\251\345\272\246-\347\256\200\345\215\225.md" "b/yjiewei/9-\344\270\212\345\215\207\347\232\204\346\270\251\345\272\246-\347\256\200\345\215\225.md" new file mode 100644 index 0000000..aa670ac --- /dev/null +++ "b/yjiewei/9-\344\270\212\345\215\207\347\232\204\346\270\251\345\272\246-\347\256\200\345\215\225.md" @@ -0,0 +1,26 @@ + +## 197.上升的温度 +### 题目:给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。 + 这道题主要是一个函数,dateDiff() 函数返回两个日期之间的时间,后者减去前者 + DATEDIFF(datepart,startdate,enddate) + //试了一下这个datepart用不了??? + + +---------+------------------+------------------+ + | Id(INT) | RecordDate(DATE) | Temperature(INT) | + +---------+------------------+------------------+ + | 1 | 2015-01-01 | 10 | + | 2 | 2015-01-02 | 25 | + | 3 | 2015-01-03 | 20 | + | 4 | 2015-01-04 | 30 | + +---------+------------------+------------------+ + +### 思路:查询比昨天更高的温度,那最好就是连接本表来使用,查询两个条件下符合的情况。这里这要是你得知道一个函数叫dateDiff(),返回两个日期之间的时间差值。 + select a.id from Weather as a + join Weather as b + on a.Temperature>b.Temperature and dateDiff(a.RecordDate,b.RecordDate)=1 + +* 注意:这里只能使用inner join或者join,不能用left join,两者的区别在于 + +![](https://www.runoob.com/wp-content/uploads/2014/03/img_innerjoin.gif) +![](https://www.runoob.com/wp-content/uploads/2014/03/img_leftjoin.gif) +![](https://www.runoob.com/wp-content/uploads/2014/03/img_rightjoin.gif) \ No newline at end of file