day6 class9 done

pull/3/head
Leo 5 years ago
parent ee765c4400
commit 9b8b8ccab2

@ -0,0 +1,234 @@
package leo;
import java.awt.*;
import java.util.Date;
import javax.swing.*;
/**
* @author Leo
* @ClassName Clock
* @DATE 2020/12/4 2:15
* @Description
*/
public class Clock extends JComponent {
/**
*
*/
private static final long serialVersionUID = -5379472973578609775L;
private Font f = new Font("微软雅黑", Font.PLAIN,15);
private Font f2 = new Font("微软雅黑",Font.BOLD,15);
private JLabel l = new JLabel("当前时间:");
private JLabel display = new JLabel();
private JLabel display2 = new JLabel("");
private int hour = 0;
private int min = 0;
private int sec = 0;
private Date now = new Date();
private Graphics2D g;
final double PI = Math.PI;
private String strTime = "" ;
@SuppressWarnings("deprecation")
public Clock(){
add(l);
l.setBounds(120, 320, 80, 20);
l.setFont(f);
add(display);
display.setBounds(195, 320, 80, 20);
display.setFont(f);
display.setBorder(BorderFactory.createLineBorder(Color.black));
add(display2);
display2.setBounds(90, 350, 250, 20);
display2.setFont(f);
hour = now.getHours();
min = now.getMinutes();
sec = now.getSeconds();
setVisible(true);
}
public void paintComponent(Graphics g1){
double x,y;
super.paintComponent(g1);
g = (Graphics2D) g1;
//反锯齿开关开
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//画表盘
g.setPaint(new GradientPaint(5,40,Color.red,15,50,Color.yellow,true));
g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
g.drawOval(75, 40, 250, 250);
g.fillOval(195, 160, 10, 10);
g.setColor(Color.black);
//画60个点
for(int i = 0;i < 60;i++)
{
double[] co = new double[2];
co = paint_Dot(i * 2 * PI / 60);
x = co[0];
y = co[1];
if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
{
g.fillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);
}
else//其他小点
{
g.fillOval((int)(x - 2.5 + 200),(int)(y - 2.5 + 165),3,3);
}
}
//画四个数字
g.setFont(f2);
g.drawString("3", 300, 171);
g.drawString("6", 195, 273);
g.drawString("9", 91, 171);
g.drawString("12", 195, 68); //g.drawString("12", 190, 68);
//画时针,分针,秒针
paint_HourPointer(hour*3600 + min*60 + sec,g);//时针走过的秒数
paint_MinutePointer(min*60 + sec,g);//分针走过的秒数
paint_SecondPointer(sec,g);//秒针走过的秒数
}
public void showUI(){
new Thread() {
@SuppressWarnings("deprecation")
public void run() {
while (true)
{
now = new Date();
hour = now.getHours();
min = now.getMinutes();
sec = now.getSeconds();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
showTime();
repaint();
}
}
}.start();
}
public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
double x,y,angle;
angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
x = 200 + 60 * Math.sin(angle);
y = 165 - 60 * Math.cos(angle);
g.setStroke( new BasicStroke(5,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
g.setPaint(new GradientPaint(200,165,Color.red,260,165,Color.blue,true));
g.drawLine(200, 165, (int)x, (int)y);
}
public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
double x,y,angle;
angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
x = 200 + 80 * Math.sin(angle);
y = 165 - 80 * Math.cos(angle);
g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
g.setPaint(new GradientPaint(200,165,Color.magenta,280,165,Color.blue,true));
g.drawLine(200, 165, (int)x, (int)y);
}
public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
double x,y,x1,y1,x2,y2,x3,y3,angle;
double cos = 90 / Math.sqrt(8125);//90*90+5*5
double sin = 5 / Math.sqrt(8125);
angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
x = 200 + 95 * Math.sin(angle);
y = 165 - 95 * Math.cos(angle);
x1 = 200 + 20 * Math.sin(angle + PI);
y1 = 165 - 20 * Math.cos(angle + PI);
x2 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); //sin(a-b)
y2 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos + Math.sin(angle)*sin ); //cos(a-b)
x3 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos + Math.cos(angle)*sin ); //sin(a+b)
y3 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); //cos(a+b)
g.setStroke( new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
g.setPaint(new GradientPaint(180,165,Color.CYAN,295,165,Color.MAGENTA,true));
g.drawLine((int)x1, (int)y1, (int)x, (int)y);
g.drawLine((int)x2, (int)y2, (int)x, (int)y);
g.drawLine((int)x3, (int)y3, (int)x, (int)y);
}
public double[] paint_Dot(double angle){
double[] co = new double[2];
co[0] = 115 * Math.cos(angle);//横坐标
co[1] = 115 * Math.sin(angle);//纵坐标
return co;
}
@SuppressWarnings("deprecation")
private void showTime(){
String date;
int hour_temp = hour,min_temp = min,sec_temp = sec;
sec_temp += 1 ;
if(sec_temp >= 60)
{
sec_temp = 0;
min_temp += 1 ;
}
if(min_temp>=60){
min_temp=0;
hour_temp+=1;
}
if(hour_temp < 10)
strTime = "0" + hour_temp + ":";
else
strTime = "" + hour_temp + ":";
if(min_temp < 10)
strTime = strTime + "0" + min_temp + ":";
else
strTime = strTime + "" + min_temp + ":";
if(sec < 10)
strTime = strTime + "0" + sec_temp;
else
strTime = strTime + "" + sec_temp;
//在窗体上设置显示时间
date = " " + (now.getYear()+1900) + "年" + (now.getMonth()+1) + "月" + now.getDate() + "日 " + "星期" ;
switch (now.getDay()) {
case 1:
date += "一";
break;
case 2:
date += "二";
break;
case 3:
date += "三";
break;
case 4:
date += "四";
break;
case 5:
date += "五";
break;
case 6:
date += "六";
break;
case 7:
date += "日";
break;
}
date += " CST"; //Chinese standard time
strTime = " " + strTime;
display.setText(strTime);
display2.setText(date);
}
public static void main(String args[]){
Clock c = new Clock();
c.showUI();
JFrame f = new JFrame("yunyaniu");
Image img=Toolkit.getDefaultToolkit().getImage("image/logo小.jpg");//窗口图标
f.setIconImage(img);
f.setSize(400,420);
f.setResizable(false);
f.add(c, BorderLayout.CENTER);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

@ -0,0 +1,158 @@
package leo.class06_09;
import com.sun.org.apache.regexp.internal.REUtil;
import java.util.HashMap;
/**
* @author Leo
* @ClassName CopyListWithRandom
* @DATE 2020/12/4 9:22
* @Description
* every node have two pointer,it's next and random
* copy they and return new node
*/
public class CopyListWithRandom {
static class Node {
public int value;
public Node next;
public Node rand;
public Node(int v) {
this.value = v;
}
}
public static Node copyRandomNodeByMap(Node head) {
if (head == null) {
return null;
}
HashMap<Node, Node> map = new HashMap<>();
Node cur = head;
while (cur != null) {
map.put(cur, new Node(cur.value));
cur = cur.next;
}
cur = head;
while (cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
return map.get(head);
}
public static Node copyRandomNode(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Node next;
while (cur != null) {
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
Node curCopy = null;
while (cur != null) {
next = cur.next.next;
curCopy = cur.next;
curCopy.rand = cur.rand == null ? null : cur.rand.next;
cur = next;
}
cur = head;
Node copyHead = head.next;
while (cur != null) {
curCopy = cur.next;
next = cur.next.next;
cur.next = next;
curCopy.next = next != null ? next.next : null;
cur = next;
}
return copyHead;
}
public static void main(String[] args){
int testTime = 1000;
int maxSize = 30;
int range = 100;
System.out.println("Start!");
for (int i = 0; i < testTime; i++) {
Node node = randomNode(maxSize, range);
Node headMap = copyRandomNodeByMap(node);
Node head = copyRandomNode(node);
Node curNode = node;
Node curHeadMap = headMap;
Node curHead = head;
while (curNode != null) {
if (curHead.value!=curNode.value|| curHeadMap.value!=curNode.value) {
System.out.println("value");
System.out.println(curNode.value);
System.out.println(curHead.value);
System.out.println(curHeadMap.value);
System.out.println("fuck");
break;
}
if (curNode.rand != null) {
if (curHead.rand.value!=curNode.rand.value|| curHeadMap.rand.value!=curNode.rand.value) {
System.out.println("rand");
System.out.println(curNode.rand.value);
System.out.println(curHead.rand.value);
System.out.println(curHeadMap.rand.value);
System.out.println("rand fuck");
break;
}
}
curNode = curNode.next;
curHead = curHead.next;
curHeadMap = curHeadMap.next;
}
}
System.out.println("End!");
}
public static Node randomNode(int maxSize, int range) {
Node head = new Node(randomInt(range));
Node cur = head;
int size = (int) (maxSize * Math.random() + 1);
for (int i = 0; i < size; i++) {
cur.next = new Node(randomInt(range));
cur = cur.next;
}
cur = head;
while (cur != null && size > 0) {
double rand = Math.random();
if (rand < 0.5) {
double v = size * Math.random() + 1;
Node temp = head;
for (int i = 0; i < v; i++) {
temp = temp.next;
size--;
}
cur.rand = temp;
}
cur = cur.next;
}
return head;
}
public static int randomInt(int range) {
return (int) ((range * Math.random() + 1) - (range * Math.random() + 1));
}
}

@ -0,0 +1,181 @@
package leo.class06_09;
import com.sun.org.apache.regexp.internal.REUtil;
import com.sun.xml.internal.bind.v2.model.core.ID;
/**
* @author Leo
* @ClassName FindFirstIntersectNode
* @DATE 2020/12/4 2:26
* @Description
* head1head2 null
*
* NO(N) O(1)
*/
public class FindFirstIntersectNode {
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
// judgment node is there a ring
Node loop1 = getLoop(head1);
Node loop2 = getLoop(head2);
//judgment different situations
if (loop1 == null && loop2 == null) {
//无环
return noLoop(head1, head2);
} else if (loop1 != null && loop2 != null) {
//有环
return bothLoop(head1, loop1, head2, loop2);
}
return null;
}
private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
if (head1 == null || head2 == null) {
return null;
}
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
cur1 = cur1.next;
n++;
}
while (cur2 != loop2) {
cur2 = cur2.next;
n--;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else {
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
public static Node getLoop(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while (slow != fast) {
if (fast.next == null || fast.next.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != null) {
n++;
cur1 = cur1.next;
}
while (cur2 != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
// 0->9->8->6->7->null
Node head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
// 1->2->3->4->5->6->7->4...
head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
head1.next.next.next.next.next.next = head1.next.next.next; // 7->4
// 0->9->8->2...
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println(getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6..
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
}
}

@ -3,6 +3,7 @@ package leo.class06_09;
import com.sun.org.apache.xpath.internal.functions.FuncFalse;
import javax.print.attribute.standard.NumberUp;
import javax.swing.plaf.TreeUI;
import java.util.Stack;
/**
@ -313,6 +314,57 @@ public class IsPalindromeList {
return verify;
}
public static boolean isPalindromeList6(Node head) {
if (head == null || head.next == null) {
return true;
}
//findMid
Node n1 = head;
Node n2 = head;
while (n2.next != null && n2.next.next != null) {
n1 = n1.next;
n2 = n2.next.next;
}
//reverse half node after
//n1 is mid
n2 = n1.next;
n1.next = null;
Node n3 = null;
while (n2 != null) {
n3 = n2.next;
n2.next = n1;
n1 = n2;
n2 = n3;
}
//n1 is head;
n3 = n1;
n2 = head;
boolean verify = true;
//compare
while (n1 != null && n2 != null) {
if (n1.value != n2.value) {
verify = false;
break;
}
n1= n1.next;
n2 = n2.next;
}
//reverse
n2 = n3.next;
n3.next = null;
while (n2 != null) {
n1 = n2.next;
n2.next = n3;
n3 = n2;
n2 = n1;
}
//return
return verify;
}
}
@ -320,7 +372,7 @@ class IsPalindromeListMain {
public static void main(String[] args){
int testTime = 1000;
int maxSize = 3;
int maxSize = 5;
int range = 100;
boolean a;
boolean b;
@ -330,7 +382,7 @@ class IsPalindromeListMain {
Node originNode = generateRandomNode(maxSize, range);
Node head = copyNode(originNode);
b = IsPalindromeList.isPalindromeListByStack(head);
a = IsPalindromeList.isPalindromeList5(head);
a = IsPalindromeList.isPalindromeList6(head);
if (!isEqualsNode(head, originNode)) {
System.out.println("not equals node!");
}

Loading…
Cancel
Save