数据结构
Mountain_YS
长得帅
展开
-
对链表的一些操作
package com.ys;import java.util.HashMap;import java.util.LinkedList;import java.util.List;public class MyLinkedList { Node head=null; public void add(int data){ Node node =new ...原创 2018-08-02 15:42:11 · 147 阅读 · 0 评论 -
Given a binary tree, find its minimum depth.
求树的最小深度递归的思想:递归出口; if(root==null)return 0; if(root.left==null&&root.right==null){ return 1; }递; int left=run(root.left);遍...原创 2018-08-15 17:40:56 · 176 阅读 · 0 评论 -
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "...原创 2018-08-15 20:26:43 · 301 阅读 · 0 评论 -
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
思路: //关键在于判断三点共线,两平行直线有且只有一个交点,所以有一个中间点,这个中间点与另外两个端点的连线的斜率相等 //由比率的性质 代码:/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * ...原创 2018-08-15 22:24:33 · 270 阅读 · 0 评论 -
Sort a linked list using insertion sort.
思路:用一个新链表来执行插入排序,将新的节点插入到正确的位置;代码:public ListNode insertionSortList(ListNode head){ if (head==null||head.next==null)return head; ListNode temp=new ListNode(Integer.MIN_VALUE); ...转载 2018-08-16 17:11:49 · 194 阅读 · 0 评论 -
Sort a linked list in O(n log n) time using constant space complexity.
思路:因为题目要求复杂度为O(nlogn),故可以考虑归并排序的思想。归并排序的一般步骤为:1)将待排序数组(链表)取中点并一分为二;2)递归地对左半部分进行归并排序;3)递归地对右半部分进行归并排序;4)将两个半部分进行合并(merge),得到结果。 所以对应此题目,可以划分为三个小问题:1)找到链表中点 (快慢指针思路,快指针一次走两步,慢指针一次走一步,快...原创 2018-08-16 19:21:53 · 233 阅读 · 0 评论 -
二叉树遍历的各种实现
package com.ys;import java.awt.*;import java.util.ArrayList;import java.util.Stack;public class Demo13 { //递归版 //前序遍历 public void preTraverse(TreNode node){ System.out.pr...原创 2018-08-22 19:12:11 · 390 阅读 · 0 评论