Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

For example:
Given n = 13,
Return 6, because digit 1 occurred 6 times in total in the following numbers: 1, 10, 11, 12, 13.

Let’s go for discussion. Write you solution/thought in the comment box. Later I will add the solution.

Share Button

Linked List

Introduction

Linked list is a linear data structure like arrays. But arrays are stored at contiguous location in the memory where linked list use pointer to point to the next element.

Why Linked List?

Array is the most popular linear data structure but it has some limitations as follows:

1. Array size is fixed. So, we must have to know the limit of the array in advance.
2. Inserting an element in an array is expensive which is O(n).
3. Deleting is also expensive in array which is O(n).

Nevertheless, array is most popular data structure for having the ability of random access and binary search.
But Linked list over comes these disadvantages like as follows:

1. Dynamic memory allocation.
2. Insertion and deletion takes constant time O(1).

Linked list has some drawbacks like random access is not allowed. And we have to access elements sequentially. So, binary search will not fit.

Linked list data structure:

C/C++:

struct node 
{
     int data;
     struct node *next;
};

Java:

public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
}

C#:

public class ListNode {
     public int val;
     public ListNode next;
     public ListNode(int x) { val = x; }
}
Share Button

Rotate List

Given a linked 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.

Tips:
5->1->2->3->4->NULL, when k = 1
4->5->1->2->3->NULL, when k = 2

List definition.

public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
}

If you want to learn about Linked List click here.

Let’s go for discussion. Write you solution/thought in the comment box. Later I will add the solution.

Share Button

Remove Element

You are given an array A and a value T, remove all the instances of T from the array A and return the final array length.

For example, A = [1, 3, 5, 9, 3, 10], T = 3.

Return 4.

Let’s go for discussion. Write you solution/thought in the comment box. Later I will add the solution.

Share Button

One Missing Number

You are given an array of n numbers from 1 … n but one number is missing in the array. Find out that missing number from the given array.

For example,

Array = [1, 2, 3, 5, 6]

return 4.

Let’s go for discussion. Write you solution/thought in the comment box. Later I will add the solution.

Share Button

Two Sum

You’re given a list of integer numbers and one target number. Find 2 numbers from the list such that sum of that 2 numbers are equal to the target number.

Ex. array = {1, 3, 7, 10, 20}, target = 10.

You should return {3, 7}.

    
public static int[] twoSum(int[] nums, int target) {
        
}

First of all try it by yourself. Or go for discussion in the comment. Write you solution/thought in the comment box.

Solution

Continue reading Two Sum

Share Button

Reverse Integer

You are given an integer number, you have to return it as reverse integer.

Ex.
n = 1234, return 4321
n = -147, return -741

This is the method signature.

public int ReverseInteger(int x) {
        
}

Let’s go for discussion. Write you solution/thought in the comment box.

Share Button

Add Digits

You are given a non-negative integer, add them repeatedly until the result has only one digit.

For example, n = 59.

Process:

n = 5 + 9 = 14

n = 1 + 4 = 5

You should return 5.

Solution:
Continue reading Add Digits

Share Button