1137. N-th Tribonacci Number

1137. N-th Tribonacci Number

class Solution {
public int tribonacci(int n) {
		if (n == 0)
			return 0;
		else if (n == 1)
			return 1;
		else if (n == 2)
			return 1;
		int t0 = 0;
		int t1 = 1;
		int t2 = 1;
		for (int i = 3; i <= n; i++) {
			int tmpT1 = t1;
			int tmpT2 = t2;
			t2 = t0 + t1 + t2;
			t1 = tmpT2;
			t0 = tmpT1;
		}
		return t2;
	}


}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s