Posts

Showing posts with the label corner 情况多

Leetcode 443. String Compression

443. String Compression (如果没有把大的逻辑先定下来,写这种题就是找死啊) Easy 437 1387 Favorite Share Given an array of characters, compress it  in-place . The length after compression must always be smaller than or equal to the original array. Every element of the array should be a  character  (not int) of length 1. After you are done  modifying the input array  in-place , return the new length of the array.   Follow up: Could you solve it using only O(1) extra space? class Solution { public:     int compress(vector<char>& chars) {         int i = 0;         const int size = chars.size();         if(size < 2){             return size;         }         int cnt = 1;         for(int j = 1; j < size; j++){             if(chars[j] == chars[j ...

G641. Missing Ranges

Code ( Language :C++) ( Judger :ip-172-31-12-4) Edit class Solution { public : /* * @param nums: a sorted integer array * @param lower: An integer * @param upper: An integer * @return: a list of its missing ranges */ vector < string > findMissingRanges( vector < int > &nums, int lower, int upper) { // write your code here vector < string > res; if (upper < lower){ return res; } const int size = nums.size(); if (size == 0 ){ if (upper != lower){ res.push_back(to_string(lower) + "->" + to_string(upper)); } else { res.push_back(to_string(lower)); } return res; } if (lower > nums[size - 1 ] || upper < nums[ 0 ]){ return res; } if (lower != nums[ 0 ]){ long long pre = nums[ ...

1575. Spring Tour

Description 中文 English There are  n  group children ready to go on a spring tour. The array  a  indicates the number of people in each group. There are no more than four people in each group. There are now several cars. Each car can only take up to four people. The same group of children must sit in the same car, and each car need't be full. Ask how many cars you need to meet the travel needs of your children. 0 \leq n \leq 1e4 0 ≤ n ≤ 1 e 4 1 \leq a[i] \leq 4 1 ≤ a [ i ] ≤ 4 Have you met this question in a real interview?    Yes Problem Correction Example Example1 Input: a = [1,2,3,4] Output: 3 Explanation: At this time, there are 4 people in the fourth group, sitting alone in a car. The first group has 1 person, the third group has 3 people, and just 4 people get for one car. The second group of 2 people has a single car, so at least 3 cars are needed. Example2 Input: a = [1,2,2,2] Output: 2 Explanation...

1305. Integer to English Words

关键点:三位是一个基本单位 Code ( Language :C++) Edit class Solution { public : /** * @param num: a non-negative integer * @return: english words representation */ string numberToWords ( int num) { string res = convertHundred(num % 1000 ); vector < string > v = { "Thousand" , "Million" , "Billion" }; for ( int i = 0 ; i < 3 ; ++i) { num /= 1000 ; res = num % 1000 ? convertHundred(num % 1000 ) + " " + v[i] + " " + res : res; } while (res.back() == ' ' ) res.pop_back(); return res.empty() ? "Zero" : res; } string convertHundred ( int num) { vector < string > v1 = { "" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , ...

414. Divide Two Integers

int divide ( int dividend, int divisor) { // write your code here if (divisor == 0 ){ return dividend > 0 ? INT_MAX : INT_MIN; } if (dividend == 0 ) { return 0 ; } if (divisor == 1 ){ return dividend; } if (dividend == INT_MIN && divisor == -1 ){ return INT_MAX; } //这个特例很奇怪,不容易想到 bool isNeg = ((dividend > 0 && divisor < 0 ) || (dividend < 0 && divisor > 0 )); long long a = abs (( long long )(dividend)); long long b = abs (( long long )(divisor)); long long res = 0 ; while (a >= b){ int shift = 0 ; while (a >= (b << shift)){ shift++; } a -= (b << (shift - 1 )); res += ( long long )( 1 << (shift - 1 )); } ...

641. Missing Ranges

Code ( Language :C++) Edit class Solution { public : /* * @param nums: a sorted integer array * @param lower: An integer * @param upper: An integer * @return: a list of its missing ranges */ vector < string > findMissingRanges( vector < int > &nums, int lower, int upper) { // write your code here //脑子不清楚,一旦自己原来的思路没有理清楚,就出现各种混乱,所以开始就尽量把所有的corner case都搞清楚再下手coding,这里的corner无非就是nums.size() == 0 //另外比大小,出现加几个,减几个比大小时一定要用long long防止overflow。 //这个题还是很典型的。 vector < string > res; int size = nums.size(); if (upper < lower){ return res; } if (size == 0 ){ if (lower < upper){ res.push_back(to_string(lower) + "->" + to_string(upper)); } else if (lower == upper){ res.push_back(to_string(lower)); } return res; ...