LeetCode
https://leetcode.com/dangwei_90/
就是那个党伟
凡心所向,素履所往。
展开
-
1475. Final Prices With a Special Discount in a Shop
class Solution {public: vector<int> finalPrices(vector<int>& prices) { vector<int> ret = prices; for(int n = 0; n < ret.size(); n++) { for (int m = n+1; m < ret.size(); m++) { if.原创 2020-08-01 16:31:13 · 212 阅读 · 0 评论 -
1480. Running Sum of 1d Array
class Solution {public: vector<int> runningSum(vector<int>& nums) { vector<int> ret = nums; int total = 0; for(int n = 0; n < ret.size(); n++) { total = total + ret[n]; ret[n] =.原创 2020-08-01 16:29:04 · 134 阅读 · 1 评论 -
1486. XOR Operation in an Array
class Solution {public: int xorOperation(int n, int start) { int nret = start; for (int m = 1; m < n; m++) { nret = nret ^ (start + 2 * m); } return nret; }};原创 2020-08-01 16:28:21 · 208 阅读 · 0 评论 -
1491. Average Salary Excluding the Minimum and Maximum Salary
class Solution {public: double average(vector<int>& salary) { vector<int> salary_date = salary; sort(salary_date.begin(), salary_date.end()); double average_salary = 0; for (int n = 1; n < salary_dat.原创 2020-08-01 16:27:31 · 192 阅读 · 0 评论 -
1496. Path Crossing
typedef pair<int, int> step_type;class Solution {public: bool isPathCrossing(string path) { int x = 0; int y = 0; vector<step_type> step; step_type default_step(0, 0); step.push_back(default_step).原创 2020-08-01 16:26:33 · 226 阅读 · 0 评论 -
1502. Can Make Arithmetic Progression From Sequence
class Solution {public: bool canMakeArithmeticProgression(vector<int>& arr) { vector<int> date = arr; sort(date.begin(), date.end()); int dif = date[1] - date[0]; for (int n = 1; n < date.si.原创 2020-08-01 16:25:40 · 135 阅读 · 0 评论 -
1512. Number of Good Pairs
class Solution {public: int numIdenticalPairs(vector<int>& nums) { int pairs_num = 0; for (int n = 0; n < nums.size(); n++) { for(int m = n+1; m < nums.size(); m++) { if (nums[n] == nums[m]).原创 2020-08-01 16:24:54 · 216 阅读 · 0 评论 -
1518. Water Bottles
class Solution {public: int numWaterBottles(int numBottles, int numExchange) { int drink_num = numBottles; while(numBottles >= numExchange) { drink_num = drink_num + numBottles/numExchange; numBottles = numBo...原创 2020-08-01 16:23:54 · 187 阅读 · 0 评论 -
1523. Count Odd Numbers in an Interval Range
class Solution {public: int countOdds(int low, int high) { int low_odd = low % 2; int high_odd = high % 2; if (low_odd + high_odd == 2) { return low_odd + high_odd + (high-low)/2 - 1; } else { r.原创 2020-08-01 16:22:52 · 246 阅读 · 0 评论 -
1528. Shuffle String
class Solution {public: string restoreString(string s, vector<int>& indices) { string str = s; for(int n = 0; n < indices.size(); n++) { str[indices[n]] = s[n]; } return str; }};原创 2020-08-01 16:21:27 · 138 阅读 · 0 评论 -
1108. Defanging an IP Address
class Solution {public: string defangIPaddr(string address) { int nfind = 0; while(nfind != -1) { nfind = address.find("."); if (nfind != -1) { ...原创 2020-03-29 11:02:29 · 195 阅读 · 0 评论 -
1. Two Sum
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> twoS; for(int n = 0; n < nums.size(); n++) { f...原创 2019-09-21 12:30:34 · 94 阅读 · 0 评论 -
100. Same Tree
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/cla...原创 2019-09-21 12:32:28 · 130 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/cla...原创 2019-09-21 12:34:52 · 93 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
class Solution {public: int maxProfit(vector<int>& prices) { // 可以当天卖出后,当天买入 int nMax = 0; for(int n = 1; n < prices.size(); n++) { if(pri...原创 2019-09-21 12:37:06 · 113 阅读 · 0 评论 -
13. Roman to Integer
class Solution {public: int romanToInt(string s) { int nSum = 0; for(int n = 0; n < s.size(); n++) { if(s[n] == 'C') { if(s[n + ...原创 2019-09-21 12:38:35 · 117 阅读 · 0 评论 -
136. Single Number
class Solution {public: int singleNumber(vector<int>& nums) { // from discuss int nret = 0; for(auto val : nums) { nret ^= val; } ...原创 2019-09-21 12:41:16 · 93 阅读 · 0 评论 -
141. Linked List Cycle
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: bool hasCycl...原创 2019-09-21 12:44:06 · 90 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> ret; for(int n = 0; n < numbers.size(); n++) { ...原创 2019-09-21 12:45:20 · 201 阅读 · 0 评论 -
169. Majority Element
class Solution {public: int majorityElement(vector<int>& nums) { unordered_map<int, int> map; for(int n = 0; n < nums.size(); n++) { int nco...原创 2019-09-21 13:00:18 · 96 阅读 · 0 评论 -
171. Excel Sheet Column Number
class Solution {public: int titleToNumber(string s) { int ntotal = 0; for(int n = (s.size()-1); n >= 0; n--) { int nBit = s[n] - 64; for(int i =...原创 2019-09-22 11:53:17 · 140 阅读 · 0 评论 -
2. Add Two Numbers
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: ListNode* ad...原创 2019-09-22 11:54:19 · 148 阅读 · 0 评论 -
206. Reverse Linked List
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: ListNode* re...原创 2019-09-22 11:55:19 · 135 阅读 · 0 评论 -
21. Merge Two Sorted Lists
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: ListNode* me...原创 2019-09-22 11:56:42 · 186 阅读 · 0 评论 -
217. Contains Duplicate
class Solution {public: bool containsDuplicate(vector<int>& nums) { unordered_map<int, int> map; for(int n = 0; n < nums.size(); n++) { map[...原创 2019-09-22 11:57:38 · 121 阅读 · 0 评论 -
226. Invert Binary Tree
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/clas...原创 2019-09-22 11:58:36 · 114 阅读 · 0 评论 -
237. Delete Node in a Linked List
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: void deleteN...原创 2019-09-22 11:59:24 · 168 阅读 · 0 评论 -
242. Valid Anagram
class Solution {public: bool isAnagram(string s, string t) { // 使用sort排序 sort(s.begin(), s.end()); sort(t.begin(), t.end()); if(s == t) { ...原创 2019-09-22 12:00:13 · 184 阅读 · 0 评论 -
258. Add Digits
class Solution {public: int addDigits(int num) { int nRet = 0; if(num < 10) { return num; } int nTemp = 0; while(num != 0)...原创 2019-09-22 12:01:32 · 132 阅读 · 0 评论 -
260. Single Number III
class Solution {public: vector<int> singleNumber(vector<int>& nums) { unordered_map<int, int> map; for(int n = 0; n < nums.size(); n++) { ...原创 2019-09-22 12:02:33 · 105 阅读 · 0 评论 -
283. Move Zeroes
class Solution {public: void moveZeroes(vector<int>& nums) { int nmove = 0; for(int n = 0; n < nums.size(); n++) { if(nums[n] != 0) {...原创 2019-09-23 19:22:25 · 112 阅读 · 0 评论 -
292. Nim Game
class Solution {public: bool canWinNim(int n) { return (n%4 != 0); // my first solution if(n % 4 == 0) { return false; } else...原创 2019-09-23 19:23:23 · 212 阅读 · 0 评论 -
338. Counting Bits
class Solution {public: vector<int> countBits(int num) { vector<int> vec(num + 1, 0); for(int n = 1; n <= num; n++) { vec[n] = vec[n&am...原创 2019-09-23 19:25:53 · 119 阅读 · 0 评论 -
344.Reverse String
class Solution {public: string reverseString(string s) { string strReverse = s; for(int n = s.size()-1; n >= 0 ; n--) { strReverse[s.size() - 1 - n] = s[n];...原创 2019-09-23 19:26:39 · 126 阅读 · 0 评论 -
349. Intersection of Two Arrays
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { // set 不允许重复值,且从小到大排序 set<int> se(nums1.begin(), nums1.e...原创 2019-09-23 19:27:41 · 223 阅读 · 0 评论 -
371. Sum of Two Integers
class Solution {public: int getSum(int a, int b) { if(a == 0) { return b; } int nb = a ^ b; int na = (a & b) << 1; ...原创 2019-09-23 19:28:32 · 103 阅读 · 0 评论 -
383. Ransom Note
class Solution {public: bool canConstruct(string ransomNote, string magazine) { bool bcan = true; for(int n = 0; n < ransomNote.size(); n++) { int nfind = m...原创 2019-09-23 19:29:18 · 259 阅读 · 2 评论 -
387. First Unique Character in a String
class Solution {public: int firstUniqChar(string s) { unordered_map<char, int> map; for(int n = 0 ; n < s.size(); n++) { map[s[n]] ++; } ...原创 2019-09-23 19:30:06 · 92 阅读 · 0 评论 -
389. Find the Difference
class Solution {public: char findTheDifference(string s, string t) { for(int n = 0; n < t.size(); n++) { int nfind = s.find(t[n]); if(nfind == -1) ...原创 2019-09-23 19:30:51 · 121 阅读 · 0 评论 -
406. Queue Reconstruction by Height
class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { vector<pair<int, int>> rec; rec.clear();...原创 2019-09-23 19:32:02 · 247 阅读 · 0 评论