id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string result = \"\";\n int carry = 0;\n int i = a.length() - 1;\n int j = b.length() - 1;\n\n while(i >= 0 || j >= 0 || carry == 1){\n if(i >= 0)\n carry += a[i--] - '0';\n ... |
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n // good question\n // calculate the corresponding decimal value of each bit using powers of 2.\n int n=a.length();\n int m=b.length();\n int i=n-1;\n int j=m-1;\n int carry=0;\n s... |
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n int N = a.size(), M = b.size();\n int i = N-1, j = M-1;\n int carry = 0;\n string ans;\n\n while (i >= 0 && j >= 0) {\n int sum = (a[i]-'0') + (b[j]-'0') + carry;\n\n ans = to_st... |
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "\nclass Solution {\npublic:\n\n string addBinary(string a, string b) {\n \n \n if(b.size() > a.size()) swap(a,b);\n \n \n while(b.size() < a.size()) b = \"0\" + b;\n\n int carry = 0;\n\n string res = \"\";\n\n for(int i = b.size()-1; i >= 0 ... |
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "#include <cstdlib>\n\nclass Solution {\npublic:\n string addBinary(string a, string b) {\n string sum=\"\";\n int diff=std::abs(static_cast<int>(a.length())-static_cast<int>(b.length()));\n string zeros=\"\";\n int i=0;\n while(i<diff){\n zeros='0'+zeros;\n ... |
67 | <p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><... | 3 | {
"code": "class Solution {\npublic:\n string addBinary(string a, string b) {\n string binarySum = \"\";\n char c = '0';\n int l1 = a.size(), l2 = b.size(), l = max(l1, l2);\n\n if(l1 < l) {\n for(int i = 0; i < l-l1; i++) {\n a = '0' + a;\n }\n ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n int MAX_WIDTH;\n \n // Function to build a justified line of text\n string findLine(int i, int j, int eachSpaceBtwnWord, int extraeachSpaceBtwnWord, vector<string>& words, bool isLastLine) {\n string line;\n\n // If it's the last line, we left-justify i... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n // we go line by line\n // for each line, we try to get as many words from words as we can\n // for that, we reset remaining \"width\" to max width and reset the \"line window\" which is ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n string last_line (vector<string> & words, int start, int end, int maxWidth) {\n string ans = \"\";\n for (int i = start; i <= end; i++) {\n ans += words[i];\n if (i != end)\n ans += \" \";\n }\n while (ans.size(... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n string format(vector<string>& words, int maxWidth, bool lastLine) {\n string r;\n if (lastLine) { // For the last line, left-justified with spaces only at the end\n for (int i = 0; i < words.size(); i++) {\n r += words[i];\n ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n string format(vector<string>& words, int maxWidth, bool lastLine) {\n string r;\n if (lastLine) { // For the last line, left-justified with spaces only at the end\n for (int i = 0; i < words.size(); i++) {\n r += words[i];\n ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string> ans;\n string str = \"\";\n int space =0;\n int start =0;\n int totSize =0;\n int cnt = 0;\n for(int i=0;i<words.size();i++){\n int si... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string> result;\n\n int ls = 0, le = 0, space = 0;\n while (ls < words.size()) {\n le = ls + 1;\n space = maxWidth - words[ls].length();\n\n while ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& w, int m) {\n vector<string> result;\n int idx = 0;\n\n while (idx < w.size()) {\n int lth = w[idx].size();\n int last = idx + 1;\n\n while (last < w.size() &&\n ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 0 | {
"code": "class Solution {\n\n public:\n\n vector<string> fullJustify(vector<string>& words, size_t maxWidth) {\n\n vector<string> ans;\n\n vector<string> row;\n\n size_t rowLetters = 0;\n\n for (const string& word : words) {\n\n // If we place the word in this row, it will exceed the maximum width... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 1 | {
"code": "class Solution {\npublic:\n string spacejustifier(string word, int width, bool islast){\n int space=0,len = word.length();\n if(len == width)return word;\n\n for(int i=0;i<word.length();i++){\n if(word[i] == ' ')space++;\n }\n if(space==0 || islast){\n ... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 1 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>ans;\n vector<string>currentLine;\n int currentLineTotalChars = 0;\n\n for(int i = 0;i<words.size();i++){\n string ¤tWord = words[i];\n in... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 2 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n int n = words.size();\n vector<string> ans;\n int i=0;\n while(i<n){\n int currLen = words[i].length();\n int end =i+1;\n// in the if block apart from words l... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 2 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>res;\n int curr = 0, oglen = 0;\n vector<string>aux;\n //stores the words for the current row\n for(string s : words)\n {\n int l = s.length(... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 3 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>res;\n int curr = 0, oglen = 0;\n vector<string>aux;\n //stores the words for the current row\n for(string s : words)\n {\n int l = s.length(... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 3 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>res;\n int curr = 0, oglen = 0;\n vector<string>aux;\n //stores the words for the current row\n for(string s : words)\n {\n int l = s.length(... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 3 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>res;\n int curr = 0, oglen = 0;\n vector<string>aux;\n //stores the words for the current row\n for(string s : words)\n {\n int l = s.length(... |
68 | <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p>
<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e... | 3 | {
"code": "class Solution {\npublic:\n vector<string> fullJustify(vector<string>& words, int maxWidth) {\n vector<string>res;\n int curr = 0, oglen = 0;\n vector<string>aux;\n //stores the words for the current row\n for(string s : words)\n {\n int l = s.length(... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n bool check(int mid,int x){\n return (long)mid <= (long)(x/mid);\n }\n\n int mySqrt(int x) {\n if(x == 0) return 0;\n int lo = 1,hi = x;\n while(lo<hi){\n int mid = lo + (hi-lo+1)/2;\n if(check(mid,x)) lo = mid;\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n bool check(int mid,int x){\n return (long)mid <= (long)(x/mid);\n }\n\n int mySqrt(int x) {\n if(x == 0) return 0;\n int lo = 1,hi = x;\n while(lo<hi){\n int mid = lo + (hi-lo+1)/2;\n if(check(mid,x)) lo = mid;\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n bool check(int mid,int x){\n return (long)mid <= (long)(x/mid);\n }\n\n int mySqrt(int x) {\n if(x == 0) return 0;\n int lo = 1,hi = x;\n while(lo<hi){\n int mid = lo + (hi-lo+1)/2;\n if(check(mid,x)) lo = mid;\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n bool check(int mid,int x){\n return (long)mid <= (long)(x/mid);\n }\n\n int mySqrt(int x) {\n if(x == 0) return 0;\n int lo = 1,hi = x;\n while(lo<hi){\n int mid = lo + (hi-lo+1)/2;\n if(check(mid,x)) lo = mid;\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n int l = 1, h = 400000,m;\n int ans=-1;\n if(x==0){\n return 0;\n }\n while(l<=h){\n m = l +(h-l)/2;\n if(m<=x/m){\n ans=m;\n l=m+1;\n }else{\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "#include<bits/stdc++.h>\nclass Solution {\npublic:\n int mySqrt(int x) {\n if(x==0||x==1){\n return x;\n }\n int l=1;\n long long r=x;\n long long mid;\n int ans;\n while(l<=r){\n mid=l+(r-l)/2;\n if(mid<x/mid){\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n if (x == 0 || x == 1) {\n return x;\n }\n int start = 1;\n int end = x;\n while (start <= end) {\n long mid = start + (end - start) / 2;\n if (mid*mid == x) {\n return mid... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n long long i=0;\n long long s=1;\n while(i<=x){\n i=s*s;\n if(i>=x){\n break;\n }\n s++;\n }\n if(i>x){\n return s-1;\n }else{\n ret... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n // For special cases when x is 0 or 1, return x.\n if (x == 0 || x == 1)\n return x;\n \n // Initialize the search range for the square root.\n int start = 1;\n int end = x;\n int mid = -1;\n ... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n int i=1;\n if(x==0||x==1)\n return x;\n while(i<=x/i){\n i++;\n }return i-1;\n }\n};",
"memory": "8200"
} |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 0 | {
"code": "#define ll long long\nclass Solution {\npublic:\nint binary_Search(ll low,ll high,ll x){\n if (high >= low) {\n ll mid = low + (high - low) / 2;\n if (mid*mid==x)\n return mid;\n if (mid*mid <= x && (mid+1)*(mid+1)> x)\n return mid;\n \n if (mid*mid >... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 1 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n long long lo=1;\n long long hi=x;\n while(lo<=hi){\n long long mid=lo+(hi-lo)/2;\n if(mid*mid==x) return mid;\n else if(mid*mid>x) hi=mid-1;\n else lo=mid+1;\n }\n return (... |
69 | <p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
<ul>
<li>For e... | 1 | {
"code": "class Solution {\npublic:\n int mySqrt(int x) {\n int i,j=0;\n for( i=0; i<=x; i++){ // perfecr sqr\n if((long long)i*i==x){\n j=i;\n break;\n } \n // else{\n // if((i*i)<x) j=i;\n // }\n el... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n if (n == 0 || n == 1) {\n return 1;\n }\n int a = 1, b = 1;\n for (int i = 2; i <= n; i++) {\n int c = b;\n b = a+b;\n a = c;\n }\n return b;\n }\n};",
"memor... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n if(n==1)\n return 1;\n if(n==2)\n return 2;\n int prev1 = 2;\n int prev2 = 1;\n int curr;\n for(int i = 3 ; i<=n ; i++){\n curr = prev1+prev2;\n prev2 = prev1;\n ... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n int climbStairs[n+1];\n climbStairs[0]=1;\n climbStairs[1]=1;\n for(int i=2;i<=n;i++){\n climbStairs[i]=climbStairs[i-1]+climbStairs[i-2];\n }\n return climbStairs[n];\n\n }\n};",
"memory": "7100"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n int a=1;\n int b=1;\n for(int i=2;i<=n;i++) {\n int c = a+b;\n a=b;\n b=c;\n }\n return b;\n }\n};",
"memory": "7200"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n int a = 0, b = 1;\n for (int i = 0; i < n; ++i) {\n int c = a + b;\n a = b;\n b = c;\n }\n return b;\n }\n};",
"memory": "7200"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n if(n==1||n==2) return n;\n int dp1 = 1, dp2 = 2, dp;\n for(int i = 2; i < n; i++){\n dp = dp1+dp2;\n dp1 = dp2;\n dp2 = dp;\n\n }\n return dp;\n }\n};",
"memory": "7300"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int find(int ind, vector<int>& dp){\n if(ind==0) return 1;\n if(dp[ind]!=-1) return dp[ind];\n // 1 step\n int left = find(ind-1, dp);\n // 2 step\n int right=0;\n if(ind>=2)\n right = find(ind-2, dp);\n return d... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n int dp[n+1];\n dp[0]=1, dp[1]=1;\n for(int i=2; i<=n; i++){\n dp[i]= dp[i-1]+dp[i-2];\n }\n return dp[n];\n \n }\n};",
"memory": "7400"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 0 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n int dp[n+1];\n dp[0]=1,dp[1]=1;\n for (int i =2 ;i<=n;i++){\n dp[i]=dp[i-1]+dp[i-2];\n }\n return dp[n];\n }\n};",
"memory": "7400"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 1 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n vector<int> dp(3, 0);\n dp[0] = dp[1] = 1;\n for (int i = 2; i <= n; ++i) {\n dp[i % 3] = dp[(i + 2) % 3] + dp[(i + 1) % 3];\n }\n return dp[n % 3];\n }\n};",
"memory": "7500"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 1 | {
"code": "class Solution {\npublic:\n int climb_stairs_helper(int n){\n if (n == 0){ \n return 1;\n }\n\n int ans = 0; \n\n if (n >= 1){\n ans += climb_stairs_helper(n - 1); \n }\n if (n >= 2){ \n ans += climb_stairs_helper(n - 2);\n ... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 2 | {
"code": "class Solution {\npublic:\n int climbStairs(int n) {\n \n vector<int>dp(n+1);\n dp[0]=1;\n dp[1]=1;\n for(int i=2;i<=n;i++){\n // if(i==0 || i==1){\n // dp[i]=1;\n // }\n // else{\n dp[i]=dp[i-1]+dp[i-2];\n ... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 2 | {
"code": "class Solution {\npublic:\n int solve(int n,vector<int> &dp){\n //base case\n if(n<=2)\n return n;\n \n if(dp[n]!=-1) \n return dp[n]; \n \n dp[n]=solve(n-1,dp)+solve(n-2,dp);\n return dp[n];\n }\n int climbStairs(int n) {\n ... |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 2 | {
"code": "class Solution {\n public:\n int climbStairs(int n) {\n // dp[i] := the number of ways to climb to the i-th stair\n vector<int> dp(n + 1);\n dp[0] = 1;\n dp[1] = 1;\n\n for (int i = 2; i <= n; ++i)\n dp[i] = dp[i - 1] + dp[i - 2];\n\n return dp[n];\n }\n};",
"memory": "7700"
} |
70 | <p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p>
<p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<... | 2 | {
"code": "class Solution {\npublic:\n\n int recurr(int n,vector<int>& dp){\n if(n==0) return 1;\n if(n<0) return 0;\n if(dp[n]!=-1) return dp[n];\n \n return dp[n]=recurr(n-1,dp)+recurr(n-2,dp);\n }\n\n int climbStairs(int n) {\n vector<int> dp(n+1,-1);\n re... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(const string& path) {\n std::string res;\n res.reserve(path.size());\n std::string_view p{path};\n if (p[0] == '/') {\n p = p.substr(1);\n }\n size_t startPos = 0;\n for (size_t i = 0; i <= p.size(); ++i) {\n if (p[i] != '/' &... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(const string& path) {\n std::string res;\n res.reserve(path.size());\n std::string_view p{path};\n size_t startPos = 1;\n for (size_t i = 1; i <= p.size(); ++i) {\n if (p[i] != '/' && i < p.size()) {\n continue;\n }\n st... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(const string& path) {\n std::string res;\n res.reserve(path.size());\n std::string_view p{path};\n size_t startPos = 1;\n for (size_t i = 1; i <= p.size(); ++i) {\n if (p[i] != '/' && i < p.size()) {\n continue;\n }\n st... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n auto i = begin(path), j = i;\n decltype(path) component;\n while(true) {\n if(j == end(path) || *j == '/') {\n if(component == \"/.\" || component == \"/\") {\n // Skip\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n auto ans = begin(path), cur = ans;\n decltype(path) component;\n while(true) {\n if(cur == end(path) || *cur == '/') {\n if(component == \"/.\" || component == \"/\") {\n //... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string originalString = path;\n originalString = originalString + \"/\"; // Adding additional / to avoid a case where there is not / at the end.\n \n // First Replacing All /./\n string substringToReplace... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string originalString = path;\n originalString = originalString + \"/\"; // Adding additional / to avoid a case where there is not / at the end.\n \n // First Replacing All /./\n string substringToReplace... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(std::string_view path) {\n std::vector<std::string_view> components;\n while (!path.empty()) {\n int pos = path.find('/');\n std::string_view c = path.substr(0, pos);\n path = pos == path.npos ? \"\" : path.su... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string originalString = path;\n originalString = originalString + \"/\"; // Adding additional / to avoid a case where there is not / at the end.\n \n // First Replacing All /./\n string substringToReplace... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string originalString = path;\n originalString = originalString + \"/\"; // Adding additional / to avoid a case where there is not / at the end.\n \n // First Replacing All /./\n string substringToReplace... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string original = path;\n original = original + \"/\"; \n string substringToReplace = \"/./\";\n string newSubstring = \"/\";\n size_t pos = original.find(substringToReplace);\n while (pos != strin... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<pair<int, int>> folders;\n folders.resize(100);\n int len = 0;\n int lastSlash = 0;\n for (int i = 0; i <= path.size(); ++i) {\n if ((i >= path.size()) || (path[i] == '/')) {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n std::string temp = \"\", final = \"/\";\n\n int i = 1;\n\n while (i < path.size()) {\n if (path[i] != '/') temp += path[i];\n else {\n if (!temp.empty() && temp != \".\") {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string res = \"/\";\n if (path.length() == 1) return res;\n int start = 0, end = 0;\n while (end < path.length()) {\n int periods = 0;\n int others = 0;\n // skip the leading sla... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string_view> stack;\n stack.reserve(path.length() / 2); \n \n size_t start = 1; \n for (size_t i = 1; i <= path.length(); ++i) {\n if (i == path.length() || path[i] == '/') {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<pair<int, int>> folders;\n folders.resize(250);\n int len = 0;\n int lastSlash = 0;\n for (int i = 0; i <= path.size(); ++i) {\n if ((i >= path.size()) || (path[i] == '/')) {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> ans;\n string ret;\n ret.push_back('/'); \n\n for (int i = 0; i < path.size(); i++) {\n if (path[i] == '/') continue;\n if (path[i] == '.') {\n string dot_cont... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n path += \"/\";\n int n = path.size();\n std::string simplified_path;\n std::string filename;\n for (int i = 0; i < n; i++) {\n char ch = path[i];\n if (ch != '/') {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n path += \"/\";\n int n = path.size();\n std::string filename;\n std::string simplified_path;\n\n for (int i = 0; i < n; i++) {\n char ch = path[i];\n if (ch != '/') {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n assert(!path.empty() && path[0] == '/');\n vector<string> st;\n for (int i = 0; i < path.size(); i++) {\n if (path[i] == '/') {\n auto it = find_if(begin(path) + i, end(path), [](char x) { ret... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n int i = 0;\n std::vector<std::string> tokens;\n while (i < path.size()) {\n if (path[i] == '/') {\n ++i;\n if (i == path.size()) {\n break;\n }... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n stack<string> st;\n string new_path, word;\n stringstream ss(path);\n while(!ss.eof()) {\n getline(ss, word, '/');\n if (word.compare(\".\") == 0)\n continue;\n el... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n string __path = path;\n vector<string> stack;\n string ans = \"/\";\n\n for(char *tok = strtok((char*) __path.c_str(), \"/\"); tok; tok = strtok(NULL, \"/\")) {\n if(!(*tok))\n continue... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> dirs;\n const char *d = \"/\";\n char *p = strtok(path.data(), d);\n while(p != NULL){\n //cout << p << endl;\n if(strcmp(p, \"..\") == 0){\n if(dirs.size() > ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "\nclass Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> stack;\n stringstream ss(path);\n string temp;\n\n while (getline(ss, temp, '/')) {\n if (temp == \"\" || temp == \".\") {\n continue;\n } else if (temp == \... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> stack; // to store valid directory names\n string currDir;\n stringstream ss(path);\n \n while (getline(ss, currDir, '/')) {\n if (currDir == \"\" || currDir == \".\") {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> st;\n string buffer = \"\";\n int idx = 0;\n path.push_back('/');\n\n while(idx < path.size()){\n char c = path[idx];\n if(c == '/'){\n if(buffer == \"... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n deque<string> stk;\n stringstream ss(path);\n string t;\n while (getline(ss, t, '/')) {\n if (t == \"\" || t == \".\") {\n continue;\n }\n if (t == \"..\") {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> p;\n string q = \"\";\n for(int i = 1; i < path.size(); i++){\n if(path[i] == '/'){\n if(q.size() > 2){\n p.push_back(q);\n }else i... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(string path) {\n string ans;\n istringstream iss(path);\n vector<string> stack;\n\n for (string dir; getline(iss, dir, '/');) {\n if (dir.empty() || dir == \".\")\n continue;\n if (dir == \"..\") {\n if (!stack.empty())\n... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n path += \"/\";\n string temp;\n vector<string> fpath;\n for(int i=0;i<path.size();i++) {\n if (path[i] == '/') {\n if (temp.size() == 0 || temp == \".\") {\n temp = \... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(string path) {\n string ans;\n istringstream iss(path);\n vector<string> stack;\n\n for (string dir; getline(iss, dir, '/');) {\n if (dir.empty() || dir == \".\")\n continue;\n if (dir == \"..\") {\n if (!stack.empty())\n... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(string path) {\n string ans;\n istringstream iss(path);\n vector<string> stack;\n\n for (string dir; getline(iss, dir, '/');) {\n if (dir.empty() || dir == \".\")\n continue;\n if (dir == \"..\") {\n if (!stack.empty())\n... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n \n // Split path by /\n \n stack<string> dirs;\n for (int i=0; i<path.size(); ++i) {\n string d = \"\";\n while (path[i] != '/' and i < path.size()) {\n d += path[i];\... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 0 | {
"code": "class Solution {\n public:\n string simplifyPath(string path) {\n string ans;\n istringstream iss(path);\n vector<string> stack;\n\n for (string dir; getline(iss, dir, '/');) {\n if (dir.empty() || dir == \".\")\n continue;\n if (dir == \"..\") {\n if (!stack.empty())\n... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> dirs;\n int pt = 0;\n while (pt < path.size()) {\n while (pt < path.size() && path[pt] == '/') {\n pt++;\n }\n string directory_name = \"\";\n w... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> stack;\n\n stringstream ss(path);\n string temp;\n\n while (getline(ss, temp, '/')) {\n if (temp == \"..\") {\n if (!stack.empty()) stack.pop_back();\n } else ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n vector<string> stack;\n\n stringstream ss(path);\n string temp;\n\n while (getline(ss, temp, '/')) {\n if (temp == \"..\") {\n if (!stack.empty()) stack.pop_back();\n } else ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n int n=path.length();\n vector<string> ans;\n stringstream ss(path);\n string token=\"\";\n while(getline(ss,token,'/'))\n {\n if(token==\"\" || token==\".\")\n {\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n std::stack<string> st;\n\n for(int i = 1; i<path.size(); ++i){\n string s = \"\";\n while(i<path.size() && path[i]!='/'){\n s += path[i];\n i++;\n }\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n stack<string> dirs;\n int index=1, before, i;\n string cur; string s;\n\n while (index < path.size()) {\n before = index;\n while (index<path.size() && path[index]!='/')\n in... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution\n{\npublic:\n std::string simplifyPath(std::string path)\n {\n if (path.size() == 1)\n {\n return path;\n }\n std::string result;\n std::stack<std::string> stk;\n std::string temp;\n for (int i = 0; i < path.size(); i++)\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n stack<string> dirs;\n int index=1, before, i;\n string cur; string s;\n\n while (index < path.size()) {\n before = index;\n while (index<path.size() && path[index]!='/')\n in... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n stack <string> st;\n string curr = \"\";\n int n = path.size();\n for (int i=0; i<n; i++){\n if (path[i] == '/'){\n if (curr == \".\"){\n curr = \"\";\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "// #include <fmt/core.h>\n\nclass Solution {\npublic:\n\n void clear_double_dot (std::stack<std::string>& st) {\n int dd_count = 1;\n while (!st.empty() && st.top() == \"..\") {\n ++dd_count;\n st.pop();\n }\n // std::cout << \"dd: \" << dd_count << '\\n... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n std::stack<string> st;\n std::stack<string> st2;\n\n for(int i = 1; i<path.size(); ++i){\n string s = \"\";\n while(i<path.size() && path[i]!='/'){\n s += path[i];\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n string simplifyPath(string path) {\n std::stack<string> st;\n std::stack<string> st2;\n\n for(int i = 1; i<path.size(); ++i){\n string s = \"\";\n while(i<path.size() && path[i]!='/'){\n s += path[i];\n ... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "// #include <fmt/core.h>\n\nclass Solution {\npublic:\n\n void clear_double_dot (std::stack<std::string>& st) {\n int dd_count = 1;\n while (!st.empty() && st.top() == \"..\") {\n ++dd_count;\n st.pop();\n }\n std::cout << \"dd: \" << dd_count << '\\n';\... |
71 | <p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
<ul>
<li>A si... | 1 | {
"code": "class Solution {\npublic:\n\n\n string simplifyPath(string path) {\n\n vector<string> split;\n string last_dir;\n\n for(char c : path) {\n if (c == '/' ) {\n if (!last_dir.empty()) {\n split.push_back(last_dir);\n last_... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.