Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.


Note:

All letters in hexadecimal (a-f) must be in lowercase.
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.



Example 1:

Input:
26

Output:
"1a"



Example 2:

Input:
-1

Output:
"ffffffff"

Difficulty: EASY
Starter code: class Solution:
    def toHex(self, num: int) -> str:
        

Solutions:
class Solution:

	def toHex(self, num):
		if num == 0:
			return '0'
		(res, n) = ([], 0)
		nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
		while n < 8 and num != 0:
			res.insert(0, nums[num % 16])
			num = num // 16
			n += 1
		s = ''
		for i in res:
			s += i
		return s


class Solution:

	def toHex(self, num):
		if num < 0:
			num = 2 ** 32 + num
		return hex(num)[2:]


class Solution:

	def toHex(self, num):
		return '%x' % (num & 4294967295)


class Solution:

	def toHex(self, num):
		ans = []
		dic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
		if num == 0:
			return '0'
		if num < 0:
			num = num + 2 ** 32
		while num > 0:
			digit = num % 16
			num //= 16
			if digit > 9 and digit < 16:
				digit = dic[digit]
			else:
				digit = str(digit)
			ans.append(digit)
		return ''.join(ans[::-1])


class Solution:

	def toHex(self, num):
		if num < 0:
			num = 2 ** 32 - 1 - ~num
		if num == 0:
			return '0'
		s = ''
		dic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
		for i in range(8, -1, -1):
			if int(num / 16 ** i) > 0:
				if int(num / 16 ** i) < 10:
					s = s + str(int(num / 16 ** i))
				else:
					s = s + dic[int(num / 16 ** i)]
				num = num - int(num / 16 ** i) * 16 ** i
			elif len(s) > 0:
				s = s + '0'
		return s


class Solution:

	def toHex(self, num):
		if num == 0:
			return '0'
		check = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
		save = []
		num = (num + 4294967296) % 4294967296
		while num > 0:
			save.append(check[num % 16])
			num //= 16
		return ''.join(save)[::-1]


class Solution:

	def toHex(self, num):

		def toHexUnsigned(num):
			num_hex = []
			while num // 16 > 0:
				digit = num % 16
				if digit >= 10:
					num_hex.append(chr(digit - 10 + ord('a')))
				else:
					num_hex.append(str(digit))
				num = num // 16
			if num >= 10:
				num_hex.append(chr(num - 10 + ord('a')))
			elif num > 0:
				num_hex.append(str(num))
			return ''.join(num_hex[::-1])
		if num == 0:
			return '0'
		if num < 0:
			return toHexUnsigned(2 ** 32 + num)
		else:
			return toHexUnsigned(num)


class Solution(object):

	def toHex(self, num):
		if num == 0:
			return '0'
		if num < 0:
			num = num + 2 ** 32
		hexa = '0123456789abcdef'
		r = ''
		while num:
			r = hexa[num % 16] + r
			num = num // 16
		return r


class Solution:

	def toHex(self, num):
		if num == 0:
			return '0'
		if num < 0:
			num = num + 2 ** 32
		mapping = '0123456789abcdef'
		output = ''
		while num > 0:
			print(num)
			remainder = num % 16
			output = mapping[remainder] + output
			num = math.floor(num / 16)
		return output


class Solution:

	def toHex(self, num):
		if num == 0:
			return '0'
		mp = '0123456789abcdef'
		ans = ''
		for i in range(8):
			n = num % 16
			c = mp[n]
			ans = c + ans
			num = num >> 4
		return ans.lstrip('0')


class Solution:

	def toHex(self, num):
		if num == 0:
			return '0'
		hexKey = '0123456789abcdef'
		ans = ''
		for _ in range(8):
			ans = hexKey[num & 15] + ans
			num >>= 4
		return ans.lstrip('0')