
LeetCode(c++&python)
哀酱
自己的学习笔记,博客主要是写给自己看的顺便分享,可读性不佳评论不回复还请见谅见谅
展开
-
LeetCode 345 Reverse Vowels of a String
LeetCode 345 Reverse Vowels of a String//vowels : a e i o u#include <string>using namespace std;class Solution {public: bool isVowel(char s){ if (s == 'a' || s == 'e' || s == 'i' || s == '原创 2017-06-10 14:31:29 · 295 阅读 · 0 评论 -
LeetCode 541 reverse string 2
LeetCode 541 reverse string 2#include <string>using namespace std;class Solution {public: string reverseStr(string s, int k) { int i = 0; while (i < s.length()) {原创 2017-06-10 14:33:01 · 208 阅读 · 0 评论 -
LeetCode 387 First Unique Character in a String
LeetCode 387 First Unique Character in a String#include <string>#include <vector>using namespace std;class Solution {public: int firstUniqChar(string s) { vector <int> abc(26);//长度26的ve原创 2017-06-10 14:32:27 · 289 阅读 · 0 评论 -
LeetCode 344 Reverse String
LeetCode 344 Reverse String#include <string>using namespace std;class Solution {public: string reverseString(string s) { int i = 0; int j = s.size() - 1; while (i < j)原创 2017-06-10 14:30:42 · 257 阅读 · 0 评论 -
LeetCode 205 Isomorphic Strings
LeetCode 205 Isomorphic Strings#include <string>using namespace std;class Solution {public: bool isIsomorphic(string s, string t) { char s_t[128] = { 0 };//串s到t的映射,最多128个,因为ascii码原创 2017-06-10 14:29:57 · 238 阅读 · 0 评论 -
LeetCode 167 Two Sum 2-Input array is sorted
LeetCode 167 Two Sum 2-Input array is sorted#include <vector>using namespace std;class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> result;原创 2017-06-10 14:28:19 · 185 阅读 · 0 评论 -
LeetCode 26 Remove Duplicates from Sorted Array
LeetCode 26 Remove Duplicates from Sorted Array#include <vector>#include <algorithm>//unique函数,stlusing namespace std;class Solution {public: int removeDuplicates(vector<int>& nums) { n原创 2017-06-10 14:26:58 · 239 阅读 · 0 评论 -
Leetcode 9 Palindrome Number 回文数
Leetcode 9 Palindrome Number 回文数#include <sstream>using namespace std;class Solution {public: bool isPalindrome(int x) { stringstream sstr; sstr << x; string s;原创 2017-06-10 14:25:27 · 244 阅读 · 0 评论 -
Leetcode 7 Reverse Integer
Leetcode 7 Reverse Integer#include <sstream>#include <string>using namespace std;class Solution {public: int reverse(int x) { int y = 0; while(x != 0){ if (y > INT_M原创 2017-06-10 14:24:48 · 301 阅读 · 0 评论 -
Leetcode 1 Two Sum
Leetcode 1 Two Sum#include <vector>using namespace std;class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result; int length = nums.size();原创 2017-06-10 14:22:38 · 215 阅读 · 0 评论 -
Leetcode 125 Valid Palindrome
Leetcode 125 Valid Palindrome#include <string>#include <algorithm>using namespace std;class Solution {public: bool isPalindrome(string s) { if (s.empty()) return true;原创 2017-07-01 15:56:01 · 152 阅读 · 0 评论 -
Leetcode 383 Ransom Note
Leetcode 383 Ransom Note#include <string>#include <vector>using namespace std;class Solution {public: bool canConstruct(string ransomNote, string magazine) { vector <int> ransomNoteFreq原创 2017-07-01 15:57:08 · 187 阅读 · 0 评论 -
Leetcode 434 Number of Segments in a String
Leetcode 434 Number of Segments in a String#include <string>using namespace std;class Solution {public: int countSegments(string s) { if (s.length() == 0) return 0; in原创 2017-07-01 15:57:43 · 217 阅读 · 0 评论 -
Leetcode 448 Find All Numbers Disappeared in an Array
Leetcode 448 Find All Numbers Disappeared in an Array#include <vector>using namespace std;class Solution {public: vector<int> findDisappearedNumbers(vector<int>& nums) { vector<int> disap原创 2017-07-01 15:58:09 · 197 阅读 · 0 评论 -
Leetcode 520 Detect Capital
Leetcode 520 Detect Capital#include <string>using namespace std;class Solution {public: bool detectCapitalUse(string word) { if (word.length() <= 1) return true; bool原创 2017-07-01 15:58:36 · 270 阅读 · 0 评论 -
Leetcode 551 Student Attendance Record I
Leetcode 551 Student Attendance Record I#include <string>using namespace std;class Solution {public: bool checkRecord(string s) { //两个以上连续的L也就是存在LLL int aFreq = 0; int len原创 2017-07-01 15:59:05 · 271 阅读 · 0 评论 -
Leetcode 557 Reverse Words in a String III
Leetcode 557 Reverse Words in a String IIIclass Solution {public: string reverseWords(string s) { int i = 0; int length = s.length(); int left = 0; int right = 0;//原创 2017-07-01 15:59:37 · 226 阅读 · 0 评论 -
Leetcode 581 Shortest Unsorted Continuous Subarray
Leetcode 581 Shortest Unsorted Continuous Subarray#include <vector>#include <algorithm>using namespace std;//多开一个vector,从前从后比较从哪儿开始元素不一样class Solution {public: int findUnsortedSubarray(vector<原创 2017-07-01 16:00:07 · 256 阅读 · 0 评论 -
Leetcode 624 Maximum Distance in Arrays
Leetcode 624 Maximum Distance in Arrays#include <vector>#include <windef.h>//max函数using namespace std;class Solution {public: int maxDistance(vector<vector<int>>& arrays) { int distance原创 2017-07-01 16:00:42 · 429 阅读 · 0 评论 -
Leetcode 231 Power of Two
Leetcode 231 Power of Two#include <math.h>using namespace std;class Solution {public: bool isPowerOfTwo(int n) { //如果n&(n-1)是0,则n是2的n次方 if ((n &(n - 1) )== 0 && n > 0)//要把n&(n-1)再原创 2017-07-01 15:56:33 · 232 阅读 · 0 评论 -
Leetcode 66 Plus One
Leetcode 66 Plus One#include <vector>using namespace std;class Solution {public: vector<int> plusOne(vector<int>& digits) { int length = digits.size(); if (digits[length - 1] < 9原创 2017-07-02 16:07:18 · 210 阅读 · 0 评论 -
Leetcode 190 Reverse Bits
Leetcode 190 Reverse Bits#include <stdint.h>using namespace std;class Solution {public: uint32_t reverseBits(uint32_t n) { int result = 0; for (int i = 0; i < 32; i++) {原创 2017-07-02 16:07:55 · 267 阅读 · 0 评论 -
Leetcode 191 Number of 1 Bits
Leetcode 191 Number of 1 Bits#include <stdint.h>using namespace std;class Solution {public: int hammingWeight(uint32_t n) { //n减1是将n最右边的那个1变成0,如果这个1后面跟着0,则这些0全变成1,相与可以得到最右边的1 //1原创 2017-07-02 16:08:24 · 195 阅读 · 0 评论 -
Leetcode 204 Count Primes
Leetcode 204 Count Primes//比n小的质数有几个//(质数筛选定理)n不能够被小于等于根号n的任何质数整除,则n是一个质数#include <math.h>class Solution {public: int countPrimes(int n) { int count = 1; if (n <= 2)原创 2017-07-02 16:09:08 · 177 阅读 · 0 评论 -
Leetcode 326 Power of Three
Leetcode 326 Power of Threeusing namespace std;class Solution {public: bool isPowerOfThree(int n) { return n > 0 && 1162261467 % n == 0;//116226467是比int最大值小的最大的3的幂次,3的19次方 }};原创 2017-07-02 16:09:40 · 184 阅读 · 0 评论 -
Leetcode 338 Counting Bits
Leetcode 338 Counting Bits#include <vector>#include <intrin.h>//__popcnt#include<nmmintrin.h>using namespace std;class Solution {public: vector<int> countBits(int num) { vector<int> bit原创 2017-07-02 16:10:12 · 221 阅读 · 0 评论 -
Leetcode 342 Power of Four
Leetcode 342 Power of Fourusing namespace std;class Solution {public: bool isPowerOfFour(int num) { if (num > 0 && 1073741824 % num == 0)//是2的幂次 if (num & 0x55555555)//2的幂次数中,4原创 2017-07-02 16:10:41 · 194 阅读 · 0 评论 -
Leetcode 461 Hamming Distance
Leetcode 461 Hamming Distanceclass Solution {public: int hammingDistance(int x, int y) { int z = x ^ y;//异或 int count = 0; while (z){ z = z &(z - 1);原创 2017-07-02 16:11:12 · 224 阅读 · 0 评论 -
Leetcode 477 Total Hamming Distance
Leetcode 477 Total Hamming Distance#include <vector>#include <intrin.h>//__popcnt#include<nmmintrin.h>using namespace std;class Solution {public: int hammingDistance(int x, int y) { int原创 2017-07-02 16:11:59 · 315 阅读 · 0 评论 -
Leetcode 628 Maximum Product of Three Numbers
Leetcode 628 Maximum Product of Three Numbers#include <vector>#include <algorithm>#include <windef.h>using namespace std;class Solution {public: int maximumProduct(vector<int>& nums) {原创 2017-07-02 16:12:28 · 638 阅读 · 0 评论 -
Leetcode 13 Roman to Integer
Leetcode 13 Roman to Integer//罗马数字采用七个罗马字母作数字//即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)#include <string>using namespace std;class Solution {public: int romanNumber(char c){ switch原创 2017-06-22 18:38:57 · 267 阅读 · 0 评论 -
LeetCode 20 Valid Parentheses
LeetCode 20 Valid Parentheses#include <string>#include<stack>//使用c++中的栈using namespace std;class Solution {public: bool isValid(string s) { stack <char> stk;//声明一个字符栈 //遍历S中的全部字原创 2017-06-22 18:39:36 · 242 阅读 · 0 评论 -
LeetCode 21 Merge Two Sorted Lists
LeetCode 21 Merge Two Sorted Lists#include <stddef.h>//否则NULL会报错为未定义的标识符using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solu原创 2017-06-22 18:40:21 · 278 阅读 · 0 评论 -
LeetCode 27 Remove Element
LeetCode 27 Remove Element#include <vector>using namespace std;class Solution {public: int removeElement(vector<int>& nums, int val) { int size = nums.size(); int index = 0;原创 2017-06-22 18:40:55 · 220 阅读 · 0 评论 -
LeetCode 28 Implement strStr()
LeetCode 28 Implement strStr()#include <string>using namespace std;class Solution {public: int strStr(string haystack, string needle) { int position; if (haystack.find(needle) !=原创 2017-06-22 18:41:36 · 228 阅读 · 0 评论 -
LeetCode 35 Search Insert Position
LeetCode 35 Search Insert Position#include <vector>using namespace std;class Solution {public: int searchInsert(vector<int>& nums, int target) { if (nums[0] > target) return原创 2017-06-22 18:42:10 · 273 阅读 · 0 评论 -
Leetcode 88 Merge Sorted Array
Leetcode 88 Merge Sorted Array#include <vector>#include<algorithm>using namespace std;class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { if (m ==原创 2017-06-22 18:44:48 · 232 阅读 · 0 评论 -
LeetCode 203 Remove Linked List Elements
LeetCode 203 Remove Linked List Elements#include <stddef.h>//否则NULL会报错为未定义的标识符using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };原创 2017-06-22 18:45:22 · 206 阅读 · 0 评论 -
Leetcode 237 Delete Node in a Linked List
Leetcode 237 Delete Node in a Linked List#include <stddef.h>//否则NULL会报错为未定义的标识符using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};cla原创 2017-06-22 18:46:02 · 200 阅读 · 0 评论 -
LeetCode 283 Move Zeroes
LeetCode 283 Move Zeroes#include <vector>using namespace std;class Solution {public: void moveZeroes(vector<int>& nums) { int size = nums.size(); int index = 0; for (int i原创 2017-06-22 18:46:40 · 238 阅读 · 0 评论