C++
Guo_Xiaoxi
这个作者很懒,什么都没留下…
展开
-
39、C++定义一个类,实现向量的加减运算
//定义一个类,实现向量的加减运算#include<stdio.h>#include<iostream>class Vector //定义向量类Vector{public: int *p; //定义整型指针 int Size; //成员参数原创 2017-02-27 14:34:20 · 6119 阅读 · 0 评论 -
LeetCode88. Merge Sorted Array
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 is greater or equal to m + n) to hold additional原创 2017-04-10 14:24:26 · 230 阅读 · 0 评论 -
LeetCode9、Palindrome Number
#include<vector>#include<cstdlib>#include<iostream>using namespace std;class Solution {public: bool isPalindrome(int x) { vector<int> v; if(x==0){return true;} if(x<0)原创 2017-03-26 09:09:09 · 356 阅读 · 0 评论 -
LeetCode7. Reverse Integer
1、stackclass Solution {public: int reverse(int x) { if((x==0)||(x==-2147483648)){ return 0; } bool flag=(x<0); x = abs(x); int r; stack原创 2017-03-26 08:46:32 · 306 阅读 · 0 评论 -
LeetCode13. Roman to Integer
给定一个罗马数字s,( I<=s<=MMMCMXCIX)(即1到3999),将罗马数字转换成整数。 如罗马数字I,II,III,IV,V分别代表数字1, 2, 3, 4, 5。 格式: 第一行输入一个罗马数字,接下来输出对应的整数。 提示: 首先要来了解一下罗马数字表示法,基本字符有7个:I,V,X,L,C,D,M,分别表示1,5,10,50,100,500,1000。 在构原创 2017-03-26 10:34:56 · 340 阅读 · 0 评论 -
LeetCode20. Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.The brackets must close in the correct order, “()” and “()[]{}” are all valid but “原创 2017-03-26 18:53:36 · 310 阅读 · 0 评论 -
LeetCode21. Merge Two Sorted Lists
1、去重了,所以没通过#include<cstdlib>#include<iostream>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNo原创 2017-03-26 21:58:46 · 270 阅读 · 0 评论 -
LeetCode26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2017-03-27 08:46:19 · 226 阅读 · 0 评论 -
LeetCode27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The order原创 2017-03-27 10:32:03 · 292 阅读 · 0 评论 -
LeetCode83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3./** * Definition for singly-lin原创 2017-04-09 10:27:55 · 211 阅读 · 0 评论 -
LeetCode70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive inte原创 2017-04-09 09:44:07 · 215 阅读 · 0 评论 -
40、C++类的静态成员
该类所有对象共同享有的数据或方法,被称为静态成员。在定义了对象以后,每个对象访问的都是同一个静态成员,每一个对象对静态成员变量的访问都会影响静态成员变量的值。 静态成员使用static进行说明,声明形式如下: class 类名 { static 数据类型说明 变量名; static 函数类型 函数名(函数参数) }; 静态成员与实例无关,因此可以不必定义原创 2017-02-27 16:43:00 · 274 阅读 · 0 评论 -
41、初始化类中的对象
//初始化类中的对象#include<iostream>class A{public: int x; A(int m);};A::A(int m):x(m){ std::cout<<"In Class A"<<std::endl;}class B{public: class A y; B(int n);};B::B(int n):y(n)原创 2017-02-27 17:13:30 · 341 阅读 · 0 评论 -
LeetCode38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, the原创 2017-03-27 16:28:07 · 295 阅读 · 0 评论 -
LeetCode14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.运行时间 3ms#include<string>#include<vector>#include<iostream>using namespace std;class Solution {public: stri原创 2017-03-27 17:00:33 · 272 阅读 · 0 评论 -
LeetCode2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2017-03-27 20:53:16 · 227 阅读 · 0 评论 -
LeetCode5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: “babad”Output: “bab”Note: “aba” is also a valid answer. Example:Input:原创 2017-03-28 21:19:56 · 223 阅读 · 0 评论 -
LeetCode6. ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I G原创 2017-03-28 22:06:01 · 358 阅读 · 0 评论 -
LeetCode11. Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2017-03-29 13:15:51 · 262 阅读 · 0 评论 -
LeetCode28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 从一个字符串中查找给定字符串第一次出现的位置#include<string>#include<iostream>using namespace s原创 2017-03-27 12:22:10 · 265 阅读 · 0 评论 -
LeetCode35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here原创 2017-03-27 13:44:29 · 239 阅读 · 0 评论 -
LeetCode17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string “23”原创 2017-04-17 20:45:46 · 399 阅读 · 0 评论 -
LeetCode31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde原创 2017-04-21 21:04:01 · 292 阅读 · 0 评论 -
LeetCOde33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the ar原创 2017-04-22 10:43:34 · 271 阅读 · 0 评论 -
LeetCode34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is原创 2017-04-22 11:18:46 · 249 阅读 · 0 评论 -
LeetCode42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], ret原创 2017-04-22 13:33:58 · 337 阅读 · 0 评论 -
LeetCode575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these原创 2017-05-07 15:02:53 · 376 阅读 · 0 评论 -
LeetCode567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string.Example 1原创 2017-05-07 15:04:59 · 331 阅读 · 0 评论 -
LeetCode101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2017-05-07 15:58:43 · 255 阅读 · 0 评论 -
LeetCode104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.递归/** * Definition for a binary tree node.原创 2017-05-07 17:01:45 · 325 阅读 · 0 评论 -
LeetCode25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nod原创 2017-04-21 11:27:36 · 260 阅读 · 0 评论 -
LeetCode24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no原创 2017-04-21 09:28:12 · 328 阅读 · 0 评论 -
LeetCode58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as原创 2017-04-06 18:49:12 · 240 阅读 · 0 评论 -
LeetCode66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st原创 2017-04-06 19:50:45 · 220 阅读 · 0 评论 -
LeetCode66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st原创 2017-04-06 21:12:34 · 271 阅读 · 0 评论 -
LeetCode67. Add Binary
Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.#include<string>#include<cstdlib>#include<iostream>using namespace std;class Solution原创 2017-04-06 21:19:33 · 315 阅读 · 0 评论 -
LeetCode15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain duplic原创 2017-04-17 09:16:17 · 351 阅读 · 0 评论 -
LeetCode16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2017-04-17 10:50:56 · 304 阅读 · 0 评论 -
LeetCode18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution set mu原创 2017-04-20 19:54:14 · 370 阅读 · 0 评论 -
LeetCOde19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list be原创 2017-04-20 20:39:33 · 235 阅读 · 0 评论