思路一:暴力解法,直接平方,然后添加到vector中class Solution {
public:vector<int> sortedSquares(vector<int>& A) {vector<int> res;if(A.size()==0){return res;}for(auto i:A){res.push_back(i*i);}sort(res.begin(),res.end());return res;}
}; 思路二:原文:https://www.cnblogs.com/tianjiale/p/11069036.html
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10
Output: 1024.00000
Example 2:Input: 2.10000, 3
Output: 9.26100
Example 3:Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:-100.0 < x < 100.0n is a 32-bit signed integer, within the range [?231, 231 ? 1]可能是我想多了,因为int的负数范围比正数范围多一,转化为正整数要考虑他的一个...
@requires_authorization
@author johnsondu
@create_time 2015.7.1310:11
@url [longest-common-prefix](https://leetcode.com/problems/longest-common-prefix/)/*** 求出string中的最小长度* 然后依次每个string从第一个开始进行比较* 时间复杂度:O(n*m), n是string个数,m是string的长度*/class Solution {
public:string longestCommonPrefix(vector<string>& strs) {int len = strs.size();if(len == 0) return"";if(len ==...
整数反转1、题目描述给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。如果反转后整数超过 32 位的有符号整数的范围 [?231, 231 ? 1] ,就返回 0。
注意:假设环境不允许存储 64 位整数(有符号或无符号)。示例1:输入:x = 123
输出:321
示例2:输入:x = -123
输出:-321
示例3:输入:x = 120
输出:21
示例4:输入:x = 0
输出:0
2、算法描述核心思想:主要就是分情况讨论,题目本身很简单,把数字按照...
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
实现函数strStr。代码如下:int strStr(char* haystack, char* needle) {size_t len = strlen(haystack);if(strlen(needle) == 0)return 0;if(strcmp(haystack, needle) == 0)return 0;for(int i=0; i<len;i++){if(haystack[i] == needle[0]){if(strncmp(&(haystack[i]), needle, strlen(needle)...
选了个先转换成字符串,然后再翻转的做法,虽然效率不差,但属实有点拉,贴代码class Solution {
public:char* inttoString(int a,char* b){int t = a/10;if(t!=0){b = inttoString(a/10,b) +1;}*b = a%10 + ‘0‘;return b;}int reverse(int x) {if(x == -2147483648)return0;bool fu = false;// long t = x;if(x<0){fu = true;x = -x; }char a[20];memset(a,‘\0‘,sizeof(a));inttoString(x,a);int i = 0;while(a[i]!=‘...
为了跳槽,我目前为止刷了小四百道leetcode,也算是有一些经验,今天就跟大家分享下学习方法和我总结的干货。
注:文末附资料下载~
如何找到感觉
讲起算法,我看过不少书,有《算法导论》、《算法第四版》、《算法竞赛入门经典》、《剑指Offer》,但都没有让我产生质变,现在回想原因可能是:
看书的时候着急,不过脑子直接看解析,也不记笔记,过几天就忘了看完书就觉得自己会了,直接 Leetcode 随机选题,还是不会,内心受挫就不...
找到最多含有两个不同字符的子串的最长长度。例如:eoeabc,最长的是eoe为3,其他都为2.思路:用p1,p2表示两种字符串的最后一个出现的下标位置。初始p1为0. p2为-1.start初始化为0,表示两种字符串的开头。只要遍历一次string就可以得到结果了。首先我们要确定p2的值,那么i要一直到不等于s[p1]的值为止,那么位置就是p2了。然后继续往后如果来一个字符等于之前两种的其中一种,那么就要更新最后一次出现的下标。根据是谁就更新谁。...
题目:Given a linked list, return the node where the cycle begins. If there is no
cycle, return null.Follow up: Can you solve it without using extra space?解题思路:判断链表有无环,可用快慢指针进行,快指针每次走两步,慢指针每次走一步,如果快指针追上了慢指针,则存在环,否则,快指针走到链表末尾即为NULL是也没追上,则无环。为什么快慢指针可以判断有无环?因为快指针先进入环,在慢指针进入之后,如果把慢...
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1],and [2,1,1].解题思路:这道题与Permutations(http://blog.csdn.net/kangrydotnet/article/details/45691783)是姊妹题,唯一不同的是这道题的元素可以重复。其实我们在排列之前,先对整个数组进行排序。在递归过程中,若当...
这道题比较简单,观察清楚规律后很快可以写出来。但是用go写的版本,总是答案错误,本地无问题。后用Java重写。func convert(s string, numRows int) string {if numRows == 1 {return s}numChars := make(map[int][]string)size := 2 * numRows - 2for i := 0; i<len(s); i++ {x := s[i : i+1]if i % size < numRows {row := i % sizenumChars[row] = append(numChars[row], x)} else {row := size - i % sizenumChars[row] = app...
题目传送阵
class Solution {public void merge(int[] nums1, int m, int[] nums2, int n) {int p=m+n-1; //指向nums1数组的末尾int i=m-1,j=n-1; //分别指向nums1,nums2的最后一个元素while(i>=0 && j>=0){if(nums1[i]>nums2[j]){nums1[p--]=nums1[i--];}else{nums1[p--]=nums2[j--];}//nums1[p--]=nums1[i]>nums2[j]?nums1[i--]:nums2[j--];}while(j>=0){nums1[p--]=nums2[j--];}}
}
Given an array containing n distinct numbers taken from 0,1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.第一次AC代码...
题目:Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.分析:要求,移除具有n个元素的数组中全部指定的数字,返回删除后的数组长度。看似简单。事实上也能体现一个人的编程水平。解法1是优化后的,解法2是參照网上的STL解法。记录下来。代码:解释一下STL的几个算法。都包...
1、 Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null. 2、思路 1、判断两数大小,小的话,插入3、/*** Definition for ListNode.* public class ListNode {* int val;* ListNode n...