# LeetCode
1. Two Sum
```clike=
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
m.put(nums[i], i); /*第一個為值 第二個為值的位置*/
}
for (int i = 0; i < nums.length; ++i) {
int t = target - nums[i];
if (m.containsKey(t) && m.get(t) != i) { /*判斷t的值是否存在於map*/
res[0] = i;
res[1] = m.get(t);
break;
}
}
return res;
}
}
public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
```
3. Longest Substring Without Repeating Characters
```clike=
class Solution {
public int lengthOfLongestSubstring(String s) {
int[] m = new int[256];
Arrays.fill(m, -1); /*將陣列先填滿-1*/
int res = 0, left = -1;
for (int i = 0; i < s.length(); ++i) {
left = Math.max(left, m[s.charAt(i)]);
m[s.charAt(i)] = i;
res = Math.max(res, i - left);
}
return res;
}
}
```
7. Reverse Integer
```clike=
class Solution {
public int reverse(int x) {
long ans=0;
while(x!=0)
{
ans*=10;
ans+=x%10;
x/=10;
}
if((int)ans!=ans) ans=0;
return (int)ans;
}
}
```