Skip to content

Commit

Permalink
Added problem 9
Browse files Browse the repository at this point in the history
  • Loading branch information
YUXUANCHENG committed Sep 19, 2020
1 parent 1028a50 commit 4f35274
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
Binary file added 696_substring.cpp/two_stack
Binary file not shown.
61 changes: 61 additions & 0 deletions 696_substring.cpp/two_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stack>
#include <string>

using namespace std;

class Solution
{
public:
int countBinarySubstrings(string s)
{
int result = 0;
stack<char> s1;
stack<char> s2;
for (char n : s)
{
if (s1.empty() && s2.empty())
s1.push(n);
else
{
if (s1.empty() && !s2.empty())
{
while (!s2.empty())
{
char temp = s2.top();
s2.pop();
s1.push(temp);
}
}
if (n == s1.top() && s2.empty())
{
s1.push(n);
}
if (n == s1.top() && !s2.empty())
{
while (!s1.empty())
s1.pop();
while (!s2.empty())
{
char temp = s2.top();
s2.pop();
s1.push(temp);
}
}
if (n != s1.top())
{
s1.pop();
result++;
s2.push(n);
}
}
}
return result;
}
};

int main()
{
Solution solution;
solution.countBinarySubstrings("100111001");
return 0;
}
20 changes: 20 additions & 0 deletions 696_substring.cpp/two_stack.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.two_stack</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Binary file not shown.
13 changes: 13 additions & 0 deletions 9_Palindrome/parlindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
bool isPalindrome(int x) {
if(x<0|| (x!=0 &&x%10==0)) return false;
int sum=0;
while(x>sum)
{
sum = sum*10+x%10;
x = x/10;
}
return (x==sum)||(x==sum/10);
}
};

0 comments on commit 4f35274

Please sign in to comment.