数据结构
KiKi来了
这个作者很懒,什么都没留下…
展开
-
198.打家劫舍
对于第一间房屋而言,偷窃第一间房屋的钱最多,为S0=H0=1前两间房屋:S1= 2前三间房屋:偷或不投,1+3=4前4间:偷或不偷偷,3不偷,看1or2(即前两间最大)class Solution { public int rob(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int length = nums.length; .原创 2020-12-01 20:21:41 · 107 阅读 · 0 评论 -
169.多数元素
示例1:3/2=1其中3出现2次,大于1,所以输出3.方法1:HashMapclass Solution { public int majorityElement(int[] nums) { //出现次数 nums.length/2 //计算每个元素的出现次数,用HashMap HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); .原创 2020-12-01 16:02:04 · 61 阅读 · 0 评论 -
1207独一无二的出现次数
class Solution { public boolean uniqueOccurrences(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < arr.length; i++) { map.put(arr[i],map.getOrDefault(arr[i],0)+1); //计算每个key出现次.原创 2020-11-24 11:20:11 · 54 阅读 · 0 评论 -
[LeetcodeHot100Likes]1. Two Sum
超时:class Solution { public int[] twoSum(int[] nums, int target) { int[] num = new int[2]; for (int i = 0; i < nums.length; i++) { for(int j = i + 1; j < nums.length; j++) { if (nums[j] == target - n..原创 2020-11-14 10:54:11 · 79 阅读 · 0 评论 -
【递归】Number of paths
求左上角到右下角的所有路径Approach:Recursion- Earlier we have seen “Print All Paths from Top left to bottom right in Two Dimensional Array“. Current problem is the subset of that problem. Here we will just count the number of paths, will not print them.From every ce原创 2020-10-22 15:25:02 · 226 阅读 · 0 评论 -
Flood fill algorithm
Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开的经典算法。因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域。https://www.educative.io/edpresso/what-is-the-flood-fill-algorithmThe flood fill algorithm is used to determine the properties of the area arou原创 2020-10-21 09:30:36 · 195 阅读 · 0 评论 -
Check for BST
http://cslibrary.stanford.edu/110/BinaryTrees.html 简明教程方法一: 递归如果该二叉树的左子树不为空,则左子树上所有节点的值均小于它的根节点的值; 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;它的左右子树也为二叉搜索树。设计一个递归函数 helper(root, lower, upper) 来递归判断,函数表示考虑以 root 为根的子树,判断子树中所有节点的值是否都在 (l,r)(l,r) 的范围内(注意是开区间)。如果 root原创 2020-10-19 16:02:42 · 106 阅读 · 0 评论 -
Left View of Binary Tree
// { Driver Code Startsimport java.util.LinkedList; import java.util.Queue; import java.io.*;import java.util.*;class Node{ int data; Node left; Node right; Node(int data){ this.data = data; left=null; right=n.原创 2020-10-14 15:39:57 · 174 阅读 · 0 评论 -
【贪心算法】Find the first circular tour that visits all petrol pumps
An efficient approach is to use a Queue to store the current tour. We first enqueue first petrol pump to the queue, we keep enqueueing petrol pumps till we either complete the tour, or the current amount of petrol becomes negative. If the amount becomes ne原创 2020-09-24 15:16:23 · 97 阅读 · 0 评论 -
针对string读取错误的方法(+anagram题解)
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be diffe原创 2020-08-08 22:16:43 · 146 阅读 · 0 评论 -
4 图
图更多的是采用链表存储,具体的存储方法有 3 种,分别是邻接表、邻接多重表和十字链表。一、邻接表存储法本节先讲解图的邻接表存储法。邻接表既适用于存储无向图,也适用于存储有向图。邻接点:在图中,如果两个点相互连通,即通过其中一个顶点,可直接找到另一个顶点,则称它们互为邻接点。邻接表存储图的实现方式是,给图中的各个顶点独自建立一个链表,用节点存储该顶点,用链表中其他节点存储各自的临界点。例如,存储图 1a) 所示的有向图,其对应的邻接表如图 1b) 所示:与邻接点V1相关的邻接点分别是V2和V3,原创 2020-06-27 21:22:41 · 598 阅读 · 0 评论 -
第20章 线性表、栈、队列和优先队列
20.2 集合import java.util.ArrayList;public class TestCollection { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> collection1 = new ArrayList<>(); collection1.add("Atlanta"); co原创 2020-05-24 15:13:15 · 85 阅读 · 0 评论