平时没事刷刷Leetcode,还办了个年会员。为了自己150刀。为了自己的大脑投资,从不差钱儿。刷刷题能练习coding,此外看一些别人的优秀的答案,能增长见解。大家共同努力,共勉。

十、Google考题(2)

Name:Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.comalice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.  (Note that this rule does not apply for domain names.)

If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com.  (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

solution:

class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> seen = new HashSet();
for (String email : emails) {
int i = email.indexOf('@');
String local = email.substring(0, i);
String rest = email.substring(i);
if (local.contains("+")) {
local = local.substring(0, local.indexOf('+'));
}
// Note: one should escape the specific character '.',
// since it is treated as a regex expression.
local = local.replaceAll("\\.", "");
seen.add(local + rest);
} return seen.size();
}
}

九、Google考题(1)

Name:License Key Formatting

You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.

Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.

Given a non-empty string S and a number K, format the string according to the rules described above

Example 1:

Input: S = "5F3Z-2e-9-w", K = 4

Output: "5F3Z-2E9W"

Explanation: The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.

Example 2:

Input: S = "2-5g-3-J", K = 2

Output: "2-5G-3J"

Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.

Solutions:

mySolution:

class Solution {
public String licenseKeyFormatting(String S, int K) { StringBuilder sb = new StringBuilder(); String s = S.replaceAll("-", "").toUpperCase();
if (s.length() < K) {
return s;
}
int firstGroupLength = s.length() % K == 0 ? K : s.length() % K;
int groupNum = (s.length() - firstGroupLength) / K;
sb.append(s.substring(0, firstGroupLength));
for (int i = 0; i < groupNum; i++) {
sb.append("-");
sb.append(s.substring(firstGroupLength + K * i, firstGroupLength + K * (i + 1)));
}
return sb.toString();
}
}

better solution:

public String licenseKeyFormatting(String S, int K) {
String newStr = S.replaceAll("-","").toUpperCase();
StringBuilder sb = new StringBuilder(); int count = 0; for(int i = newStr.length()-1; i >=0; i--){
sb.append(newStr.charAt(i));
count++;
if(count == K && i != 0){
sb.append("-");
count = 0;
}
}
sb.reverse();
return sb.toString();
}

八、Amazon考题

Given a list of reviews, a list of keywords and an integer k. Find the most popular k keywords in order of most to least frequently mentioned.

The comparison of strings is case-insensitive.

Multiple occurances of a keyword in a review should be considred as a single mention.

If keywords are mentioned an equal number of times in reviews, sort alphabetically.

Example 1:

 1 Input:
2 k = 2
3 keywords = ["anacell", "cetracular", "betacellular"]
4 reviews = [
5 "Anacell provides the best services in the city",
6 "betacellular has awesome services",
7 "Best services provided by anacell, everyone should use anacell",
8 ]
9
10 Output:
11 ["anacell", "betacellular"]
12
13 Explanation:
14 "anacell" is occuring in 2 different reviews and "betacellular" is only occuring in 1 review.

Example 2:

 1 Input:
2 k = 2
3 keywords = ["anacell", "betacellular", "cetracular", "deltacellular", "eurocell"]
4 reviews = [
5 "I love anacell Best services; Best services provided by anacell",
6 "betacellular has great services",
7 "deltacellular provides much better services than betacellular",
8 "cetracular is worse than anacell",
9 "Betacellular is better than deltacellular.",
10 ]
11
12 Output:
13 ["betacellular", "anacell"]
14
15 Explanation:
16 "betacellular" is occuring in 3 different reviews. "anacell" and "deltacellular" are occuring in 2 reviews, but "anacell" is lexicographically smaller.

代码解决方案:

 1 package com.example.demo;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.PriorityQueue;
10 import java.util.Queue;
11 import java.util.Set;
12
13 public class Test02 {
14 public static void main(String[] args) {
15 int k1 = 2;
16 String[] keywords1 = { "anacell", "cetracular", "betacellular" };
17 String[] reviews1 = { "Anacell provides the best services in the city", "betacellular has awesome services",
18 "Best services provided by anacell, everyone should use anacell", };
19 int k2 = 2;
20 String[] keywords2 = { "anacell", "betacellular", "cetracular", "deltacellular", "eurocell" };
21 String[] reviews2 = { "I love anacell Best services; Best services provided by anacell",
22 "betacellular has great services",
23 "deltacellular provides much better services than betacellular",
24 "cetracular is worse than anacell", "Betacellular is better than deltacellular.", };
25 System.out.println(solve(k1, keywords1, reviews1));
26 System.out.println(solve(k2, keywords2, reviews2));
27 }
28
29 private static List<String> solve(int k, String[] keywords, String[] reviews) {
30 List<String> res = new ArrayList<>();
31 Set<String> set = new HashSet<>(Arrays.asList(keywords));
32 Map<String, Integer> map = new HashMap<>();
33 for (String r : reviews) {
34 String[] strs = r.split("\\W");
35 Set<String> added = new HashSet<>();
36 for (String s : strs) {
37 s = s.toLowerCase();
38 if (set.contains(s) && !added.contains(s)) {
39 map.put(s, map.getOrDefault(s, 0) + 1);
40 added.add(s);
41 }
42 }
43 }
44 Queue<Map.Entry<String, Integer>> maxHeap = new PriorityQueue<>(
45 (a, b) -> a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue());
46 maxHeap.addAll(map.entrySet());
47 while (!maxHeap.isEmpty() && k-- > 0) {
48 res.add(maxHeap.poll().getKey());
49 }
50 return res;
51 }
52 }

这道题挺经典的,考察了许多基础知识点。大公司的一些考题,确实能看出技术者的功底。

七、二叉树反转

使用递归的思想:

 1 class Solution {
2 public TreeNode invertTree(TreeNode root) {
3 if (root == null) {
4 return null;
5 }
6 TreeNode right = invertTree(root.right);
7 TreeNode left = invertTree(root.left);
8 root.right = left;
9 root.left = right;
10 return root;
11 }
12 }

六、判断一个数字是否为2的N次方

1 class Solution {
2 public boolean isPowerOfTwo(int n) {
3 if (n == 0) return false;
4 while (n % 2 == 0) n /= 2;
5 return n == 1;
6 }
7 }

上面是第一个方法,大家都能想到。复杂度是O(logN)。不多说

接下来重点是第二个方法,用的是bit的运算。我看完答案后,真是佩服作者的功底。牛逼!!!

1 class Solution {
2 public boolean isPowerOfTwo(int n) {
3 if (n == 0) return false;
4 long x = (long) n;
5 return (x & (-x)) == x;
6 }
7 }

这个方法的复杂度为O(1)。

五、寻找宝藏

You have a map that marks the location of a treasure island. Some of the map area has jagged rocks and dangerous reefs. Other areas are safe to sail in. There are other explorers trying to find the treasure. So you must figure out a shortest route to the treasure island.

Assume the map area is a two dimensional grid, represented by a matrix of characters. You must start from the top-left corner of the map and can move one block up, down, left or right at a time. The treasure island is marked as X in a block of the matrix. X will not be at the top-left corner. Any block with dangerous rocks or reefs will be marked as D. You must not enter dangerous blocks. You cannot leave the map area. Other areas O are safe to sail in. The top-left corner is always safe. Output the minimum number of steps to get to the treasure.

Example:

Input:

[['O', 'O', 'O', 'O'],
['D', 'O', 'D', 'O'],
['O', 'O', 'O', 'O'],
['X', 'D', 'D', 'O']] Output: 5
Explanation: Route is (0, 0), (0, 1), (1, 1), (2, 1), (2, 0), (3, 0) The minimum route takes 5 steps.

这道题目我当时一点思路也没有,后来看到答案之后,debug跟了一遍,有了些思路,以后再遇到,能有一个前进的方向了。

参考答案:

 1 public class Demo{
2 private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
3
4 public static int minSteps(char[][] grid) {
5 Queue<Point> q = new ArrayDeque<>();
6 q.add(new Point(0, 0));
7 grid[0][0] = 'D'; // mark as visited
8 for (int steps = 1; !q.isEmpty(); steps++) {
9 for (int sz = q.size(); sz > 0; sz--) {
10 Point p = q.poll();
11
12 for (int[] dir : DIRS) {
13 int r = p.r + dir[0];
14 int c = p.c + dir[1];
15
16 if (isSafe(grid, r, c)) {
17 if (grid[r][c] == 'X') return steps;
18 grid[r][c] = 'D';
19 q.add(new Point(r, c));
20 }
21 }
22 }
23 }
24 return -1;
25 }
26
27 private static boolean isSafe(char[][] grid, int r, int c) {
28 return r >= 0 && r < grid.length && c >= 0 && c < grid[0].length && grid[r][c] != 'D';
29 }
30
31 private static class Point {
32 int r, c;
33 Point(int r, int c) {
34 this.r = r;
35 this.c = c;
36 }
37 }
38
39 public static void main(String[] args) {
40 char[][] grid = {{'O', 'O', 'O', 'O'},
41 {'D', 'O', 'D', 'O'},
42 {'O', 'O', 'O', 'O'},
43 {'X', 'D', 'D', 'O'}};
44 System.out.println(minSteps(grid));
45 }
46 }

四、Two sum

题目:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

解决方法:
第一种(大多数人都想到这种方法,思路简单,但是复杂度较高):

 1 class Solution {
2
3 public int[] twoSum(int[] nums, int target) {
4 for (int i = 0, len = nums.length; i < len; i++) {
5 for (int j = i + 1; j < len; j++) {
6 if (nums[i] + nums[j] == target) {
7 return new int[] { i, j };
8 }
9 }
10 }
11 return new int[] { 0, 0 };
12 }
13 }

第二种(复杂度最低,思路上绕了个弯子,接着HashMap):

 1 public int[] twoSum(int[] nums, int target) {
2 Map<Integer, Integer> map = new HashMap<>();
3 for (int i = 0; i < nums.length; i++) {
4 int complement = target - nums[i];
5 if (map.containsKey(complement)) {
6 return new int[] { map.get(complement), i };
7 }
8 map.put(nums[i], i);
9 }
10 throw new IllegalArgumentException("No two sum solution");
11 }

 三、LRU实现方式

思路:hashMap + 双向链表(因为双向链表,算法复杂度低)

代码如下:

 1 public class Node<K, V> {
2 Node<K, V> prev;
3 Node<K, V> next;
4 K k;
5 V v;
6
7 public Node(K k, V v) {
8 this.k = k;
9 this.v = v;
10 }
11 }

 1 public class LRUCache<K, V> {
2
3 Node<K, V> head;
4 Node<K, V> tail;
5 HashMap<K, Node<K, V>> map;
6 int capacity;
7
8 public LRUCache(int capacity) {
9 map = new HashMap<K, Node<K, V>>();
10 this.capacity = capacity;
11 }
12
13 public V get(K key) {
14 Node<K, V> node = map.get(key);
15 if (node == null) {
16 return null;
17 }
18 V value = node.v;
19 // move node to tail
20 removeNode(node);
21 offerNode(node);
22 return value;
23 }
24
25 public void put(K key, V value) {
26 if (map.containsKey(key)) {
27 Node<K, V> node = map.get(key);
28 node.v = value;
29
30 // move node to tail
31 removeNode(node);
32 offerNode(node);
33 } else {
34
35 // add to tail
36 Node<K, V> node = new Node<K, V>(key, value);
37 offerNode(node);
38 map.put(key, node);
39
40 if (map.size() > capacity) {
41 map.remove(head.k);
42 removeNode(head);
43 }
44 }
45 }
46
47 private void removeNode(Node<K, V> node) {
48 if (node.prev != null) {
49 node.prev.next = node.next;
50 } else {
51 head = node.next;
52 }
53
54 if (node.next != null) {
55 node.next.prev = node.prev;
56 } else {
57 tail = node.prev;
58 }
59 }
60
61 /*
62 * move node to tail
63 */
64 private void offerNode(Node<K, V> node) {
65 if (tail != null) {
66 tail.next = node;
67 }
68 node.prev = tail;
69 node.next = null;
70 tail = node;
71
72 if (head == null) {
73 head = tail;
74 }
75 }
76
77 }

一、字符串反转

input:“abcde”

output:"edcba"

解决方案:

从后往前,一个个放入到新的char 数组

 1 public String forReverse(String original) {
2 char[] temp = original.toCharArray();
3 StringBuffer sb = new StringBuffer();
4 int tempLenth = temp.length;
5 for (int i = tempLenth - 1; i >= 0; i--) {
6 sb.append(temp[i]);
7 }
8 return sb.toString();
9
10 }

两端同时交换(从两边到中间)

 1         String input = "Hello world";
2 char[] temparray = input.toCharArray();
3 int left, right=0;
4 right = temparray.length-1;
5
6 for (left=0; left < right ; left++ ,right--)
7 {
8 // Swap values of left and right
9 char temp = temparray[left];
10 temparray[left] = temparray[right];
11 temparray[right]=temp;
12 }
13
14 for (char c : temparray)
15 System.out.print(c);

两端同时交换(从中间到两端)

 1 public String forReverse2(String original) {
2 char[] value = original.toCharArray();
3 int count = value.length;
4 int n = count - 1;
5 for (int j = (n - 1) >> 1; j >= 0; j--) {
6 int k = n - j;
7 char cj = value[j];
8 char ck = value[k];
9 value[j] = ck;
10 value[k] = cj;
11
12 }
13 return String.copyValueOf(value);
14 }

二、字母排序

题目:给定一个字符串(里面全是大写字母,从A到Z,可以重复),如“CAEEFDK”。让你从新进行排序。

要求:

①必须以辅音字母开头(元音字母为:A、E、I、O、U,其余的字母全是辅音字母)

②2个辅音字母不可以连续放在一起

③2个元音字母不可以连续放在一起

求,给定一个字符串后,对它进行重排,那么可以有多少种组合?

例子:

给定字符串“AAA”,组合数为0

给定字符串“ABEK”,组合数为4

解题算法如下(我自己想到的算法,正确与否,有兴趣的朋友一起探讨):

class Solution {
public int solution(String S) {
int sum = 1;
// null check
if (S == null || S.length() == 0) {
return 0;
} // Divide into two groups
StringBuilder vowel = new StringBuilder();
StringBuilder consonant = new StringBuilder();
char[] original = S.toCharArray();
for (char temp : original) {
if (temp == 'A' || temp == 'E' || temp == 'I' || temp == 'O' || temp == 'U') {
vowel.append(temp);
} else {
consonant.append(temp);
}
} // All vowels
String vowelS = vowel.toString();
String consonantS = consonant.toString();
if (consonantS.length() == 0) {
return 0;
} // vowelS length
int countVowel = vowelS.length();
// consonantS length
int countconsonant = consonantS.length();
if ((countconsonant - countVowel) != 1 && countVowel != countconsonant) {
return 0;
} int countSamll = countVowel < countconsonant ? countVowel : countconsonant; for (int i = 0; i < countSamll; i++, countconsonant--, countVowel--) {
sum = sum * countconsonant * countVowel;
}
return sum;
}
}

解题思路,我就不说了,代码里写了,不明白的地方,大家相互探讨!如有不正之处,望指点。

Leetcode——练习的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. C# 中的本地函数

    今天我们来聊一聊 C# 中的本地函数.本地函数是从 C# 7.0 开始引入,并在 C# 8.0 和 C# 9.0 中加以完善的. 引入本地函数的原因 我们来看一下微软 C# 语言首席设计师 Mads ...

  2. .Net 开源项目 FreeRedis 实现思路之 - Redis 6.0 客户端缓存技术

    写在开头 FreeRedis 是一款继 CSRedisCore 之后重写的 .NET redis 客户端开源组件,以 MIT 协议开源托管于 github,目前支持 .NET 5..NETCore 2 ...

  3. FL Studio进行侧链的三种方式(上)

    在本系列教程中,我们将学习如何在FL Studio中进行侧链.侧链是一种信号处理技术,通过它我们可以使用一个信号波形的振幅(音量)来控制另一个信号的某些参数.在电子音乐中,例如trance,house ...

  4. 系统兼容软件CrossOver和虚拟机软件,哪个好用?

    想要在Mac上运行Windows软件的方法有很多种,比较常见的有安装双系统以及虚拟机.但是安装双系统会导致一个很大的问题,就是占用了过多的硬盘空间,这样一来会导致可使用的空间减少. 目前来说,大家都不 ...

  5. HDU 4920 Matrix multiplication 题解(内存访问连续性/卡常)

    题目链接 题目大意 多组输入,给你两个n×n的矩阵,要你求他们相乘%3的值 题目思路 这个题目主要是要了解内存访问连续化,要尽量每次访问连续的内存 所以第一种方法会超时,第二种则AC.一种卡常技巧 代 ...

  6. IDEA创建web工程,不用Archetype(超简单)

    Idea不用Archetype创建Web项目 以新建模块为例. 新建Maven项目 不勾选[Create from artchetype],直接Next pom中添加一句话: <artifact ...

  7. MySQL的两种日志类型,redo log,binlog

    文章内容学习:极客时间-林晓彬老师-MySQL实战45讲 整理而得 我们知道MySQL数据库在发生意外宕机的情况下,可以将数据恢复到历史的某个时间点,能实现这个功能依靠的是日志,MySQL提供两种类型 ...

  8. SpringBoot中JPA的学习

    SpringBoot中JPA的学习 准备环境和项目配置 写一下学习JPA的过程,主要是结合之前SpringBoot + Vue的项目和网上的博客学习一下. 首先,需要配置一下maven文件,有这么两个 ...

  9. On-Demand Learning for Deep Image Restoration

    摘要 论文来源:ICCV 2017 之前的缺点:目前的机器学习方法只专注于在特定困难程度的图像损坏(如一定程度的噪声或模糊)情况下进行良好的训练模型. 改进的方法:提出了一种基于深度卷积神经网络的按需 ...

  10. SpringBoot中的Tomcat是如何启动的?

    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>s ...