美文网首页
leetcode笔记

leetcode笔记

作者: 小烈yhl | 来源:发表于2018-12-04 10:42 被阅读0次
    1. Next Greater Element I
      You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

    The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

    public class nextGreaterElement {
    
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            int i,j;
            int index;
            
            ArrayList<Integer> list = new ArrayList<Integer>();
            for(i=0; i< nums1.length; i++){
                index=findIndex(nums1[i],nums2); //找到nums1中某个元素是在nums2中的位置index
                 for(j=index+1 ; j<nums2.length; j++)//比较这个数与nums2 index之后的元素是否比此数要大,如果大的话就加入列表中,没有的话就-1;
                 {
                     if(nums1[i] < nums2[j])
                     { list.add(nums2[j]);
                             break;       }
                     
                 }
                 if(list.size() != (i+1))
                     list.add(-1);
                
                }
            int []  results = new int [list.size()];
            for(int c=0; c<list.size();c++)
                results[c] = list.get(c);
            return results;
                
            
        }
        
        public int findIndex(int a, int[] b){
            for(int i=0; i<b.length; i++)
                if(a == b[i])
                    return i;
            return 0;
            
        }
    
    }
    
    1. Find Mode in Binary Search Tree
      Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.

    Assume a BST is defined as follows:

    • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
    • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
    • Both the left and right subtrees must also be binary search trees.

    For example:
    Given BST [1,null,2,2],

    1
    \
    2
    /
    2

    return [2].

    思路:
    1、前序遍历
    2、将数值作为key,出现次数作为value放入hashmap
    3、遍历map找出出现次数最多的数值

    public class leet501 {
    
     public int[] findMode(TreeNode root){
           int max = 0;
           ArrayList<Integer> fres = new ArrayList<Integer>();
           bianli(root);
           for(Map.Entry entry: count.entrySet()) { //找到hash中最大的value
               if((int)entry.getValue()>=max)
                   max = (int) entry.getValue();
           }
           for(Map.Entry entry: count.entrySet()) //找到最大的数有几个,并将其输入到数组表中
           {
               if((int)entry.getValue() == max)
                 fres.add((int) entry.getKey());
                  
           }
           int size = fres.size();
           int[] results = new int [size];
           for(int i=0; i<size; i++)
               results[i] = fres.get(i);
           
           return results;
           
       }
       private HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();//左边是treenode值,右边是出现次数
       
       public void bianli(TreeNode p)
       {
           if(p!=null)
           {
            
              
                   visit(p);
              
                   bianli(p.left);
               
                   bianli(p.right);
               
           }
          
           
           
           
           
       }
       
       public void visit(TreeNode p) {
           if(count.containsKey(p.val)) //如果node里面的number存在与hash的key中,就将其value+1,否则将其置入并设置值为1
           {
               int a;
               a = count.get(p.val)+1;
               count.remove(p.val);
               count.put(p.val, a);
           }
           else count.put(p.val,1);
       }
    
    1. Minimum Area Rectangle
      Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

    If there isn't any rectangle, return 0.

    Example 1:

    Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
    Output: 4
    Example 2:

    Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
    Output: 2
    思路:
    1、将所有垂直于x轴的线找出来,然后将其线段两点的坐标标识放入map
    2、比较没两两线段的端点y坐标是否相等
    疑问:
    能通过部分编译,但是有很多情况直接返回0值????

    public class leet939 {
        
        public int minAreaRect(int[][] points) {
           
           Map<Integer,Integer> store = new HashMap<Integer,Integer>();
           store = findYside(points);
           int nowc = 0;
    
           ArrayList<Integer> co = new ArrayList<Integer>();
           
           
            for(Map.Entry entry: store.entrySet()) {
                for(Map.Entry entry1: store.entrySet())
                {
                    if((entry1.getKey() == entry.getKey())&&(entry1.getValue() == entry.getValue())) continue;
                    int y1,y2,y1_,y2_;
                    y1 = points[(int) entry.getKey()][1];
                    y2 =points[(int) entry.getValue()][1];
                    y1_ = points[(int) entry1.getKey()][1];
                    y2_ =points[(int) entry1.getValue()][1];
                    if( ((y1==y1_)&&(y2==y2_)) || ((y1==y2_)&&(y2==y1_)))
                    {
                        if(nowc == 0)
                        nowc = Math.abs((y1-y2)*(points[(int)entry.getKey()][0]-points[(int)entry1.getKey()][0] ));
                        else 
                        {
                           int temp = Math.abs((y1-y2)*(points[(int)entry.getKey()][0]-points[(int)entry1.getKey()][0] ));
                           if(temp < nowc)
                               nowc = temp;
                        
                        }
                        
                    }
                }
        
            }
          
           return nowc;
           
            
           }
      
     public Map findYside(int [][] points){
           Map<Integer,Integer> store = new HashMap<Integer,Integer>();
           for(int i=0; i< points.length-1; i++)
               for(int j=i+1; j<points.length; j++)
                  if(  points[i][0] == points[j][0] )
                      store.put(i, j);
           return store;
                      
                       
                 
                 
          
    
    
    }
    }
    

    相关文章

      网友评论

          本文标题:leetcode笔记

          本文链接:https://www.haomeiwen.com/subject/dxfmcqtx.html