Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
guolinaileen / Valid Parentheses.cpp
Last active December 14, 2015 13:58
check if stack is empty at the end
class Solution {
public:
bool isValid(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s.length()==0) return true;
stack<char> st;
for(int i=0; i<s.length(); i++)
{
if(s[i]=='(' || s[i]=='{' || s[i]=='[')
class Solution {
public:
int maxArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int end=height.size()-1;
if(end<=0) return 0;
int start=0;
int maxWater=0;
while(start<end)
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<0) return false;
int counter=1;
while(x/counter>=10)
{
counter*=10;
class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(*str=='\0') return 0;
while(*str==' ') str++;
bool negative=false;
if(*str=='-')
{
public class Solution {
public int reverse(int x) {
// Start typing your Java solution below
// DO NOT write main() function
if(x==0) return 0;
boolean negative=false;
if(x<0) { negative=true; x=-x; }
int result=0;
while(x!=0)
{
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int length=s.length();
bool exist[256]={false};
int start=0;
int maxL=0;
for(int i=0; i<length; i++)
import java.io.*;
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String s;
int numberOfLines=Integer.parseInt(in.readLine());
for(int i=0; i<numberOfLines; i++)
{
s=in.readLine();
String []array=s.split();
public class Solution {
public int jump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int length=A.length;
if(length==0) return 0;
int startPos=0; //the start position of the current jump
int max=0; //total max jump
int tempMax=0;
int jumpCounter=0;
import java.util.*;
public class Solution {
public ArrayList<String> anagrams(String[] strs) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String> result=new ArrayList<String>();
if(strs.length==0) return result;
Hashtable<String, ArrayList<String>> table=new Hashtable<String, ArrayList<String>>();
for(int i=0; i<strs.length; i++)
{
@guolinaileen
guolinaileen / Pow(x, n).java
Last active March 29, 2020 23:17
binary strategy: 2^n=2^(n/2) * 2^(n/2) * 2^(n%2)
public class Solution {
public double pow(double x, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(n==0) return 1;
if(x==0) return 0;
if(n==1) return x;
if(n<0)
{
n=-n; x=1/x;