Was able to do this from one list where, get the compliment and check from i+1 index forward within the list to see if the value exists in there or not
Python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i, num in enumerate(nums):
compliment = target - num
if compliment in nums[i+1:]:
idx = nums.index(compliment, i+1)
out = [i, idx]
return out
return None
C++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map<int, int> sumMap;
for (int i = 0; i < nums.size(); i++) {
int compliment = target - nums[i];
if (sumMap.find(compliment) != sumMap.end()) {
int compliment_index = sumMap[compliment];
return {compliment_index, i};
}
else {
sumMap[nums[i]] = i;
}
}
return {};
}
};
Set the default cases first. Then got the last digit, and started storing the reverse. Then removed the final digit.
Time complexity: O(log n) because it will run different amount of times based on the number of digits
Space complexity: O(1) because we used a fixed number of variables
class Solution {
public:
bool isPalindrome(int x) {
if (x == 0) {
return true;
}
if (x < 0 || x % 10 == 0) {
return false;
}
if (x < 10) {
return true;
}
int copy = x;
long long reversed = 0;
while (x != 0) {
int digit = x % 10; // last digit
reversed = (reversed * 10) + digit; // store reverse
x /= 10;
}
return reversed == copy;
}
};