Skip to content

Commit 0bbac45

Browse files
authored
add leetcode
1 parent 74ccf8f commit 0bbac45

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: LeetCode-167. Two Sum II - Input array is sorted
3+
permalink: leetcode-167-two-sum-II
4+
date: 2017-09-12 21:10:31
5+
tags: LeetCode
6+
copyright: true
7+
---
8+
9+
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
10+
<!-- more -->
11+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
12+
13+
You may assume that each input would have exactly one solution and you may not use the same element twice.
14+
15+
__Input:__ numbers={2, 7, 11, 15}, target=9
16+
__Output:__ index1=1, index2=2
17+
18+
__思路__
19+
因为数组是有序的,所以通过二分法解决。
20+
21+
__代码__
22+
```
23+
class Solution {
24+
public int[] twoSum(int[] numbers, int target) {
25+
if(numbers==null || numbers.length < 1) return null;
26+
int i=0, j=numbers.length-1;
27+
while(i<j) {
28+
int x = numbers[i] + numbers[j];
29+
if(x<target) {
30+
++i;
31+
} else if(x>target) {
32+
--j;
33+
} else {
34+
return new int[]{i+1, j+1};
35+
}
36+
}
37+
return null;
38+
}
39+
}
40+
```
41+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: LeetCode-205. Isomorphic Strings
3+
permalink: leetcode-205-isomorphic-strings
4+
date: 2017-09-12 21:33:46
5+
tags: LeetCode
6+
copyright: true
7+
---
8+
9+
Given two strings s and t, determine if they are isomorphic.
10+
<!-- more -->
11+
Two strings are isomorphic if the characters in s can be replaced to get t.
12+
13+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
14+
__For example,__
15+
Given `"egg", "add"`, return `true`.
16+
Given `"foo", "bar"`, return `false`.
17+
Given `"paper", "title"`, return `true`.
18+
19+
__Note:__
20+
You may assume both s and t have the same length.
21+
22+
__代码__
23+
```
24+
public boolean isIsomorphic(String s, String t) {
25+
if (s == null || t == null || s.length() != s.length()) {
26+
return false;
27+
}
28+
HashMap<Character,Character> m1 = new HashMap<Character,Character>();
29+
HashMap<Character,Character> m2 = new HashMap<Character,Character>();
30+
for (int i = 0; i < s.length(); i++) {
31+
if (!m1.containsKey(s.charAt(i))) {
32+
if (m2.containsKey(t.charAt(i))) return false;
33+
m1.put(s.charAt(i), t.charAt(i));
34+
m2.put(t.charAt(i), s.charAt(i));
35+
} else {
36+
if (m1.get(s.charAt(i)) != t.charAt(i)) return false;
37+
}
38+
}
39+
return true;
40+
}
41+
```

0 commit comments

Comments
 (0)