weekly 8

Algorithm

1.Merge Two Sorted Lists

  • What 两个链表増序合并

  • How 两个链表值比较然后插到新链表中,返回新链表;如果直接用其中一个链表然后另一个对其比较插入可能效率会快点。
  • Key Codes
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1!=null&&l2==null){
            return l1;
        }
        if(l1==null&&l2!=null){
            return l2;
        }
        if(l1==null&&l2==null){
            return null;
        }
        int a;
        if(l1.val<=l2.val){
            a=l1.val;l1=l1.next;
        }else{
            a=l2.val;l2=l2.next;
        }
        ListNode temp = new ListNode(a);
        ListNode b = new ListNode(0);
        b.next=temp;
        while(l1!=null||l2!=null){
            if(l1!=null&&l2!=null){
                if(l1.val<=l2.val){
                    temp.next = new ListNode(l1.val);l1=l1.next;
                }else{
                    temp.next = new ListNode(l2.val);l2=l2.next;
                }
                temp=temp.next;
                continue;
            } else if(l1==null){
                temp.next= new ListNode(l2.val);l2=l2.next;
                temp=temp.next;
                continue;
            } else if(l2==null){
                temp.next= new ListNode(l1.val);l1=l1.next;
                temp=temp.next;
                continue;
            }
        }
        temp.next=null;
        return b.next;
    }
}

Review

Report: Google to pay Apple $9 billion to remain default search engine on Safari

  • What 花多少钱能成为Safari的默认搜索引擎。

  • How

  • 1.Google在2014年花了$10亿左右,2017年是$30亿,2018年是$90亿,预计明年将是$120亿,作为Safari的默认搜索引擎的费用。
  • 2.Safari 是世界第二大的浏览器,仅次于 Chrome。
  • 3.全球市场份额:Chrome 59.7%,Safari 14.5%。美国市场份额:Chrome 49%,Safari 31%。
  • 4.为什么Google要这么做?考虑到Safari是第二大浏览器(因为iPhone),Google作为其默认搜索引擎有助于保持搜索巨头的移动搜索优势,巩固覆盖范围和广告点击量。

Tip

  • What 遍历Map的四种方法

  • How

    • 第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  • 第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  • 第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  • 第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }

Share

苹果Apple ID泄露调查:撞库或云上贵州内鬼可能性比较高

猜你喜欢

转载自www.cnblogs.com/cmh1111/p/10186648.html