Skip to main content

Posts

Simple ID card building application using flutter

import 'package:flutter/material.dart' ; void main () => runApp( MaterialApp ( home: NinjaCard () , )) ; class NinjaCard extends StatefulWidget { @override _NinjaCardState createState () => _NinjaCardState () ; } class _NinjaCardState extends State<NinjaCard> { int level = 0 ; @override Widget build (BuildContext context) { return Scaffold ( backgroundColor: Colors. grey [ 900 ] , appBar: AppBar ( title: Text ( 'Ninja ID Card' ) , centerTitle: true, backgroundColor: Colors. grey [ 850 ] , elevation: 0.0 , ) , floatingActionButton: FloatingActionButton ( onPressed: (){ setState(() { level += 1 ; }) ; } , child: Icon (Icons. add ) , backgroundColor: Colors. grey [ 500 ] , ) , body: Padding ( padding: EdgeInsets . fromLTRB ( 30.0 , 40 , 40.0 , 0 ) , child: Column ( crossAxisAlignment: Cross...

valarray class in C++

Valarray is a class similar to vector but is efficient than vector if it comes to mathematical operations. It provides many element-wise operations, various forms of generalized subscript operators, slicing and indirect access. In some mathematical operations, valarrays are more efficient than vector operations. Some of the APIs provided by valarray class are: apply(the function that performs the operation on every element) This API applies manipulation on the given arguments to all the array elements. valarray<int> arr = {1,2,3,4,5,6}; arr.apply([] (int x){return x = x+ 5;}); The above code increments every element by 5 sum() This API performs the sum of all elements in the given array valarray<int> arr = {1,2,3,4,5}; arr.sum(); The above code sums up all the elements of the array which is 15. min() This API finds out the smallest element in the array. max() This API finds out the largest element in the array. valarray<int> arr = {1,2,...

STL APIs in C++

Here is the most used of the many functions that STL provides:   sort(first_iterator, last_iterator)           - It sorts the elements in ascending order by default. To sort it in descending order we have to pass comparison function as the third parameter. greater<int>() can be used to achieve that purpose. reverse(first_iterator, last_iterator)          - It reverses the vector *max_element(first_iterator, last_iterator)          - It returns the maximum element of the vector *min_element(first_iterator, last_iterator)          - It returns the minimum element of the vector accumulate(first_iterator, last_iterator, initial sum value)          - It returns the sum of the elements plus the initial sum value count(first_iterator, last_iterator, element)         - It returns the number o...

Leet Code Problem #114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Approach to the solution: Traverse the left sub tree until the left most leaf found recursively. Store the right child in temp variable. Take that left node and make it as right child of leaf parent. Now traverse through the right child of the parent until the leaf node is found. Assign its right child as temp. Do the same thing for right sub tree as well. Solution in C++:   /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void flatten(TreeNode* root) { cal(root); } void cal(TreeNode* root){ if(!root) re...

Leet Code Problem #537. Complex Number Multiplication

Given two strings representing two complex numbers . You need to return a string representing their multiplication. Note i 2 = -1 according to the definition. Example 1: Input: "1+1i", "1+1i" Output: "0+2i" Explanation: (1 + i) * (1 + i) = 1 + i 2 + 2 * i = 2i,  and you need convert it to the form of 0+2i.   Solution in C++:     string complexNumberMultiply(string a, string b) {         int a1 = 0;         int a2 = 0;         int b1 = 0;         int b2 = 0;         string ret;         cal(a, a1, a2);         cal(b, b1, b2);         int real = a1 * b1 - a2 * b2;         int imag = a1 * b2 + a2 * b1;      ...

Most frequently asked interview question #15 with solution

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10. This is how the UTF-8 encoding would work: Char. number range | UTF-8 octet sequence (hexadecimal) | (binary) --------------------+--------------------------------------------- 0000 0000-0000 007F | 0xxxxxxx 0000 0080-0000 07FF | 110xxxxx 10xxxxxx 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx Given an array of integers representing the data, return whether it is a valid utf-8 encoding. Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data. Example 1: dat...

Most frequently asked interview question #14 with solution

Note: This is a companion problem to the System Design problem: Design TinyURL . TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk . Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL. Solution in C++: class Solution { public:     map<string, string> url;         // Encodes a URL to a shortened URL.     string encode(string longUrl) {         static int count = 0;         char *a = (char*)calloc(sizeof(char), 30);         ++count;         spr...