Saturday, October 31, 2015

Leetcode: Valid Sudoku (whole project)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
------------------------------------------------------------
Check raw, collomn and then squares. See codes for details. For debugging reason, I change the input format to vector<string>, so that I can easily write some test input. 
NOTE: for square check, I originally wrote:
                           for (int m = i; m<m + 3; m++){
                                  for (int n = j; n<n + 3; n++){
which will be wrong as it will continue until the end. The correct one should be:
                           for (int m = i; m<i + 3; m++){

                                  for (int n = j; n<j + 3; n++){
where i, j from previous procedures. 
///////////////////////////////////////////////////////////////
//codes
#include "stdafx.h"
# include <cstdlib>
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
# include <map>         // std::map
# include <unordered_map>
# include <string>
#include <bitset>
#include <ctype.h>
using namespace std;

class Solution {
public:
       bool isValidSudoku(vector<string>& board) {
              map<char, int> tmp;
              //check raw
              for (int i = 0; i<9; i++){
                     tmp.clear();
                     for (int j = 0; j<9; j++){
                           if (board[i][j] != '.'){
                                  if (tmp.find(board[i][j]) != tmp.end())
                                         return false;
                                  else tmp[board[i][j]] = i;
                           }
                     }
              }

              //check collom
              for (int i = 0; i<9; i++){
                     tmp.clear();
                     for (int j = 0; j<9; j++){
                           if (board[j][i] != '.'){
                                  if (tmp.find(board[j][i]) != tmp.end())
                                         return false;
                                  else tmp[board[j][i]] = i;
                           }
                     }
              }

              //check square
              for (int i = 0; i<9; i = i + 3){
                     for (int j = 0; j<9; j = j + 3){
                           tmp.clear();
                           for (int m = i; m<i + 3; m++){
                                  for (int n = j; n<j + 3; n++){
                                         if (board[m][n] != '.'){
                                                if (tmp.find(board[m][n]) != tmp.end())
                                                       return false;
                                                else tmp[board[m][n]] = i;
                                         }
                                  }
                           } 
                     }
              }
              return true;
       }
};
int main(int argc, char *argv[])

{

       vector<string> input = { ".87654321", "2........", "3........", "4........", "5........", "6........", "7........", "8........", "9........" };

       Solution s;
       bool i = s.isValidSudoku(input);
}


///////////////////////////////////////////////////////////////////
// codes for Leetcode
class Solution {
public:
       bool isValidSudoku(vector<vector<char>>& board) {
              map<char, int> tmp;
              //check raw
              for (int i = 0; i<9; i++){
                     tmp.clear();
                     for (int j = 0; j<9; j++){
                           if (board[i][j] != '.'){
                                  if (tmp.find(board[i][j]) != tmp.end())return false;
                                  else tmp[board[i][j]] = i;
                           }
                     }
              }

              //check collom
              for (int i = 0; i<9; i++){
                     tmp.clear();
                     for (int j = 0; j<9; j++){
                           if (board[j][i] != '.'){
                                  if (tmp.find(board[j][i]) != tmp.end())return false;
                                  else tmp[board[j][i]] = i;
                           }
                     }
              }

              //check square
              for (int i = 0; i<9; i = i + 3){
                     for (int j = 0; j<9; j = j + 3){
                           tmp.clear();
                           for (int m = i; m<i + 3; m++){
                                  for (int n = j; n<j + 3; n++){
                                         if (board[m][n] != '.'){
                                                if (tmp.find(board[m][n]) != tmp.end())return false;
                                                else tmp[board[m][n]] = i;
                                         }
                                  }
                           }
                     }
              }
              return true;
       }
};

Friday, October 30, 2015

Leetcode: Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
Subscribe to see which companies asked this question
--------------------------------------------
--------------------------------------------
It only gives you the access to a node in a list. So it's impossible to delete the node itself as we don't know the previous node! Instead of delete the node, a compensation method is to copy the value of next to this node and delete the next node. 
///////////////////////////////////////////////////
//code
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode *p=node->next;
        node->val=node->next->val;
        node->next=p->next;
        delete p;
    }
};


Leetcode: Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:
A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
begin to intersect at node c1.

Notes:
  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

-----------------------------------------
----------------------------------------
1. find the length of two lists: lenA , lenB,
(after 1, we can check whether a connection exist or not by checking the final nodes of the two lists are the same or not. If not, return directly!)
2. using two pointers pa, pb. Move the pointer forward along the longer list by abs(lenA-lenB) nodes. 
3. then move both pointers ahead step by step and check whether there is a intersection. 
NOTE 1:
 a case below:
when only one node exist and is also the intersection:
pa->[1]<-pb
NOTE 2:
when pa->NULL,
           pb->NULL,
then we have   pa==pb???. The answer is yes!!
////////////////////////////////////////////////////////////////
//codes
class Solution {
public:
       ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
              //check input
              if (headA == NULL || headB == NULL)return NULL;
              //find the length of two list
              ListNode *pa = headA, *pb = headB;
              int countA = 0, countB = 0;
              while (pa != NULL){
                     pa = pa->next;
                     countA++;
              }
              while (pb != NULL){
                     pb = pb->next;
                     countB++;
              }
              //check there are intersection or not
              if (pa != pb)return NULL;
              //move pointer forward by abs(countA-countB)
              pa = headA;
              pb = headB;
              if (countA>countB){
                     for (int i = 0; i<countA - countB; i++){
                           pa = pa->next;
                     }
              }
              else{
                     for (int i = 0; i<countB - countA; i++){
                           pb = pb->next;
                     }
              }
              //FIND THE INTERSECTION NODE
              while (pa!= NULL){
                     if (pa == pb)return pa;
                     else{
                           pa = pa->next;
                           pb = pb->next;
                     }
              }
       }
};