MySubmissions
Question
TotalAccepted: 92780 Total Submissions: 310153 Difficulty: Easy
Given two sorted integer arrays nums1 and nums2,merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that isgreater or equal to m + n) to hold additionalelements from nums2. The number of elements initialized in nums1and nums2 are m and n respectively.
Subscribe to see which companies asked this question
class Solution {
public:
void merge(vector
if(nums1.empty()) //哈哈,改了两次可以了
{
if(nums2.empty())return;
for(int k=0;k nums1[k]=nums2[k]; } if(!nums1.empty()&&nums2.empty())return; int i; int x=m-1; int y=n-1; for(i=m+n-1;i>=0;i--) { if(nums1[x]>nums2[y]) { nums1[i]=nums1[x]; x--; } else { nums1[i]=nums2[y]; y--; } if(x==-1) //主要改了这,还有下面的if,因为需要考虑到,遍历到某个列表开头没有节点的情况 { for(int p=0;p<=y;p++) { nums1[p]=nums2[p]; } break; } if(y==-1) { break; } } } }; TotalAccepted: 98605 Total Submissions: 324359 Difficulty: Easy Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along theshortest path from the root node down to the nearest leaf node. class Solution { public: int minDepth(TreeNode *root) { if (root == NULL) return 0; if (root->left == NULL) return minDepth(root->right) + 1; else if (root->right ==NULL) return minDepth(root->left) + 1; else return min(minDepth(root->left), minDepth(root->right)) + 1; } }; Total Accepted: 50318 Total Submissions: 170599 Difficulty: Easy Givenan array of integers and an integer k,find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jisat most k. class Solution { public: bool containsNearbyDuplicate(vector if(nums.empty()) return false; int size=nums.size(); map int i; for( i=0;i { if(m[nums[i]]==1)return true; else m[nums[i]]=1; if(i==size-1)break; } if(i==size-1)return false; m[nums[0]]=0; for(int j=i;j { if(m[nums[j]]==1)return true; else m[nums[j]]=1; // if(i==size-1)break; m[nums[j-k]]=0; // 额,开始时候,j-k-1,有问题,改了之后可以了,复杂度n,还不错。。。。 } return false; } }; MySubmissions Question Total Accepted: 47733 Total Submissions: 132069 Difficulty: Easy Writea program to check whether a given number is an ugly number. Uglynumbers are positive numbers whose prime factors only include Notethat class Solution { public: bool isUgly(int num) { if(num<=0)return false; while(num%2==0) num=num>>1; while(num%3==0) num /= 3; while(num%5==0) num /= 5; if(num==1)return true; else return false; } }; TotalAccepted: 32685 Total Submissions: 111070 Difficulty: Easy Find the total area covered by two rectilinear rectanglesin a 2D plane. Each rectangle is defined by its bottom left corner andtop right corner as shown in the figure. Assume that the total area is never beyond the maximumpossible value of int. Credits: Subscribe to see which companies asked this question class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){ //开始时候想分1个点交叉,2个点交叉//...4个点交叉讨论,发现不行,后来查到下面这个方法 int area= (C-A)*(D-B)+(G-E)*(H-F); if (E > C || G < A || F > D || H < B) return area; int overLapArea=0; int h1 = max(A, E); //先计算重合的横坐标 int h2 = min(C, G); int h = h2 - h1; int v1 = max(B, F); //计算重合的纵坐标 int v2 = min(D, H); int v = v2 - v1; overLapArea=h*v; area -= overLapArea; return area; } }; MySubmissions Question TotalAccepted: 56594 Total Submissions: 194167 Difficulty: Easy Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binaryas 00000010100101000001111010011100), return 964176192 (representedin binary as00111001011110000010100101000000). Follow up: Related problem: Reverse Integer Credits: Subscribe to see which companies asked this question //其实可以使用数组做,不过想到空间利用率,还是直接位操作比较好 class Solution { public: uint32_t reverseBits(uint32_t n) { //哈哈,一次通过,如果原数第i位是1,则第31-i位置是1 uint32_t temp; for(int i=0;i<32;i++) { if((1<
temp += 1<<(31-i); } return temp; } }; My Submissions Question Total Accepted: 99325 Total Submissions: 342842 Difficulty: Easy Given a string containingjust the characters The brackets must close inthe correct order, Subscribe to see which companies asked this question class Solution { public: bool isValid(string s) { stack int size=(int)s.length(); for(int i=0;i { if((s[i]=='(')||(s[i]=='[')||(s[i]=='{')) stk.push(s[i]); else { if(stk.empty())return false; char temp=stk.top(); if((temp=='('&&s[i]==')')||(temp=='['&&s[i]==']')||(temp=='{'&&s[i]=='}')) //if((temp=='('&&s[i]==')')||(temp=='['||s[i]==']')||(temp=='{'&&s[i]=='}')) //尼玛,这个小马虎的错找了半个小时!!!!气死了 stk.pop(); else return false; } } if(stk.empty()) return true; else return false; } }; My Submissions Question Total Accepted: 86714 Total Submissions: 299284 Difficulty: Easy Given a string s consists of upper/lower-case alphabetsand empty space characters If the last word does notexist, return 0. Note: A word is defined as a character sequence consists of non-spacecharacters only. For example, Subscribe to see which companies asked this question //我知道可以用循环到最后做(比如统计空格数,然后再数到空格位置,再数长度,很复杂,因此直接不用) //我想到了倒着做的方法会更好 class Solution { public: int lengthOfLastWord(string s) { //哈哈哈,一次通过!! if(s.empty())return 0; string temp; int size=s.size(); int count=0; int flagConsistSpace=1; for(int i=size-1; i>=0;i--) { if(s[i]==' '&&flagConsistSpace) continue; if(s[i]!=' ')flagConsistSpace=0; if(!flagConsistSpace&&s[i]==' ') break; count++; } return count; } }; My Submissions Question TotalAccepted: 50240 Total Submissions: 173192 Difficulty: Easy Given two strings s and t,determine if they are isomorphic. Two strings are isomorphic if the characters in s canbe replaced to get t. All occurrences of a character must be replaced with another characterwhile preserving the order of characters. No two characters may map to the samecharacter but a character may map to itself. For example, Given "foo", "bar", return false. Given "paper", "title", return true. Note: class Solution { public: bool isIsomorphic(string s, string t) { if(s.empty()&&t.empty())return true; int Ssize=s.size(); int Tsize=t.size(); if(Ssize!=Tsize)return false; int i; int j; map map for(i=0;i map it=m.find(s[i]); if(it==m.end()) { if(show[t[i]]==1)returnfalse;//此处改了改,表明不能让两个及以上的s[i]指向相同的s[j] m[s[i]]=t[i]; show[t[i]]=1; } else if(m[s[i]]!=t[i])returnfalse;//同样元素对应到不同的值,也错 else continue; } return true; } }; My Submissions Question Total Accepted: 22552 Total Submissions: 77861 Difficulty: Easy You are playing thefollowing Bulls and Cows game with your friend: You write down a number and ask yourfriend to guess what the number is. Each time your friend makes a guess, youprovide a hint that indicates how many digits in said guess match your secretnumber exactly in both digit and position (called "bulls") and howmany digits match the secret number but locate in the wrong position (called"cows"). Your friend will use successive guesses and hints toeventually derive the secret number. For example: Hint: Write a function to returna hint according to the secret number and friend's guess, use Please note that bothsecret number and friend's guess may contain duplicate digits, for example: In this case, the 1st You may assume that thesecret number and your friend's guess only contain digits, and their lengthsare always equal. Credits: Subscribe to see which companies asked this question class Solution { public: string getHint(string secret, string guess) { string ret; if(secret.empty()||guess.empty())return ret; //哈哈,就改了一次,就ac了 int size=secret.size(); int AshowTimes[10]={0}; int BshowTimes[10]={0}; int A=0; int B=0; int i;111.Minimum Depth of Binary Tree(BFS)
219. Contains Duplicate II (先判断k个,在判n-k个)
263. Ugly Number(除尽)
2,3, 5
. For example, 6,8
are ugly while 14
is not ugly since it includesanother prime factor 7
.1
is typically treated as an uglynumber.223. Rectangle Area(重合坐标)
Special thanks to @mithmatt for adding this problem, creating the above imageand all test cases.190. Reverse Bits (1<)
If this function is called many times, how would you optimize it?
Special thanks to @ts for adding this problem and creating all testcases.20. Valid Parentheses(见到([{,就入栈)
'('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid."()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.58. Length of Last Word(从尾到头)
' '
, return the length of lastword in the string.
Given s = "Hello World"
,
return 5
.205. Isomorphic Strings(map对应及判断出现过)
Given "egg", "add", return true.
You may assume both s and t havethe same length.2016.3.20
299. Bulls and Cows(数组记录次数)
Secret number: "1807"
Friend's guess: "7810"
1
bull and 3
cows. (The bull is 8
, the cows are 0
, 1
and 7
.)A
to indicate the bulls and B
to indicate the cows. Inthe above example, your function should return "1A3B"
.Secret number: "1123"
Friend's guess: "0111"
1
in friend's guess isa bull, the 2nd or 3rd 1
is a cow, and yourfunction should return "1A1B"
.
Special thanks to @jeantimex for adding this problem and creating all test cases.