1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + n…
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢出 3.start.end直接等于mid 4.最后比较两个位置 class Solution { public: int search(vector<int>& nums, int target) { if(nums.empty()) ; ; ; int mid; < end){ m…
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { ; while(ver<n) { if(isBadVersion(ver)) return ver; ver++; } return ver; } }…
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本是第一个坏的. 思路:二分法,middle-1好,middle坏,middle就是第一个坏的版本 Java实现: /* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(i…
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student Name: Wang, Yixiao Student Number:20162314 Tutor:Mr.Lou.Mr.Wang Experiment date:2017.11.6 Secret level: Unsecretive Experiment time:60 minutes Major/Electiv…
Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisite 生成随机整数数组,打印数组, 元素交换. #include <stdlib.h> #include <iostream> #include <time.h> using namespace std; #define MAX 10 // 数组最大长度 void init…
 *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4ms] 2. Reverse Integer [12ms] 题意:将一个32bit signed integer反转输出,如果反转之后超出32位补码范围 [-2^31,2^31-1],则输出0 思路:边取模边算结果,结果存longlong判界 3. Palindrome Number [112ms]…
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! email: tomqianmaple@gmail.com 后续更多的easy题已经不属于Top Interview Questions,所以现在就不集中精力去做了,只做到目前的100题左右. Palindrome Linked List Lowest Common Ancestor of a Bina…
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! email: tomqianmaple@gmail.com Two Sum Reverse Integer Palindrome Number Roman to Integer Longest Common Prefix Valid Parentheses Merge Two Sorted Lists…
Common Sorting Algo: Bubble Sort: Runime: O(n2) average and worst case. Memory: O(1). void BubbleSortArray(){ for(int i=1;i<n;i++) for(int j=0;i<n-i;j++) if(a[j]>a[j+1]){//比较交换相邻元素 int temp; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } Selection Sort…
9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. A has enough buffer at the end to hold B, we can merge two arrays from end to start index, like merg…
leetcode 34 最早出现和最后出现 class Solution { public int[] searchRange(int[] nums, int target) { int []ans={-1,-1} ; for(int i=0;i<nums.length;i++){ if(nums[i]==target){ ans[0]=i; break; } } for(int j=nums.length-1;j>=0;j--){ if(nums[j]==target){ ans[1]=j;…
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Build in functions in java.util.Collections Need to implement a comparator - a special class which returns an integer comparision of two object, if  compare(a,b), if return negative number, a will be before b, otherw…
前言:这是关于LeetCode上面练习题C++的笔记,有些地方参考有网友的解题方法(可能有些参考没能注明,望谅解),如有需要改进的地方希望留言指教,多谢! 目录: ZigZag Conversion Reverse digits of an integer Implement atoi to convert a string to an integer Determine whether an integer is a palindrome Write a function to find th…
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.com/problems/first-bad-version/description/ 题目描述 You are a product manager and currently leading a team to develop a new product. Unfortunately, the la…
#-*- coding: UTF-8 -*-# The isBadVersion API is already defined for you.# @param version, an integer# @return a bool# def isBadVersion(version):class Solution(object):    def firstBadVersion(self, n):        """        :type n: int        :…
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve…
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve…
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { , r = n , ans ; while(l <= r){ ) / ; if(!isBadVersion(mid)){ l…
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a…
有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品. 正确答案: // Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { ; int high = n; ; while (low < high) { ver = low + (high - low)…
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve…
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] O…
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack…
27. Remove Element Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memor…
26.Remove Duplicates from Sorted Array Given a sorted array nums, 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 by modifying the input…
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代…
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be…
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is writ…