12月14日
49.字母异位词分组
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, ArrayList<String>> map = new HashMap<>();
for(String str : strs) {
char[] c = str.toCharArray();
Arrays.sort(c);
String key = String.valueOf(c);
if(!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(str);
}
return new ArrayList<>(map.values());
}
}
12月15日
738.单调递增的数字
class Solution {
public int monotoneIncreasingDigits(int N) {
if(N < 10) {
return N;
}
char[] c = String.valueOf(N).toCharArray();
int idx = -1;
for(int i = c.length-1; i > 0; i --) {
if(c[i-1] > c[i]) {
c[i-1] --;
c[i] = '9';
idx = i;
}
}
if(idx != -1) {
for(int i = idx; i < c.length; i ++) {
c[i] = '9';
}
}
return Integer.parseInt(new String(c));
}
}
12月17日
714.买卖股票的最佳时机含手续费
// 动态规划
class Solution {
public int maxProfit(int[] prices, int fee) {
if(prices.length < 1) {
return 0;
}
int[][] db = new int[prices.length][2];
db[0][0] = 0;
db[0][1] = -1 * prices[0];
for(int i = 1; i < prices.length; i ++) {
//第i天手中无股票的情况
db[i][0] = Math.max(db[i-1][0], db[i-1][1]+prices[i]-fee);
//第i天手中有股票的情况
db[i][1] = Math.max(db[i-1][1], db[i-1][0]-prices[i]);
}
return db[prices.length-1][0];
}
}
/*
贪心
t记录买入的价格,如果买入的价格低于t,则更新t
如果赚钱,则卖出,同时更新t为卖出当天的价格,
如果下一天可以赚更多,则就更改前一天卖为下一天卖
*/
class Solution {
public int maxProfit(int[] prices, int fee) {
if(prices.length < 1) {
return 0;
}
int t = prices[0] + fee;
int ans = 0;
for(int i = 1; i < prices.length; i ++) {
if(prices[i] + fee < t) {
t = prices[i] + fee;
} else if(prices[i] > t) {
ans += prices[i] - t;
t = prices[i];
}
}
return ans;
}
}
290.单词规律
/*
用一个哈希表来存储对应的字母和单词
如果有该字母,但单词不对应,或者有该单词,字母不对应,则返回false
如果都没有,则添加键值对
*/
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] str = s.split(" ");
if(pattern.length() != str.length) {
return false;
}
HashMap<Character, String> map = new HashMap<>();
for(int i = 0; i < str.length; i ++) {
char c = pattern.charAt(i);
if(map.containsKey(c)) {
if(!map.get(c).equals(str[i])) {
return false;
}
} else {
if(map.containsValue(str[i])) {
return false;
}
map.put(c, str[i]);
}
}
return true;
}
}
/*
哈希表使用containsValue查询值比较慢,所以使用两个哈希表分别存储一遍
*/
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] str = s.split(" ");
if(pattern.length() != str.length) {
return false;
}
HashMap<Character, String> map1 = new HashMap<>();
HashMap<String, Character> map2 = new HashMap<>();
for(int i = 0; i < str.length; i ++) {
char c = pattern.charAt(i);
if(map1.containsKey(c) && !map1.get(c).equals(str[i])) {
return false;
}
if(map2.containsKey(str[i]) && !map2.get(str[i]).equals(c)) {
return false;
}
map1.put(c, str[i]);
map2.put(str[i], c);
}
return true;
}
}
12月18日
389.找不同
class Solution {
public char findTheDifference(String s, String t) {
int[] table = new int[26];
for(int i = 0; i < t.length(); i ++) {
table[t.charAt(i) - 'a'] ++;
}
for(int i = 0; i < s.length(); i ++) {
table[s.charAt(i) - 'a'] --;
}
for(int i = 0; i < 26; i ++) {
if(table[i] != 0) {
return (char)('a' + i);
}
}
return ' ';
}
}
12月19日
48.旋转图象
class Solution {
//先上下翻转,再演对角线翻转
public void rotate(int[][] matrix) {
for(int i = 0; i < matrix.length/2; i ++) {
int[] temp = matrix[i];
matrix[i] = matrix[matrix.length-i-1];
matrix[matrix.length-i-1] = temp;
}
for(int i = 0; i < matrix.length; i ++) {
for(int j = 0; j < i; j ++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
}
12月20日
316.去除重复字母
class Solution {
public String removeDuplicateLetters(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i ++) {
Character c = s.charAt(i);
if(stack.contains(c)) {
continue;
}
while(!stack.isEmpty() && stack.peek() > c && s.indexOf(stack.peek(), i) != -1){
stack.pop();
}
stack.push(c);
}
char[] res = new char[stack.size()];
for(int i = res.length-1; i >= 0; i --) {
res[i] = stack.pop();
}
return new String(res);
}
}