This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::collections::HashMap; | |
| struct Solution(); | |
| impl Solution { | |
| pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { | |
| let mut hm: HashMap<i32, Vec<i32>> = HashMap::new(); | |
| for i in 0..nums.len() { | |
| hm.entry(nums[i]).or_insert(vec![]).push(i as i32); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::ops::Deref; | |
| struct CustomSmartPointer { | |
| data: String, | |
| } | |
| impl Deref for CustomSmartPointer { | |
| type Target = String; | |
| fn deref(&self) -> &Self::Target { | |
| &self.data | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io; | |
| fn main() { | |
| loop { | |
| println!("Input a number!"); | |
| let mut nth = String::new(); | |
| io::stdin() | |
| .read_line(&mut nth) | |
| .expect("Error in reading nth"); | |
| let nth: u32 = match nth.trim().parse() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function topologicalSort(n: number, edges: number[][]): number[] { | |
| const numberOfEdges = Array(n).fill(0); | |
| const bidirectionalEdges: number[][] = []; | |
| for (let i = 0; i < n; i++) { | |
| bidirectionalEdges.push([]); | |
| } | |
| const sorted: number[] = []; | |
| // Construct the # of edges dictionary | |
| for (const [nodeA, nodeB] of edges) { | |
| numberOfEdges[nodeA]++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function KMPsearch(s: string, w: string): number[] { | |
| const acc: number[] = []; | |
| const LPS: number[] = createLPSArray(w); | |
| let [i, j] = [0, 0]; | |
| while (i < s.length) { | |
| if (s[i] === w[j]) { | |
| i += 1; | |
| j += 1; | |
| if(j === w.length) { | |
| acc.push(i-j); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createLPSArray(w: string): number[] { | |
| const lps: number[] = [0]; | |
| let len = 0; | |
| let i = 1; | |
| while (i < w.length) { | |
| if (w.charAt(i) === w.charAt(len)) { | |
| lps.push(++len); | |
| i++; | |
| } else { | |
| if (len !== 0) len = lps[len-1]; // This is the big point! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TNode { | |
| letter: string; | |
| num: number; | |
| children: { [letter: string]: TNode }; | |
| constructor(letter: string) { | |
| this.letter = letter; | |
| this.num = 1; | |
| this.children = {}; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function prims(n: number, edges: number[][], start: number): number { | |
| // Write your code here | |
| const edgeDict: {[s: number]: number[][]} = {}; | |
| // Create the edgeDict | |
| for (const edge of edges) { | |
| const [s, t] = [edge[0], edge[1]]; | |
| if (typeof edgeDict[s] === "undefined") edgeDict[s] = [edge]; | |
| else edgeDict[s].push(edge); | |
| if (typeof edgeDict[t] === "undefined") edgeDict[t] = [edge]; | |
| else edgeDict[t].push(edge); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TNode { | |
| v: number; | |
| parent: TNode; | |
| rank: number; | |
| weight: number; | |
| constructor(v: number) { | |
| this.v = v; | |
| this.parent = null; | |
| this.rank = 0; | |
| this.weight = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Tree, TNode } from './printTree'; | |
| test("Only root", () => { | |
| const root = new TNode("1"); | |
| const tree = new Tree(root); | |
| tree.print(); | |
| expect(true).toBe(true); | |
| }) | |
| test("same value length, height 2", () => { |
NewerOlder