Monthly Archives: June 2013

Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. C++: 01 /** 02  * Definition for singly-linked list. 03  * struct ListNode { 04  *     int val; 05  *     ListNode … Continue reading

Posted in Array and linked list, Stack and Queue | Leave a comment

Unique Paths II

Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in … Continue reading

Posted in Dynamic programming | Leave a comment

Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of … Continue reading

Posted in Dynamic programming | Leave a comment

Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. C++: 01 class Solution {02 public:03  … Continue reading

Posted in Dynamic programming | Leave a comment

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. C++: 01 /** 02  * Definition for singly-linked list. 03  * struct ListNode … Continue reading

Posted in Array and linked list, Recursive | Leave a comment

Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. C++: 01 class Solution { 02 public: 03     string addBinary(string a, string b) { 04         // Start typing your C/C++ … Continue reading

Posted in Array and linked list, String | Leave a comment

Valid Number

Validate if a given string is numeric. Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => true Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. C++: 01 class Solution … Continue reading

Posted in Array and linked list, String | Leave a comment