Procedures are good or bad:
The first principle is: Speed up
The second principle is: less memory
The third principle is: data should be small
There are several ways of timing programs
1) clock() function of C++, header file is time.h
This timing method is based on the CPU clock as the timing unit, also known as wall time.
The timing method is:
clock_t start=clock()
clock_t finish=clock()
total_time=(double)(finish-start)/CLOCKS_PER_SEC
2) Millisecond-level accuracy of <boost/timer.hpp> (best use)
timer t // declare timer, start timer
...
t.elapsed()// the elapsed time of output, in s
3) Functions of Libraries
Procedure consumed memory:
It can be obtained in the following way:
2.1)ps aux
2.2) top (best use)
2.3)pmap -d $pid
pid is the pid of the current program, which can be obtained by 2.1)
The first way of thinking
Using virtual header nodes
package Soution; public class Soution1 { public ListNode removeElements(ListNode head,int val) { while(head!=null&& head.val==val) { ListNode delNode = head; head = head.next; delNode.next =null; } if(head ==null) return null; ListNode prev = head; while(prev.next!=null) { if(prev.next.val ==val) { ListNode delNode=prev.next; prev.next=delNode.next; delNode.next = null; } else prev = prev.next; } return head; } }
The second way of thinking
Do not use virtual header nodes
package Soution; public class Soution11 { public ListNode removeElements(ListNode head,int val) { ListNode dummyHead = new ListNode(-1); dummyHead.next = head; ListNode prev = dummyHead; while(prev.next!=null) { if(prev.next.val ==val) { prev.next=prev.next.next; } else prev = prev.next; } return dummyHead.next; } public static void main(String[] args) { int[] nums = {1,2,6,3,4,5,6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Soution11().removeElements(head,6)); System.out.println(res); } }
The third way of thinking
recursion
package Soution; public class Soution111 { public ListNode removeElements(ListNode head,int val) { if(head==null) { return null; } ListNode res =removeElements(head.next,val); if(head.val==val) return res; else { head.next =res; return head; } } public static void main(String[] args) { int[] nums = {1,2,6,3,4,5,6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Soution111().removeElements(head,6)); System.out.println(res); } }
Code optimization
package Soution; public class YouHuaSoution111 { public ListNode removeElements(ListNode head,int val) { if(head==null) { return null; } head.next =removeElements(head.next,val); if(head.val==val) return head.next; else { head.next =head; return head; } } public static void main(String[] args) { int[] nums = {1,2,6,3,4,5,6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Soution111().removeElements(head,6)); System.out.println(res); } }
Continue to optimize
package Soution; public class YouHuaSoution111 { public ListNode removeElements(ListNode head,int val) { if(head==null) { return null; } head.next =removeElements(head.next,val); // if(head.val==val) // return head.next; // else { // head.next =head; // return head; // } return head.val ==val?head.next:head; } public static void main(String[] args) { int[] nums = {1,2,6,3,4,5,6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Soution111().removeElements(head,6)); System.out.println(res); } }
Like me can pay attention to me, we can exchange learning together.
Public address:
Let me fall in love with it Computer
qq group: 473989408