学习目标:
学习内容:
lintCode刷题:
·查询2020年五月之前开课的课程
SQL的条件查询语句,日期记得单引号(')或者双引号(")
select name,created_at
from courses
where created_at>='2020-01-01' AND created_at<'2020-05-01';
·翻转字符串
用split把字符串根据" "转换为字符串数组,用StringBuffer来实现拼接, 从最后一个拼到第二个,当数组中的元素不为" "时把它加入到StringBuffer,当元素的下一个元素不为空时在他后面加一个空格,最后在把第一个放进最后一个,不需要放入空格
public class Solution {
public String reverseWords(String s) {
String[] strings = s.split(" ");
if (strings.length == 0) return "";
StringBuffer stringBuffer = new StringBuffer();
for (int i = 1; i < strings.length; i++) {
if (!strings[strings.length - i].equals(""))
stringBuffer.append(strings[strings.length - i]);
if (!strings[strings.length - 1 - i].equals(""))
stringBuffer.append(" ");
}
if (!strings[0].equals(""))
stringBuffer.append(strings[0]);
s = stringBuffer.toString();
return s;
}
}
·简化路径
根据题目意思,/../需要返回上一级,/./ 为当前的目录,所以需要用到先进后出的栈,在遇到./的时候跳过,遇到../时弹出当前目录,最后取出的时候遍历并"/"+取最上面的累加即可
import java.util.Stack;
public class Solution {
public String simplifyPath(String path) {
String[] strings = path.split("/");
Stack<String> respath = new Stack<>();
for (String str : strings) {
if (!str.equals("") && !str.equals(".") && !str.equals(".."))
respath.push(str);
if (str.equals("..") && !respath.isEmpty())
respath.pop();
}
String resultpath = "";
while (!respath.isEmpty()) {
resultpath = "/" + respath.pop()+resultpath;
}
if (resultpath.length() == 0)
return "/";
else
return resultpath;
}
}
自己写的方法比较笨拙,单纯考虑遇到情况时不进入循环,也可以换一种思路,在遇到"."或者空时跳过这次循环即可
public class Solution {
public String simplifyPath(String path) {
String[] splits = path.split("/");
String ans = "";
Stack<String> s = new Stack<>();
for (String split : splits) {
if (split.length() == 0 || split.equals(".")) continue;
if (split.equals("..")) {
if (!s.isEmpty()) s.pop();
continue;
}
s.push(split);
}
while (!s.isEmpty()) ans = "/" + s.pop() + ans;
if (ans.length() == 0) ans += "/";
return ans;
}
}
·线段树的构造
递归创建,左节点和右节点分别进行创建方法的递归
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end) {
* this.start = start, this.end = end;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
public SegmentTreeNode build(int start, int end) {
if (start==end)
return new SegmentTreeNode(start,end);
if (start>end)
return null;
SegmentTreeNode root = new SegmentTreeNode(start,end);
int mid = (root.start+root.end)/2;
root.left=build(root.start,mid);
root.right=build(mid+1,root.end);
return root;
}
}
·线段树的构造
递归创建,左节点和右节点分别进行查询方法的递归
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The maximum number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
if (start > end) {
return 0;
}
if (root.start == root.end) {
return root.max;
}
int mid = (root.start + root.end) / 2;
int left = query(root.left, start, Math.min(mid, end));
int right = query(root.right, Math.max(mid + 1, start), end);
return Math.max(right, left);
}
}
·线段树的修改
递归创建,左节点和右节点分别进行修改方法的递归
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of segment tree.
* @param index: index.
* @param value: value
* @return: nothing
*/
public void modify(SegmentTreeNode root, int index, int value) {
if (root==null||index< root.start||index> root.end)
return;
if (root.start==index&&root.end==index) {
root.max = value;
return;
}
int mid = root.start + (root.end - root.start) / 2;
if (index<=mid){
modify(root.left, index, value);
}else {
modify(root.right, index, value);
}
root.max=Math.max(root.left.max,root.right.max);
}
}
学习时间:
2021-9-8 9:00-11:45、13:30-17:30
学习产出:
学习博客*1
刷题*6