演算法和資料結構-簡版1

幻海流心發表於2019-01-26

001 基礎

  1. 多塊程式碼合在一起,只看最高複雜度的運算
  2. 時間複雜度
    1. 常數次數,1,2,3—,時間複雜度都是O(1)
    2. n,2n,3n—,常數*n次,時間複雜度都是O(n)
  3. 常見遞迴演算法時間複雜度
    1. 二分查詢 時間複雜度是 O(logn)
    2. 二叉樹遍歷 時間複雜度是 O(n)
    3. 排序查詢 時間複雜度是 O(n)
    4. 快排,歸併排序 時間複雜度是 O(nlogn)

002 陣列及連結串列

陣列

  1. 陣列是記憶體裡連續的一段儲存區域.通過陣列下標可以隨機的訪問任意一個元素.
  2. 訪問任意陣列元素的時間複雜度是O(1)
  3. 為了保證陣列元素在記憶體中的連續性,插入和刪除陣列元素,時間複雜度是O(n)

連結串列

  1. 單連結串列
  2. 雙連結串列
  3. 插入和刪除的時間複雜度是O(1)
  4. 查詢的時間複雜度是O(n),因為必須從連結串列頭部遍歷查詢

003 幾道題

反轉單連結串列Assume that we have linked list 1 >
2 >
3 >
0, we would like to change it to 0 >
1 >
2 >
3./** * Definition for singly-linked list. * public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
val = x;

} *
} */
//自己的解法:class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null){
return head;

} ListNode curr = head.next;
head.next = null;
while(curr != null){
ListNode nextTemp = curr.next;
curr.next = head;
head = curr;
curr = nextTemp;

} return head;

}
}//官方答案:public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;

} return prev;

}官方答案分析:反轉最終結果是原始第一節點的next=null;
然後原始第二節點的next是原始第一節點----想象原始第一節點左邊1個虛擬的節點prev=null.這樣反轉,則原始第一節點的next變為prev,原始第二節點的next變為原始第一節點----複製程式碼
兩兩交換單連結串列中的節點/Swap Nodes in PairsGiven a linked list, swap every two adjacent nodes and return its head.Given 1->
2->
3->
4, you should return the list as 2->
1->
4->
3./** * Definition for singly-linked list. * public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
val = x;

} *
} */
//自己的解法:class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;

} ListNode result = head.next;
ListNode curr = head;
ListNode prev = null;
while(curr != null &
&
curr.next != null){
ListNode currTemp = curr.next.next;
if(prev != null){
prev.next = curr.next;

} curr.next.next = curr;
curr.next = currTemp;
prev = curr;
curr = currTemp;

} return result;

}
}複製程式碼
判斷連結串列是否有環/Linked List CycleGiven a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list./** * Definition for singly-linked list. * class ListNode { 
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
*
} *
} */
//使用Set自動判重public class Solution
{
public boolean hasCycle(ListNode head) {
Set<
ListNode>
set = new HashSet<
ListNode>
();
while(head != null){
if(set.contains(head)){
return true;

} set.add(head);
head = head.next;

} return false;

}
}//使用Map記錄曾經遍歷過的節點public class Solution {
public boolean hasCycle(ListNode head) {
ListNode curr = head;
Map<
ListNode,Boolean>
map = new HashMap<
ListNode,Boolean>
();
while(curr != null){
if(map.get(curr) != null){
return true;

} map.put(curr,true);
curr = curr.next;

} return false;

}
}//反人類的想法:龜兔賽跑public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null){
return false;

} ListNode slow = head;
ListNode fast = head.next;
while(slow != fast){
if(fast == null || fast.next == null){
return false;

} slow = slow.next;
fast = fast.next.next;

} return true;

}
}複製程式碼
Linked List Cycle IIGiven a linked list, return the node where the cycle begins. If there is no cycle, return null.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.Note: Do not modify the linked list./** * Definition for singly-linked list. * class ListNode { 
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
*
} *
} */
public class Solution
{
public ListNode detectCycle(ListNode head) {
Set<
ListNode>
set = new HashSet<
ListNode>
();
while(head != null){
if(!set.add(head)){
return head;

} head = head.next;

} return null;

}
}複製程式碼
Reverse Nodes in k-GroupGiven a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.Example:Given this linked list: 1->
2->
3->
4->
5For k = 2, you should return: 2->
1->
4->
3->
5For k = 3, you should return: 3->
2->
1->
4->
5Note:Only constant extra memory is allowed.You may not alter the values in the list's nodes, only nodes itself may be changed./** * Definition for singly-linked list. * public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
val = x;

} *
} */class Solution {
public boolean isGroup(ListNode item, int k){
for(int i=0;
i<
k;
i++){
if(item == null) return false;
item = item.next;

} return true;

} public ListNode[] reverseCurrGroup(ListNode prev,ListNode group,int k){
ListNode curr = group;
ListNode first = null;
ListNode last = null;
for(int i=0;
i<
k;
i++){
ListNode next = curr.next;
curr.next = first;
first = curr;
curr = next;

} if(prev != null){
prev.next = first;

} return new ListNode[]{first,group,curr
};

} public ListNode reverseKGroup(ListNode head, int k) {
if(isGroup(head,k)){
ListNode[] nodes = reverseCurrGroup(null,head,k);
ListNode result = nodes[0];
ListNode prev = nodes[1];
ListNode group = nodes[2];
while(isGroup(group,k)){
nodes = reverseCurrGroup(prev,group,k);
prev = nodes[1];
group = nodes[2];

} if(prev != null){
prev.next = group;

} return result;

}else{
return head;

}
}
}複製程式碼

來源:https://juejin.im/post/5c4c1bdc5188250f743e17c2#comment

相關文章