Climbing Stairs

https://leetcode.com/problems/climbing-stairs/discuss/155645/Jave-Soulution

package test4;

/**
* ClimbingStairs Problem
* @author asharda
*
*/
public class ClimbingStairs {

public static void main(String[] args) {

ClimbingStairs s=new ClimbingStairs();
System.out.println(s.climbStairs(4));
//s.climbStairs(4);
}

public int climbStairs(int n) {

int result = 1, temp;
int a = 1;
for (int i = 1; i < n; i++) {
temp = result;
result += a;
a = temp;

}

return result;
}

}

Advertisements
Posted in Uncategorized | Leave a comment

Dequeue in Python

”’
Created on Aug 11, 2018

@author: asharda
”’
import collections
deq=collections.deque([“Mon”,”Tue”,”Wed”])
deq.append(“Thu”)
deq.appendleft(“Sun”)
print(deq)
deq.pop()
print(deq)
deq.reverse()
print(deq)

Posted in Uncategorized | Leave a comment

LinkedLists in Python

”’
Created on Aug 3, 2018

@author: asharda
”’

class node:
def __init__(self,data=None):
self.data=data
self.nextVal=None

n1=node(1)
n2=node(2)
n3=node(3)
n1.nextVal=n2
n2.nextVal=n3
t1=n1
while t1:
print(t1.data)
t1=t1.nextVal

Posted in Python, Uncategorized | Leave a comment

Using sets in Python

”’
Created on Aug 3, 2018

@author: asharda
”’
days=set([“Mon”,”Tue”,”Wed”,”Thu”,”Fri”])
daysA=set([“Tue”,”Wed”,”Thu”,”Fri”])
print(days-daysA)
print(days & daysA)
for d in days:
print(d)
days.add(“Sun”)

Posted in Python, Uncategorized | Leave a comment

Excellent Recursion Article

Posted in Uncategorized | Leave a comment

Rotate Array

/**Program to rotate array
* @author asharda
*
*/
public class RotateArray {

/**
* @param args
*/
public static void main(String[] args) {

RotateArray r =new RotateArray();
int arr[]= {1,2,3,4,5,6,7};
int b[]=r.rotateArray(arr,2);
for(int i:b)
{
System.out.println(i);
}

}

/**
* Rotate Array
* @param arr
* @param number
* @return
*/

public int[] rotateArray(int arr[],int number)
{

for(int i=0;i<number;i++)
{
int []b=arr.clone();
for(int j=0;j<arr.length;j++)
{
if(j==arr.length-1)
{
arr[j]=b[0];
}
else
{
arr[j]=b[j+1];
}
}//end of j
}//end of i

return arr;
}
}

Posted in Data Structures, Java, Java 8 | Leave a comment

Different Array Operations in Python

”’
Created on Jul 25, 2018

@author: asharda
”’

import array

def printArr(arr):
for i in range(0,len(arr)):
print(arr[i])

arr=array.array(‘i’,[1,2,3,4])
#print(arr)

printArr(arr)
print(“After Reverse”)
arr.reverse()
printArr(arr)

print(“After Append”)
arr.reverse()
arr.append(5)
printArr(arr)

print(“After Insert”)
arr.insert(0, 0)
printArr(arr)

Posted in Python, Uncategorized | Tagged | Leave a comment