核心代码模式
class Solution {
public boolean isValid(String s) {
int len=s.length();
Deque<Character> q=new ArrayDeque<>();
for(char c:s.toCharArray())
{
//前置符号直接放
if(c=='[')q.push(']');
else if(c=='{')q.push('}');
else if(c=='(')q.push(')');
//为后置符号则直接取出来比较,不对直接返回false
else if(q.isEmpty()||q.pop()!=c)return false;
}
//还有则false
return q.isEmpty();
}
}
手写输入输出
import java.util.*;
public class Javaacm
{
//输入格式
//()[]{}
public static void main(String []args)
{
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
Deque<Character> st=new ArrayDeque<>();
for(char c:s.toCharArray())
{
if(c=='[')st.push(']');
else if(c=='{')st.push('}');
else if(c=='(')st.push(')');
else if(st.isEmpty()||st.pop()!=c)
{
System.out.print(false);
}
}
System.out.print(st.isEmpty());
return ;
}
}
核心代码模式
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
m--;n--;
//倒着比较插入
for(int i=nums1.length-1;n>=0&&m>=0;i--)
{
if(nums1[m]<=nums2[n])nums1[i]=nums2[n--];
else nums1[i]=nums1[m--];
}
//如果1先比完则把2插进去
while(n>=0)nums1[n]=nums2[n--];
return ;
}
}
手写输入输出
import java.util.*;
public class Javaacm
{
//输入格式
// [1,2,3,0,0,0]
// 3
// [2,5,6]
// 3
public static void main(String []args)
{
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
String[] str=s.substring(1,s.length()-1).split(",");
int nums1[]=new int[str.length];
int n1=scanner.nextInt();
for(int i=0;i<n1;i++)
nums1[i]=Integer.valueOf(str[i]);
s=scanner.next();
str=s.substring(1,s.length()-1).split(",");
int nums2[]=new int[str.length];
int n2=scanner.nextInt();
for(int i=0;i<n2;i++)
nums2[i]=Integer.valueOf(str[i]);
n1--;n2--;
for(int i=nums1.length-1;n1>=0&&n2>=0;i--)
{
if(nums1[n1]<=nums2[n2])nums1[i]=nums2[n2--];
else nums1[i]=nums1[n1--];
}
while(n2>=0)nums1[n2]=nums2[n2--];
for(int i:nums1)
System.out.print(i+" ");
return ;
}
}
核心代码模式
class Solution {
public int maxProfit(int[] prices) {
int ans=0;
int min=prices[0];
for(int i:prices)
{
ans=Math.max(ans,i-min);
min=Math.min(min,i);
}
return ans;
}
}
手写输入输出
import java.util.*;
public class Javaacm
{
//输入格式
// [7,1,5,3,6,4]
public static void main(String []args)
{
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
String[] str=s.substring(1,s.length()-1).split(",");
int nums[]=new int[str.length];
for(int i=0;i<nums.length;i++)
nums[i]=Integer.valueOf(str[i]);
int ans=0;
int min=nums[0];
for(int i:nums)
{
ans=Math.max(ans,i-min);
min=Math.min(min,i);
}
System.out.println(ans);
return ;
}
}
核心代码模式
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ans=new ArrayList<>();
Queue<TreeNode> q=new ArrayDeque<>();
if(root==null)return ans;
q.offer(root);
while(!q.isEmpty())
{
int n=q.size();
List<Integer>layer =new ArrayList<>();
for(int i=0;i<n;i++)
{
TreeNode cur=q.poll();
layer.add(cur.val);
if(cur.left!=null)q.offer(cur.left);
if(cur.right!=null)q.offer(cur.right);
}
ans.add(layer);
}
for(int i=1;i<ans.size();i+=2)
Collections.reverse(ans.get(i));
return ans;
}
}
手写输入输出
import java.util.*;
class TreeNode
{
TreeNode left,right;
int val;
TreeNode(int val){this.val=val;}
TreeNode(String str){val=Integer.valueOf(str);}
TreeNode(int val,TreeNode left,TreeNode right)
{
this.val=val;
this.left=left;
this.right=right;
}
}
public class Javaacm
{
//输入格式
// [3,9,20,null,null,15,7]
public static void main(String []args)
{
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
if(s.equals("[]")){
System.out.println("[]");return ;
}
String[] str=s.substring(1,s.length()-1).split(",");
TreeNode root=buildtree(str,0);
List<List<Integer>> ans=new ArrayList<>();
Queue<TreeNode> q=new ArrayDeque<>();
q.offer(root);
while(!q.isEmpty())
{
int n=q.size();
List<Integer> layer=new ArrayList<>();
for(int i=0;i<n;i++)
{
TreeNode cur=q.poll();
layer.add(cur.val);
if(cur.left!=null)q.offer(cur.left);
if(cur.right!=null)q.offer(cur.right);
}
ans.add(layer);
}
for(int i=1;i<ans.size();i+=2)
Collections.reverse(ans.get(i));
System.out.println(ans);
return ;
}
static TreeNode buildtree(String []str,int idx)
{
if(idx>=str.length||str[idx].equals("null"))
{
return null;
}
TreeNode root=new TreeNode(str[idx]);
root.left=buildtree(str,2*idx+1);
root.right=buildtree(str,2*idx+2);
return root;
}
}
核心代码模式
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//只返回p,q,最近公共祖先三个节点,其余都为null
if(root==null||root==p||root==q)return root ;
TreeNode l=lowestCommonAncestor(root.left,p,q);
TreeNode r=lowestCommonAncestor(root.right,p,q);
if(l!=null&&r!=null)return root;
return l!=null?l:r;
}
}
手写输入输出
import java.util.*;
class TreeNode
{
TreeNode left,right;
int val;
TreeNode(int val){this.val=val;}
TreeNode(String str){val=Integer.valueOf(str);}
TreeNode(int val,TreeNode left,TreeNode right)
{
this.val=val;
this.left=left;
this.right=right;
}
}
public class Javaacm
{
//输入格式
//[3,5,1,6,2,0,8,null,null,7,4]
//5 1
static int p,q;
public static void main(String []args)
{
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
if(s.equals("[]")){
System.out.println("[]");return ;
}
String[] str=s.substring(1,s.length()-1).split(",");
TreeNode root=buildtree(str,0);
p=scanner.nextInt();
q=scanner.nextInt();
System.out.println(findpq(root).val);
return ;
}
static TreeNode findpq(TreeNode root)
{
if(root==null||root.val==p||root.val==q)
return root;
TreeNode l=findpq(root.left);
TreeNode r=findpq(root.right);
if(l!=null&&r!=null)return root;
return l!=null?l:r;
}
static TreeNode buildtree(String []str,int idx)
{
if(idx>=str.length||str[idx].equals("null"))
{
return null;
}
TreeNode root=new TreeNode(str[idx]);
root.left=buildtree(str,2*idx+1);
root.right=buildtree(str,2*idx+2);
return root;
}
}