-
-
Save prakhar987/711f9474b9ba24096ebe37c28991ad2a to your computer and use it in GitHub Desktop.
class Solution { | |
public: | |
struct Trie{ | |
vector<Trie *>child; | |
int index=-1; | |
}; | |
void insert(Trie *root,string s,int index) | |
{ | |
int index; | |
for(int i=0;i<s.size();i++) | |
{ | |
index=s[i] -'a'; | |
if(root->child[index]==NULL) | |
root->child[index]=new Trie(); | |
root=root->child[index]; | |
} | |
root->index=index; | |
} | |
int find(Trie *root, string s) | |
{ | |
int index; | |
for(int i=0;i<s.size();i++) | |
{ | |
index=s[i] - 'a'; | |
if(root->child[index]==NULL) | |
return false; | |
root=root->child[index]; | |
} | |
return root->index; | |
} | |
vector<vector<int>> palindromePairs(vector<string>& words) { | |
// Build the reversed trie | |
Trie *root=new Trie(); | |
for(int i=0;i<words.size();i++) | |
insert(root,reverse(words[i].begin(),words[i].end()),i); | |
// Start Traversal | |
vector<vector<int>>answer(); | |
vector<int>tmp(2,0); | |
for(int i=0;i<words.size();i++) | |
{ | |
} | |
} | |
}; |
139
class Solution {
public:
map<string,bool>m;
string s;
struct Trie
{
vector<Trie*>child = vector<Trie*>(26,NULL);
bool leaf;
};
void insert(Trie *root,string s)
{
int index;
for(int i=0;i<s.size();i++)
{
index = s[i] - 'a';
if(root->child[index]==NULL)
root->child[index] = new Trie();
root = root->child[index];
}
root->leaf=true;
}
bool search(Trie *root,int x,int y)
{
int index;
for(int i=x;i<=y;i++)
{
index= s[i] - 'a';
if(root->child[index]==NULL)
return false;
root=root->child[index];
}
return root->leaf;
}
bool func(Trie *root, int index)
{
if(index==s.size())
return true;
for(int i=index;i<s.size();i++)
{
if(search(root,index,i))
{
bool tmp = func(root,i+1);
if(tmp)
return true;
}
}
return false;
}
bool wordBreak(string st, vector<string>& wordDict) {
// Construct Trie
Trie *root = new Trie();
s=st;
for(int i=0;i<wordDict.size();i++)
insert(root,wordDict[i]);
return func(root,0);
}
};
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.image as mpimg
from math import sin,cos
import time
Load Image and Variables
img = mpimg.imread('./image.png')
step=20
start=time.time()
def data_gen():
gen_list = ([t,t] for t in np.arange(200,300,0.1))
return gen_list
def init():
pass
fig, ax = plt.subplots()
point, = ax.plot([0], [0], 'ro')
point.set_data(0, 0)
point1, = ax.plot([0], [0], 'ro')
point1.set_data(0, 0)
##Stationary Point
point2, = ax.plot([0], [0], 'bo')
point2.set_data(0, 0)
point3, = ax.plot([0], [0], 'bo')
point3.set_data(0, 0)
point4, = ax.plot([0], [0], 'bo')
point4.set_data(0, 0)
point5, = ax.plot([0], [0], 'bo')
point5.set_data(0, 0)
##Line
line, = ax.plot([], [], lw=2,ls='solid',c='y')
ax.grid()
plt.imshow(img)
def run(data):
t, y = data
point.set_data(t, y)
point1.set_data(t+step,y+step)
point2.set_data(200+step*cos(t/10),300+step*sin(y/10))
point3.set_data(450+step*cos(t/10),70+step*sin(y/10))
point4.set_data(200+step/2*cos(t/10),50+step/2*sin(y/10))
point5.set_data(452+cos(t/10),50+sin(y/10))
if(time.time() - start >= 5):
line.set_data([200,t+step], [200,y+step])
return point,point1,point2,line
ani = animation.FuncAnimation(fig, run, data_gen, init_func=init,interval=10)
plt.show()
class Solution {
public:
int answer;
void func(TreeNode *root, int sum,int *answer)
{
if(root==NULL)
return;
if(sum==0)
*answer++;
func(root->left,sum-root->val,answer);
func(root->right,sum-root->val,answer);
}
void inorder(TreeNode *root, int sum, int answer)
{
if(root==NULL)
return;
inorder(root->left,sum,answer);
func(root,sum,answer);
inorder(root->right,sum,answer);
}
int pathSum(TreeNode root, int sum) {
int answer=0;
inorder(root,sum,&answer);
return answer;
}
};