网站怎样上线,天天做网站,分类信息网址,wordpress多站点管理Every day a Leetcode
题目来源#xff1a;2833. 距离原点最远的点
解法1#xff1a;贪心
要使得到达的距离原点最远的点#xff0c;就看 left 和 right 谁大#xff0c;将 left 和 right 作为矢量相加#xff0c;再往同方向加上 underline。
答案即为 abs(left - rig…Every day a Leetcode
题目来源2833. 距离原点最远的点
解法1贪心
要使得到达的距离原点最远的点就看 left 和 right 谁大将 left 和 right 作为矢量相加再往同方向加上 underline。
答案即为 abs(left - right) underline。
代码
/** lc appleetcode.cn id2833 langcpp** [2833] 距离原点最远的点*/// lc codestart
class Solution
{
public:int furthestDistanceFromOrigin(string moves){int left 0, right 0, underline 0;for (char c : moves){if (c L)left;else if (c R)right;elseunderline;}return abs(left - right) underline;}
};
// lc codeend结果 复杂度分析
时间复杂度O(n)其中 n 是字符串 moves 的长度。
空间复杂度O(1)。