ADP Lifion开发二轮,考数据库和算法

白人小哥。形式是视频加coderpad。

 

【编码】
20. Valid Parentheses

和原题不一样的地方:case:{{},所以loop字符串最后要加个判断条件!

//loop
for (char c: s.toCharArray()) {
  //start judging
  if (c == '(') {
    stack.push(')');
  }else if (c == '{') {
    stack.push('}');
  }else if (c == '[') {
    stack.push(']');
  }else {
    //start to pop
    //wrong case
    if (stack.pop() == null || stack.pop != c) {
      return false;
    }
  }
}

//add judgement
if (!stack.isEmpty()) 
  return false;

follow up:如果不用stack,怎么做

差了下,可以用数组遍历或者正则表达式

 

class Solution {
public:
    bool isValid(string s) {
        int top = -1;
        for(int i =0;i<s.length();++i){
            if(top<0 || !isMatch(s[top], s[i])){
                ++top;
                s[top] = s[i];
            }else{
                --top;
            }
        }
        return top == -1;
    }
    bool isMatch(char c1, char c2){
        if(c1 == '(' && c2 == ')') return true;
        if(c1 == '[' && c2 == ']') return true;
        if(c1 == '{' && c2 == '}') return true;
        return false;
    }
};
View Code
public class Solution {
    public boolean isValid(String s) {
        int length;
    
        do {
            length = s.length();
            s = s.replace("()", "").replace("{}", "").replace("[]", "");
        } while(length != s.length());
    
        return s.length() == 0;
    }
}
View Code

 我当时的草稿:

{} false
{[} false
{[}] false
{{}

//push: }
//push: }
//pop:}
stack: }

stack - push/pop 

//loop
for (char c: s.toCharArray()) {
  //start judging
  if (c == '(') {
    stack.push(')');
  }else if (c == '{') {
    stack.push('}');
  }else if (c == '[') {
    stack.push(']');
  }else {
    //start to pop
    //wrong case
    if (stack.pop() == null || stack.pop != c) {
      return false;
    }
  }
}

//add judgement
if (!stack.isEmpty()) 
  return false;

//
enter :(
push: )
enter: )
pop: )
-- correct

//time : n
//space: n

//list_left = ['(', '{'...]
list_right = [')', '}'...]
list_left[0], list_right[0]

//encapsulation

//order
//linkedhashset, linkedhashmap
//queue
View Code

 

【数据库设计和查询】
设计一组表格(写出它们的字段就行了):state, county, official...(还可以自己加)
一些约束关系:
1.一个state可能包括多个county
2.一个state分给每个county的预算金额每年都在变化
3.一个official管理一个county,但是ta以前可能也管理过的county,需要罗列这种历史。

查询:
查询今天所有county的预算金额?

 

总结:

反正同一个pk的数据不能出现两次,只能用不会重复的key来做pk

eg
student1,吃汉堡
不能又出现一条
student1,吃冰淇淋
eg
official_work(county_id, official_id, start_date, end_date)对
official_work(official_id, county_id, start_date, end_date)不对

 

我当时的草稿:

table: id, field, field

state(state_id, state_name)
county(county_id, county_name, state_id@)
official(official_id, name)
official_work(county_id, official_id, start_date, end_date)
allocation_percentage(allocation_id, allocation_percentage, start_date, end_date, county_id@)

county(NYC,NY)

(1, 15%, JAN, FEB)
(2, 25%, FEB, MAR)

state_name, county_name, allocation_percentage
SELECT state_name, county_name, allocation_percentage
FROM state JOIN county JOIN allocation_percentage
WHERE (start_date <= SYSTEM.GETDATE() AND end_date >= SYSTEM.GETDATE());
View Code

 

 
posted @ 2021-02-20 03:11  苗妙苗  阅读(56)  评论(0)    收藏  举报