[1.Two sum](https://leetcode.com/problems/two-sum/description/)
面試過程:
Interviewer:Hello, I am your interviewer, now I have a question for you, please provide your solution.
In a database context, the need to find two data records whose specific attributes sum up to a target value is often required for querying and data analysis purposes. This involves searching for two records in a dataset where the sum of particular attributes matches a predefined target value. Such queries and analyses are commonly performed in various database-driven applications, allowing users to extract meaningful insights or perform specific operations based on data criteria.
Interviewee: OK i think this is a two sum ploblem.
I need to two for loop .
the first for loop : from i to length number
the second for loop:from i+1 to length number
if number i + number j == target
return i,j
```from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1 , len(nums)):
if(nums[i] + nums[j]) == target:
return [i,j]
return []
```
Interviewer: OK! How to reduce execution time?
Interviewee: i think i need to use dictionary to store number and index. and one for loop to iterate through each element in the list of integers nums.
now i start to coding.
```
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_dict = {}
for i in range(len(nums)):
num1 = nums[i]
num2 = target - num1
if num2 in num_dict:
return [num_dict[num2], i]
num_dict[num1] = i
```
[9.Palindrome](https://leetcode.com/problems/palindrome-number/description/)
面試官: 你好,從我手上這份簡歷,我感受到您對程式設計的熱情和專業,但在我們談及工作內容前我想幫公司同仁先認識你在程式開發的想法和風格」
接下來我想問妳幾個問題:字串是對稱的,即從左到右和從右到左閱讀都是相同的。例如,"radar" 和 "deified" 都具有這個特殊性質。字串是由相同的字符組成,但字符的順序可以不同。例如,"abb" 和 "bab" 都具有這個特殊性質。
面試者: 先將他轉換為字串 接著用一個result儲存
如果他們一樣 他就是回文
面試官: 有沒有辦法在沒有使用字串的情況完成
面試者: 可以 因為X是整數 所以只要將他一直除10 就能將它倒放回來
```
class Solution:
def isPalindrome(self, x: int) -> bool:
num = 0
a = x
while(a > 0):
temp = a % 10
num = num * 10 + temp
a = int(a / 10)
if x == num and x >= 0:
return True
else:
return False
```
[7. reverse integer](https://leetcode.com/problems/reverse-integer/description/)
面試官: OK 那你有辦法在數字有正負號時將他反轉嗎?
面試者: 先將整數轉換為字串 若轉換後最後一個字為'-' 則將他加入到字串最前面
再將字串轉為整數判斷有沒有超過範圍 即可獲得答案
```
class Solution:
def reverse(self, x: int) -> int:
s = str(x)
s = s[::-1]
if s[-1] == '-':
s = '-' + s[:-1]
ans = int(s)
if ans > 2**31-1 or ans < -2**31:
return 0
return ans
```
# 初步檢討
* 一開始講想法的時候要表達更明確一點
* 語句要通順
* 英文發音有很大進步空間
* 使用每個方式應該要更清楚解釋給interviewer了解為什麼
* 沒有做好 REACTO
# 改善方法
* 對程式需要更加理解才能在當下表達的順暢
* 多練習英文口說與聽力
* 多看幾次影片將REACTO的流程學起來
# 第二次作業
[【LeetCode】 136. Single Number](/6sYq2tGiQWW85WU3pbj9JQ)
>video
### 面試過程
#### Interviewer:「嘿,你知道嗎,有一個題目 %%,就像是在大雜燴裡找出不一樣的食材一樣。假設你有一大堆水果,但有一個是唯一的,我需要找到一個聰明的方法來區分這個獨特的水果。」
Interviwee:你的意思是 我要從整堆水果裡挑出一個只有一個的東西嗎
#### Interviewer:對的
Interviwee:我以一個例子來說 假設一堆水果為[西瓜,西瓜,草莓,蘋果,草莓] 那麼我就找個唯一一個烙單的 :蘋果]
我打算用一個hash map 若水果出現就將它加入到hash table裡,如果他又出現一次就將原本在hash table裡面的拿掉,最後留在hash_table裡面的就是我們要找的
接下來我開始寫CODE
```c=
class Solution {
public:
int singleNumber(std::vector<int>& nums) {
std::unordered_map<int, int> hash; ##這裡創建了一個名為 hash 的無序映射 (unordered map),用於存儲數字及其出現的數。
for (int i = 0; i < nums.size(); i++) {
if (hash.find(nums[i]) == hash.end()) {
hash[nums[i]] = 1;
} else {
hash[nums[i]]++;
}
}
for (std::unordered_map<int, int>::iterator it = hash.begin(); it != hash.end(); it++) {
if (it->second == 1) {
return it->first;
}
return -1;
}
};
```
#### Interviewer:很好 但我覺得將物品加入hash table後又刪除造成程式碼有點長,有沒有什麼方法能夠縮短妳的程式?
Interviewee:有 我想使用XOR 因為
A XOR 0 = A
A XOR A = 0
```c=
class Solution {
public:
int singleNumber(vector<int>& nums) {
int result = 0;
for (int i = 0; i < nums.size(); i++) {
result ^= nums[i];
}
return result;
}
};
```