leetcode笔记
- 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.
public class nextGreaterElement {public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int i,j;
int index;
ArrayList list = new ArrayList();
for(i=0;
i< nums1.length;
i++){
index=findIndex(nums1[i],nums2);
//找到nums1中某个元素是在nums2中的位置index
for(j=index+1 ;
j
- 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.
- 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.
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 fres = new ArrayList();
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 count = new HashMap();
//左边是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);
}
- 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.
Example 1:
【leetcode笔记】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 store = new HashMap();
store = findYside(points);
int nowc = 0;
ArrayList co = new ArrayList();
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 store = new HashMap();
for(int i=0;
i< points.length-1;
i++)
for(int j=i+1;
j
推荐阅读
- EffectiveObjective-C2.0|EffectiveObjective-C2.0 笔记 - 第二部分
- Android中的AES加密-下
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- leetcode|leetcode 92. 反转链表 II
- 【读书笔记】贝叶斯原理
- 【韩语学习】(韩语随堂笔记整理)
- 人性的弱点-笔记
- 读书笔记:博登海默法理学|读书笔记:博登海默法理学 —— 正义的探索(1)
- D034+3组苏曼+《写作这回事》读书笔记
- 《自我的追寻》读书笔记3