387. First Unique Character in a String

387. First Unique Character in a String

Problems

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Solution

遍历,数组统计记录出现次数。
如果数组未记录过,则将其index添加进列表中保存。

遍历列表,如果数组统计结果为1,则返回对应的index。
否则返回-1。

Code 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int firstUniqChar(String s) {
List<Integer> arr = new ArrayList<>();
int[] bin = new int[26];

for(int i = 0; i < s.length(); i++){
if(bin[s.charAt(i) - 'a'] == 0) arr.add(i);
bin[s.charAt(i) - 'a']++;
}

for(int i = 0; i < arr.size(); i++){
if(bin[s.charAt(arr.get(i)) - 'a'] == 1) return arr.get(i);
}

return -1;
}
}

Solution

遍历,建立哈希表,记录出现次数。
再次遍历,如果出现次数为1,则返回下标。

Code 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int firstUniqChar(String s) {
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for ( int i = 0; i < s.length(); i++ ){
char curChar = s.charAt(i);
if ( !map.containsKey(curChar) ){
map.put(curChar, 1);
}
else{
map.put(curChar, map.get(curChar)+1);
}
}

for ( int i = 0; i < s.length(); i++ ){
char curChar = s.charAt(i);
if ( map.get(curChar) == 1 ){
return i;
}
}
return -1;
}
}

118. Pascal's Triangle

Given an integer numRows, return the first numRows of Pascal’s triangle.

动态规划,直接按照杨辉三角形的定义计算。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> generate(int numRows) {
ArrayList<List<Integer>> ans = new ArrayList<List<Integer>>(numRows);

for (int i = 0; i < numRows ; i++){
List<Integer> arr = new ArrayList<Integer>(i+1);

for (int j = 0; j <= i; j++){
if ( j == 0 || j == i ){
arr.add(1);
}
else{
arr.add(ans.get(i-1).get(j-1)+ans.get(i-1).get(j));
}
}
ans.add(arr);
}
return ans;
}
}

11. Container With Most Water

问题
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.

双指针在首尾,二者容量取决于两者中较小的一个。
贪心算法,保留两个指针上较大的元素,移动较小一边的指针。
由于指针移动时距离只会减小,因此当新的元素比上一个更大时才有可能比之前的容量更大。
遍历一次找到最大容量。
时间复杂度:O(n)

感觉这个移动有点博弈论的味了,每次都移动自己最差的一边,虽然可能变得更差,但是总比不动(或者减小)强,动最差的部分可能找到更好的结果,但是动另一边总会更差或者不变,兄弟们,这不是题,这是人生,逃离舒适圈!!(这解释我觉得无敌了,哈哈哈)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public int maxArea(int[] height) {
int best = 0;

int i = 0;
int j = height.length - 1;

while ( i < j ){
int product = 0;
if (height[i] < height[j]){
product = height[i] * ( j -i );
i++;
}
else{
product = height[j] * ( j -i );
j--;
}
if ( product > best){
best = product;
}
}
return best;
}
}

344. Reverse String

问题简述
Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

双指针,同时更新并交换两个数值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public void reverseString(char[] s) {
int i = 0;
int j = s.length - 1;

while( i < j ) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;

i++;
j--;
}
}
}

167. Two Sum II - Input Array Is Sorted

问题描述
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.

Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

由于是有序数列,因此可以采用双指针。
左右两侧和不等于目标时,根据大小结果移动左右指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int[] twoSum(int[] numbers, int target) {
int i = 0;
int j = numbers.length - 1;
int[] ans = new int[2];

while( i < j ){
int diff = target - numbers[j];
if ( diff == numbers[i]){
ans[0] = i+1;
ans[1] = j+1;
return ans;
}
else if ( diff < numbers[i] ) {
j--;
}
else{
i++;
}
}
return ans;
}
}