Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leetcode刷题之路——20190807(160、168) #25

Open
yangxy6 opened this issue Aug 7, 2019 · 0 comments
Open

leetcode刷题之路——20190807(160、168) #25

yangxy6 opened this issue Aug 7, 2019 · 0 comments

Comments

@yangxy6
Copy link
Owner

yangxy6 commented Aug 7, 2019

160

/*
 * @lc app=leetcode id=160 lang=javascript
 *
 * [160] Intersection of Two Linked Lists
 */
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 * 时间复杂度O(n),空间复杂度O(1)
 * 思路:想法特别精妙,两个链表长度不一致,短+长拼接和长+短拼接,最后节点相等时不是相交节点就是(null===null)
 * 举栗子:[0,9,1,2,4]和[3,2,4] 两个链表拼接后[0,9,1,2,4,3,2,4]和[3,2,4,0,9,1,2,4]相交节点在2开始相同
 */
var getIntersectionNode = function(headA, headB) {
  if (headA === null || headB === null) return null;
  let curA = headA;
  let curB = headB;
  // curA===curB说明相交或者全部节点走完直接返回
  while (curA !== curB) {
    //A节点走完走B 实现A+B
    curA = curA === null ? headB : curA.next;
    curB = curB === null ? headA : curB.next;
  }
  return curA;
};

168

/*
 * @lc app=leetcode id=168 lang=javascript
 * √ 18/18 cases passed (48 ms)
 * √ Your runtime beats 87.86 % of javascript submissions
 * √ Your memory usage beats 14.29 % of javascript submissions (34 MB)
 * [168] Excel Sheet Column Title
 */
/**
 * @param {number} n
 * @return {string}
 * 思路:循环n/26直到n为0,count % 26 === 0正好是26倍数时需要count/26-1,同时将Z放到map第一位。
 */
var convertToTitle = function(n) {
  const map = ['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'];
  let ans = '';
  let count = n;

  while (count) {
    ans = map[count % 26] + ans;
    count = count % 26 === 0 ? Math.floor(count / 26) - 1 : Math.floor(count / 26);
  }
  return ans;
};
console.log(convertToTitle(701));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant