面试笔试杂项积累-leetcode 66-70
来源:互联网 发布:电脑转盘抽奖软件 编辑:程序博客网 时间:2024/04/29 17:22
66.66-Plus One-Difficulty:Easy
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
思路
模拟加一进位,循环加一,而且只是加一,很简单
public class Solution { public int[] PlusOne(int[] digits) { for (int i = digits.Length - 1; i > -1; i--) { if (digits[i] + 1 >= 10) { digits[i] = 0; } else { ++digits[i]; return digits; } } int[] fin = new int[digits.Length + 1]; Array.Copy(digits, 0, fin, 1, digits.Length); fin[0] = 1; return fin; }}
67.67-Add Binary-Difficulty:Easy
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
思路
模拟二进制相加,
由于多次操作string,首次使用了StringBuilder,哈哈
分几种情况,二者是0,二者是1,一0一1,分别判断相加。
public class Solution { public string AddBinary(string a, string b) { string max = ""; string min = ""; StringBuilder final = new StringBuilder(); if (a.Length >= b.Length) { max = a; min = b; } else { max = b; min = a; } int temp = 0; for (int i = 0; i < max.Length; i++) { if (i < min.Length) { if (min[min.Length - 1 - i] != max[max.Length - 1 - i]) { if (temp > 0) { final.Append("0"); } else { final.Append("1"); } } else { if (min[min.Length - 1 - i] == 48) { if (temp > 0) { final.Append("1"); --temp; } else { final.Append("0"); } } else { if (temp > 0) { final.Append("1"); } else { final.Append("0"); ++temp; } } } } else { if (temp > 0) { if (max[max.Length - 1 - i] == 48)//0 { final.Append("1"); --temp; } else//1 { final.Append("0"); } } else { min = new string(ReverseStr(final.ToString())); max = max.Substring(0, max.Length - i );/// return max + min;///// } } } if (temp > 0) { final.Append("1"); } max = new string(ReverseStr(final.ToString())); return max; } public static char[] ReverseStr(string message) { char[] arr = message.ToCharArray(); Array.Reverse(arr); return arr; }}
68.68-Text Justification-Difficulty:Hard
Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' '
when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. "]
Note: Each word is guaranteed not to exceedL in length.
思路
很简单,找到可以插入的单词个数之后根据长度可以求出所有空格长度,再平均分配给除最后一个单词所有单词的后面。
注意最后一行需要左对齐
public class Solution { public IList<string> FullJustify(string[] words, int maxWidth) { IList<string> fin = new List<string>(); int times = 1; int i_temp = 0; string str_temp = ""; int length_temp = 0; int fillsapce = 0; int space_times = 0; bool isLast = false; for (int i = 0; i < words.Length; i++) { times = 1; i_temp = i; str_temp = ""; length_temp = words[i].Length; if (i_temp + 1 < words.Length) while (length_temp + times + words[i_temp + 1].Length <= maxWidth) { length_temp += words[i_temp + 1].Length; ++times; ++i_temp; if (i_temp + 2 > words.Length) { isLast = true; break; } } fillsapce = maxWidth - length_temp; if (isLast)//最后一行左对齐 { for (int j = 0; j < times; j++) { words[i + j] += " "; --fillsapce; } for (int j = 0; j < fillsapce; j++) { words[i + times - 1] += " "; } for (int j = 0; j < times; j++) { str_temp += words[j + i]; } } else//非最后一行以及最后一行是一个单词的情况 { space_times = times > 1 ? times - 1 : times; for (int j = 0; j < fillsapce; j++) { words[i + j % space_times] += " "; } for (int j = 0; j < times; j++) { str_temp += words[j + i]; } } i += (times - 1); fin.Add(str_temp); } return fin; }}
69.69-Sqrt(x)-Difficulty:Medium
Implement int sqrt(int x)
.
Compute and return the square root of x.
思路
模拟sqrt函数
注意超时问题
试了一下c# Math.Pow函数= =。。。可以通过
使用binary search可以解决超时问题
public class Solution { public int MySqrt(int x) { if (x < 0) return -1; if (x == 0) return 0; int low = 1; // the low should be set to 1. it's different from the search in array. int high = x / 2 + 1; // the high should be set to x / 2 + 1 int mid; while (low <= high) { mid = (high + low) / 2; if ((x / mid >= mid) && ((mid + 1) > x / (mid + 1))) { //to avoid overflow return mid; } else if (x / mid < mid) { high = mid - 1; } else { low = mid + 1; } } return -1; }}
70.70-Climbing Stairs-Difficulty:Easy
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?
思路
有多少不同的方式能爬到数组顶端,一次能爬1/2步。
把上面的左上角到右下角换成线性的了= =。。依旧动态规划问题
public class Solution { public int ClimbStairs(int n) { if (n <= 1) return 1; if (n == 2) return 2; int[] temp = new int[n]; temp[0] = 1; temp[1] = 2; for (int i = 2; i < n; i++) { temp[i] = temp[i - 1]+temp[i-2]; } return temp[n - 1]; }}
- 面试笔试杂项积累-leetcode 66-70
- 面试笔试杂项积累-leetcode 1-5
- 面试笔试杂项积累-leetcode 6-10
- 面试笔试杂项积累-leetcode 11-15
- 面试笔试杂项积累-leetcode 16-20
- 面试笔试杂项积累-leetcode 21-25
- 面试笔试杂项积累-leetcode 26-30
- 面试笔试杂项积累-leetcode 31-35
- 面试笔试杂项积累-leetcode 36-40
- 面试笔试杂项积累-leetcode 41-45
- 面试笔试杂项积累-leetcode 46-50
- 面试笔试杂项积累-leetcode 51-55
- 面试笔试杂项积累-leetcode 56-60
- 面试笔试杂项积累-leetcode 61-65
- 面试笔试杂项积累-leetcode 71-75
- 面试笔试杂项积累-leetcode 76-80
- 面试笔试杂项积累-leetcode 81-85
- 面试笔试杂项积累-leetcode 86-90
- java中的内部类总结
- HDOJ 2604 Queuing (递推+矩阵快速幂)
- RTMFP打洞
- 有关HTML5 Video对象的ontimeupdate事件的问题
- ExtJs表格控件(三)----单元格编辑与提交修改
- 面试笔试杂项积累-leetcode 66-70
- 在计算机中安装Arch Linux
- std:forward 完美转发
- IO流(Input Output Stream)总结1
- 20160201
- Fragment简述
- 白书练习 下落的小球
- JS学习--常见弹出框
- lua协程用法举例