current position:Home>Java lesson 29 136 The number 1189 appears only once Maximum number of balloons
Java lesson 29 136 The number 1189 appears only once Maximum number of balloons
2022-04-29 06:11:19【Yake1965】
The first 29 course
136. A number that appears only once
Leetcode
Knowledge point : map, HashMap, put, get, keySet, Map.Entry, entrySet
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (Integer i : nums) {
map.put(i, map.get(i) == null ? 1 : map.get(i) + 1);
}
// for (Integer i: map.keySet()) if (map.get(i) == 1) return i;
for (Map.Entry<Integer, Integer> m : map.entrySet()) if (m.getValue() == 1) return m.getKey();
return -1; // can't find it.
// Method 2 : An operation
int res = 0;
for (Integer i : nums) res ^= i;
return res;
}
}
1189. “ balloon ” Maximum number of
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
# d = {c: 0 for c in 'balon'}
# for c in text:
# if c in 'balon':
# d[c] += 1
# return min(d[c]//2 if c in 'ol' else d[c] for c in d)
c = Counter(x for x in text if x in 'balon')
c['l'] //= 2
c['o'] //= 2
return min(c.values()) if len(c) == 5 else 0
class Solution {
public int maxNumberOfBalloons(String text) {
int[] cnt = new int[5];
for (char ch : text.toCharArray()){
if (ch == 'a') cnt[0]++;
if (ch == 'b') cnt[1]++;
if (ch == 'l') cnt[2]++;
if (ch == 'o') cnt[3]++;
if (ch == 'n') cnt[4]++;
}
// for(int i = 0; i < text.length(); i++) {
// switch(text.charAt(i)){
// case 'a': cnt[0]++; break;
// case 'b': cnt[1]++; break;
// case 'l': cnt[2]++; break;
// case 'o': cnt[3]++; break;
// case 'n': cnt[4]++; //break;
// // default: continue;
// }
// }
cnt[2] /= 2;
cnt[3] /= 2;
int ans = cnt[0];
for (int i = 0; i < 5; i++) ans = Math.min(ans, cnt[i]);
return ans;
}
}
class Solution {
public int maxNumberOfBalloons(String text) {
Map<Character, Integer> map = new HashMap<>();
for (char c : text.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
map.put('o', map.getOrDefault('o', 0)/2);
map.put('l', map.getOrDefault('l', 0)/2);
int ans = map.getOrDefault('a', 0);
for (char c : "balon".toCharArray())
ans = Math.min(ans, map.getOrDefault(c, 0));
return ans;
}
}
copyright notice
author[Yake1965],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261000558787.html
The sidebar is recommended
- Singleton mode - front end design mode
- QT sets that child elements do not inherit the parent style
- Using react to realize comment function
- Front end Vue template
- Front end vuejs set sail
- Front end Vue data binding and MVVM
- Vue data proxy and hijacking in front end
- Vue style of front end
- Front end Vue event handling
- Principle of Vue monitoring data change in front end
guess what you like
Vue calculation attribute and monitoring attribute of the front end
Front end Vue conditional rendering and list rendering
The front-end Vue collects form data
Vue built-in instruction of front end
Unknown HttpClient factory netty at org. openqa. selenium. remote...
Where is the user information obtained by the front end
What is the gutter principle of Ant Design Vue a-row
Element form account password
CSS to add horizontal lines around the text
Solution to vscode Vue project always reporting space or semicolon warning
Random recommended
- [entry DIARY 7 - ref of react]
- React - higher order functions and controlled components
- Calling setcookie in Ajax fails without any error in the IDE
- Differences, advantages and disadvantages between Ajax and Axios
- Choose Java or front-end for training and learning
- Simple sorting of JavaScript deep copy and shallow copy
- Analysis of event loop mechanism in JavaScript
- Epidemic prevention and control system of front and rear end separation based on Vue + nodejs
- Implementation of enterprise personnel management system by separating the front and back ends of springboot + Vue
- Vue difference between two arrays
- Vue openlayer add wind farm effect
- How to get child nodes of elements in HTML
- The file code with HTML suffix of vscode is not highlighted
- Use the table in Vue element admin in Vue admin template?
- [JavaScript] convert numerical value to numerical value
- Differences, advantages and disadvantages between Ajax and Axios
- Quietly tell you: the wrong place in react18 document
- The new smart spirit 1 starts in Asia! Breaking 100 in 6.7 seconds + frameless door, 190000 pre-sale
- Analysis of event loop mechanism in JavaScript
- Error occurred when installing vant on webstorm
- With the blessing of tinnove system, Changan uni-v does not have its own table
- Judging the application direction of small flat wire motor from SM 4x4 driverless technology verification vehicle
- Vue3 + nuxt3 build SSR website application, and realize server-side rendering from 0 to 1 without secret Fen sharing
- Vue3 + nuxt3 build SSR website application, and realize server-side rendering from 0 to 1 without secret Fen sharing
- Vue3 + nuxt3 build SSR website application, and realize server-side rendering from 0 to 1 without secret Fen sharing
- React bread crumbs
- HTTP network protocol
- Vue3 + nuxt3 build SSR website application, and realize server-side rendering from 0 to 1 without secret Fen sharing
- Vue3 + nuxt3 build SSR website application, and realize server-side rendering from 0 to 1 without secret Fen sharing
- Use of vuex
- Element UI El table modify input value view not updated
- Getsnapshotbeforeupdate example of react
- Recently, the MySQL server on the cloud was hacked. It took 20 minutes to modify the password after being mercilessly encrypted. This time I'll be hacked with your last name
- Java project: sporting goods Mall (java + springboot + jsp + HTML + Maven + MySQL)
- Java project: shared study room reservation management system (java + springboot + thymeleaf + HTML + Maven + MySQL)
- Java project: sporting goods Mall (java + springboot + jsp + HTML + Maven + MySQL)
- Java project: shared study room reservation management system (java + springboot + thymeleaf + HTML + Maven + MySQL)
- Sennheiser momentum true wireless 3 true wireless headphones will be available on May 10
- Coffeescript 2.7.0 release, JavaScript translation language
- Im instant messaging development: load balancing of high-performance HTTP server