LeetCode(Medium)

目录:

  1. Linked List Cycle

  2. Binary Tree Right Side View


Linked List Cycle 返回目录

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

思路:两个指针,一个指向头结点,另一个指向头结点的下一个结点,然后第一个指针每次前进一步,第二个指针每次前进两步,如果两个指针能够相遇,则存在环。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     bool hasCycle(ListNode *head)
12     {
13         if(head == NULL || head->next == NULL)
14             return false;
15         ListNode *p = head->next;
16         while(p != NULL && p->next != NULL && p != head)
17         {
18             head = head->next;
19             p = p->next->next;
20         }
21         if(p == head)
22             return true;
23         else
24             return false;
25     }
26 };

Binary Tree Right Side View  返回目录

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   2     3         <---
 \       5     4       <---

You should return [1, 3, 4].

思路:分别遍历根结点的左子树和右子树的右边,然后将右子树的右孩子,以及左子树的孩子中所在层数大于右子最后一个孩子所在层数的孩子保存起来即可!

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> rightSideView(TreeNode *root)
13     {
14         vector<int> ret;
15         if(root == NULL)
16             return ret;
17         ret.push_back(root->val);
18         vector<int> retl;
19         vector<int> retr;
20         retl = rightSideView(root->left);
21         retr = rightSideView(root->right);
22         for(int i = 0; i < retr.size(); ++i)
23                 ret.push_back(retr[i]);
24         if(retr.size() < retl.size())
25         {
26             for(int i = retr.size(); i < retl.size(); ++i)
27                 ret.push_back(retl[i]);
28         }
29         return ret;
30     }
31 };
时间: 2024-10-28 14:17:00

LeetCode(Medium)的相关文章

Leetcode medium难度顺序题解

被迫重操旧业(?) 再不刷题面试就真要翻车了.... 好在medium题难度还比较水,幸亏它不考什么神DP或数据结构或blabla不然我还面个锤子(x) 但是现场写代码还不准出错ATP顶8住啊所以还是练练手感叭.... 就,按顺序随便做几个.点中等难度然后按题号排序这样. 2. 两数相加 高精度但是用单向链表. 一开始ATP写的很麻烦,基本思路是先把两个数字重叠的部分相加,然后再单独处理较长的数字多出来的那部分,然后再处理进位这样.一共三个while循环. 但是后来发现好像直接改一下判断条件,如

每日温度(LeetCode Medium难度算法题)题解

LeetCode 题号739中等难度 每日温度 题目描述: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]. 提示:气温 列表长度的范围是 [1, 30000].每个气温的值的均为华氏度,都是在 [30, 100] 

leetcode -- medium part

22. Generate Parentheses 1 class Solution 2 { 3 private: 4 void generateParenthesis(vector<string> &v, string s, int l, int r) // l和r记录剩余左右括号的数量 5 { 6 if(l == 0 && r == 0) // 当且仅当左右括号数量都为0时,正常结束 7 v.push_back(s); 8 9 if(l > 0) 10 gene

LeetCode Medium: 8. String to Integer (atoi)

一.题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to b

LeetCode Medium:22. Generate Parentheses

一.题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()"

LeetCode Medium: 31. Next Permutation

一.题目 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

LeetCode Medium: 36. Valid Sudoku

一.题目 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of t

LeetCode Medium: 39. Combination Sum

一.题目 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeated number may be chosen from candidates unlimi

Amazon onsite

onsite5轮+Discussion with HR 由于很人很多,我已经忘记了每个interview的国籍啥的,全是男的,一个印度人,一个亲切的中国人,三个白人.大部分都是behavior question 和project experience, 我没有被直接问到任何tech问题但是在project experience里面会穿插很多与tech相关的问题,比如,你用了什么model,why use it?how to evaluate it?the problem of the model